Page 1 of 1

More information on C Library Implementation

Posted: Sun Jul 02, 2017 9:14 pm
by climjark
Hello all, i have been writing my own Operating System , Xenia (github : metallicsoul92/xenia ) and i was wondering, other then looking at other C Standard library implementations, if there is any other ways of implementing it. I have read the open standards library file, but it just gave me a information on what should be defined, and not how.

Re: More information on C Library Implementation

Posted: Sun Jul 02, 2017 11:47 pm
by Love4Boobies
If you're told both what to implement and how then what is your role, other than that of a secretary/typist? The C standard defines the semantics (how things should behave) and you have to solve the problem of how this should happen by making trade-offs that make sense given your own set of constraints. Different constraints should yield different implementations, especially since they often compete. But don't worry, this is great practice! If you are struggling to do it, you are learning something in the process and becoming a better engineer. Don't be afraid to spend a lot of time not knowing what to do.

Re: More information on C Library Implementation

Posted: Mon Jul 03, 2017 1:17 am
by Solar
P.J. Plaugher, "The Standard C Library". It's an actual book on how to implement an actual C standard library ('89).

I bought it, I read it, I learned a lot, and then did it rather differently as I didn't agree with all of Plaugher's ideas and designs (also, copyright). But it gave some good advice, raised some awareness for things I would have considered too late (resulting in major rewrites), and gave me an impression of how I did not want to do certain things. ;-)

Re: More information on C Library Implementation

Posted: Tue Jul 04, 2017 10:48 am
by Geri
c library is basically just a few function like strcpy, memset and stuff like that, maybe totally 50 or 60 function, basically one day of work.

Re: More information on C Library Implementation

Posted: Tue Jul 04, 2017 11:24 am
by bzt
climjark wrote:it just gave me a information on what should be defined, and not how.
That makes sense as the standard library's main purpose is to hide kernel interface details and provide a standard way for applications to interact with the OS. Therefore it needs to define functions (which are common among OSes), but cannot say how to implement them in general (as the implementation varies from kernel to kernel).
It worth pointing out that implementing those functions in userspace would improve performance but would also lower security. For example: memcpy() can use rep movsb instruction (no priviledge level switch, no overhead, but any address can be passed, most OS implementation choose this), or could use a memcpy system call (slow, as priviledge level has to be changed and transfer passed to kernel, but can limit input to only specific memory address regions). Likewise fopen() could be implemented in userspace only (assumes single address space for the system (like CP/M and Singularity) not really good for security, but fast as hell), or with system calls (as overhead is insignificant compared to disk latency and security is more of importance most OSes (SunOS, Linux, BSDs, WindowsNT etc.) choose this). The standard library's prototype for memcpy() and fopen() would be the same regardless of the choice.

Re: More information on C Library Implementation

Posted: Tue Jul 04, 2017 3:34 pm
by alexfru
Geri wrote:c library is basically just a few function like strcpy, memset and stuff like that, maybe totally 50 or 60 function, basically one day of work.
Your days must be very long then! I've spent several weeks implementing the library for Smaller C. With the floating point support it's probably several months. And there still are a few limitations and bugs that need addressing (nothing urgent, though).

Re: More information on C Library Implementation

Posted: Wed Jul 05, 2017 1:57 am
by Solar
Geri wrote:..maybe totally 50 or 60 function, basically one day of work.
Yeah, right. And we're all idiots for not realizing how easy it all is.

<stdio.h> has 64 functions (including printf(), and that alone you won't do correctly in one day). <stdlib.h> has 48 functions, including (among other non-trivials) malloc() and free(). <string.h> has 33 functions, some of which very habitually get implemented naively, and wrongly (looking at you, strncpy()). And that's the easy part.

You need <math.h> code, including proper handling of INFs, NaNs, over-/underflow etc.; and printf() / scanf() / strtod() to support it. You would need a multibyte / Unicode implementation for <wchar.h>, <wctype.h> and <uchar.h> to be considered "complete". There's <locale.h>, and since C11 you need <threads.h>...

Re: More information on C Library Implementation

Posted: Wed Jul 05, 2017 8:43 am
by Geri
alexfru wrote:Your days must be very long then
it took me 1 or 2 days to implement most the functions (string and number management like atoi, fgetc, itoa, strcpy, etc, it took a few minutes each. after all an strstr is just like 2 for cycle in each other....). later i invested another day to optimize some of the functions (memcpy for example) to suit for subleq instruction better. mathematical functions (sin, sqrtf, etc) took additional few days, i didnt however optimised those. my complete standard library including stdlib, math functions, and the full client-side interaction with the os is together approx 2000 line (and approx 1000 on the kernel side, plus of couse on the kernel side i had to implement for example a malloc or fread functions as well, but i consider that to the implementation of kernel and not as implementation of the c library).

Re: More information on C Library Implementation

Posted: Wed Jul 05, 2017 9:13 am
by Solar
I'd like to see the source to those claims. Ideally in a repo with the corresponding commit times. I'm especially interested in putting your printf() through its paces.

Tried the link in your signature, but all I get there is binaries.

Re: More information on C Library Implementation

Posted: Wed Jul 05, 2017 9:19 am
by Geri
dawn os is not opensource