Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Linux kernel equivalent to the memset function?

I'm writing a driver that requires me to clear all the memory allocated to zero. memset is a userspace function, but I would like to know if the kernel provides a macro that helps me do this.

like image 554
Dr.Knowitall Avatar asked Mar 26 '12 23:03

Dr.Knowitall


3 Answers

I looked it up in a Linux driver development book I have (it's German, so I will give a loose translation of what it basically says) and it gives the following description:

#include <asm/io.h>
void *memset(void *s, int c, size_t n);

Fills the first n Bytes of the given region of memory at s with the constant byte value c. If you want to set regions of memory mapped I/O memory to a constant value, you must use the memset_io() function.

Returns a pointer to s.


Doing some more investigation... If you look into the <asm/io.h> sources, you can find:

#include <linux/string.h> /* for memset() and memcpy() */

...but also the definition of the memset_io() function, so I guess that this should be the correct #include.

like image 88
mozzbozz Avatar answered Nov 04 '22 04:11

mozzbozz


It's true memset() in

include <string.h >

not supposed to appear in kernel code.And the fact is it may cost you a lot to trace down where memset() is included during driver development. Actually memset() is defined in

#include<linux/string.h>

it says:

#ifndef __KERNEL__
#include <string.h>
#else
/* We don't want strings.h stuff being used by user stuff by accident */

Hope this help you type down 'memset()' with 100% sure :)

like image 22
Chrysanthemumer Avatar answered Nov 04 '22 05:11

Chrysanthemumer


According to this thread and people commenting here that have used it, memset is available in kernel code. Maybe you just forgot to

#include <string.h>
like image 6
Niklas B. Avatar answered Nov 04 '22 04:11

Niklas B.