From: chaoskagami Date: Tue, 20 Dec 2016 04:10:17 +0000 (-0500) Subject: Remove malloc (not working, obviously) X-Git-Tag: v0.3.1~55 X-Git-Url: https://chaos.moe/g/?a=commitdiff_plain;h=8ce6f1527b1b985c736d66037b455f0139cccaab;p=corbenik%2Fcorbenik.git Remove malloc (not working, obviously) --- diff --git a/include/common.h b/include/common.h index cae7211..9fa1ce2 100644 --- a/include/common.h +++ b/include/common.h @@ -1,6 +1,8 @@ #ifndef __COMMON_H #define __COMMON_H +#include + #include #include @@ -15,7 +17,6 @@ #include #include -#include #include #include #include diff --git a/include/std/allocator.h b/include/std/allocator.h deleted file mode 100644 index c0fad2b..0000000 --- a/include/std/allocator.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef __FCRAM_H -#define __FCRAM_H - -#include -#include - -#define FCRAM_SPACING 0x100000 ///< Space between most of the locations (remove me) - -/* Grow program break. - * - * \param bytes Number of bytes to grow by. - */ -void *sbrk(size_t bytes); - -#ifdef MALLOC_DEBUG - -/* Prints stats for allocation to stderr. - */ -void print_alloc_stats(void); - -/* Allocate memory for use (debugging only, don't call) - * - * \param size Size in bytes to allocate. - * \param info Info to store about malloc - */ -void* malloc_chkd(size_t size, const char* info); - -/* Free in-use memory allocated by malloc (debugging only, don't call) - * - * \param ptr Pointer to free. - * \param info Info to store about free - */ -void free_chkd(void* ptr, const char* info); - -#define STRINGIFY(x) #x -#define TOSTRING(x) STRINGIFY(x) -#define malloc(x) malloc_chkd( x , __FILE__ ":" TOSTRING(__LINE__) ) -#define free(x) free_chkd( x , __FILE__ ":" TOSTRING(__LINE__) ) - -#else - -/* Allocate memory for use. - * - * \param size Size in bytes to allocate. - */ -void *malloc (size_t size); - -/* Free in-use memory allocated by malloc. - * - * \param ptr Pointer to free. - */ -void free (void* ptr); - -/* Reallocates memory to size. Guaranteed to preserve the original data. - * - * \param ptr Pointer to reallocate - * \param size Size to reallocate as - */ -void *realloc(void* ptr, size_t size); - -#endif - -#endif diff --git a/include/std/memory.h b/include/std/memory.h index 464b847..ca4df94 100644 --- a/include/std/memory.h +++ b/include/std/memory.h @@ -6,8 +6,6 @@ #include #include -int atoi(const char *str); - uint8_t *memfind(uint8_t *startPos, uint32_t size, const void *pattern, uint32_t patternSize); /* Basically strdup, because newlib's memory handling is crap. diff --git a/source/Makefile.am b/source/Makefile.am index 8a3142a..0dae7ba 100644 --- a/source/Makefile.am +++ b/source/Makefile.am @@ -19,4 +19,4 @@ corbenikdir = $(top_srcdir)/source inc_dir = $(top_srcdir)/include -corbenik_SOURCES = patch/reboot.c patch/svc.c patch/module.c patch/emunand.c main.c std/fs.c std/draw.c std/memory.c std/abort.c std/allocator.c menu.c firm/util.c firm/keys.c firm/firmlaunch.c firm/version.c firm/firm.c firm/decryptor.c interpreter.c input.c patcher.c chainloader.c config-backend-file.c menu-backend.c start.s interrupt.c arm11.c test-framework.c +corbenik_SOURCES = patch/reboot.c patch/svc.c patch/module.c patch/emunand.c main.c std/fs.c std/draw.c std/memory.c std/abort.c menu.c firm/util.c firm/keys.c firm/firmlaunch.c firm/version.c firm/firm.c firm/decryptor.c interpreter.c input.c patcher.c chainloader.c config-backend-file.c menu-backend.c start.s interrupt.c arm11.c test-framework.c diff --git a/source/config-backend-file.c b/source/config-backend-file.c index abef848..1d72774 100644 --- a/source/config-backend-file.c +++ b/source/config-backend-file.c @@ -8,6 +8,8 @@ char *config_file_path = NULL; int changed_consoles = 0; uint32_t cid[4]; +#define PATCH_MAX 0x100000 + void regenerate_config(void) { @@ -21,7 +23,7 @@ regenerate_config(void) if (!(conf_handle = fopen(config_file_path, "w"))) poweroff(); - fwrite(config, 1, sizeof(struct config_file) + FCRAM_SPACING / 2, conf_handle); + fwrite(config, 1, sizeof(struct config_file) + PATCH_MAX, conf_handle); fclose(conf_handle); } @@ -111,8 +113,8 @@ load_config(void) cid_cp >>= 4; } - config = (struct config_file*)malloc(sizeof(struct config_file) + FCRAM_SPACING / 2); - memset(config, 0, sizeof(struct config_file) + FCRAM_SPACING / 2); + config = (struct config_file*)malloc(sizeof(struct config_file) + PATCH_MAX); + memset(config, 0, sizeof(struct config_file) + PATCH_MAX); enable_list = (uint8_t*)config + sizeof(struct config_file); fclose(f); } @@ -121,7 +123,7 @@ load_config(void) if (!(conf_handle = fopen(config_file_path, "r"))) { regenerate_config(); } else { - fread(config, 1, sizeof(struct config_file) + FCRAM_SPACING / 2, conf_handle); + fread(config, 1, sizeof(struct config_file) + PATCH_MAX, conf_handle); fclose(conf_handle); @@ -157,7 +159,7 @@ save_config(void) if (!(conf_handle = fopen(config_file_path, "w"))) while(1); - fwrite(config, 1, sizeof(struct config_file) + FCRAM_SPACING / 2, conf_handle); + fwrite(config, 1, sizeof(struct config_file) + PATCH_MAX, conf_handle); fclose(conf_handle); } diff --git a/source/std/allocator.c b/source/std/allocator.c deleted file mode 100644 index 9c26e56..0000000 --- a/source/std/allocator.c +++ /dev/null @@ -1,159 +0,0 @@ -#include - -// 16 <- AES block size. -#define SALLOC_ALIGN 16 - -struct alloc_info* first_mem = NULL; -static uint32_t *heap_end = NULL; -extern uint32_t __end__; /* Defined by the linker */ - -void* sbrk(size_t incr) { - uint32_t *prev_heap_end; - - if (heap_end == NULL) { - heap_end = &__end__; - } - - // FIXME - Make sure heap isn't leaking into stack here. That would be bad. - - prev_heap_end = heap_end; - - heap_end += incr; - return (void*) prev_heap_end; -} - -// This is an incredibly crappy and inefficient implementation of malloc/free nicked from stackoverflow. - -typedef struct free_block { - size_t size; - size_t real_size; -#ifdef MALLOC_DEBUG - const char* info; -#endif - struct free_block* next; - - uint32_t canary; - uint32_t pad[3]; // Otherwise, not a 16-multiple -} free_block; - -static free_block free_block_list_head = { - 0, - 0, -#ifdef MALLOC_DEBUG - NULL, -#endif - 0, - 0, - {0} -}; - -static const size_t align_to = 64; - -#ifdef MALLOC_DEBUG -static size_t alloc_count = 0; -static size_t free_count = 0; -static size_t allocated_memory = 0; -#endif - -#ifdef MALLOC_DEBUG -void* malloc_chkd(size_t size, const char* info) { -#else -void* malloc(size_t size) { -#endif - size_t bsize = (size + sizeof(free_block) + (align_to - 1)) & ~ (align_to - 1); - - free_block* block = free_block_list_head.next; - free_block** head = &(free_block_list_head.next); - - while (block != 0) { - if (block->size >= bsize) { - *head = block->next; - - block->real_size = size; -#ifdef MALLOC_DEBUG - block->info = info; - - ++alloc_count; - allocated_memory += block->size; -#endif - - return ((char*)block) + sizeof(free_block); - } - head = &(block->next); - block = block->next; - } - - block = (free_block*)sbrk(bsize); - block->size = bsize; - block->real_size = size; - block->canary = 0x1337d00d; // Arbitrary. No special meaning. - -#ifdef MALLOC_DEBUG - block->info = info; - - ++alloc_count; - allocated_memory += bsize; -#endif - - return ((char*)block) + sizeof(free_block); -} - -void* malloc_zero(size_t size) { - void* ret = malloc(size); - - if (ret) - memset(ret, 0, size); - - return ret; -} - -#ifdef MALLOC_DEBUG -void free_chkd(void* ptr, const char* info) { -#else -void free(void* ptr) { -#endif - if (ptr == NULL) return; - - free_block* block = (free_block*)(((char*)ptr) - sizeof(free_block )); - -#ifdef MALLOC_DEBUG - if (block->canary != 0x1337d00d) { - panic("%s: Attempt free non-pointer.\n", info); - } - - ++free_count; - if (allocated_memory < block->size) { - fprintf(stderr, "%s: Invalid free detected.\n" - " Allocated at: %s\n", - info, block->info); - } - allocated_memory -= block->size; -#endif - - block->next = free_block_list_head.next; - free_block_list_head.next = block; -} - -#ifdef MALLOC_DEBUG -void print_alloc_stats(void) { - fprintf(stderr, "[A] %u [F] %u [M] %u [B] %lu\n", alloc_count, free_count, allocated_memory, (uint32_t)heap_end - (uint32_t)&__end__); -} -#endif - -void *realloc(void* ptr, size_t size) { - if (ptr == NULL) - return malloc(size); - - free_block* current = (free_block*)(((char*)ptr) - sizeof(free_block)); - - if (size < current->size || size < current->real_size) - return ptr; - - void* new_ptr = malloc(size); - - memcpy(new_ptr, ptr, current->real_size); - - free(ptr); - - return new_ptr; -} diff --git a/source/std/memory.c b/source/std/memory.c index 977e7f9..60b7a0a 100644 --- a/source/std/memory.c +++ b/source/std/memory.c @@ -23,20 +23,6 @@ strdupcat(const char* str, const char *cat) return out; } -int -atoi(const char *str) -{ - int res = 0; - - while (str[0] && str[0] >= '0' && str[0] <= '9') { - res *= 10; - res += str[0] - '0'; - str++; - } - - return res; -} - #define ALPHABET_LEN 256 #define NOT_FOUND patlen #define max(a, b) ((a < b) ? b : a)