Skip to content

Conversation

@LowByteFox
Copy link
Contributor

Adds Object Pool SingleSizeObjectPool to the stdlib, allows users to quickly allocate/reserve objects from the pool and to quickly deallocate/return objects back to the pool, adds module std::core::mem::objectpool and tests.

Example usage:

fn int main(String[] args)
{
	SingleSizeObjectPool pool;
	pool.init(mem, int);
	int *obj = pool.alloc();
	// do something with obj
	pool.dealloc(obj);
	return 0;
}

@LowByteFox LowByteFox changed the title Stdlib: implement SingleSizeObjectPool Stdlib: SingleSizeObjectPool implementation Aug 2, 2025
@lerno
Copy link
Collaborator

lerno commented Aug 2, 2025

It could be interesting to track:

  1. The total memory allocated by the pool
  2. The total memory in use (or free either works)

That would also help testing it for correctness.

Secondly, it would be interesting to have a concurrent version of the same. That might be something to consider in the future.

@LowByteFox
Copy link
Contributor Author

LowByteFox commented Aug 3, 2025

  1. The total memory allocated by the pool

I'm guessing to keep track of the buffer size and extra nodes (in bytes), right? Also could in theory try to allocate memory once and have it store both the node and the buffer, not sure if I can do something like flexible array member like in C, but then I suppose there will be alignment issues, unless I would align it to usz?

  1. The total memory in use (or free either works)

I'm assuming this should also be in bytes?

Secondly, it would be interesting to have a concurrent version of the same. That might be something to consider in the future.

I'm wondering how this could be done than maybe just protecting it with a mutex? I don't know, maybe some discussion could be had and implemented in the future.

@lerno
Copy link
Collaborator

lerno commented Aug 3, 2025

Well, given that we allocate them a slab at a time, we only need to see how many slabs we have created. Then the amount free is the number of elements in the pool.

@lerno
Copy link
Collaborator

lerno commented Aug 3, 2025

The simplest way is just protecting it with a mutex. That could be a different implementation, so no need to reuse exactly this one.

@lerno lerno merged commit 0e10b71 into c3lang:master Aug 4, 2025
43 checks passed
@lerno
Copy link
Collaborator

lerno commented Aug 4, 2025

Thank you for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants