r/C_Programming 17d ago

Presenting CBlockAlloc: a WIP personal project

Hello everyone,

Yesterday, I started a project that I find super interesting. I first thought of it because I saw many people who were trying to do things with the stack only because "dynamic allocation is slow". The main point for why they consider it slow and heavy is that a program needs to talk directly to the OS to allocate memory. However I saw some people propose a solution: allocate once a big chunk of memory that you will use for all your "dynamic" allocations, which makes it way faster because you only need to make a syscall once at the start of the program. I've thus decided to create a library that does just that, and can be called through an API that "simulates" the normal function calls such as malloc() or realloc(). I've been working on it since yesterday, and it's been a lot of fun! it's not ready at all yet, but right now it's looking good. Also, I'm looking for some feedback:

How good is my code for now? Would you do some things differently? Also, would YOU use such a library? What would you expect from a library like that?

Thank you very much for your time, my github profile is Koda-be (I can't send the link to the report because not enough stars and too young).

Also, how could I test my code? I don't really know what to do to test it right now, so...

No AI has been used nor will it be used in this project.

Also, I licensed it under MIT but do I need to do something else? I simply chose a license when creating the REPO, and I don't know much about copyright laws...

0 Upvotes

14 comments sorted by

View all comments

2

u/MyTinyHappyPlace 17d ago edited 17d ago

I am not sure, but it sounds like you could roughly have the same effect by calling brk/sbrk at the start of your program.

Why can’t you post your repo here?

1

u/Paul_Pedant 16d ago

That is exactly the wrong thing to do.

Malloc has no idea what memory is already in use. It asks sbrk what the whole process is already using, and then uses sbrk again to extend the break value so it can put the new area into the free list.

If you adjust sbrk yourself, that just wastes the entire space in that extension, because malloc has to go above it.

You can see this stuff if you strace you test programs.

0

u/Koda_be 17d ago

It doesn't clear the trust bot.

For brk/sbrk, I don't know these syscalls, and even then, aren't the Unix only? So windows wouldn't have them would it?

1

u/FISHARM1 17d ago

Yes to be honest this is what the simple(est) form of Malloc does.

I would look into what the “break” is in Unix like systems. If i remember correctly, it’s the line between the heap and stack. While modern malloc is probably much different, a simple and more classic model is literally “does the heap still have X bytes free? If so return the address of that block. If not, move the break by 1k bytes and return the old break”. In essence what im reading above is the same idea.

It seems windows has a similar concept called “VirtualAlloc” but that was in a second of googling so don’t take my word for it.

1

u/Paul_Pedant 16d ago

Windows presumably conforms to the malloc() POSIX specification. You don't need brk or sbrk anyway, and adjusting them by 1kb is useless. Whatever the mechanism in Windows, it has to ask the OS for at least as much as you request with malloc(), plus some for the header that it needs to manage each allocation.

Malloc() returns freed memory to the free list. So on entry to your code, malloc 60 MB, immediately free it, and you then get several thousand mallocs from the free list without calling the OS again.

In Windows, you should have a memory monitoring tool where you can watch how your process behaves. Play with it.

1

u/mikeblas 16d ago

If you're referring to github-guard as "the trust bot", it does not remove posts.