1kmem_alloc(9F) Kernel Functions for Drivers kmem_alloc(9F)
2
3
4
6 kmem_alloc, kmem_zalloc, kmem_free - allocate kernel memory
7
9 #include <sys/types.h>
10 #include <sys/kmem.h>
11
12
13
14 void *kmem_alloc(size_t size, int flag);
15
16
17 void *kmem_zalloc(size_t size, int flag);
18
19
20 void kmem_free(void*buf, size_t size);
21
22
24 Architecture independent level 1 (DDI/DKI).
25
27 size Number of bytes to allocate.
28
29
30 flag Determines whether caller can sleep for memory. Possible flags
31 are KM_SLEEP to allow sleeping until memory is available, or
32 KM_NOSLEEP to return NULL immediately if memory is not avail‐
33 able.
34
35
36 buf Pointer to allocated memory.
37
38
40 The kmem_alloc() function allocates size bytes of kernel memory and
41 returns a pointer to the allocated memory. The allocated memory is at
42 least double-word aligned, so it can hold any C data structure. No
43 greater alignment can be assumed. flag determines whether the caller
44 can sleep for memory. KM_SLEEP allocations may sleep but are guaranteed
45 to succeed. KM_NOSLEEP allocations are guaranteed not to sleep but may
46 fail (return NULL) if no memory is currently available. The initial
47 contents of memory allocated using kmem_alloc() are random garbage.
48
49
50 The kmem_zalloc() function is like kmem_alloc() but returns zero-filled
51 memory.
52
53
54 The kmem_free() function frees previously allocated kernel memory. The
55 buffer address and size must exactly match the original allocation.
56 Memory cannot be returned piecemeal.
57
59 If successful, kmem_alloc() and kmem_zalloc() return a pointer to the
60 allocated memory. If KM_NOSLEEP is set and memory cannot be allocated
61 without sleeping, kmem_alloc() and kmem_zalloc() return NULL.
62
64 The kmem_alloc() and kmem_zalloc() functions can be called from inter‐
65 rupt context only if the KM_NOSLEEP flag is set. They can be called
66 from user context with any valid flag. The kmem_free() function can be
67 called from from user, interrupt, or kernel context.
68
70 copyout(9F), freerbuf(9F), getrbuf(9F)
71
72
73 Writing Device Drivers
74
76 Memory allocated using kmem_alloc() is not paged. Available memory is
77 therefore limited by the total physical memory on the system. It is
78 also limited by the available kernel virtual address space, which is
79 often the more restrictive constraint on large-memory configurations.
80
81
82 Excessive use of kernel memory is likely to affect overall system per‐
83 formance. Overcommitment of kernel memory will cause the system to hang
84 or panic.
85
86
87 Misuse of the kernel memory allocator, such as writing past the end of
88 a buffer, using a buffer after freeing it, freeing a buffer twice, or
89 freeing a null or invalid pointer, will corrupt the kernel heap and may
90 cause the system to corrupt data or panic.
91
92
93 The initial contents of memory allocated using kmem_alloc() are random
94 garbage. This random garbage may include secure kernel data. Therefore,
95 uninitialized kernel memory should be handled carefully. For example,
96 never copyout(9F) a potentially uninitialized buffer.
97
99 kmem_alloc(0, flag) always returns NULL. kmem_free(NULL, 0) is legal.
100
101
102
103SunOS 5.11 16 Jan 2006 kmem_alloc(9F)