# HG changeset patch # User Jonathan Pevarnek # Date 1300248575 14400 # Node ID 3fb0ec050ff3de3782d9266c0ae04fc51472fee8 # Parent 7c2adb65ceacb98f2b38a893a557c36f5cf9436e malloc has been restructured to now work in a more logical manner malloc now allocates memory from the beginning of a block instead of the end. Additionally, memory is now 8-byte aligned instead of 16-byte aligned. malloc_init should now be called to set up all the memory information. Fixed a bug where malloc would not have iterated to find new memory locations. diff -r 7c2adb65ceac -r 3fb0ec050ff3 arch/io.c --- a/arch/io.c Tue Mar 15 22:55:36 2011 -0400 +++ b/arch/io.c Wed Mar 16 00:09:35 2011 -0400 @@ -178,7 +178,6 @@ /* console I/O functions */ -#define CON_LEN 132 #define CON_DEV 0x0009 struct pmcw { diff -r 7c2adb65ceac -r 3fb0ec050ff3 include/std.h --- a/include/std.h Tue Mar 15 22:55:36 2011 -0400 +++ b/include/std.h Wed Mar 16 00:09:35 2011 -0400 @@ -19,15 +19,13 @@ //MALLOC CODE -union block { - struct { - union block *next; - unsigned int size; - } s; - double foo; +struct block{ + struct block *next; + unsigned int size; }; -typedef union block Block; +typedef struct block Block; +void malloc_init(size_t memSize); void* malloc(size_t size); //void free(void *ptr); diff -r 7c2adb65ceac -r 3fb0ec050ff3 src/init.c --- a/src/init.c Tue Mar 15 22:55:36 2011 -0400 +++ b/src/init.c Wed Mar 16 00:09:35 2011 -0400 @@ -24,12 +24,13 @@ int i, n; char *buffer; char input[30]; + malloc_init(0x1400000); //20MB memory while(1) { sPrint("How long do you want the string? "); n = atoi(sGet(input, 30)); - buffer = (char*) malloc(n + 2); + buffer = (char*) malloc(n); if (!buffer) { //allocation failed, too large sPrint("ERROR\n"); break; @@ -41,8 +42,8 @@ // dumpBuffer(buffer, n); - sPrint(append(itoa((int)buffer, input), "\n")); - sPrint(append(buffer, "\n")); + sPrint(append(itoa((unsigned long)buffer, input), "\n")); + sPrint(buffer), sPrint("\n"); } /* struct Stack theStack; diff -r 7c2adb65ceac -r 3fb0ec050ff3 src/std.c --- a/src/std.c Tue Mar 15 22:55:36 2011 -0400 +++ b/src/std.c Wed Mar 16 00:09:35 2011 -0400 @@ -128,26 +128,29 @@ static Block *allocp = NULL; //the location of the last item allocated by malloc +void malloc_init(size_t memSize) +{ + allocp = (void*)0x200000; + allocp->next = allocp; + allocp->size = memSize/sizeof(u64); +} + void* malloc(size_t size) { - unsigned int nBlocks = 1 + (size + sizeof(Block) - 1)/sizeof(Block); - //We are allocating a number of blocks equal to the number needed rounded up - //plus one for the actual info block - Block *cur, *prev; - if(allocp == NULL) { - allocp = (void*)0x200000; //this is where memory should start - allocp->s.next = allocp; - allocp->s.size = (0x1400000/sizeof(Block)); //I am going to limit the heap to 20MB for now - } + unsigned int nUnits = ((size + sizeof(Block)) + sizeof(u64) - 1)/sizeof(u64); + Block *cur, *prev, *temp; + if(allocp == NULL) return NULL; prev = allocp; - for(cur = prev->s.next;; ) { - if(cur->s.size >= nBlocks) { //this section is pretty much exactly copied from K&R - if(cur->s.size == nBlocks) { - prev->s.next = cur->s.next; + for(cur = prev->next;; prev = cur, cur = cur->next) { + if(cur->size >= nUnits) { + if(cur->size == nUnits) { + prev->next = cur->next; } else { - cur->s.size -= nBlocks; - cur += cur->s.size; - cur->s.size = nBlocks; + temp = cur + nUnits; + temp->size = cur->size - nUnits; + temp->next = cur->next; + prev->next = temp; + cur->size = nUnits; } allocp = prev; return (void*)(cur + 1);