I am currently developing on the Sega Saturn.
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.
I am searching for a generic, portable, and small malloc
library that will allow me to declare different RAM zones and then allow me to malloc
or free
within those zones.
An example would be the vdp2 graphic chip. It has a dedicated VRAM zone for the color palette of screen mode 8b.
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?
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.
Basically, I want to be able to declare a few dynamic memory zones and alloc/free into them:
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));
The main malloc
of the tool-chain that came with the Saturn did not worked out of the box, so I quickly wrote this shit:
#define mallocSizeK 512
#define mallocSizeB mallocSizeK*1024
void * mymalloc(u32 n)
{ static u8 m[mallocSizeB];
static u32 c=0;
if(!n) return (void*)&c;
while(n%4) n++;
u32 nn = c+n;
if(nn > mallocSizeB) return 0;
u8 * p = &m[c]; c = nn; memset(p,0,n);
return (void*)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 =) )
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?
edit > ok i done it myself, here is a gist
not very evolved but work fine with this test :
u32* b1 = bnew(M68KVRAM,512);
void*mtest[500]; u32 allocSize = 8;
u32 n = 0, nb=0, total=0;
while(n<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>>10,nb,allocSize);
printl("free them all .. ");
u32 freertn = 0; for(n=0;n<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);