home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18376 < prev    next >
Encoding:
Text File  |  1992-12-23  |  3.4 KB  |  83 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!microsoft!hexnut!jimad
  3. From: jimad@microsoft.com (Jim Adcock)
  4. Subject: Re: overloading operator new
  5. Message-ID: <1992Dec23.221234.25999@microsoft.com>
  6. Date: 23 Dec 92 22:12:34 GMT
  7. Organization: Microsoft Corporation
  8. References: <1992Dec18.125656.4293@meadow.uucp>
  9. Lines: 72
  10.  
  11. In article <1992Dec18.125656.4293@meadow.uucp> marc@meadow.UUCP (Marc Riehm) writes:
  12. |I want to be able to take control of memory management in c++, and I've run
  13. |into a problem with overloading operator new.  I have c++ classes which
  14. |provide memory "pool" management: these act as a layer over malloc().  What I
  15. |want to do is to be able to specify which pool to allocate from when I
  16. |instantiate an object of another class.
  17. |
  18. |Thus, say I had class MyObject -- I want to be able to instantiate instances
  19. |of this class in some manner like...
  20. |MyObject *myObj1 = new MyObject(bigHeap)   where bigHeap is a memory pool,  or
  21. |MyObject *myObj2 = new MyObject(altHeap)  etc.
  22. |The store for myObj1 would come from bigHeap, and that for myObj2 from altHeap.
  23. |
  24. |My Turbo C++ 2.0 manual (the only C++ documentation I have available :( )
  25. |says that the first argument of MyObject::new must be a size_t.  i.e. we
  26. |overload new via a method which looks like...
  27. |void *MyObject::new(size_t)
  28. |They imply that there can be other arguments to MyObject::new.  Could anyone
  29. |tell me whether or not this is true and, if it is, how to declare the method
  30. |and how to use MyObject::new?  Are there any side effects on the constructor?
  31.  
  32. You're on the right track.  What you need to use is the "placement" 
  33. option to operator new.  "Placement" refers to the ability of operator
  34. new to be overloaded with additional parms, for example an additional
  35. void* parameter that specifies where an object is to be created:
  36.  
  37. void* MyObject::operator new(size_t, void* place) 
  38. {
  39.     return place;
  40. }
  41.  
  42. This gets used using the following "funny" syntax, for example:
  43.  
  44.     allocAddress* allocation = new malloc(sizeof(MyObject);
  45.  
  46.     MyObject* pOb = new (allocAddress) MyObject;
  47.  
  48. Note that I'm merely explaining here where the term "placement" comes
  49. from, and showing you the weird location that where the extra parms
  50. get specified in using the "placement" operator new.  *You* can 
  51. specify the extra parameters to your operator new to be what you want,
  52. with a meaning that you choose -- you're not restricted to using
  53. these extra parameters to *mean* placement, its just just that in the
  54. common lingo these parameters have come to be *called* the "placement"
  55. parameters to operator new.  Here's a sketch of what you're talking about:
  56.  
  57. void* MyObject::operator new(size_t size, POOLSPECIFIER pool)
  58. {
  59.     return AllocateFromPool(pool, size);
  60. }
  61.  
  62. gets used as:
  63.  
  64.     MyObject* pOb = new (somePool) MyObject;
  65.  
  66. If you think about it, in a "normal" new the MyObject constructor can
  67. have its own parameters:
  68.  
  69.     MyObject* pOb = new MyObject(ctorParms);
  70.  
  71. So the "placement" operator new parameters have to be specified some
  72. other "weird" place:
  73.  
  74.     MyObject* pOb = new (placementParms) MyObject(ctorParms);
  75.  
  76. Thus solves the easy part of your problem.  A harder part of the problem
  77. becomes how you correctly DEallocate these allocations back to their
  78. proper pool, because there is no equivalent "placement" operators to 
  79. operator delete.  One MyObject::operator delete(void*) has to work 
  80. for all the pools.  If pools are implemented using contiguous chunks
  81. of memory for example, you could keep a sorted list of address
  82. ranges corresponding to the different pools.
  83.