Mercurial > sos > sos
annotate src/std.c @ 87:480f5685b3c2
Fixed bugs in delete, moved arrayLookup to sarpn
arrayLookup was a specialized function that, though useful, did not belong in
the standard library
delete was not properly determining the location for the end block and also not
actually updating the disk with the re-written table. Both fixed.
author | Jonathan Pevarnek <pevarnj@gmail.com> |
---|---|
date | Wed, 27 Apr 2011 22:49:00 -0400 |
parents | 25be3895c62a |
children | 191e99dffd6c |
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 | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
148 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
|
149 { |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
150 char *ret = dest; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
151 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
|
152 while((*dest++ = *src++)); |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
153 return ret; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
154 } |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
155 |
55
25be3895c62a
The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
54
diff
changeset
|
156 void* memcpy(void *destination, const void *source, size_t num) |
25be3895c62a
The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
54
diff
changeset
|
157 { |
25be3895c62a
The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
54
diff
changeset
|
158 int i; |
25be3895c62a
The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
54
diff
changeset
|
159 const u8 *from = source; |
25be3895c62a
The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
54
diff
changeset
|
160 u8 *to = destination; |
25be3895c62a
The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
54
diff
changeset
|
161 for(i = 0; i < num; i++) |
25be3895c62a
The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
54
diff
changeset
|
162 to[i] = from[i]; |
25be3895c62a
The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
54
diff
changeset
|
163 return destination; |
25be3895c62a
The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
54
diff
changeset
|
164 } |
25be3895c62a
The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
54
diff
changeset
|
165 |
25be3895c62a
The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
54
diff
changeset
|
166 |
25be3895c62a
The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
54
diff
changeset
|
167 |
25be3895c62a
The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
54
diff
changeset
|
168 //DYNAMIC MEMORY |
25be3895c62a
The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
54
diff
changeset
|
169 |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
170 static Header base; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
171 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
|
172 |
22
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
173 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
|
174 { |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
175 allocp = &base; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
176 allocp->size = 0; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
177 allocp->next = (void*)HEAP_START; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
178 allocp->next->next = &base; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
179 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
|
180 if((sizeof(blockUnit)%sizeof(u64))) { |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
181 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
|
182 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
|
183 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
|
184 } |
22
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
185 } |
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
186 |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
187 void* malloc(size_t size) |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
188 { |
22
3fb0ec050ff3
malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
21
diff
changeset
|
189 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
|
190 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
|
191 Header *prev = allocp; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
192 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
|
193 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
|
194 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
|
195 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
|
196 prev->next = cur->next; |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
197 } else { |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
198 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
|
199 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
|
200 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
|
201 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
|
202 cur->size = nUnits; |
21
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 allocp = prev; |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
205 return (void*)(cur + 1); |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
206 } 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
|
207 return NULL; |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
208 } |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
209 } |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
210 } |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
211 |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
212 void free(void *ptr) |
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
213 { |
24
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
214 Header *toFree = (Header *)ptr - 1; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
215 Header *scan; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
216 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
|
217 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
|
218 toFree->next = scan->next; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
219 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
|
220 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
|
221 scan->size += toFree->size; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
222 scan->next = toFree->next; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
223 toFree = scan; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
224 } |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
225 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
|
226 toFree->size += toFree->next->size; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
227 toFree->next = toFree->next->next; |
45a80ea314ae
The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
23
diff
changeset
|
228 } |
21
7c2adb65ceac
Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
15
diff
changeset
|
229 } |
26
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
230 |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
231 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
|
232 { |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
233 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
|
234 sPrint("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, 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"); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
237 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
|
238 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
|
239 sPrint("\n"); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
240 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
|
241 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
|
242 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
|
243 } |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
244 |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
245 void printMem() |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
246 { |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
247 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
|
248 do { |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
249 printHeader(cur); |
c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
24
diff
changeset
|
250 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
|
251 } 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
|
252 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
|
253 } |