Archived

This topic is now archived and is closed to further replies.

LeoMaheo

Overloading ::new and ::delete

Recommended Posts

Hi! I''m writing a threaded application but the hardware (GameBoyAdvance) doesn''t support threads: ''new'' and ''delete'' aren''t thread safe. One solution is: #define NEW(ptr, constructor) DISABLE_THREAD_SWITCHING(); \ (ptr) = new (constructor); \ RESTORE_THREAD_SWITCHING(); But I rather avoid macros and overload ''new'' instead: void* operator new(size_t s ) throw() { DISABLE_THREAD_SWITCHING(); void* p = malloc(s ); RESTORE_THREAD_SWITCHING(); return p; } but as far as I know, ''operator delete'' can''t be overloaded in the same way - then I have to use macros anyway ? And worse, since the std-c++-lib uses both ''new'' and ''delete'' I really need to get a thread-safe ''delete''. Do I really have to make thread-safe interfaces to the std-lib classes? for example template class ListInterface : private List { void push_front(const T& x) { DISABLE_THREAD_SWITCHING; List:ush_front(x); RESTORE_THREAD_SWITCHING; } } Has anyone encounterd a problem similar to this before and has some good idea? Perhaps you know how to make ''new'' and ''delete'' thread-safe in some good way (And don''t you think that the designers of C++ ought to have allowed ''delete'' to be overloaded, or done something else that solves the problem ? Thanks for your time

Share this post


Link to post
Share on other sites
on MSDN, do a search on "the operator delete function" -thats the article title...

delete takes a void pointer.. *duh*
void delete[](void* vp){}

[edited by - evilcrap on July 29, 2002 11:41:05 AM]

Share this post


Link to post
Share on other sites
I''m kinda curious, how are you compiling C++ code for the GBA? I didn''t think a C++ compiler was available for it...

I would just overload the global new & delete, set the nmi (non-maskable interupt) bit in the GBA and call into malloc & free. That assumes there''s no virtual memory to deal with (unless they also added harddrives and a protected mode to the GBA...)

Share this post


Link to post
Share on other sites
quote:
I''m kinda curious, how are you compiling C++ code for the GBA? I didn''t think a C++ compiler was available for it...


Have a look at
http://www.io.com/~fenix/devkitadv/
or
http://www.ngine.de/ham.html


Right now I do this (well, thanks everybody, overloading ''delete'' did work) :

  
void* operator new(size_t s) throw() {
DISABLE_THREAD_SWITCHING();
void *p = malloc(s);
RESTORE_THREAD_SWITCHING();
return p;
}

void operator delete(void* p) throw() {
DISABLE_THREAD_SWITCHING();
free(p);
RESTORE_THREAD_SWITCHING();
}


Do you know if this code works correctly for all klasses and virtual classes, virtual destructors, etc. ?

I still have a problem with the std::list etc.: they use the default ''new'' and ''delete'' even when I overload (/override ) them (perhaps some strange macro or #include? or allocator?? I really don''t no much about allocators ) - does anyone know if one can make the std-libraries to use my own new/delet ?

Share this post


Link to post
Share on other sites
quote:
Original post by LeoMaheo
I still have a problem with the std::list etc.: they use the default ''new'' and ''delete'' even when I overload (/override ) them (perhaps some strange macro or #include? or allocator?? I really don''t no much about allocators ) - does anyone know if one can make the std-libraries to use my own new/delet ?



Most implementations of STL do far out, freaky things with macros and system functions, with the result that they probably never even bother to call a function called "new".

The easiest, and most Right Way to do this, is to write your own custom allocator. It''s surprisingly painless. Check the STL docs.



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites