-
-
Save pavly-gerges/099cd59a762a77ad06afa7b6d8df511f to your computer and use it in GitHub Desktop.
Linux Kernal mem allocation example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* An example of Memory Allocation */ | |
#include<linux/slab.h> | |
#include<linux/init.h> | |
#include<linux/module.h> | |
#include<linux/kernel.h> | |
#define size 10 | |
MODULE_DESCRIPTION("example of memory allocation "); | |
MODULE_AUTHOR("cvam"); | |
MODULE_LICENSE("GPL"); | |
int alloc_init(void) | |
{ | |
void *pointer; | |
pointer = kmalloc(size, GFP_KERNEL); | |
if(!pointer) | |
{ printk(KERN_ALERT" Faild "); | |
//report error | |
} | |
printk(" I got : %zu Byes", ksize(pointer)); | |
kfree(pointer); | |
return 0; | |
} | |
void alloc_exit(void) | |
{ | |
printk(KERN_ALERT"Time to exit"); | |
} | |
module_init(alloc_init); | |
module_exit(alloc_exit); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment