封装kmalloc/malloc的一些小函数

本文介绍了一种在C语言中封装内存分配与释放的方法。通过定义gbuffer结构体并提供一系列内联函数来实现内存的初始化、分配、释放等功能。这种方法简化了内存管理流程,并能适用于不同的操作系统环境。

由于开发时常需要对分配的内存长度进行管理,C语言下又没有像C++那样方便的类, 所以只好重新封装了下kmalloc/kfree的内存分配释放函数. 操作起来还算比较方便:)

struct gbuffer{ u8 *buf; u32 len; }; typedef struct gbuffer gbuffer; typedef struct gbuffer gbuffer_t; static inline void gbuffer_init(gbuffer *p) { p->len = 0; p->buf = NULL; } static inline void __gbuffer_init(gbuffer *p, u8 *buf, u32 len) { p->len = len; p->buf = buf; } static inline int gbuffer_empty(gbuffer *p) { return ( p->buf == NULL ); } static inline void gbuffer_free(gbuffer *p) { if ( NULL == p ) return; #ifdef __KERNEL__ if ( likely( p->buf != NULL ) ){ kfree( p->buf ); p->buf = NULL; } #else if ( NULL != p->buf ) { free( p->buf ); } #endif p->len = 0; } static inline void _gbuffer_free(gbuffer *p) { if ( NULL == p ) return; #ifdef __KERNEL__ if ( likely( p->buf != NULL ) ){ kfree( p->buf ); p->buf = NULL; } kfree( p ); #else if ( NULL != p->buf ) { free( p->buf ); } free( p ); #endif } static inline gbuffer_t* __gbuffer_alloc(void) { gbuffer_t *p = NULL; #ifdef __KERNEL__ p = kzalloc( sizeof(*p), GFP_KERNEL ); if ( unlikely( NULL == p ) ){ return NULL; } #else p = malloc( sizeof(*p) ); if ( NULL == p ) return NULL; #endif p->buf = NULL; p->len = 0; return p; } static inline gbuffer_t* _gbuffer_alloc(u32 len) { gbuffer_t *p = NULL; #ifdef __KERNEL__ p = kzalloc( sizeof(*p), GFP_KERNEL ); if ( unlikely( NULL == p ) ){ return NULL; } p->buf = kzalloc( len, GFP_KERNEL ); if ( unlikely( NULL == p->buf ) ){ kfree( p ); return NULL; } #else p = malloc( sizeof(*p) ); if ( NULL == p ) return NULL; p->buf = malloc( len ); if ( NULL == p->buf ){ free( p ); return -1; } #endif p->len = len; return p; } static inline int gbuffer_alloc( gbuffer *p, u32 len ) { if ( NULL == p ) return -1; #ifdef __KERNEL__ p->buf = kzalloc( len, GFP_KERNEL ); if ( unlikely( NULL == p->buf ) ){ return -1; } #else p->buf = malloc( len ); if ( NULL == p->buf ){ return -1; } #endif p->len = len; return 0; }

函数中可以将释放资源函数写成一个宏统一调用吗static int __init read_samp_init(void) { int retInit = -1; struct device *pRetDev = NULL; printk(KERN_INFO "%s: initializing module.\n", DEVICE_NAME); /* KERN_INFO - level notice */ /* kernel allocate an unsed device number */ retInit = alloc_chrdev_region(&devNum, 0, 1, DEVICE_NAME); if (retInit < 0) { printk(KERN_ERR "%s: register device number failed.\n", DEVICE_NAME); return -1; } majNum = MAJOR(devNum); printk(KERN_INFO "%s: register name successfully, the major number is %d.\n", DEVICE_NAME, majNum); /* static init cdev struct */ cdev_init(&readSampCdev, &fileOps); readSampCdev.owner = THIS_MODULE; /* add cdev into sys -- register */ retInit = cdev_add(&readSampCdev, devNum, 1); if (retInit < 0) { printk(KERN_ERR "%s: failed to add cdev of %s.\n", DEVICE_NAME, DEVICE_NAME); unregister_chrdev_region(devNum, 1); return -1; } /* create device class */ pReadSampClass = class_create(THIS_MODULE, CLASS_NAME); /* kernel limited mem: error -> 0xffffffff invalid address */ if (IS_ERR(pReadSampClass)) { cdev_del(&readSampCdev); unregister_chrdev_region(devNum, 1); printk(KERN_ERR "%s: create device class %s failed.\n", DEVICE_NAME, CLASS_NAME); return -1; } /* device node -- interface between userspace and kernel */ /* udev -- auto response device_create and create node */ /* MKDEV(majNum, 0) -- major number + minor number */ /* device_create(class, parent, dev_num, data_may_use, name_of_sub_dir); */ pRetDev = device_create(pReadSampClass, NULL, devNum, NULL, DEVICE_NAME); if(IS_ERR(pRetDev)) { class_destroy(pReadSampClass); cdev_del(&readSampCdev); unregister_chrdev_region(devNum, 1); printk(KERN_ERR "%s: create device file failed.\n", DEVICE_NAME); return -1; } printk(KERN_INFO "%s: create device file successfully.\n", DEVICE_NAME); /* init the buffer used for write or read */ pDevBuf = kmalloc(SIZE_DEV_BUF, GFP_KERNEL); /* malloc kernel buffer, GFP_KERNEL - common flag */ if (NULL == pDevBuf) { device_destroy(pReadSampClass, devNum); class_destroy(pReadSampClass); /* include unregister */ cdev_del(&readSampCdev); unregister_chrdev_region(devNum, 1); printk(KERN_ERR "%s: malloc kernel buffer failed.\n", DEVICE_NAME); return -1; /* ssize_t -- a count number, negative -- err */ } printk(KERN_INFO "%s: create the buffer successfully.\n", DEVICE_NAME); return 0; }
08-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值