Mercurial > sos > sos
annotate 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 |
rev | line source |
---|---|
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
1 #include <std.h> |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
2 |
26
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
3 char* itoa(long long n, char *a, unsigned short base) |
2 | 4 { |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
5 char *ret = a; |
2 | 6 if(n < 0) { |
7 *a++ = '-'; | |
8 n *= -1; | |
9 } | |
10 char *b = a; | |
11 if(!n) *b++ = '0'; | |
12 for(; n; b++) { | |
26
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
13 int temp = n%base; |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
14 if(temp < 10) *b = '0' + temp; |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
15 else *b = 'a' + temp - 10; |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
16 n = n/base; |
2 | 17 } |
18 *b-- = '\0'; | |
19 for(; a < b; a++, b--) { //reverse | |
20 char temp = *b; | |
21 *b = *a; | |
22 *a = temp; | |
23 } | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
24 return ret; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
25 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
26 |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
27 char* ftoa(double x, char *a, unsigned int prec) |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
28 { |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
29 char *ret = a; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
30 int n = (int) x; //integer part |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
31 double d = x - (double) n; //fractional part; |
26
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
32 itoa(n, a, 10); |
5
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
33 if(prec) { //only do the decimal part if decimal parts were asked for |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
34 while(*a && *++a); //get to the null character from itoa |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
35 int i; //counter variable for the for loop |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
36 *a++ = '.'; //put the decimal in place |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
37 for(i = 0; i < prec; i++) { |
5
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
38 d = d*10; //the integer part is the decimal digit |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
39 *a++ = ((int) d) + '0'; //add the integer part of d to the string |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
40 d -= (int) d; //chop off the integer part |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
41 } a--; //move back to the last digit |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
42 while(*a != '.') { |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
43 if(*a == '0') { |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
44 a--; |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
45 continue; |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
46 } else if(*a > '0' && *a <= '9') { |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
47 a++; |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
48 break; |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
49 } |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
50 } |
5
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
51 *a = '\0'; |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
52 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
53 return ret; |
2 | 54 } |
55 | |
56 int atoi(char *a) | |
57 { | |
8
25b2b501a5fa
Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
5
diff
changeset
|
58 short neg = 0; |
2 | 59 int n = 0; |
60 if(*a == '-') { | |
61 neg = 1; | |
62 a++; | |
8
25b2b501a5fa
Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
5
diff
changeset
|
63 } else if(*a == '+') a++; |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
64 while(*a >= '0' && *a <= '9') |
2 | 65 n = n*10 + (*a++ - '0'); |
66 if(neg) n *= -1; | |
67 return n; | |
68 } | |
69 | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
70 double atof(char *a) |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
71 { |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
72 int n = atoi(a); |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
73 double x = 0; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
74 double dec = .1; |
5
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
75 while(*a != '.') { |
8
25b2b501a5fa
Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
5
diff
changeset
|
76 if(!(*a >= '0' && *a <= '9')) return n; |
5
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
77 a++; |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
78 } a++; //a will be immediately after the decimal point |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
79 while(*a >= '0' && *a <= '9') { //goes through the decimal part |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
80 x += (*a - '0')*dec; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
81 dec *= .1; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
82 a++; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
83 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
84 return n + x; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
85 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
86 |
2 | 87 void sPrint(char *a) |
88 { | |
89 char *b = a; | |
90 while(*b && *++b); | |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
91 do { |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
92 putline(a, (b - a > CON_LEN)?CON_LEN:(b - a)); |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
93 a += CON_LEN; |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
94 } while(a < b); |
2 | 95 } |
96 | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
97 int strcmp(const char *a, const char *b) |
2 | 98 { |
99 while(1) { | |
100 if(*a - *b) return *a - *b; | |
101 if(*a == '\0') return 0; | |
102 a++; | |
103 b++; | |
104 } | |
105 return -1; | |
106 } | |
107 | |
15
525eab23e68a
I no longer need to bug Jeff
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
8
diff
changeset
|
108 char* sGet(char *a, unsigned int n) |
2 | 109 { |
110 int length = getline(a, n); | |
111 a[(length < n)?length:n - 1] = '\0'; | |
4
90cd3d9a6ca3
Moved function array declaration, modified the ftoa function so it does not return trailing 0s
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
3
diff
changeset
|
112 return a; |
2 | 113 } |
114 | |
115 int arrayLookup(char *text, const char array[][10], int last) | |
116 { | |
117 int i; | |
118 for(i = 0; i < last; i++) | |
119 if(!strcmp(array[i], text)) return i; | |
120 return last; | |
121 } | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
122 |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
123 char* append(char *dest, char *src) |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
124 { |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
125 char *ret = dest; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
126 while(*dest&& *++dest); //get to null in first string |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
127 while((*dest++ = *src++)); |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
128 return ret; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
129 } |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
130 |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
131 static Header base; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
132 static Header *allocp = NULL; //the location of the last known free block |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
133 |
22
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
134 void malloc_init(size_t memSize) |
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
135 { |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
136 allocp = &base; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
137 allocp->size = 0; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
138 allocp->next = (void*)HEAP_START; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
139 allocp->next->next = &base; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
140 allocp->next->size = memSize/sizeof(blockUnit); |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
141 if((sizeof(blockUnit)%sizeof(u64))) { |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
142 sPrint("WARNING: MEMORY NOT 8-BYTE ALIGNED\n"); |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
143 char foo[40]; |
26
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
144 sPrint(append("MEMORY BLOCK SIZE IS: ", append(itoa(sizeof(Header), foo, 10), "\n"))); |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
145 } |
22
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
146 } |
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
147 |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
148 void* malloc(size_t size) |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
149 { |
22
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
150 if(allocp == NULL) return NULL; |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
151 size_t nUnits = ((size + sizeof(Header)) + sizeof(blockUnit) - 1)/sizeof(blockUnit); |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
152 Header *prev = allocp; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
153 Header *cur, *temp; |
22
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
154 for(cur = prev->next;; prev = cur, cur = cur->next) { |
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
155 if(cur->size >= nUnits) { |
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
156 if(cur->size == nUnits) { |
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
157 prev->next = cur->next; |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
158 } else { |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
159 temp = cur + nUnits; //This requires sizeof(blockUnit) == sizeof(Header). TODO fix |
22
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
160 temp->size = cur->size - nUnits; |
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
161 temp->next = cur->next; |
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
162 prev->next = temp; |
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
163 cur->size = nUnits; |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
164 } |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
165 allocp = prev; |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
166 return (void*)(cur + 1); |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
167 } else if(cur == allocp) { //We went back to the start... |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
168 return NULL; |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
169 } |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
170 } |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
171 } |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
172 |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
173 void free(void *ptr) |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
174 { |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
175 Header *toFree = (Header *)ptr - 1; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
176 Header *scan; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
177 for(scan = allocp; !(toFree > scan && toFree < scan->next); scan = scan->next) |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
178 if(scan->next < scan && (toFree > scan || toFree < scan->next)) break; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
179 toFree->next = scan->next; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
180 scan->next = toFree; |
26
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
181 if(scan + scan->size == toFree) { |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
182 scan->size += toFree->size; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
183 scan->next = toFree->next; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
184 toFree = scan; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
185 } |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
186 if(toFree + toFree->size == toFree->next) { |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
187 toFree->size += toFree->next->size; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
188 toFree->next = toFree->next->next; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
189 } |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
190 } |
26
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
191 |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
192 void printHeader(Header* head) |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
193 { |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
194 char buffer[20]; |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
195 sPrint("Memory address is: "); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
196 sPrint(itoa((size_t)head, buffer, 16)); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
197 sPrint("\n"); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
198 sPrint("The size of the block is: "); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
199 sPrint(itoa(head->size, buffer, 10)); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
200 sPrint("\n"); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
201 sPrint("Next memory address is: "); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
202 sPrint(itoa((size_t)head->next, buffer, 16)); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
203 sPrint("\n\n"); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
204 } |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
205 |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
206 void printMem() |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
207 { |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
208 Header *cur = allocp; |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
209 do { |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
210 printHeader(cur); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
211 cur = cur->next; |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
212 } while(cur != allocp); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
213 sPrint("PRINTOUT DONE\n\n"); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
214 } |