Fixes for the GOS Raw32 memory allocator.

ugfx_release_2.6
inmarket 2013-12-21 15:12:36 +10:00
parent 225aa27233
commit 86083b368d
1 changed files with 8 additions and 6 deletions

View File

@ -109,14 +109,16 @@ void gfxExit(void) {
// Free Slot - immediately follows the memslot structure // Free Slot - immediately follows the memslot structure
typedef struct freeslot { typedef struct freeslot {
memslot *nextfree; // The next free slot memslot *nextfree; // The next free slot
} } freeslot;
#define GetSlotSize(sz) ((((sz) + (sizeof(freeslot) - 1)) & ~(sizeof(freeslot) - 1)) + sizeof(memslot)) #define GetSlotSize(sz) ((((sz) + (sizeof(freeslot) - 1)) & ~(sizeof(freeslot) - 1)) + sizeof(memslot))
#define NextFree(pslot) ((freeslot *)Slot2Ptr(pslot))->nextfree #define NextFree(pslot) ((freeslot *)Slot2Ptr(pslot))->nextfree
#define Ptr2Slot(p) ((memslot *)(p) - 1) #define Ptr2Slot(p) ((memslot *)(p) - 1)
#define Slot2Ptr(pslot) ((pslot)+1) #define Slot2Ptr(pslot) ((pslot)+1)
static memslot * firstSlot, *lastSlot, *freeSlots; static memslot * firstSlot;
static memslot * lastSlot;
static memslot * freeSlots;
static char heap[GOS_RAW_HEAP_SIZE]; static char heap[GOS_RAW_HEAP_SIZE];
static void _gosHeapInit(void) { static void _gosHeapInit(void) {
@ -131,7 +133,7 @@ void gfxExit(void) {
if (lastSlot) if (lastSlot)
lastSlot->next = (memslot *)ptr; lastSlot->next = (memslot *)ptr;
else else
firstSlot = lastSlot = freeSlot = (memslot *)ptr; firstSlot = lastSlot = freeSlots = (memslot *)ptr;
lastSlot->next = 0; lastSlot->next = 0;
lastSlot->sz = sz; lastSlot->sz = sz;
@ -156,8 +158,8 @@ void gfxExit(void) {
p->sz = sz; p->sz = sz;
if (lastSlot == p) if (lastSlot == p)
lastSlot = new; lastSlot = new;
NextFree(new) = freeSlots; NextFree(new) = NextFree(p);
freeSlots = new; NextFree(p) = new;
} }
// Remove it from the free list // Remove it from the free list
if (prev) if (prev)
@ -223,7 +225,7 @@ void gfxExit(void) {
if ((new = gfxAlloc(sz))) if ((new = gfxAlloc(sz)))
return 0; return 0;
memcpy(new, ptr, p->sz - sizeof(memslot)); memcpy(new, ptr, p->sz - sizeof(memslot));
free(ptr); gfxFree(ptr);
return new; return new;
} }