Mercurial > sos > sos
comparison src/std.c @ 26:c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
I now have a decent test program for dynamic memory implemented as a separate
program (dynamic.c)
The stack now stores its current size, this is used in operations.c to check
the size
Pop should now also be called with a pointer to an eltType. Description of
behavior is in stack.h
author | Jonathan Pevarnek <pevarnj@gmail.com> |
---|---|
date | Sun, 20 Mar 2011 14:17:24 -0400 |
parents | 45a80ea314ae |
children | 1a070e843bf6 |
comparison
equal
deleted
inserted
replaced
25:2b19746a4e97 | 26:c1ad124f2aaf |
---|---|
1 #include <std.h> | 1 #include <std.h> |
2 | 2 |
3 char* itoa(int n, char *a) | 3 char* itoa(long long n, char *a, unsigned short base) |
4 { | 4 { |
5 char *ret = a; | 5 char *ret = a; |
6 if(n < 0) { | 6 if(n < 0) { |
7 *a++ = '-'; | 7 *a++ = '-'; |
8 n *= -1; | 8 n *= -1; |
9 } | 9 } |
10 char *b = a; | 10 char *b = a; |
11 if(!n) *b++ = '0'; | 11 if(!n) *b++ = '0'; |
12 for(; n; b++) { | 12 for(; n; b++) { |
13 *b = n%10 + '0'; | 13 int temp = n%base; |
14 n = n/10; | 14 if(temp < 10) *b = '0' + temp; |
15 else *b = 'a' + temp - 10; | |
16 n = n/base; | |
15 } | 17 } |
16 *b-- = '\0'; | 18 *b-- = '\0'; |
17 for(; a < b; a++, b--) { //reverse | 19 for(; a < b; a++, b--) { //reverse |
18 char temp = *b; | 20 char temp = *b; |
19 *b = *a; | 21 *b = *a; |
25 char* ftoa(double x, char *a, unsigned int prec) | 27 char* ftoa(double x, char *a, unsigned int prec) |
26 { | 28 { |
27 char *ret = a; | 29 char *ret = a; |
28 int n = (int) x; //integer part | 30 int n = (int) x; //integer part |
29 double d = x - (double) n; //fractional part; | 31 double d = x - (double) n; //fractional part; |
30 itoa(n, a); | 32 itoa(n, a, 10); |
31 if(prec) { //only do the decimal part if decimal parts were asked for | 33 if(prec) { //only do the decimal part if decimal parts were asked for |
32 while(*a && *++a); //get to the null character from itoa | 34 while(*a && *++a); //get to the null character from itoa |
33 int i; //counter variable for the for loop | 35 int i; //counter variable for the for loop |
34 *a++ = '.'; //put the decimal in place | 36 *a++ = '.'; //put the decimal in place |
35 for(i = 0; i < prec; i++) { | 37 for(i = 0; i < prec; i++) { |
137 allocp->next->next = &base; | 139 allocp->next->next = &base; |
138 allocp->next->size = memSize/sizeof(blockUnit); | 140 allocp->next->size = memSize/sizeof(blockUnit); |
139 if((sizeof(blockUnit)%sizeof(u64))) { | 141 if((sizeof(blockUnit)%sizeof(u64))) { |
140 sPrint("WARNING: MEMORY NOT 8-BYTE ALIGNED\n"); | 142 sPrint("WARNING: MEMORY NOT 8-BYTE ALIGNED\n"); |
141 char foo[40]; | 143 char foo[40]; |
142 sPrint(append("MEMORY BLOCK SIZE IS: ", append(itoa(sizeof(Header), foo), "\n"))); | 144 sPrint(append("MEMORY BLOCK SIZE IS: ", append(itoa(sizeof(Header), foo, 10), "\n"))); |
143 } | 145 } |
144 } | 146 } |
145 | 147 |
146 void* malloc(size_t size) | 148 void* malloc(size_t size) |
147 { | 149 { |
174 Header *scan; | 176 Header *scan; |
175 for(scan = allocp; !(toFree > scan && toFree < scan->next); scan = scan->next) | 177 for(scan = allocp; !(toFree > scan && toFree < scan->next); scan = scan->next) |
176 if(scan->next < scan && (toFree > scan || toFree < scan->next)) break; | 178 if(scan->next < scan && (toFree > scan || toFree < scan->next)) break; |
177 toFree->next = scan->next; | 179 toFree->next = scan->next; |
178 scan->next = toFree; | 180 scan->next = toFree; |
179 if(scan + toFree->size == toFree) { | 181 if(scan + scan->size == toFree) { |
180 scan->size += toFree->size; | 182 scan->size += toFree->size; |
181 scan->next = toFree->next; | 183 scan->next = toFree->next; |
182 toFree = scan; | 184 toFree = scan; |
183 } | 185 } |
184 if(toFree + toFree->size == toFree->next) { | 186 if(toFree + toFree->size == toFree->next) { |
185 toFree->size += toFree->next->size; | 187 toFree->size += toFree->next->size; |
186 toFree->next = toFree->next->next; | 188 toFree->next = toFree->next->next; |
187 } | 189 } |
188 } | 190 } |
191 | |
192 void printHeader(Header* head) | |
193 { | |
194 char buffer[20]; | |
195 sPrint("Memory address is: "); | |
196 sPrint(itoa((size_t)head, buffer, 16)); | |
197 sPrint("\n"); | |
198 sPrint("The size of the block is: "); | |
199 sPrint(itoa(head->size, buffer, 10)); | |
200 sPrint("\n"); | |
201 sPrint("Next memory address is: "); | |
202 sPrint(itoa((size_t)head->next, buffer, 16)); | |
203 sPrint("\n\n"); | |
204 } | |
205 | |
206 void printMem() | |
207 { | |
208 Header *cur = allocp; | |
209 do { | |
210 printHeader(cur); | |
211 cur = cur->next; | |
212 } while(cur != allocp); | |
213 sPrint("PRINTOUT DONE\n\n"); | |
214 } |