My new distro "Glacies" - GNU/Linux

Users browsing this thread: 1 Guest(s)
josuah
Long time nixers
Stralloc from DJB : https://cr.yp.to/lib/stralloc.html
Advised: reading malloc(3) and realloc(3) man page and the link above. That's it...

The libstralloc is a very tiny (~60 lines?) library to build strings onto a simple struct with a pointer to a malloc()ed buffer (->s) the available size of that buffer (->a), and the used size (->n).

You end up using stralloc_cats(sa, "text") without extra checking for "is there enough room in the string sa?":
if there is not enough room, stralloc_cats will realloc sa->s and denote thew available size in sa->a.

Why having both ->a and ->n ? Because while adding more memory, stralloc_cats() will do a little planning and ask for more than just the new length of the string to build, so that it does not have to call realloc too often...

BUT, this, the libc already does it! :)

Check by reallocating a string and printing its address:

----in----
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char *p = NULL;
printf("%p\n", p = realloc(p, 10));
printf("%p\n", p = realloc(p, 11));
printf("%p\n", p = realloc(p, 13));
printf("%p\n", p = realloc(p, 20));
return 0;
}
----out----
0x734306a2460
0x734306a2460
0x734306a2460
0x734bce1e0e0
----end----

Only the 1st and the 4th call to realloc did actually copied the old buffer to a new, larger one.


My very humble benchmark did show that, on some example of malloc I used,:

- using strlen() to add bytes by bytes was much slower than using stralloc (so keeping the size ->n is a really good idea, which most programming languages does),

- keeping track of the usable size (->a) of the buffer adds very little advantage over using realloc every time a string is added


Why the second point? Note that malloc()/realloc() is not a syscall!

Most malloc implementations (if not all), have a block describing the buffer just before it, so the "usable buffer size" is already saved, somewhere *before* the ->s pointer, and it very fast for realloc() to pick it up :

Possible memory layout:
--------
-- ,---- S (char *, struct stralloc)
-- | --- A (size_t, struct stralloc)
-- | --- N (size_t, struct stralloc)
-- | ---
-- | --- [...]
-- | ---
-- | ---
-- | --- [...]
-- | --- (??? memory used by malloc)
-- | --- (??? memory used by malloc)
-- `> - s[0] (char, malloc()ed buffer)
-------- s[1] (char)
-------- s[2] (char)
-------- s[3] (char)
-------- [...]
--------

Somewhere in "??? memory used by malloc" (imlementation-specific), there is the capacity of the buffer stored, for internal use by malloc/realloc (which precisely know where to look).

So I got rid of of the ->a field (capacity) in the stralloc library, to only have ->s (sring pointer) and ->n (total length) left.

Beside than that (which mean I had to go over-pedantic), I rarely found a way to do more concise than DJB's libc and be confident that I was not breaking something elsewhere doing so.

Programming with DJB's libraries makes you an alien. A minimalist, efficient, safe and correct alien.


Messages In This Thread
My new distro "Glacies" - by eadwardus - 08-12-2019, 03:55 PM
RE: My new distro "Glacies" - by bouncepaw - 09-12-2019, 01:54 PM
RE: My new distro "Glacies" - by z3bra - 09-12-2019, 08:08 PM
RE: My new distro "Glacies" - by eadwardus - 09-12-2019, 11:59 PM
RE: My new distro "Glacies" - by z3bra - 10-12-2019, 04:25 AM
RE: My new distro "Glacies" - by eadwardus - 10-12-2019, 11:52 AM
RE: My new distro "Glacies" - by jkl - 10-12-2019, 07:40 PM
RE: My new distro "Glacies" - by josuah - 10-12-2019, 08:48 PM
RE: My new distro "Glacies" - by josuah - 10-12-2019, 08:50 PM
RE: My new distro "Glacies" - by josuah - 10-12-2019, 08:52 PM
RE: My new distro "Glacies" - by eadwardus - 11-12-2019, 03:28 AM
RE: My new distro "Glacies" - by eadwardus - 31-12-2019, 11:52 PM
RE: My new distro "Glacies" - by josuah - 13-01-2020, 08:42 PM
RE: My new distro "Glacies" - by josuah - 13-01-2020, 09:12 PM
RE: My new distro "Glacies" - by eadwardus - 15-01-2020, 04:31 PM
RE: My new distro "Glacies" - by josuah - 18-01-2020, 12:12 PM
RE: My new distro "Glacies" - by josuah - 18-01-2020, 12:16 PM
RE: My new distro "Glacies" - by eadwardus - 19-01-2020, 02:20 PM
RE: My new distro "Glacies" - by josuah - 25-01-2020, 02:01 PM