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
相关文档:
Linux的一个吸引人的特性就是用户可以自行定制整个系统,你可是运行一个只有1M的“迷你”Linux,也可以运行一个几G的强大Linux。而无论你运行怎样的Linux,你都是先从引导程序开始运行的。对于普通用户,大多都是在个人电脑上运行Linux的。
个人电脑,又叫PC机,是我们常见的使用Intel或AMD的芯片的电 ......
五:kmem_cache_create()分析
我们以一个例子来跟踪分析一下slab的机制:
下面是一个测试模块的代码:
#include <linux/config.h>
#include <linux/module.h>
#include <linux/slab.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ericxiao <xgr178@163.com>");
MODULE_DESCRI ......
七:kmem_cache_free()的实现
kmem_cache_free用于把从slab中分配的对象释放掉,同分配一样,它首先会把它放到AC中,如果AC满了,则把对象释放到share链中,如果share也满了,也就把它释放至slab。来看具体的代码:
void kmem_cache_free (kmem_cache_t *cachep, void *objp)
{
unsi ......
#include <stdio.h>
#include <unistd.h>
#define FOO "foo"
int main(void)
{
if(!access(FOO, F_OK))
{
if(!unlink(FOO))
{
}
else
{
printf("remove %s failed\n", FOO);
}
}
else
{
printf("%s not existed\ ......
Linux 下面使用RPC需要使用到命令rpcgen.
在Linux下开发RPC程序流程如下:
1.写一个rpc程序,
如test.x
2.使用rpcgen生成必须的文件,通常是客户端和服务器端以及头文件
$rpcgen test.x
3.使用rpcgen生成服务器端和客户端的C语言代码
&n ......