Writing Device Drivers

Memory Allocation

These interfaces dynamically allocate memory for the driver to use.

void *kmem_alloc(size_t size, int flag);

kmem_alloc(9F) allocates a block of kernel virtual memory of length size and returns a pointer to it. If flag is KM_SLEEP, kmem_alloc(9F) may block, waiting for memory to become available. If flag is KM_NOSLEEP, kmem_alloc(9F) returns NULL if the request cannot be satisfied immediately.

void kmem_free(void *cp, size_t size);

kmem_free(9F) releases a block of memory of length size, starting at address addr, that was previously allocated by kmem_alloc(9F). size must be the original amount allocated.

void *kmem_zalloc(size_t size, int flags);

kmem_zalloc(9F) calls kmem_alloc(9F) to allocate a block of memory of length size, and calls bzero(9F) on the block to zero its contents before returning its address.

These interfaces allocate/free memory that can be exported to applications using devmap_umem_setup(9F).

void *ddi_umem_alloc(size_t size, int flag,  
				ddi_umem_cookie_t *cookiep);

ddi_umem_alloc(9F) allocates a block of kernel virtual memory of length size and returns a pointer to it. The memory can be used inside the kernel and it can be exported to user applications using devmap_umem_setup(9F). If flag is DDI_UMEM_SLEEP, ddi_umem_alloc(9F) may block, waiting for memory to become available. If flag is DDI_UMEM_NOSLEEP, ddi_umem_alloc(9F) returns NULL if the request cannot be satisfied immediately. If the DDI_UMEM_PAGEABLE flag is set, the memory can be paged out.

void ddi_umem_free(ddi_umem_cookie_t cookie);

ddi_umem_free(9F) releases a block of memory that was previously allocated by ddi_umem_alloc(9F).