Mercurial > sos > sos
annotate src/std.c @ 54:39fcefab46ed
Modified the init_fs function to take advantage of dynamic memory
init_fs (renamed) now takes advantage of dynamic memory and allocates space
for exactly as many direntries as it needs to keep track of
TODO redo caching to Jeff's far superior idea
author | Jonathan Pevarnek <pevarnj@gmail.com> |
---|---|
date | Sat, 09 Apr 2011 21:55:59 -0400 |
parents | cced4d365c5e |
children | 25be3895c62a |
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 |
54
39fcefab46ed
Modified the init_fs function to take advantage of dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
28
diff
changeset
|
3 void init_all(u64 __memsize) |
39fcefab46ed
Modified the init_fs function to take advantage of dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
28
diff
changeset
|
4 { |
39fcefab46ed
Modified the init_fs function to take advantage of dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
28
diff
changeset
|
5 init_io_int(); |
39fcefab46ed
Modified the init_fs function to take advantage of dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
28
diff
changeset
|
6 init_console(); |
39fcefab46ed
Modified the init_fs function to take advantage of dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
28
diff
changeset
|
7 malloc_init(__memsize - HEAP_START); |
39fcefab46ed
Modified the init_fs function to take advantage of dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
28
diff
changeset
|
8 } |
39fcefab46ed
Modified the init_fs function to take advantage of dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
28
diff
changeset
|
9 |
27
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
10 double abs(double num) |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
11 { |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
12 if(num < 0) return num*-1; |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
13 else return num; |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
14 } |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
15 |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
16 char* itoa(s64 n, char *a, unsigned short base) |
2 | 17 { |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
18 char *ret = a; |
2 | 19 if(n < 0) { |
20 *a++ = '-'; | |
21 n *= -1; | |
22 } | |
23 char *b = a; | |
24 if(!n) *b++ = '0'; | |
25 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
|
26 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
|
27 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
|
28 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
|
29 n = n/base; |
2 | 30 } |
31 *b-- = '\0'; | |
32 for(; a < b; a++, b--) { //reverse | |
33 char temp = *b; | |
34 *b = *a; | |
35 *a = temp; | |
36 } | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
37 return ret; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
38 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
39 |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
40 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
|
41 { |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
42 char *ret = a; |
27
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
43 if(x == INF || x == -INF) { |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
44 strcpy(a, "INF"); |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
45 } else if(x != x) { //NAN != NAN |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
46 strcpy(a, "NAN"); |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
47 } else { |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
48 s64 n = (s64) x; //integer part |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
49 double d = abs(x - (double) n); //fractional part; |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
50 itoa(n, a, 10); |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
51 if(prec) { //only do the decimal part if decimal parts were asked for |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
52 while(*a && *++a); //get to the null character from itoa |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
53 int i; //counter variable for the for loop |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
54 *a++ = '.'; //put the decimal in place |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
55 for(i = 0; i < prec; i++) { |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
56 d *= 10; //the integer part is the decimal digit |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
57 *a++ = ((int) d) + '0'; //add the integer part of d to the string |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
58 d -= (int) d; //chop off the integer part |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
59 } a--; //move back to the last digit |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
60 while(*a != '.') { |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
61 if(*a == '0') { |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
62 a--; |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
63 continue; |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
64 } else if(*a > '0' && *a <= '9') { |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
65 a++; |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
66 break; |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
67 } else { |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
68 sPrint("ERROR: SOMETHING IS VERY WRONG\n"); |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
69 break; |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
70 } |
5
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
71 } |
27
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
72 *a = '\0'; |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
73 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
74 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
75 return ret; |
2 | 76 } |
77 | |
27
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
78 s64 atoi(char *a) |
2 | 79 { |
8
25b2b501a5fa
Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
5
diff
changeset
|
80 short neg = 0; |
27
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
81 s64 n = 0; |
2 | 82 if(*a == '-') { |
83 neg = 1; | |
84 a++; | |
8
25b2b501a5fa
Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
5
diff
changeset
|
85 } 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
|
86 while(*a >= '0' && *a <= '9') |
2 | 87 n = n*10 + (*a++ - '0'); |
88 if(neg) n *= -1; | |
89 return n; | |
90 } | |
91 | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
92 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
|
93 { |
27
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
94 s64 n = atoi(a); |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
95 double x = 0; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
96 double dec = .1; |
27
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
97 short neg = 0; |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
98 if(*a == '-') { |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
99 neg = 1; |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
100 a++; |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
101 } |
5
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
102 while(*a != '.') { |
27
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
103 if(!(*a >= '0' && *a <= '9') && *a != '-') return n; |
5
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
104 a++; |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
105 } 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
|
106 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
|
107 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
|
108 dec *= .1; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
109 a++; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
110 } |
27
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
111 if(neg) x*=-1; |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
112 return n + x; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
113 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
114 |
2 | 115 void sPrint(char *a) |
116 { | |
117 char *b = a; | |
118 while(*b && *++b); | |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
119 do { |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
120 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
|
121 a += CON_LEN; |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
122 } while(a < b); |
2 | 123 } |
124 | |
27
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
125 void strcpy(char *dest, const char *src) |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
126 { |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
127 while((*dest++ = *src++)); |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
128 } |
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
129 |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
130 int strcmp(const char *a, const char *b) |
2 | 131 { |
132 while(1) { | |
133 if(*a - *b) return *a - *b; | |
134 if(*a == '\0') return 0; | |
135 a++; | |
136 b++; | |
137 } | |
138 return -1; | |
139 } | |
140 | |
15
525eab23e68a
I no longer need to bug Jeff
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
8
diff
changeset
|
141 char* sGet(char *a, unsigned int n) |
2 | 142 { |
143 int length = getline(a, n); | |
144 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
|
145 return a; |
2 | 146 } |
147 | |
148 int arrayLookup(char *text, const char array[][10], int last) | |
149 { | |
150 int i; | |
151 for(i = 0; i < last; i++) | |
152 if(!strcmp(array[i], text)) return i; | |
153 return last; | |
154 } | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
155 |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
156 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
|
157 { |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
158 char *ret = dest; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
159 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
|
160 while((*dest++ = *src++)); |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
161 return ret; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
162 } |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
163 |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
164 static Header base; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
165 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
|
166 |
22
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
167 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
|
168 { |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
169 allocp = &base; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
170 allocp->size = 0; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
171 allocp->next = (void*)HEAP_START; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
172 allocp->next->next = &base; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
173 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
|
174 if((sizeof(blockUnit)%sizeof(u64))) { |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
175 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
|
176 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
|
177 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
|
178 } |
22
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
179 } |
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
180 |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
181 void* malloc(size_t size) |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
182 { |
22
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
183 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
|
184 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
|
185 Header *prev = allocp; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
186 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
|
187 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
|
188 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
|
189 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
|
190 prev->next = cur->next; |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
191 } else { |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
192 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
|
193 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
|
194 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
|
195 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
|
196 cur->size = nUnits; |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
197 } |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
198 allocp = prev; |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
199 return (void*)(cur + 1); |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
200 } 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
|
201 return NULL; |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
202 } |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
203 } |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
204 } |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
205 |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
206 void free(void *ptr) |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
207 { |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
208 Header *toFree = (Header *)ptr - 1; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
209 Header *scan; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
210 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
|
211 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
|
212 toFree->next = scan->next; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
213 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
|
214 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
|
215 scan->size += toFree->size; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
216 scan->next = toFree->next; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
217 toFree = scan; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
218 } |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
219 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
|
220 toFree->size += toFree->next->size; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
221 toFree->next = toFree->next->next; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
222 } |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
223 } |
26
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
224 |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
225 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
|
226 { |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
227 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
|
228 sPrint("Memory address is: "); |
27
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
229 sPrint(itoa((intptr_t)head, buffer, 16)); |
26
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
230 sPrint("\n"); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
231 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
|
232 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
|
233 sPrint("\n"); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
234 sPrint("Next memory address is: "); |
27
1a070e843bf6
Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
26
diff
changeset
|
235 sPrint(itoa((intptr_t)head->next, buffer, 16)); |
26
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
236 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
|
237 } |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
238 |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
239 void printMem() |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
240 { |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
241 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
|
242 do { |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
243 printHeader(cur); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
244 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
|
245 } 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
|
246 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
|
247 } |