In article <3d400f58-1b60-40dc-81bc-01e5e692fdcb@
59g2000hsb.googlegroups.com>,
says...
> Hi!
>
> I've got a pretty complex data structure that keeps instances of
> structs allocated with the new operator.
> Now my issue is that those structures are required to be allocated /
> deallocated pretty often resulting
> in a huge perfomance issue. Usually what I am doing is to create
> arrays and initialize their sizes a few
> times bigger than originally required to avoid another allocation on
> the next run. Then I do simply reset them
> and do a memcpy at the existing location. This works great with POD
> structus but my structs are complex
> in the way that they not only use inheritance but they do also use
> internal objects that do require an
> constructor / destructor. So what is the general way to handle such
> things? Am I missing something?
You probably want to overload operator new for the class(es) of the
objects you're allocating/freeing so frequently. At startup, your
operator new will grab a big chunk of memory for the data, and break it
up into chunks, each the size of a single object. As objects are
allocated, it'll give out the addresses of object-sized chunks, and mark
each chunk as being in use when it does so. When an object is freed,
it'll mark each chunk as being available again.
You can probably find existing source code for a few different versions
of this -- Andrei Alexandrescu's Loki library has one, and Boost has
another, and so on.
--
Later,
Jerry.
The universe is a figment of its own imagination.