# C++ Object Pool

## Recommended Posts

Hi!

I wrote object pool. This is single-header library for fast object allocation. All objects inside of the pool placed in a contiguous memory block to be cache-friendly. Detailed description is in repository.

Here is some examples:

class Foo {
private:
int fooBar;
public:
Foo() {}
Foo(int foobar) : fooBar(foobar) {}
};

...

Pool<Foo> pool(1024); // make pool with default capacity of 1024 objects
Handle<Foo> bar = pool.Spawn(); // spawn object with default constructor
Handle<Foo> baz = pool.Spawn(42); // spawn object with any args

// do something

// return object to pool
pool.Return(bar);
pool.Return(baz);

Results of tests are in attached files.

Please, criticize it and give me some feedback.

##### Share on other sites

You currently use a global stamp counter. That will overflow relatively quickly (still an eternity but whatever).

I believe what you want, instead, is to increment the stamp of each container slot independently. Comparison of two stamps should be valid only if index is equal - so its ok if two indices have the same stamp. So stamps are per-index.

With the global counter, IsValid can start returning false positives as soon as you do 32^2 insertions over the entire container (which is a problem if the pool has many objects in it - the insertions will add up).

With per-index counters, you will only get the problem if all those insertions are to the exact same index/slot. If theyre spread over the entire container (as they naturally should be), the stamp counters will keep incrementing slowly (minimizing chance of bugs from IsValid false positives).

The real benefit is that you can switch to 1 or 2 byte stamps (I recall someone using 2 bytes) and it should still take forever for potential ambiguity to occur (tens of thousands of reuses of the exact same index, before the first stamps get reused). With current global stamp that wouldnt be safe (the counter would wrap around way too quickly since it accumulates all insertions over entire container).

Of course you cant reset the per-index stamps (or you lose the count), so the 'free' flags and such need to get their own variable (instead of being specific stamp values), or use a few bits of the stamp counter for those flags (if you do the latter, ensure those bits dont interfere with comparison, and that overflow doesnt corrupt the bits).

## Create an account

Register a new account

• 9
• 10
• 12
• 10
• 10
• ### Similar Content

• Hi,

I am working on a project where I'm trying to use Forward Plus Rendering on point lights. I have a simple reflective scene with many point lights moving around it. I am using effects file (.fx) to keep my shaders in one place. I am having a problem with Compute Shader code. I cannot get it to work properly and calculate the tiles and lighting properly.

Is there anyone that is wishing to help me set up my compute shader?
Thank you in advance for any replies and interest!

• I'm looking for some references that could help me learn how to program some really basic 2D enemy behaviours.
I wasn't sure whether to post this here or in the AI section but I think it might be more suitable to be posted here since it has more to do with basic maths than any AI related algorithms.
Could anyone help recommend some resources (books, posts, videos) that could help me understand how to properly implement the basics of enemy movement in 2d games ? So far I've only managed to get them to chase the player character and to stop moving on collision, but the movement is pretty unrealistic and once the collision occurs the enemies all "pile up" on the player character. I'm doing this in C++ so no guides that explain how to script this using an engine api please.

• Good Evening,
I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
I am really stucked right now because of the fundamental question:
Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on.
In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
Should I treat those debug objects as entities/components?
For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
Regards,
LifeArtist
• By nickyc95
Hi.
I'm kind of late to this party but I thought I would ask anyway as I haven't found a concrete answer.

When creating a game engine, when should you choose one methodology over another (more specifically OOP and DOD)? Which areas benefit from DOD? Which areas benefit from OOP? Do people typically mix multiple methodologies throughout a project? I.e. certain sub-systems created in one, and others in the another?
DOD - Data Oriented Design
OOP - Object Oriented Design

Pretty simple
Thanks
• By Estra
Memory Trees is a PC game and Life+Farming simulation game. Harvest Moon and Rune Factory , the game will be quite big. I believe that this will take a long time to finish
Looking for
Programmer
1 experience using Unity/C++
2 have a portfolio of Programmer
3 like RPG game ( Rune rune factory / zelda series / FF series )
4 Have responsibility + Time Management
and friendly easy working with others Programmer willing to use Skype for communication with team please E-mail me if you're interested
Split %: Revenue share. We can discuss. Fully Funded servers and contents
and friendly easy working with others willing to use Skype for communication with team please E-mail me if you're interested
we can talk more detail in Estherfanworld@gmail.com Don't comment here
Thank you so much for reading
Memory Trees : forget me not

Thank you so much for reading
Ps.Please make sure that you have unity skill and Have responsibility + Time Management,
because If not it will waste time not one but both of us