* 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);
返回2^order个未清0连续物理页面,flags与kmalloc中flags一致,允许的最大order值为10或者11(根据体系结构不同)
*alloc_pages
Prototype:
struct page *alloc_pages_node(int nid, unsigned int flags,
unsigned int order);
Kernel中页分配器实现,__get_free_pages即调用alloc_pages实现的
The real core of the Linux page allocator is a function called alloc_pages_node:
*vmalloc
分配地址连续虚存,而不保证物理地址连续,大部分情况下适合“软件”,而不是驱动程序。相对而言,kmalloc和__get_free_pages虚存map到物理内存只需要增减一个偏移,而使用vmalloc分配需要修改页表,故vmalloc的开销较大,分配少数几个页面的效率太低。
*per-cpu variables
Each cpu hold an independant copy in their respective processor’s caches, so there is no lock required and improve better performance, implemented as a linux 2.6 feature. Defined in <linux/percpu.h>.
DEFINE_PER_CPU(type, name);
get_cpu_var(sockets_in_use)++;
put_cpu_var(sockets_in_use);