comparison src/std.c @ 24:45a80ea314ae

The stack now theroretically is a properly working stack! I added the free function I converted the code for the stack so that it uses a linked list to store the stack values. TODO: create a way to destroy stacks.
author Jonathan Pevarnek <pevarnj@gmail.com>
date Wed, 16 Mar 2011 12:00:43 -0400
parents f68b59af5ea6
children c1ad124f2aaf
comparison
equal deleted inserted replaced
23:f68b59af5ea6 24:45a80ea314ae
124 while(*dest&& *++dest); //get to null in first string 124 while(*dest&& *++dest); //get to null in first string
125 while((*dest++ = *src++)); 125 while((*dest++ = *src++));
126 return ret; 126 return ret;
127 } 127 }
128 128
129 static Block *allocp = NULL; //the location of the last item allocated by malloc 129 static Header base;
130 static Header *allocp = NULL; //the location of the last known free block
130 131
131 void malloc_init(size_t memSize) 132 void malloc_init(size_t memSize)
132 { 133 {
133 allocp = (void*)HEAP_START; 134 allocp = &base;
134 allocp->next = allocp; 135 allocp->size = 0;
135 allocp->size = memSize/sizeof(u64); 136 allocp->next = (void*)HEAP_START;
137 allocp->next->next = &base;
138 allocp->next->size = memSize/sizeof(blockUnit);
139 if((sizeof(blockUnit)%sizeof(u64))) {
140 sPrint("WARNING: MEMORY NOT 8-BYTE ALIGNED\n");
141 char foo[40];
142 sPrint(append("MEMORY BLOCK SIZE IS: ", append(itoa(sizeof(Header), foo), "\n")));
143 }
136 } 144 }
137 145
138 void* malloc(size_t size) 146 void* malloc(size_t size)
139 { 147 {
140 unsigned int nUnits = ((size + sizeof(Block)) + sizeof(u64) - 1)/sizeof(u64);
141 Block *cur, *prev, *temp;
142 if(allocp == NULL) return NULL; 148 if(allocp == NULL) return NULL;
143 prev = allocp; 149 size_t nUnits = ((size + sizeof(Header)) + sizeof(blockUnit) - 1)/sizeof(blockUnit);
150 Header *prev = allocp;
151 Header *cur, *temp;
144 for(cur = prev->next;; prev = cur, cur = cur->next) { 152 for(cur = prev->next;; prev = cur, cur = cur->next) {
145 if(cur->size >= nUnits) { 153 if(cur->size >= nUnits) {
146 if(cur->size == nUnits) { 154 if(cur->size == nUnits) {
147 prev->next = cur->next; 155 prev->next = cur->next;
148 } else { 156 } else {
149 temp = cur + nUnits; 157 temp = cur + nUnits; //This requires sizeof(blockUnit) == sizeof(Header). TODO fix
150 temp->size = cur->size - nUnits; 158 temp->size = cur->size - nUnits;
151 temp->next = cur->next; 159 temp->next = cur->next;
152 prev->next = temp; 160 prev->next = temp;
153 cur->size = nUnits; 161 cur->size = nUnits;
154 } 162 }
158 return NULL; 166 return NULL;
159 } 167 }
160 } 168 }
161 } 169 }
162 170
163 /*
164 void free(void *ptr) 171 void free(void *ptr)
165 { 172 {
173 Header *toFree = (Header *)ptr - 1;
174 Header *scan;
175 for(scan = allocp; !(toFree > scan && toFree < scan->next); scan = scan->next)
176 if(scan->next < scan && (toFree > scan || toFree < scan->next)) break;
177 toFree->next = scan->next;
178 scan->next = toFree;
179 if(scan + toFree->size == toFree) {
180 scan->size += toFree->size;
181 scan->next = toFree->next;
182 toFree = scan;
183 }
184 if(toFree + toFree->size == toFree->next) {
185 toFree->size += toFree->next->size;
186 toFree->next = toFree->next->next;
187 }
166 } 188 }
167 */