Linux平台上几个常见内核内存分配函数

  #include <linux/slab.h>

  void *kmalloc(size_t size, int flags);

  Kmalloc分配一段未清0的连续物理内存页,并返回虚存地址。有点是快,并且可指定flag,如DMA内存,高地址区域内存等。缺点是不能分配大于128KB(处于跨平台考虑),几个重要的flag:

  GFP_ATOMIC

  Used to allocate memory from interrupt handlers and other code outside of a process context. Never sleeps.

  GFP_KERNEL

  Normal allocation of kernel memory. May sleep.

  GFP_USER

  Used to allocate memory for user-space pages; it may sleep.

  GFP_HIGHUSER

  Like GFP_USER, but allocates from high memory, if any. High memory is described in the next subsection.

  * slab allocator(lookaside cache)

  从Memcached的实现知道有这么一个内存管理策略,其显着特点是分配一组相同大小的内存块作为内存池,其实现对应于源代码中的<linux/slab.h>和mm/slab.c。

  Prototype:

  #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);

  /proc/slabinfo

  A virtual file containing statistics on slab cache usage.

  *__get_free_pages

  Prototype:

  _ _get_free_pages(unsigned int flags, unsigned int order);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/27532.html