title: simple and portable malloc library
body: <p>I am currently developing on the Sega Saturn.</p>
  
  <p>The console has several dedicated chips; some of which have their own dedicated RAM and/or VRAM.  An example would be how main RAM is split into two different 1MB zones.</p>
  
  <p>I am searching for a generic, portable, and small <code>malloc</code> library that will allow me to declare different RAM zones and then allow me to <code>malloc</code> or <code>free</code> within those zones.</p>
  
  <p>An example would be the vdp2 graphic chip.  It has a dedicated VRAM zone for the color palette of screen mode 8b.</p>
  
  <p>Here, I could use a classic method and preload palettes that worked for all game graphics,
  but why not only load palettes that are actually in use by my current graphics?</p>
  
  <p>Here I need a system to allocate and free colors (palette entries), but I wish it to fail if the user attempts to allocate more than 255 colors - as this is the maximum palette size.</p>
  
  <p>Basically, I want to be able to declare a few dynamic memory zones and alloc/free into them:</p>
  
  <pre><code>u32 palRam  = defineMallocZone(COLOR_PALETTE,COLOR_PALETTE_SIZE);
  u32 m68kRam = defineMallocZone(M68KVRAM,M68KVRAMSIZE);
  u32 highRam = defineMallocZone(HIGHRAM,1024*1024);
  
  u16* my64colors = (u16*)magicMalloc(palRam,64*sizeof(u16));
  </code></pre>
  
  <p>The main <code>malloc</code> of the tool-chain that came with the Saturn did not worked out of the box, so I quickly wrote this shit:</p>
  
  <pre><code>#define mallocSizeK 512
  #define mallocSizeB mallocSizeK*1024
  
  void * mymalloc(u32 n)
  {   static u8 m[mallocSizeB];
      static u32 c=0;
      if(!n) return (void*)&amp;c;
      while(n%4) n++;
      u32 nn = c+n;
      if(nn &gt; mallocSizeB) return 0;
      u8 * p = &amp;m[c]; c = nn; memset(p,0,n);
      return (void*)p;
  }
  </code></pre>
  
  <p>It's ok for my current tests, but it won't be fine in the long run, (It really need a free! But this is not what I want overall =) )</p>
  
  <p>To summarize, I need a minimalistic, basic, and simple piece of code to do handle memory zones as outlined above.  For now, the memory management algorithm need not be efficient, I just want it to work.  Or maybe can you send me a link to a classic and simple memory management algorithm that I can try to implement myself?</p>
  
  <p>edit > ok i done it myself, <a href="https://gist.github.com/r043v/6100683" rel="nofollow">here is a gist</a></p>
  
  <p>not very evolved but work fine with this test :</p>
  
  <pre><code>u32* b1 = bnew(M68KVRAM,512);
  void*mtest[500]; u32 allocSize = 8;
  u32 n = 0, nb=0, total=0;
  
  while(n&lt;500){
      u32 sz = rand()%(allocSize*1024);
      void *a = bmalloc(b1,sz);
      if(!a) break;
      memset(a,1,sz);
      mtest[n++] = a;
      total += sz;
  };
  
  nb = n; printlr("allocated %u/512 kb in %u 0~%uk blocs",total&gt;&gt;10,nb,allocSize);
  
  printl("free them all .. ");
  u32 freertn = 0; for(n=0;n&lt;nb;n++) freertn |= (u32)bfree(b1,mtest[n]);
  if(freertn) printlr("fail"); else printlr("ok");
  
  *mtest = bmalloc(b1,512*1024);
  if(*mtest) memset(*mtest,200,512*1024);
  printlr("final 512k allocation return : 0x%x",*mtest);
  </code></pre>