In C++, the equivalent is "placement new", which is how you manually call a constructor on a block of memory (whereas regular new allocates memory and calls the constructor). However, you've got to be very careful doing this kind of stuff, if you want to keep regular C++ behaviour -- destructors won't be called when the memory goes out of scope, so when using placement-new, you must also manually call the destructors of your objects at the appropriate times.
Just like old times.
Instead of overriding the new operator with my own version, I've chosen to invent my own keyword via a macro - eiNew (ei is my engine's "macro prefix", short for "eight", the name of the engine). If I use any 3rd party code that relies on new, then it behaves as usual.
That's a good idea. My allocator didn't override malloc() at link time like many would, so I suppose I'm familiar with that coexistence.
My eiNew macro uses a stack allocator to grab enough memory for the new object, then uses placement new to construct the object in that area. It also adds the address of the object to a linked-list belonging to a "scope" object (which is a RAII-type object), which is used to call the destructor when the scope is destructed.
An object that maintains the scope of the variables, and calls their destructors for them? Very nice.
But for cases where I want to use memory other than the built-in call stack, my eiNew macro mimics this regular behaviour for my own buffers:
I see that the stack pointer simply increments whenever you need memory. My allocator allows for private arenas and memory pools that can be created in a function call and make a stack allocator, but I'm sure that it's nowhere near as fast as a simple linear allocator.
Most of the time when I use malloc, it's to grab big buffers, like "buffer" above, and then I use eiNew for everything.
Reading through, I assume that it means that you only ever allocate objects in order, and the lifetime of your objects follow a strict schedule. Do you ever have a need to allocate an object whose lifetime is not decided by the place where it is allocated?