[comp.lang.c++] Making operator new smarter

rpk@rice-chex.ai.mit.edu (Robert Krajewski) (09/11/90)

Has anybody tried hacking operator new (size_t) to behave better
with typical C++ usage ?  In particular, it seems that the constant
copying and destruction of objects that have a new'ed part will
tend to result in allocations requests for a piece of memory that
is the same size as one that was just freed.

Robert P. Krajewski
Internet: rpk@ai.mit.edu ; Lotus: robert_krajewski.lotus@crd.dnet.lotus.com

mat@mole-end.UUCP (Mark A Terribile) (09/18/90)

In article <10564@life.ai.mit.edu>, rpk@rice-chex.ai.mit.edu (Robert Krajewski) writes:
> Has anybody tried hacking operator new (size_t) to behave better
> with typical C++ usage ?  In particular, it seems that the constant
> copying and destruction of objects that have a new'ed part will
> tend to result in allocations requests for a piece of memory that
> is the same size as one that was just freed.

For class types, this isn't very hard at all.  It's a few days work to
write a pool allocator, write an interface that will allow a class type
to get access to a pool of memory chunks of the appropriate size, and get
it all running with almost arbitrary classes.  Here it's important that
the destructors be virtual so that you get the correct operator delete
called.

This has no doubt been done about a dozen, a gross, and a score times;
howzabout a version in a library?

As an aside, if you are on a Sun under SonOS4.x there are some calls you
can make to tell  malloc()  to use simple pools for memory requests of
size  N  or less.
-- 

 (This man's opinions are his own.)
 From mole-end				Mark Terribile

mjv@objects.mv.com (Michael J. Vilot) (09/24/90)

Mark Terribilie pointed out that class-specific opertors new and delete can be
tuned to provide efficient pool management.

One small point I'd like to comment on:  it's wise to use the forms:

  class Managed {
  public:
    ...
    void* operator new(size_t);
    void  operator delete(void*, size_t);
  };

because these operators are inherited.  That is, invoking `new' on a class
derived from `Managed' will invoke Managed::operator new(), with the size of
the derived class.

Writing the pool management implementation used by new/delete should account
for the fact that derived objects may be larger than base objects.

--
Mike Vilot,  ObjectWare Inc, Nashua NH
mjv@objects.mv.com  (UUCP:  ...!decvax!zinn!objects!mjv)