Regarding overloading new and delete operators

Discussion in 'C++' started by rohits123@gmail.com, Feb 1, 2007.

  1. Guest

    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 ?
    , Feb 1, 2007
    #1
    1. Advertising

  2. 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 = 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;

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 1, 2007
    #2
    1. Advertising

  3. Guest

    On Feb 1, 5:16 pm, "Erik Wikström" <> 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 = 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;
    >
    > --
    > Erik Wikström- Hide quoted text -
    >
    > - Show quoted text -


    It seems to be working.
    Thanks alot Erik.
    , Feb 1, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Natalie
    Replies:
    0
    Views:
    395
    Ron Natalie
    Jul 9, 2003
  2. HeroOfSpielburg
    Replies:
    1
    Views:
    389
    Alf P. Steinbach
    Aug 6, 2003
  3. Nimmi Srivastav

    Overloading new and delete operators

    Nimmi Srivastav, Jan 31, 2004, in forum: C++
    Replies:
    3
    Views:
    2,676
    John Harrison
    Feb 2, 2004
  4. Douglas Peterson
    Replies:
    1
    Views:
    2,764
    Larry Brasfield
    Jan 22, 2005
  5. tharinda_g
    Replies:
    0
    Views:
    324
    tharinda_g
    Jun 24, 2008
Loading...

Share This Page