Regarding overloading new and delete operators



 DEVELOP > c-Plus-Plus > Regarding overloading new and delete operators

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 31 Jan 2007 11:34:55 PM
Object: Regarding overloading new and delete operators
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 ?
.

User: "=?iso-8859-1?q?Erik_Wikstr=F6m?="

Title: Re: Regarding overloading new and delete operators 01 Feb 2007 02:16:29 AM
On Feb 1, 6:34 am,
wrote:

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 =3D 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 =3D 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;
--
Erik Wikstr=F6m
.
User: ""

Title: Re: Regarding overloading new and delete operators 01 Feb 2007 04:56:47 AM
On Feb 1, 5:16 pm, "Erik Wikstr=F6m" <eri...@student.chalmers.se> wrote:

On Feb 1, 6:34 am,

wrote:





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 =3D 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 =3D 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;

--
Erik Wikstr=F6m- Hide quoted text -

- Show quoted text -

It seems to be working.
Thanks alot Erik.
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER