🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by Alan Gray Wed, 09 Aug 1995 07:09
overloaded new/delete
#1683
Author: Alan Gray
Date: Wed, 09 Aug 1995 07:09
112 lines
2742 bytes
I pose the following problem.

Given a personalised allocator

    class Malloc {

    public:

      Malloc();

      void *allocate(size_t sz);
      void  deallocate(void *ptr);
    };

    void *operator new(size_t sz,Malloc *m=0)
    // ------------------------------------------------
    // here sz == sizeof(class/struct)
    // ------------------------------------------------
    {
      if(m) {
        return(m->allocate(sz));
      } else {
        return(::operator new(sz));
      }
    }
    void *operator new[](size_t sz,Malloc *m=0)
    {
      if(m) {
        return(m->allocate(sz));
      } else {
        return(::operator new(sz));
      }
    }

and then use Malloc to allocate a series of objects,
then finally delete the objects, how can this be achieved.
The draft standard does not cover this.

    class X {
    public:
      X();
      virtual ~X();
    };
    class Y : public X {
    public:
      Y();
     ~Y();
    };

    void fred(Malloc *m)
    {
      double *xarray = new(m) double[1024];
      double *yarray = new(m) double[1024];
      double *zvalue = new(m) double;
      X      *xone   = new(m) Y[1024];
      X      *xtwo   = new(m) Y;
      ....
      ....


      delete [] xarray;   // eek - gets the wrong version of ::operator 
delete(size_t sz)
      delete [] yarray;   // dito
      delete    zvalue;   // dito
      delete [] xone;     // dito
      delete    xtwo;     // dito
    }

do we need to be able to overload as follows

    void operator delete(void *ptr,Malloc *m=0)
    {
      if(m) {
        m->deallocate(ptr);
      } else {
        ::operator delete(ptr));
      }
    }
    void operator delete[](void *ptr,Malloc *m=0)
    {
      if(m) {
        m->deallocate(ptr);
      } else {
        ::operator delete(ptr));
      }
    }

then fred will look like

    void fred(Malloc *m)
    {
      double *xarray = new(m) double[1024];
      double *yarray = new(m) double[1024];
      double *zvalue = new(m) double;
      X      *xone   = new(m) Y[1024];
      X      *xtwo   = new(m) Y;
      ....
      ....


      delete(m) [] xarray;
      delete(m) [] yarray;
      delete(m)    zvalue;
      delete(m) [] xone;
      delete(m)    xtwo;
    }


Alan Gray
4D Solutions
PO BOX 103 Narrabeen NSW 201
Australia
Re: overloaded new/delete
#1690
Author: Steve Clamage
Date: Wed, 09 Aug 1995 15:26
65 lines
2461 bytes
In article nta@oznet03.ozemail.com.au, agray@ozemail.com.au (Alan Gray) writes:
>I pose the following problem.
>
>Given a personalised allocator
>
>    class Malloc {
>
>    public:
>
>      Malloc();
>
>      void *allocate(size_t sz);
>      void  deallocate(void *ptr);
>    };
>
>    void *operator new(size_t sz,Malloc *m=0)	<=== this will cause an ambiguity
>    // ------------------------------------------------
>    // here sz == sizeof(class/struct)
>    // ------------------------------------------------
>    {
>      if(m) {
>        return(m->allocate(sz));
>      } else {
>        return(::operator new(sz));		<=== ambiguous call
>      }
>    }


Both the predefined operator new and your placement version can be called
with a single size_t argument. You have to remove the default argument
value, which will give the effect you appear to want.
	void* operator new(size_t sz, Malloc* m)
	{
		if( m ) return m->allocate(sz);
		... otherwise do something about the error
	}
Thus:
	... new T ... // uses predefined operator new
	... new (&allocator) T ... // uses your operator new

>
>do we need to be able to overload as follows
>
>    void operator delete(void *ptr,Malloc *m=0) ... <=== same ambiguity problem
>
> ... The draft standard does not cover this.

It is covered. There is no syntax for getting an overloaded operator delete
via a delete-expression. You can define a "placement delete", but it is
called only if
 - a constructor invoked from a placement-new expression exits via an exception, or
 - you call the operator delete explicitly.
That is:
	T* p ...
	delete p; // never calls a placement delete
	// but you can do this
	p->~T(); // call destructor
	operator delete(p, &allocator);

You can use class-specific operator new and delete in an Allocator base
class, and these will be inherited by any class derived from Allocator.
You can find examples of how to do this in "The C++ Programming
Language", 2nd edition and in other books.
---
Steve Clamage, stephen.clamage@eng.sun.com
Re: overloaded new/delete
#1692
Author: Scott Meyers
Date: Wed, 09 Aug 1995 18:07
18 lines
1129 bytes
In article <40ak2i$9oa@engnews2.Eng.Sun.COM> clamage@Eng.Sun.COM writes:
| It is covered. There is no syntax for getting an overloaded operator delete
| via a delete-expression. You can define a "placement delete", but it is
| called only if
|  - a constructor invoked from a placement-new expression exits via an exception, or
|  - you call the operator delete explicitly.
| That is:
| 	T* p ...
| 	delete p; // never calls a placement delete
| 	// but you can do this
| 	p->~T(); // call destructor
| 	operator delete(p, &allocator);

Okay, what am I missing?  DWP 3.7.3.2 says that if operator delete has a
second parameter, it must be of type size_t.  How can &allocator be of that
type? 

Scott
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads