Currently a lot of us bring in some form of c library in order obtain simple functions like memset or memcpy. Bringing in the entire C library is totally unnecessary for these kind of functions which in turn might use some form of compiler hook or library in order to use optimized functions. With the compiler there are specialized functions but these often have funky names which makes it non portable.
The functions I really need for the bare metal stuff are:
memcpy
memset
memcmp
memsearchbyte - search for a given byte, strlen could be implemented easily using this
memsearchword - search for a given word
memsearcharray- search memory for a given byte array, can implemented using memcmp
With these types of functions you can come pretty far. You are welcome to mention any other function you think should be added.
LLVM already have some of this as intrinsics but it would be nice to have a "standard bare metal library" that gives us this basic functionality. Most of the C library operates on its stupid null terminated strings, something that kernels should avoid anyway. Rust which is more modern has done this more or less with its core library. Of course you could write all these things yourself but you don't really want to write an optimized function for every CPU architecture each with its own special tricks to make it optimized.
Do we have any such library?
Bare metal library
Re: Bare metal library
Yes, it is called libc.OSwhatever wrote:Do we have any such library?
I honestly don't understand what's the fuzz. Just like Rust has a runtime library, the same way C has a runtime library too, and it is (surprisingly) called libc. That is the core library for C. Linking your kernel with it statically will only include the functions that your kernel actually uses, no need for dynamic linking nor for shared objects.OSwhatever wrote:Rust which is more modern has done this more or less with its core library.
So you have three options:
a) statically link with a libc (pick any, I would recommend musl)
b) because most libc are Open Source, simply copy'n'paste the functions you need into your source
c) use a compiler (like gcc) which has builtin versions of the memory functions, like __builtin_memcmp, __builtin_memcpy etc.
Btw, "memsearchbyte" is called "memchr", and "memsearcharray"'s name is "memmem" in C standard. The "__builtin_" prefix is handled by the compiler 100% transparently, but you could also use some defines to support non-ansi C compilers. So what do you mean by non portable?OSwhatever wrote:these often have funky names which makes it non portable.
Just a sidenote: to clearify, all libc functions of memcpy, memchr, memcmp, memem, memset etc. operate on raw memory buffers, and not on null terminated strings.
Cheers,
bzt
Re: Bare metal library
And that's strstr(): http://www.cplusplus.com/reference/cstring/strstr/OSwhatever wrote:memsearchword - search for a given word
Re: Bare metal library
True, but the OP wants to avoid null terminated C strings. That's why they need memmem instead, which works with Pascal strings too. Originally it was a glibc extension, but musl and many other libc implementations support it too.kzinti wrote:And that's strstr(): http://www.cplusplus.com/reference/cstring/strstr/
Cheers,
bzt
-
- Member
- Posts: 5575
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Bare metal library
bzt wrote:c) use a compiler (like gcc) which has builtin versions of the memory functions, like __builtin_memcmp, __builtin_memcpy etc.
Using the GNU Compiler Collection (GCC) wrote:Many of these functions are only optimized in certain cases; if they are not optimized in a particular case, a call to the library function is emitted.