kmalloc()、vmalloc()、__get_free_pages()实例 (例5-2)

 
 
 
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>

void kmalloc_test(void)
{
	char *buff;

	printk("kmalloc_test\n");

	buff = kmalloc(1024, GFP_KERNEL);
	if(buff != NULL)
	{
		sprintf(buff, "test memory \n");
		printk(buff);

		kfree(buff);
	}

	buff = kmalloc(32*PAGE_SIZE, GFP_KERNEL);
	if(buff != NULL)
	{
		printk("Big Memory Ok \n");
		kfree(buff);
	}
}

void vmalloc_test(void)
{
	char *buff;

	printk("vmalloc_test \n");

	buff = vmalloc(33*PAGE_SIZE);
	if(buff != NULL)
	{
		sprintf(buff, "vmalloc test ok \n");
		printk(buff);

		vfree(buff);
	}
}

void get_free_pages_test(void)
{
	char *buff;
	int order;

	printk("get_free_pages test \n");

	order = get_order(8192*10);
	buff = __get_free_pages(GFP_KERNEL, order);
	if(buff != NULL)
	{
		sprintf(buff, "__get_free_pages test ok [%d] \n", order);
		printk(buff);

		free_pages(buff, order);
	}
}


int memtest_init(void)
{
	char *data;

	printk("Module Memory Test \n");

	kmalloc_test();
	vmalloc_test();
	get_free_pages_test();

	return 0;
}

void memtest_exit(void)
{
	printk("Module Memory Test End \n");
}

module_init(memtest_init);
module_exit(memtest_exit);

MODULE_LICENSE("Dual BSD/GPL");


-----------------------------------------------------------------------------------------------------------------------------

 

运行:

root@xiangpingli:/home/linux/study/5.3.2 # insmod basicmem.ko
root@xiangpingli:/home/linux/study/5.3.2 # dmesg
[ 4309.728614] Module Memory Test
[ 4309.728617] kmalloc_test
[ 4309.728618] test memory
[ 4309.728620] Big Memory Ok
[ 4309.728621] vmalloc_test
[ 4309.728679] vmalloc test ok
[ 4309.728713] get_free_pages test
[ 4309.728715] __get_free_pages test ok [5]
root@xiangpingli:/home/linux/study/5.3.2 #

你可能感兴趣的:(Module,null,include)