Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple and portable malloc library

Tags:

c

malloc

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);
like image 540
r043v Avatar asked Jul 24 '13 15:07

r043v


People also ask

What is the library for malloc?

In computing, malloc is a subroutine for performing dynamic memory allocation. malloc is part of the standard library and is declared in the stdlib.

What does malloc() do in C?

What is malloc() in C? malloc() is a library function that allows C to allocate memory dynamically from the heap. The heap is an area of memory where something is stored. malloc() is part of stdlib. h and to be able to use it you need to use #include <stdlib.

Does Stdio have malloc?

Malloc and free is defined under stdlib. h. So without including it your program wont work.

What does malloc stand for?

The name "malloc" stands for memory allocation. The malloc() function reserves a block of memory of the specified number of bytes. And, it returns a pointer of void which can be casted into pointers of any form.


1 Answers

Basically, you need two memory management routines: One that stores its internal working in one part of RAM while managing another (in this case, VRAM), and the other could be any normal malloc routine. For VRAM, do you need it to handle both 16 and 256 color palettes or just 256? If a single size, then a "Unit Allocator" type algorithm would suit you just fine.

For your main RAM, a linked list style algorithm would probably be sufficient. An implementation is done here, but with just a little bit of leg work, you can find MANY, MANY implementations on the internet.

I haven't looked at the Saturn myself in 16 years, so this was a fun thing to read and edit for you :)

like image 135
Michael Dorgan Avatar answered Sep 21 '22 15:09

Michael Dorgan