Regarding overloading new and delete operators

R

rohits123

To optimize the memory usage , I am using a huge block of memory for
my system and
then dividing the initial chunk in to 4 pools.

I have overloaded new and delete such that memory from a particular
pool can be taken and
freed .
As far as possible memory can be taken from particular pool and memory
is freed (comlete pool as whole ) when pool is completely used.

By overlaoding new and delete , I can pass the information about a
particular pool say:

CMyClass* myClass = new (POOL_1) CMyClass;
....
delete(POOL_1) s;

But the problem is while using the abtsract containers say vector, I
am not able the POOL related information till new and delete which are
used inside allocator.

I am using the userdefined allocator :
vector<int,MySpace::MyAllocator<int> > v;

so that overloaded version of new and delete are called , but it is
not possible to pass the POOL related information in case of
containers .

Any suggestions ?
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

To optimize the memory usage , I am using a huge block of memory for
my system and
then dividing the initial chunk in to 4 pools.

I have overloaded new and delete such that memory from a particular
pool can be taken and
freed .
As far as possible memory can be taken from particular pool and memory
is freed (comlete pool as whole ) when pool is completely used.

By overlaoding new and delete , I can pass the information about a
particular pool say:

CMyClass* myClass = new (POOL_1) CMyClass;
...
delete(POOL_1) s;

But the problem is while using the abtsract containers say vector, I
am not able the POOL related information till new and delete which are
used inside allocator.

I am using the userdefined allocator :
vector<int,MySpace::MyAllocator<int> > v;

so that overloaded version of new and delete are called , but it is
not possible to pass the POOL related information in case of
containers .

Any suggestions ?

I've never tried this myself so I don't know if it's possible, but
couldn't you add the pool as a parameter to the allocator?

template<class T, int P = 0>
class MyAllocator
{
const int pool_;

public:
MyAllocator() : pool_(P) { /* ... */ }
};

And you could use it something like:

vector<int,MySpace::MyAllocator<int, 2> > v;

or, if the default pool is ok then just

vector<int,MySpace::MyAllocator<int> > v;
 
R

rohits123

I've never tried this myself so I don't know if it's possible, but
couldn't you add the pool as a parameter to the allocator?

template<class T, int P = 0>
class MyAllocator
{
const int pool_;

public:
MyAllocator() : pool_(P) { /* ... */ }

};

And you could use it something like:

vector<int,MySpace::MyAllocator<int, 2> > v;

or, if the default pool is ok then just

vector<int,MySpace::MyAllocator<int> > v;

It seems to be working.
Thanks alot Erik.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top