Topic: Overloading global delete
Author: "Ryan Eason" <reason@primary.net>
Date: 1997/12/19 Raw View
I am overloading the global new in my software with a extra argument
that
points to a class that handles memory management. Without going into
the
gory details...this works well. I wanted to do the same the global
delete
but find that there is a imposed limitation on the delete function that
it
can never take additional arguments. My question is what was the
reasoning
behind limiting the arguments of delete?
Here's a little detail....(not real code)
// create the memory manager
memoryManager myMemoryManager(memoryManagerArgs);
// create a class using the memory manager
someClass* x = new (myMemoryManager) someClass();
// to delete this I'd like to say
delete(myMemoryManager) x; //destructs and free's memory
// but because C++ won't let me overload delete with more args
// I have to say
x->~someClass(); //call destructor
myMemoryManager.Delete(x); //deallocate memory from manager
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: jodle@bix.com (jodle)
Date: 1997/12/21 Raw View
[Moderator's note: followups to comp.std.c++ only. -mod (fjh).]
Ryan Eason (reason@primary.net) wrote:
: I am overloading the global new in my software with a extra argument that
: points to a class that handles memory management. Without going into the
: gory details...this works well. I wanted to do the same the global delete
: but find that there is a imposed limitation on the delete function that it
: can never take additional arguments. My question is what was the
: reasoning behind limiting the arguments of delete?
I can't speak to the issue of the reasoning since I wasn't involved in the
standardization process and don't recall publication of this rationale.
It would seem to me that a key feature in the role of dynamically
allocated objects it their persistence outside the scope in which they're
allocated. This implies that information about the manner in which they
are allocated is not necessarily evident at the point of deletion. Just
look at the difficulties that arise because we alias memory allocated with
new or new[] with the same kind of pointer that we supposedly remember to
deallocate with delete and delete[] respectively.
It's my opinion that if the semantics of deletion for a type differ from
those for the traditional cases, the safe thing to do is to change the
type of handle that reference the type. If a special allocator is used,
and a special deallocator is required in association with that allocator,
a special pointer type should be provided.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1997/12/22 Raw View
Ryan Eason wrote:
>
> // create the memory manager
> memoryManager myMemoryManager(memoryManagerArgs);
>
> // create a class using the memory manager
> someClass* x = new (myMemoryManager) someClass();
>
> // to delete this I'd like to say
> delete(myMemoryManager) x; //destructs and free's memory
>
> // but because C++ won't let me overload delete with more args
> // I have to say
> x->~someClass(); //call destructor
> myMemoryManager.Delete(x); //deallocate memory from manager
I think the key is to modify your "operator new" so that it allocates enough
extra space at the beginning to hold a pointer to the memory manager, and then
return a pointer past that. Then, operator delete will automatically know what
memory manager to return each object to, without depending upon the programmer
to get it right:
// untested
void* operator new(size_t n, memoryManager& mm) {
void* p = 0;
if (!n) n = 1;
while (true) {
p = mm.allocate(n + sizeof(memoryManager*));
if (p) {
*reinterpret_cast<memoryManager**>(p) = &mm;
return reinterpret_cast<memoryManager**>(p) + 1;
}
if (new_handler_fn) (*new_handler_fn)();
else return 0;
}
}
void operator delete(void* p) {
if (p) reinterpret_cast<memoryManager**>(p)[-1]->free(p);
}
--
Ciao,
Paul
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Bill Wade" <bill.wade@stoner.com>
Date: 1997/12/24 Raw View
Ryan Eason <reason@primary.net> wrote in article
<pgpmoose.199712190950.6502@isolde.mti.sgi.com>...
> // to delete this I'd like to say
> delete(myMemoryManager) x; //destructs and free's memory
If your compiler supports template members you can make the syntax
myMemoryManager.Delete(x);
work. Otherwise you can make the syntax
Delete(myMemoryManager, x)
work. In either case Delete is a template function, based on the type of
*x.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: jag@xilinx.com (Jagadeesh Vasudevamurthy)
Date: Wed, 15 Jun 1994 00:37:05 GMT Raw View
We would like to overload new and delete operators globally.
The new operator should be able to take heap
(In our implementation heap is an abstract class) and the size
of the object. We know that it is possible to do in C++.
However global delete cannot be overloaded to pass heap and the size
of the objecte along with the pointer. In fact. we want delete
to look something like this:
delete(void *p, Heap* h, size_t size)
In C++ we are not able to overload global delete
(atleast in lucid C++ and g++). If the delete cannot be over loaded
in this fashion, then we should be able to get heap and the size
from the pointer (void *p) itself. I am wondering how this
can be done in most efficient way.
Can some one suggest a way to overcome this problem.
I usually don't read news. So, please direct all suggestions
to jag@xilinx.com
--
///\'/ Jagadeesh Vasudevamurthy
\\\ ` Xilinx Inc. | email: jag@xilinx.com
/// 2100 Logic Drive | (408) 879-4414
\\\/.\ San Jose, CA 95124 | (408) 559-7114 (FAX)
Author: ekaulaki@kaulakis.win.net (Edward C. Kaulakis)
Date: Fri, 17 Jun 1994 10:45:49 GMT Raw View
In article <1994Jun15.003705.28147@xilinx.com>, Jagadeesh Vasudevamurthy (jag@xilinx.com) writes:
....
>If the delete cannot be over loaded
>in this fashion, then we should be able to get heap and the size
>from the pointer (void *p) itself. I am wondering how this
>can be done in most efficient way.
>
>Can some one suggest a way to overcome this problem.
If you take control of ::new & ::delete then it should be easy
enough for you to implement heap& getheap(void* vp) as you suggest.
A feasible implementation has a heap& in the hidden header to a
new'd block...
>I usually don't read news. So, please direct all suggestions
>to jag@xilinx.com
>
As pure pragmatism, you'll get more help from the Net if you offer
to summarize back to the Net...
Cheers,
Ed Kaulakis