Am 30.01.2014 09:18, schrieb Joe The Smoe:
> to manage memory allocation for a multi-threaded application and reduce
> the use of mutex, each thread is given an object of a class named "pool"
> which allocates, recycles and eventually releases a set of memory
> blocks (the classical memory pool).
>
> The different objects allocated by each thread can now be allocated
> using the given pool object thanks to a placement new and new[]
> operators given to each concerned class.
>
> The problem I meet is that I have something working pretty well except
> for the placement new[] which places the first element of the array 4
> bytes after the address returned by the new[] operator.
This is a quite typical strategy, namely to be able to record the number
of elements the corresponding delete[] must destroy.
> This causes me
> some problem at the time the array has to be released because the memory
> address of the array (the address of the first element) does not match
> the address given by the pool to the placement new[] operator (for the
> pool this is not a memory block that it has allocated).
How, actually, are you releasing the memory? If you delete the objects
by delete[], as you should (and not delete, without the brackets), you
should get the right memory address, adjusted with the offset. If this
is not the case, it is possibly a compiler bug (g++ does *not* show this
bug).
Note well you need to provide four operator deletes:
operator delete(void *);
deletes objects. You need to find your pool from the object. This delete
is used for regular delete in the program flow, even if deleting objects
from the pool.
operator delete[](void *);
Used for regular array destruction.
operator delete(void *,Pool);
used if an operator-new fails in the construction of an object in the
initializer list
operator delete[](void *,Pool);
the same for arrays.
Note specifically that your regular delete operator does *not* get the
pool passed in, even if it needs it. It's up to your new()
implementation to store it in an appropriate place if required. Plus,
"placement new" does not match "placement delete" as one may think.
"placement new" is complemented by "regular delete" plus "placement delete".
Greetings,
Thomas
> to manage memory allocation for a multi-threaded application and reduce
> the use of mutex, each thread is given an object of a class named "pool"
> which allocates, recycles and eventually releases a set of memory
> blocks (the classical memory pool).
>
> The different objects allocated by each thread can now be allocated
> using the given pool object thanks to a placement new and new[]
> operators given to each concerned class.
>
> The problem I meet is that I have something working pretty well except
> for the placement new[] which places the first element of the array 4
> bytes after the address returned by the new[] operator.
This is a quite typical strategy, namely to be able to record the number
of elements the corresponding delete[] must destroy.
> This causes me
> some problem at the time the array has to be released because the memory
> address of the array (the address of the first element) does not match
> the address given by the pool to the placement new[] operator (for the
> pool this is not a memory block that it has allocated).
How, actually, are you releasing the memory? If you delete the objects
by delete[], as you should (and not delete, without the brackets), you
should get the right memory address, adjusted with the offset. If this
is not the case, it is possibly a compiler bug (g++ does *not* show this
bug).
Note well you need to provide four operator deletes:
operator delete(void *);
deletes objects. You need to find your pool from the object. This delete
is used for regular delete in the program flow, even if deleting objects
from the pool.
operator delete[](void *);
Used for regular array destruction.
operator delete(void *,Pool);
used if an operator-new fails in the construction of an object in the
initializer list
operator delete[](void *,Pool);
the same for arrays.
Note specifically that your regular delete operator does *not* get the
pool passed in, even if it needs it. It's up to your new()
implementation to store it in an appropriate place if required. Plus,
"placement new" does not match "placement delete" as one may think.
"placement new" is complemented by "regular delete" plus "placement delete".
Greetings,
Thomas
Aucun commentaire:
Enregistrer un commentaire