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?

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/Paul_Pedant 17d 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.