Skip to content

Instantly share code, notes, and snippets.

@pavly-gerges
Forked from cvam0000/kmalloc
Last active June 5, 2022 10:25
Show Gist options
  • Save pavly-gerges/099cd59a762a77ad06afa7b6d8df511f to your computer and use it in GitHub Desktop.
Save pavly-gerges/099cd59a762a77ad06afa7b6d8df511f to your computer and use it in GitHub Desktop.
Linux Kernal mem allocation example
/* 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