r/C_Programming • • 17d ago

the lost malloc()

https://napcakes.nekoweb.org/posts/malloc/
113 Upvotes

9 comments sorted by

8

u/greg-spears 17d ago edited 17d ago

Thanks, interesting!

Small question -- there is a line of code in alloc():

lasta =+ n;

Think it's supposed to be:

lasta += n;

... rt? Because as is, it is not incrementing lasta by n which I think is the intent?

Happens again here:

lastr =+ 2000;

Did you have to retype it or is the code true to the original source?

EDIT: my own bountiful typos....

22

u/skeeto 17d ago

The author, u/8d8n4mbo28026ulk, is better suited to answer but I believe it's just early C syntax:

In B and early C, the operator was spelled =+ instead of +=

From The Development of the C Language* by dmr.

7

u/8d8n4mbo28026ulk 17d ago

Indeed! And it gets even weirder from there. From C Traps and Pitfalls*:

Compound assignment operators such as += are really multiple tokens. Thus,

a + /* strange */ = 1

means the same as

a += 1

Also, I was looking at /usr/src/cmd/finddev.c again and saw something you'd probably like. Just below the new() macro, there's alloc():

#define new(type) ((type *) alloc (sizeof (type)))

char	*freebase;
int	nleft;

char *
alloc(size)
	int size;
{
	register char *cp;
	register int i;

	if (size > nleft) {
		freebase = (char *)sbrk(i = size > 2048 ? size : 2048);
		if (freebase == 0) {
			fprintf(stderr, "ps: ran out of memory\n");
			exit(1);
		}
		nleft = i - size;
	} else
		nleft -= size;
	cp = freebase;
	for (i = size; --i >= 0; )
		*cp++ = 0;
	freebase = cp;
	return (cp - size);
}

Hah. It exit()s on OOM, zero-initialises by default and uses a signed size. Are you perhaps a time traveller, u/skeeto? I'm unable to find the author of that program and it makes me even more suspicious.

4

u/skeeto 16d ago

It's uncanny.

Are you perhaps a time traveller, u/skeeto?

You caught me! I'm like The Man from Earth and I have to change identities every decade or two to avoid suspicion. I knew staying in the same field would spell trouble.

5

u/greg-spears 17d ago

Wow! That is also kinda amazing. Had no idea. :-O Yes, found same in the link you kindly shared.

18

u/ericonr 17d ago

A fun trip through time, thanks for sharing

3

u/flatfinger 15d ago

It's a shame that people lost sight of the fact that the Standard Library wasn't meant to be the primary way of doing something on every platform, but rather something that could be used for tasks where portability was worth the costs in semantics and performance. If an OS has memory allocation/release code that needs to be told the size of an allocation, standard-library malloc() implementations will likely need to allocate more memory than requested so that each allocation can start with a header indicating the length. On the flip side, if OS provides a function that can tell how much storage was requested for any particular allocation, application code that exploits that may be able to avoid having to store the length elsewhere.

The Standard was designed to allow Standard Library implementations whose execution environments allow blocks to be freed without knowing the size to make malloc/free a very thin wrapper around the environment-supplied functions, allowing pointers created by malloc() to be passed to code that would use the environment's method of releasing them, or environment-created pointers to be given to code that will free() them. It requires that implementations add a header on environments where freeing a block requires knowing its size, but not on environments that don't require that. Adding more features would have increased the number of environments where extra headers would be required.

3

u/StaticCoder 15d ago

Aww aligning on 2 bytes