linuxÄں˿ռäÉêÇ볬¹ý2MBÁ¬Ðø¿Õ¼äµÄʵÏÖº¯Êý¡£
/*
kmalloc can apply 128KB memory only. This func support any continous memory allocate more than 2MB.
*/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kallsyms.h>
#define KMEM_PAGES //apply more than one page
#define KMEM_DEBUG //debug message switch
/*
// Pure 2^n version of get_order
static __inline__ __attribute_const__ int get_order(unsigned long size)
{
int order;
size = (size - 1) >> (PAGE_SHIFT - 1);
order = -1;
do {
size >>= 1;
order++;
} while (size);
return order;
}
*/
/*
alloc memory for DMA using in kernel space.
*/
void *kmem_alloc(size_t size, dma_addr_t *dma_handle, unsigned long flags)
{
struct page *page; //start page address
void *cpu_addr = NULL; //cpu io address
struct page *end; //end of pages
unsigned int PageOrder=0;
size = PAGE_ALIGN(size); //must be times of 4KB
PageOrder=get_order(size);
#ifdef CONFIG_ISOLATE_HIGHMEM //allocate in high memory
page = alloc_pages(GFP_HIGHUSER, PageOrder);
#else //allocate from low memory
page = alloc_pages(GFP_KERNEL, PageOrder); //get_order(size) means how many 4KB page do you want to apply. 2^n
#end
Ïà¹ØÎĵµ£º
ShellÊÇϵͳµÄÓû§½çÃ棬ÌṩÁËÓû§ÓëÄں˽øÐн»»¥²Ù×÷µÄÒ»ÖÖ½Ó¿Ú¡£Ëü½ÓÊÕÓû§ÊäÈëµÄÃüÁî²¢°ÑËüËÍÈëÄÚºËÈ¥Ö´ÐС£
¡¡¡¡Êµ¼ÊÉÏShellÊÇÒ»¸öÃüÁî½âÊÍÆ÷£¬Ëü½âÊÍÓÉÓû§ÊäÈëµÄÃüÁî²¢ÇÒ°ÑËüÃÇË͵½Äںˡ£²»½öÈç´Ë£¬ShellÓÐ×Ô¼ºµÄ±à³ÌÓïÑÔÓÃÓÚ¶ÔÃüÁîµÄ±à¼£¬ËüÔÊÐíÓû§±àдÓÉshellÃüÁî×é³ÉµÄ³ÌÐò¡£Shell±à³ÌÓïÑÔ¾ßÓÐÆÕͨ±à³ÌÓ ......
¡¡¡¡ÄãÏë³ÉÖªµÀÈçºÎ³ÉΪһ¸öLinuxÄں˿ª·¢Õßô£¿»òÕßÄãµÄÀÏ°å¸æËßÄ㣬“ȥΪÕâ¸öÉ豸дһ¸öLinuxÇý¶¯¡£“ÕâƪÎĵµµÄÄ¿µÄ£¬¾ÍÊÇͨ¹ýÃèÊöÄãÐèÒª¾ÀúµÄ¹ý³ÌºÍÌáʾÄãÈçºÎºÍÉçÇøÒ»Æð¹¤×÷£¬À´½Ì¸øÄãΪ´ïµ½ÕâЩĿµÄËùÐèÒªÖªµÀµÄËùÓÐ֪ʶ¡£±¾ÎÄÒ²³¢ÊÔ½âÊÍÉçÇøΪʲôÕâÑù¹¤×÷µÄһЩÔÒò¡£
¡¡¡¡Äں˼¸ºõÈ«ÊÇÓÃCд³ÉµÄ£¬ÓÐ ......
inux±ØѧµÄ60¸öÃüÁî
LinuxÌṩÁË´óÁ¿µÄÃüÁÀûÓÃËü¿ÉÒÔÓÐЧµØÍê³É´óÁ¿µÄ¹¤×÷£¬Èç´ÅÅ̲Ù×÷¡¢Îļþ´æÈ¡¡¢Ä¿Â¼²Ù×÷¡¢½ø³Ì¹ÜÀí¡¢ÎļþȨÏÞÉ趨µÈ¡£ËùÒÔ£¬ÔÚLinuxϵͳÉϹ¤×÷Àë²»¿ªÊ¹ÓÃϵͳÌṩµÄÃüÁî¡£ÒªÏëÕæÕýÀí½âLinuxϵͳ£¬¾Í±ØÐë´ÓLinuxÃüÁîѧÆð£¬Í¨¹ý»ù´¡µÄÃüÁîѧϰ¿ÉÒÔ½øÒ»²½Àí½âLinuxϵͳ¡£
²»Í¬Linux·¢ÐаæµÄÃüÁîÊýÁ¿² ......
Áù£ºkmem_cache_allocµÄʵÏÖ·ÖÎö:
ÎÒÃÇÔÚÉÏÃæ¿ÉÒÔ¿´µ½£¬´´½¨Ò»¸öcacheÃèÊö·ûµÄʱºò£¬²¢Ã»ÓÐÕâÖ®·ÖÅäslabÊý¾Ý¡£ÏÖÔÚÎÒÃÇÀ´¿´Ò»ÏÂÔõô´ÓcacheÖÐÉêÇë¶ÔÏó
void * kmem_cache_alloc (kmem_cache_t *cachep, int flags)
{
return __cache_alloc(cachep, flags);
}
ʵ¼ÊÉÏ»áµ÷ÓÃ__cache_allo ......
¾Å£º¼¸µã²¹³ä£º
1: SlabÖÐʹÓõÄÒ³Ã涼»á¼ÓÉÏ“PG_slab”±êÖ¾£¬ÒÔ¸úÒ»°ãµÄÒ³ÃæÇø±ð¡£ÁíÍ⣬ÔÚÊÍ·ÅÄÚ´æµÄʱºò£¬¾³£ÐèÒªÓõ½´ÓÒ³Ãæµ½slabµÄ¶ÔӦת»»¹Øϵ¡£ÄÇÊÇÔõÑù±êʶµÄÄØ£¿
¹ØÓÚ±êÖ¾£º
×¢ÒâÓÐÒÔÏ´úÂ룺
static void *kmem_getpages(kmem_cache_t *cachep, int flags, int nodeid)
{
&n ......