gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Memory management

#127026 - Phantaseur - Sun Apr 29, 2007 11:06 am

What happens in NDS when I call 'malloc' function?
As I know my program have monopolistic access to all resources of NDS, so who provides memory management? Or memory manager is built in program by compiler when I use 'malloc' function? So can I manage memory manually adresating certain areas of RAM, can I have access to whole RAM not using memory allocation functions or write my own ones?

If I can overwrite 'malloc', 'realloc' & 'free' functions and create my own memory manager what can I do with 'new' & 'delete' operators of c++?

#127042 - tepples - Sun Apr 29, 2007 3:14 pm

Phantaseur wrote:
What happens in NDS when I call 'malloc' function?
As I know my program have monopolistic access to all resources of NDS, so who provides memory management? Or memory manager is built in program by compiler when I use 'malloc' function?

Yes. The C library 'newlib' implements 'malloc' and 'free' in terms of any part of main RAM that isn't allocated to code or global/static data.

Quote:
So can I manage memory manually adresating certain areas of RAM, can I have access to whole RAM not using memory allocation functions or write my own ones?

Correct.

Quote:
If I can overwrite 'malloc', 'realloc' & 'free' functions and create my own memory manager what can I do with 'new' & 'delete' operators of c++?

I believe the standard C++ library just calls 'malloc' and 'free'.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#127063 - Sausage Boy - Sun Apr 29, 2007 6:57 pm

You can define your own version of operator new. Here's a snippet of psuedo code found in the book Effective C++ (3rd edition) by Scott Meyers.

Code:
void* operator new(std::size_t size) throw(std::bad_alloc)
{
   using namespace std;
   if (size==0) {
      size = 1;
   }

   while (true) {
      attempt to allocate size bytes

      if (the allocation was successful)
         return (a pointer to the memory);

      new_handler globalHandler = set_new_handler(0);
      set_new_handler(globalHandler);

      if (globalHandler) (*globalHandler)();
      else throw std::bad_alloc();
   }
}


The book contains some comments for the code and some explanations, but at least this will give you some terms to google with. I suggest you buy the book. Basically what we're doing is trying to allocate memory in an infinite loop, and if we fail, we call the new handler to see if it can free some memory. If it can't, it'll remove itself and we throw an exception. Also, we need to provide a valid pointer for requests for 0 bytes, so we treat it as a request for 1 byte.

It is not operator new's responsibility to call constructors and stuff, that's handled by the new operator (riiiiight, not confusing :P). You can't change the new operator. But really, get the book, or perhaps some other book. My explanations can't even be compared to those from our friend Meyers.
_________________
"no offense, but this is the gayest game ever"