歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux平台上幾個常見內核內存分配函數

Linux平台上幾個常見內核內存分配函數

日期:2017/2/28 16:39:26   编辑:Linux教程

  * kmalloc

  Prototype:

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

Copyright © Linux教程網 All Rights Reserved