C standard library redesign

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: C standard library redesign

Post by bluemoon »

thepowersgang wrote:For example, fopen returns a file pointer, short of adding either a FILE** argument to it (so it can return an error code)

Code: Select all

int fopen(FILESTRUCT* file, const char* filename, int mode);
IMO is quite elegant. If FILESTRUCT is opaque you add function to allocate/initialize the space required (aka new/constructor)
PS. why should fopen allocate new space while it means to open a file?
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: C standard library redesign

Post by Kevin »

rdos wrote:The whole idea of using an error string instead of an error code is so code should never parse them or use them for anything else than logging or displaying them to the user.
Yes. Which is why it's a good solution for many cases, but in some cases it won't be enough and you need to add some kind of error code anyway. Just because your programs don't make use of error information internally (or even give useful error messages) it's not said that noone else does better.
thepowersgang wrote:For example, fopen returns a file pointer, short of adding either a FILE** argument to it (so it can return an error code), or adding an int* argument (to recieve the error code), there has to be a second channel for error information (with a return value of NULL indicating the error).
What's wrong with an additional pointer argument?
Developer of tyndur - community OS of Lowlevel (German)
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: C standard library redesign

Post by Owen »

bluemoon wrote:
thepowersgang wrote:For example, fopen returns a file pointer, short of adding either a FILE** argument to it (so it can return an error code)

Code: Select all

int fopen(FILESTRUCT* file, const char* filename, int mode);
IMO is quite elegant. If FILESTRUCT is opaque you add function to allocate/initialize the space required (aka new/constructor)
PS. why should fopen allocate new space while it means to open a file?
Because a FILE is useless without a file associated with it.

Also, you want FILE to be opaque. Lots of stuff in there, and also allocating one on the stack will mean somebody very quickly breaks things
rdos
Member
Member
Posts: 3297
Joined: Wed Oct 01, 2008 1:55 pm

Re: C standard library redesign

Post by rdos »

thepowersgang wrote:Just chipping my 2c in.

I agree that non-reentrant functions should be avoided in modern code, and errno does (as was originally designed) make almost everything non-reentrant, there are times when it is nearly unavoidable.
At least on x86, there is a method to make them reentrant. You use the FS or GS register an let it point to per-thread data (TLS = Thread Local Storage). Unfortunately, newlib was built at a time when this wasn't invented yet, and so have some really gruesome way to solve this issue.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: C standard library redesign

Post by Combuster »

Code: Select all

int fopen(FILESTRUCT* file, const char* filename, int mode);
I think that needs to be:

Code: Select all

int fopen(FILESTRUCT** file, const char* filename, int mode);
So that the size and contents of struct FILE isn't needed for the caller.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
linguofreak
Member
Member
Posts: 510
Joined: Wed Mar 09, 2011 3:55 am

Re: C standard library redesign

Post by linguofreak »

bwat wrote:How does the programmer/sysadmin communicate to Linux that a certain application is vital? For example, let us say I have a system where application A is vital, but application B is not. For the sake of argument assume B is dynamically allocating memory without freeing in an infinite loop. How can I make sure that in the case where resources become so scarce that progress cannot be made for B, that B and not A is killed?
A solution I've wondered about is having a special kernel worker thread that runs in pre-reserved memory whose one purpose is to be woken in the event of an OOM condition and present the user with a list of running processes and a prompt to select one of them to be killed. It wouldn't be able to make any calls to the rest of the kernel that might allocate memory, of course, and that might just kill the idea (while writing such an interactive OOM handler itself without allocating memory might not be overly difficult, it might involve having non-allocating duplicates of a fair number of kernel functions that need to allocate memory in normal use).
palk
Posts: 16
Joined: Mon Nov 15, 2010 8:30 pm

Re: C standard library redesign

Post by palk »

thepowersgang wrote:For example, fopen returns a file pointer, short of adding either a FILE** argument to it (so it can return an error code), or adding an int* argument (to recieve the error code), there has to be a second channel for error information (with a return value of NULL indicating the error). This also allows the reason for an error to be seamlessly propagated up the call chain, without forcing a calling convention on parent functions.
No pointer necessary; you are allowed to return structs:

Code: Select all

struct fopen_result {
    FILE * handle;
    int result_code;
};

struct fopen_result fopen(const char * foo);

struct fopen_result result = fopen("blah");
if (result.result_code) {
    switch (result.result_code) {
        case ENOMEM:
            ...
        case ENOENT:
            ...
    }
    exit(EXIT_FAILURE);
}
FILE * file = result.handle;
...
palk
Posts: 16
Joined: Mon Nov 15, 2010 8:30 pm

Re: C standard library redesign

Post by palk »

Throwing my two cents in:

I think C's standard library would benefit from generic data structures such as linked lists, red-black trees, hash tables, arbitrarily large bit vectors. It seems like most of the time in C, code is managing or moving data, so I'd want to fast-track that.

I would replace most of the "signal handling" stuff and try to actually support an event-driven architecture that works internally to a thread (single process, single thread mode), between threads (single process, multithread mode), and between processes (multiprocess, multithread mode). I haven't looked into this, but most GUI toolkits (written in C) seem to support this, so I would think it's not infeasible, and worst case you'd have to add support into the OS to help out (which is kinda what you're here for in the first place, right?).

Finally, and this is more of a peeve, I'd rework the types and defines provided by stddef.h, stdint.h, inttypes.h, stdbool.h, etc. to make uintX_t have the definition of uint_fastX_t and provide every uintX_t type for every value of X up to 64 (unsigned long long is required to be at least 64 bits, so this is fine to do). Then, e.g., uint11_t would mean "the fastest type that is at least 11 bits." The macros PRIX32 and co. would be in the same header as the type definition, but I'd make the names shorter: FX32, Fu32, Fo32, etc. It's very annoying having to include stddef.h to get the type definition of size_t, stdint.h to get SIZE_MAX and the bit-count aware types, and then inttypes.h to get the format macros. Who thought it would be good to separate them?
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: C standard library redesign

Post by sortie »

palk wrote:It's very annoying having to include stddef.h to get the type definition of size_t, stdint.h to get SIZE_MAX and the bit-count aware types, and then inttypes.h to get the format macros. Who thought it would be good to separate them?
Nobody - the <stddef.h> header is from C89 while <stdint.h> is from C99. They put the stuff in different headers to mitigate namespace pollution and to avoid trouble with existing systems and programs that already declared similarly named macros (which is, for instance, why we get to dead with <stdbool.h> instead of just adding the bool data type).

I do agree, a new incompatible standard library ought to redesign the types API - although it's not that bad if you wish to be compatible and will be implementing large parts of ISO C as well.
User avatar
Oranos
Member
Member
Posts: 29
Joined: Sat Oct 15, 2011 6:52 pm
Location: Land of Bald Eegels

Re: C standard library redesign

Post by Oranos »

palk wrote:Throwing my two cents in:

I think C's standard library would benefit from generic data structures such as linked lists, red-black trees, hash tables, arbitrarily large bit vectors. It seems like most of the time in C, code is managing or moving data, so I'd want to fast-track that.

I would replace most of the "signal handling" stuff and try to actually support an event-driven architecture that works internally to a thread (single process, single thread mode), between threads (single process, multithread mode), and between processes (multiprocess, multithread mode). I haven't looked into this, but most GUI toolkits (written in C) seem to support this, so I would think it's not infeasible, and worst case you'd have to add support into the OS to help out (which is kinda what you're here for in the first place, right?).

Finally, and this is more of a peeve, I'd rework the types and defines provided by stddef.h, stdint.h, inttypes.h, stdbool.h, etc. to make uintX_t have the definition of uint_fastX_t and provide every uintX_t type for every value of X up to 64 (unsigned long long is required to be at least 64 bits, so this is fine to do). Then, e.g., uint11_t would mean "the fastest type that is at least 11 bits." The macros PRIX32 and co. would be in the same header as the type definition, but I'd make the names shorter: FX32, Fu32, Fo32, etc. It's very annoying having to include stddef.h to get the type definition of size_t, stdint.h to get SIZE_MAX and the bit-count aware types, and then inttypes.h to get the format macros. Who thought it would be good to separate them?
.. I don't like any of those suggestions. I find this scheme sufficient for types:

Code: Select all

uint = word size
uint8 = 8 bits; 'Byte' is an alias
uint16 = 16 bits
uint32 = 32 bits
uint64 = 64 bits
But, why is everyone talking about modifying the existing C standard library? I thought the original post was about starting a library from scratch, not making trivial changes to a few functions. I find everything about it naive and it leaves me to question like: "who the hell standardized this skiv?" #-o
FallenAvatar
Member
Member
Posts: 283
Joined: Mon Jan 03, 2011 6:58 pm

Re: C standard library redesign

Post by FallenAvatar »

Oranos wrote:
palk wrote:...
.. I don't like any of those suggestions. I find this scheme sufficient for types:

Code: Select all

uint = word size
uint8 = 8 bits; 'Byte' is an alias
uint16 = 16 bits
uint32 = 32 bits
uint64 = 64 bits
But, why is everyone talking about modifying the existing C standard library? I thought the original post was about starting a library from scratch, not making trivial changes to a few functions. I find everything about it naive and it leaves me to question like: "who the hell standardized this skiv?" #-o
First off, what did palk's post have to do with BASIC type definitions?

Second, did you read the title of the thread?

- Monk
User avatar
Oranos
Member
Member
Posts: 29
Joined: Sat Oct 15, 2011 6:52 pm
Location: Land of Bald Eegels

Re: C standard library redesign

Post by Oranos »

First off, what did palk's post have to do with BASIC type definitions?
I'm not aware of BASIC type definitions.
Second, did you read the title of the thread?
No.
So, what I'm wondering is what improvements do you think could be made if one were to start from scratch?
User avatar
AndrewAPrice
Member
Member
Posts: 2300
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: C standard library redesign

Post by AndrewAPrice »

Alternatives:

There are a few exception libraries out there for C. Handle what you want to handle, for example if you just want to know if it failed, just listen for EXCEPTION. If you want to handle code branches for different circumstances, listen for DEVICEEXCEPTION, PERMISSIONEXCEPTION, FILENOTFOUNDEXCEPTION, etc.

Alternatively, make the entire library event based and non-blocking. Each IO operation is asynchronous and you pass it a callback function. Handle errno as a callback parameter.
My OS is Perception.
Post Reply