1 | initial version |
Here are the steps to implement kmalloc in C:
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/kernel.h>
void *pointer;
pointer = kmalloc(size, GFP_KERNEL);
where size
is the amount of memory to allocate and GFP_KERNEL
is the allocation flag.
if (!pointer){
printk(KERN_INFO "Memory allocation failed");
}
strcpy(pointer, "Hello world");
kfree(pointer);
Make sure to free the memory to avoid memory leaks.