annotate src/std.c @ 94:191e99dffd6c

Moved some std.h functions to string.h, worked on a simple version of sprintf sprintf is a very simple version of the C Standard Library version. It should work for doubles, ints, and strings. I also added the strlen function.
author Jonathan Pevarnek <pevarnj@gmail.com>
date Sat, 14 May 2011 12:54:29 -0400
parents 480f5685b3c2
children d8f21e4a75e3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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>
94
191e99dffd6c Moved some std.h functions to string.h, worked on a simple version of sprintf
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 87
diff changeset
2 #include <string.h>
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
3
54
39fcefab46ed Modified the init_fs function to take advantage of dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 28
diff changeset
4 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
5 {
39fcefab46ed Modified the init_fs function to take advantage of dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 28
diff changeset
6 init_io_int();
39fcefab46ed Modified the init_fs function to take advantage of dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 28
diff changeset
7 init_console();
39fcefab46ed Modified the init_fs function to take advantage of dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 28
diff changeset
8 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
9 }
39fcefab46ed Modified the init_fs function to take advantage of dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 28
diff changeset
10
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
11 double abs(double num)
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
12 {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
13 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
14 else return num;
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
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
17 char* itoa(s64 n, char *a, unsigned short base)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
18 {
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
19 char *ret = a;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
20 if(n < 0) {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
21 *a++ = '-';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
22 n *= -1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
23 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
24 char *b = a;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
25 if(!n) *b++ = '0';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
26 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
27 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
28 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
29 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
30 n = n/base;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
31 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
32 *b-- = '\0';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
33 for(; a < b; a++, b--) { //reverse
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
34 char temp = *b;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
35 *b = *a;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
36 *a = temp;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
37 }
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
38 return ret;
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
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
41 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
42 {
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
43 char *ret = a;
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
44 if(x == INF || x == -INF) {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
45 strcpy(a, "INF");
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
46 } 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
47 strcpy(a, "NAN");
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
48 } else {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
49 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
50 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
51 itoa(n, a, 10);
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
52 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
53 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
54 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
55 *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
56 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
57 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
58 *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
59 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
60 } 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
61 while(*a != '.') {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
62 if(*a == '0') {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
63 a--;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
64 continue;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
65 } 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
66 a++;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
67 break;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
68 } else {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
69 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
70 break;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
71 }
5
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
72 }
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
73 *a = '\0';
3
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 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
76 return ret;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
77 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
78
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
79 s64 atoi(char *a)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
80 {
8
25b2b501a5fa Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 5
diff changeset
81 short neg = 0;
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
82 s64 n = 0;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
83 if(*a == '-') {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
84 neg = 1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
85 a++;
8
25b2b501a5fa Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 5
diff changeset
86 } 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
87 while(*a >= '0' && *a <= '9')
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
88 n = n*10 + (*a++ - '0');
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
89 if(neg) n *= -1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
90 return n;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
91 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
92
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
93 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
94 {
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
95 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
96 double x = 0;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
97 double dec = .1;
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
98 short neg = 0;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
99 if(*a == '-') {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
100 neg = 1;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
101 a++;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
102 }
5
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
103 while(*a != '.') {
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
104 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
105 a++;
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
106 } 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
107 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
108 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
109 dec *= .1;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
110 a++;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
111 }
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
112 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
113 return n + x;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
114 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
115
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
116 void sPrint(char *a)
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
117 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
118 char *b = a;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
119 while(*b && *++b);
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
120 do {
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
121 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
122 a += CON_LEN;
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
123 } while(a < b);
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
124 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
125
15
525eab23e68a I no longer need to bug Jeff
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 8
diff changeset
126 char* sGet(char *a, unsigned int n)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
127 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
128 int length = getline(a, n);
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
129 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
130 return a;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
131 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
132
55
25be3895c62a The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 54
diff changeset
133 //DYNAMIC MEMORY
25be3895c62a The filesystem now supports a much better cache system
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 54
diff changeset
134
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
135 static Header base;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
136 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
137
22
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
138 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
139 {
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
140 allocp = &base;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
141 allocp->size = 0;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
142 allocp->next = (void*)HEAP_START;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
143 allocp->next->next = &base;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
144 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
145 if((sizeof(blockUnit)%sizeof(u64))) {
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
146 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
147 }
22
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
148 }
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
149
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
150 void* malloc(size_t size)
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
151 {
22
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
152 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
153 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
154 Header *prev = allocp;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
155 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
156 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
157 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
158 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
159 prev->next = cur->next;
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
160 } else {
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
161 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
162 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
163 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
164 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
165 cur->size = nUnits;
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
166 }
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
167 allocp = prev;
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
168 return (void*)(cur + 1);
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
169 } 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
170 return NULL;
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 }
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
174
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
175 void free(void *ptr)
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
176 {
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
177 Header *toFree = (Header *)ptr - 1;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
178 Header *scan;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
179 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
180 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
181 toFree->next = scan->next;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
182 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
183 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
184 scan->size += toFree->size;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
185 scan->next = toFree->next;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
186 toFree = scan;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
187 }
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
188 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
189 toFree->size += toFree->next->size;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
190 toFree->next = toFree->next->next;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
191 }
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
192 }
26
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 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
195 {
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
196 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
197 sPrint("Memory address is: ");
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
198 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
199 sPrint("\n");
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
200 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
201 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
202 sPrint("\n");
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
203 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
204 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
205 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
206 }
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 void printMem()
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
209 {
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
210 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
211 do {
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
212 printHeader(cur);
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
213 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
214 } 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
215 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
216 }