Quick Reference

The functions and symbols related to memory allocation follow.

#include <linux/malloc.h> , void *kmalloc(size_t size, int flags); , void kfree(void *obj);

The most frequently used interface to memory allocation.

#include <linux/mm.h> , GFP_KERNEL , GFP_ATOMIC , __GFP_DMA , __GFP_HIGHMEM

kmalloc flags. __GFP_DMA and __GFP_HIGHMEM are flags that can be OR’d to either GFP_KERNEL or GFP_ATOMIC.

#include <linux/malloc.h> , kmem_cache_t *kmem_cache_create(char *name, size_t size, size_t offset, unsigned long flags, constructor(), destructor()); , int kmem_cache_destroy(kmem_cache_t *cache);

Create and destroy a slab cache. The cache can be used to allocate several objects of the same size.

SLAB_NO_REAP , SLAB_HWCACHE_ALIGN , SLAB_CACHE_DMA

Flags that can be specified while creating a cache.

SLAB_CTOR_ATOMIC , SLAB_CTOR_CONSTRUCTOR

Flags that the allocator can pass to the constructor and the destructor functions.

void *kmem_cache_alloc(kmem_cache_t *cache, int flags); , void kmem_cache_free(kmem_cache_t *cache, const void *obj);

Allocate and release a single object from the cache.

unsigned long get_zeroed_page(int flags); , unsigned long __get_free_page(int flags); , unsigned long __get_free_pages(int flags, unsigned long order); , unsigned long __get_dma_pages(int flags, unsigned long order);

The page-oriented allocation functions. get_zeroed_page returns a single, zero-filled page. All the other versions of the call do not initialize the contents of the returned page(s). __get_dma_pages is only a compatibility macro in Linux 2.2 and later (you can use __GFP_DMA instead).

void free_page(unsigned long addr); , void free_pages(unsigned long addr, unsigned long order);

These functions release page-oriented allocations.

#include <linux/vmalloc.h> , void * vmalloc(unsigned long size); , void vfree(void * addr); , #include <asm/io.h> , void * ioremap(unsigned long offset, unsigned long size); , void iounmap(void *addr);

These functions allocate or free a contiguous virtual address space. ioremap accesses physical memory through virtual addresses, while vmalloc allocates free pages. Regions mapped with ioremap are freed with iounmap, while pages obtained from vmalloc are released with vfree.

#include <linux/bootmem.h> , void *alloc_bootmem(unsigned long size); , void *alloc_bootmem_low(unsigned long size); , void *alloc_bootmem_pages(unsigned long size); , void *alloc_bootmem_low_pages(unsigned long size);

Only with version 2.4 of the kernel, memory can be allocated at boot time using these functions. The facility can only be used by drivers directly linked in the kernel image.

Get Linux Device Drivers, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.