From d35478a39840f150ddda9d0a1d44187f366f86c5 Mon Sep 17 00:00:00 2001 From: YamaArashi Date: Wed, 6 Jan 2016 18:57:32 -0800 Subject: move C files --- c/include/global.h | 20 ----- c/src/malloc.c | 214 ----------------------------------------------------- include/global.h | 20 +++++ src/malloc.c | 214 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 234 insertions(+), 234 deletions(-) delete mode 100644 c/include/global.h delete mode 100644 c/src/malloc.c create mode 100644 include/global.h create mode 100644 src/malloc.c diff --git a/c/include/global.h b/c/include/global.h deleted file mode 100644 index 4354c8651..000000000 --- a/c/include/global.h +++ /dev/null @@ -1,20 +0,0 @@ -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned int u32; -typedef signed char s8; -typedef signed short s16; -typedef signed int s32; - -typedef u8 bool8; -typedef u16 bool16; -typedef u32 bool32; - -#define NULL (void *)0 - -#define TRUE 1 -#define FALSE 0 - -#define CPU_SET_SRC_FIX (1 << 24) -#define CPU_SET_32BIT (1 << 26) - -extern void CpuSet(void *src, void *dest, u32 controlData); diff --git a/c/src/malloc.c b/c/src/malloc.c deleted file mode 100644 index 2ca41b81a..000000000 --- a/c/src/malloc.c +++ /dev/null @@ -1,214 +0,0 @@ -#include "global.h" - -extern void *gHeapStart; -extern u32 gHeapSize; - -#define MALLOC_SYSTEM_ID 0xA3A3 - -struct MemBlock { - // Whether this block is currently allocated. - bool16 flag; - - // Magic number used for error checking. Should equal MALLOC_SYSTEM_ID. - u16 magic; - - // Size of the block (not including this header struct). - u32 size; - - // Previous block pointer. Equals gHeapStart if this is the first block. - struct MemBlock *prev; - - // Next block pointer. Equals gHeapStart if this is the last block. - struct MemBlock *next; - - // Data in the memory block. (Arrays of length 0 are a GNU extension.) - u8 data[0]; -}; - -void PutMemBlockHeader(void *block, struct MemBlock *prev, struct MemBlock *next, u32 size) -{ - struct MemBlock *header = (struct MemBlock *)block; - - header->flag = 0; - header->magic = MALLOC_SYSTEM_ID; - header->size = size; - header->prev = prev; - header->next = next; -} - -void PutFirstMemBlockHeader(void *block, u32 size) -{ - PutMemBlockHeader(block, (struct MemBlock *)block, (struct MemBlock *)block, size - 16); -} - -void *AllocInternal(void *heapStart, u32 size) -{ - struct MemBlock *pos = (struct MemBlock *)heapStart; - struct MemBlock *head = pos; - struct MemBlock *splitBlock; - u32 foundBlockSize; - - if (size & 3) - size = 4 * ((size / 4) + 1); - - for (;;) { - // Loop through the blocks looking for unused block that's big enough. - - if (!pos->flag) { - foundBlockSize = pos->size; - - if (foundBlockSize >= size) { - if (foundBlockSize - size <= 31) { - // The block isn't much bigger than the requested size, - // so just use it. - pos->flag = TRUE; - } else { - // The block is significantly bigger than the requested - // size, so split the rest into a separate block. - foundBlockSize -= sizeof(struct MemBlock); - foundBlockSize -= size; - - splitBlock = (struct MemBlock *)(pos->data + size); - - pos->flag = TRUE; - pos->size = size; - - PutMemBlockHeader(splitBlock, pos, pos->next, foundBlockSize); - - pos->next = splitBlock; - - if (splitBlock->next != head) - splitBlock->next->prev = splitBlock; - } - - return pos->data; - } - } - - if (pos->next == head) - return NULL; - - pos = pos->next; - } -} - -void FreeInternal(void *heapStart, void *pointer) -{ - if (pointer) { - struct MemBlock *head = (struct MemBlock *)heapStart; - struct MemBlock *block = (struct MemBlock *)((u8 *)pointer - sizeof(struct MemBlock)); - block->flag = FALSE; - - // If the freed block isn't the last one, merge with the next block - // if it's not in use. - if (block->next != head) { - if (!block->next->flag) { - block->size += sizeof(struct MemBlock) + block->next->size; - block->next->magic = 0; - block->next = block->next->next; - if (block->next != head) - block->next->prev = block; - } - } - - // If the freed block isn't the first one, merge with the previous block - // if it's not in use. - if (block != head) { - if (!block->prev->flag) { - block->prev->next = block->next; - - if (block->next != head) - block->next->prev = block->prev; - - block->magic = 0; - block->prev->size += sizeof(struct MemBlock) + block->size; - } - } - } -} - -void *AllocZeroedInternal(void *heapStart, u32 size) -{ - void *mem = AllocInternal(heapStart, size); - - if (mem != NULL) { - u32 zero; - u32 sizeInWords; - - if (size & 3) - size = 4 * ((size / 4) + 1); - - zero = 0; - sizeInWords = (size << 9) >> 11; - - CpuSet(&zero, mem, CPU_SET_32BIT | CPU_SET_SRC_FIX | sizeInWords); - } - - return mem; -} - -bool32 CheckMemBlockInternal(void *heapStart, void *pointer) -{ - struct MemBlock *head = (struct MemBlock *)heapStart; - struct MemBlock *block = (struct MemBlock *)((u8 *)pointer - sizeof(struct MemBlock)); - - if (block->magic != MALLOC_SYSTEM_ID) - return FALSE; - - if (block->next->magic != MALLOC_SYSTEM_ID) - return FALSE; - - if (block->next != head && block->next->prev != block) - return FALSE; - - if (block->prev->magic != MALLOC_SYSTEM_ID) - return FALSE; - - if (block->prev != head && block->prev->next != block) - return FALSE; - - if (block->next != head && block->next != (struct MemBlock *)(block->data + block->size)) - return FALSE; - - return TRUE; -} - -void InitHeap(void *heapStart, u32 heapSize) -{ - gHeapStart = heapStart; - gHeapSize = heapSize; - PutFirstMemBlockHeader(heapStart, heapSize); -} - -void *Alloc(u32 size) -{ - AllocInternal(gHeapStart, size); -} - -void *AllocZeroed(u32 size) -{ - AllocZeroedInternal(gHeapStart, size); -} - -void Free(void *pointer) -{ - FreeInternal(gHeapStart, pointer); -} - -bool32 CheckMemBlock(void *pointer) -{ - return CheckMemBlockInternal(gHeapStart, pointer); -} - -bool32 CheckHeap() -{ - struct MemBlock *pos = (struct MemBlock *)gHeapStart; - - do { - if (!CheckMemBlockInternal(gHeapStart, pos->data)) - return FALSE; - pos = pos->next; - } while (pos != (struct MemBlock *)gHeapStart); - - return TRUE; -} diff --git a/include/global.h b/include/global.h new file mode 100644 index 000000000..4354c8651 --- /dev/null +++ b/include/global.h @@ -0,0 +1,20 @@ +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef signed char s8; +typedef signed short s16; +typedef signed int s32; + +typedef u8 bool8; +typedef u16 bool16; +typedef u32 bool32; + +#define NULL (void *)0 + +#define TRUE 1 +#define FALSE 0 + +#define CPU_SET_SRC_FIX (1 << 24) +#define CPU_SET_32BIT (1 << 26) + +extern void CpuSet(void *src, void *dest, u32 controlData); diff --git a/src/malloc.c b/src/malloc.c new file mode 100644 index 000000000..2ca41b81a --- /dev/null +++ b/src/malloc.c @@ -0,0 +1,214 @@ +#include "global.h" + +extern void *gHeapStart; +extern u32 gHeapSize; + +#define MALLOC_SYSTEM_ID 0xA3A3 + +struct MemBlock { + // Whether this block is currently allocated. + bool16 flag; + + // Magic number used for error checking. Should equal MALLOC_SYSTEM_ID. + u16 magic; + + // Size of the block (not including this header struct). + u32 size; + + // Previous block pointer. Equals gHeapStart if this is the first block. + struct MemBlock *prev; + + // Next block pointer. Equals gHeapStart if this is the last block. + struct MemBlock *next; + + // Data in the memory block. (Arrays of length 0 are a GNU extension.) + u8 data[0]; +}; + +void PutMemBlockHeader(void *block, struct MemBlock *prev, struct MemBlock *next, u32 size) +{ + struct MemBlock *header = (struct MemBlock *)block; + + header->flag = 0; + header->magic = MALLOC_SYSTEM_ID; + header->size = size; + header->prev = prev; + header->next = next; +} + +void PutFirstMemBlockHeader(void *block, u32 size) +{ + PutMemBlockHeader(block, (struct MemBlock *)block, (struct MemBlock *)block, size - 16); +} + +void *AllocInternal(void *heapStart, u32 size) +{ + struct MemBlock *pos = (struct MemBlock *)heapStart; + struct MemBlock *head = pos; + struct MemBlock *splitBlock; + u32 foundBlockSize; + + if (size & 3) + size = 4 * ((size / 4) + 1); + + for (;;) { + // Loop through the blocks looking for unused block that's big enough. + + if (!pos->flag) { + foundBlockSize = pos->size; + + if (foundBlockSize >= size) { + if (foundBlockSize - size <= 31) { + // The block isn't much bigger than the requested size, + // so just use it. + pos->flag = TRUE; + } else { + // The block is significantly bigger than the requested + // size, so split the rest into a separate block. + foundBlockSize -= sizeof(struct MemBlock); + foundBlockSize -= size; + + splitBlock = (struct MemBlock *)(pos->data + size); + + pos->flag = TRUE; + pos->size = size; + + PutMemBlockHeader(splitBlock, pos, pos->next, foundBlockSize); + + pos->next = splitBlock; + + if (splitBlock->next != head) + splitBlock->next->prev = splitBlock; + } + + return pos->data; + } + } + + if (pos->next == head) + return NULL; + + pos = pos->next; + } +} + +void FreeInternal(void *heapStart, void *pointer) +{ + if (pointer) { + struct MemBlock *head = (struct MemBlock *)heapStart; + struct MemBlock *block = (struct MemBlock *)((u8 *)pointer - sizeof(struct MemBlock)); + block->flag = FALSE; + + // If the freed block isn't the last one, merge with the next block + // if it's not in use. + if (block->next != head) { + if (!block->next->flag) { + block->size += sizeof(struct MemBlock) + block->next->size; + block->next->magic = 0; + block->next = block->next->next; + if (block->next != head) + block->next->prev = block; + } + } + + // If the freed block isn't the first one, merge with the previous block + // if it's not in use. + if (block != head) { + if (!block->prev->flag) { + block->prev->next = block->next; + + if (block->next != head) + block->next->prev = block->prev; + + block->magic = 0; + block->prev->size += sizeof(struct MemBlock) + block->size; + } + } + } +} + +void *AllocZeroedInternal(void *heapStart, u32 size) +{ + void *mem = AllocInternal(heapStart, size); + + if (mem != NULL) { + u32 zero; + u32 sizeInWords; + + if (size & 3) + size = 4 * ((size / 4) + 1); + + zero = 0; + sizeInWords = (size << 9) >> 11; + + CpuSet(&zero, mem, CPU_SET_32BIT | CPU_SET_SRC_FIX | sizeInWords); + } + + return mem; +} + +bool32 CheckMemBlockInternal(void *heapStart, void *pointer) +{ + struct MemBlock *head = (struct MemBlock *)heapStart; + struct MemBlock *block = (struct MemBlock *)((u8 *)pointer - sizeof(struct MemBlock)); + + if (block->magic != MALLOC_SYSTEM_ID) + return FALSE; + + if (block->next->magic != MALLOC_SYSTEM_ID) + return FALSE; + + if (block->next != head && block->next->prev != block) + return FALSE; + + if (block->prev->magic != MALLOC_SYSTEM_ID) + return FALSE; + + if (block->prev != head && block->prev->next != block) + return FALSE; + + if (block->next != head && block->next != (struct MemBlock *)(block->data + block->size)) + return FALSE; + + return TRUE; +} + +void InitHeap(void *heapStart, u32 heapSize) +{ + gHeapStart = heapStart; + gHeapSize = heapSize; + PutFirstMemBlockHeader(heapStart, heapSize); +} + +void *Alloc(u32 size) +{ + AllocInternal(gHeapStart, size); +} + +void *AllocZeroed(u32 size) +{ + AllocZeroedInternal(gHeapStart, size); +} + +void Free(void *pointer) +{ + FreeInternal(gHeapStart, pointer); +} + +bool32 CheckMemBlock(void *pointer) +{ + return CheckMemBlockInternal(gHeapStart, pointer); +} + +bool32 CheckHeap() +{ + struct MemBlock *pos = (struct MemBlock *)gHeapStart; + + do { + if (!CheckMemBlockInternal(gHeapStart, pos->data)) + return FALSE; + pos = pos->next; + } while (pos != (struct MemBlock *)gHeapStart); + + return TRUE; +} -- cgit v1.2.3