annotate src/std.c @ 22:3fb0ec050ff3

malloc has been restructured to now work in a more logical manner malloc now allocates memory from the beginning of a block instead of the end. Additionally, memory is now 8-byte aligned instead of 16-byte aligned. malloc_init should now be called to set up all the memory information. Fixed a bug where malloc would not have iterated to find new memory locations.
author Jonathan Pevarnek <pevarnj@gmail.com>
date Wed, 16 Mar 2011 00:09:35 -0400
parents 7c2adb65ceac
children f68b59af5ea6
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>
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
2
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
3 char* itoa(int n, char *a)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
4 {
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
5 char *ret = a;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
6 if(n < 0) {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
7 *a++ = '-';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
8 n *= -1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
9 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
10 char *b = a;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
11 if(!n) *b++ = '0';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
12 for(; n; b++) {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
13 *b = n%10 + '0';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
14 n = n/10;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
15 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
16 *b-- = '\0';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
17 for(; a < b; a++, b--) { //reverse
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
18 char temp = *b;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
19 *b = *a;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
20 *a = temp;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
21 }
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
22 return ret;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
23 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
24
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
25 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
26 {
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
27 char *ret = a;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
28 int n = (int) x; //integer part
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
29 double d = x - (double) n; //fractional part;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
30 itoa(n, a);
5
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
31 if(prec) { //only do the decimal part if decimal parts were asked for
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
32 while(*a && *++a); //get to the null character from itoa
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
33 int i; //counter variable for the for loop
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
34 *a++ = '.'; //put the decimal in place
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
35 for(i = 0; i < prec; i++) {
5
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
36 d = d*10; //the integer part is the decimal digit
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
37 *a++ = ((int) d) + '0'; //add the integer part of d to the string
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
38 d -= (int) d; //chop off the integer part
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
39 } a--; //move back to the last digit
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
40 while(*a != '.') {
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
41 if(*a == '0') {
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
42 a--;
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
43 continue;
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
44 } else if(*a > '0' && *a <= '9') {
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
45 a++;
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
46 break;
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
47 }
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
48 }
5
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
49 *a = '\0';
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
50 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
51 return ret;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
52 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
53
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
54 int atoi(char *a)
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
55 {
8
25b2b501a5fa Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 5
diff changeset
56 short neg = 0;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
57 int n = 0;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
58 if(*a == '-') {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
59 neg = 1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
60 a++;
8
25b2b501a5fa Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 5
diff changeset
61 } 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
62 while(*a >= '0' && *a <= '9')
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
63 n = n*10 + (*a++ - '0');
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
64 if(neg) n *= -1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
65 return n;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
66 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
67
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
68 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
69 {
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
70 int n = atoi(a);
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
71 double x = 0;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
72 double dec = .1;
5
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
73 while(*a != '.') {
8
25b2b501a5fa Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 5
diff changeset
74 if(!(*a >= '0' && *a <= '9')) return n;
5
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
75 a++;
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
76 } 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
77 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
78 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
79 dec *= .1;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
80 a++;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
81 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
82 return n + x;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
83 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
84
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
85 void sPrint(char *a)
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
86 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
87 char *b = a;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
88 while(*b && *++b);
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
89 do {
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
90 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
91 a += CON_LEN;
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
92 } while(a < b);
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
93 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
94
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
95 int strcmp(const char *a, const char *b)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
96 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
97 while(1) {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
98 if(*a - *b) return *a - *b;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
99 if(*a == '\0') return 0;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
100 a++;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
101 b++;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
102 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
103 return -1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
104 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
105
15
525eab23e68a I no longer need to bug Jeff
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 8
diff changeset
106 char* sGet(char *a, unsigned int n)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
107 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
108 int length = getline(a, n);
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
109 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
110 return a;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
111 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
112
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
113 int arrayLookup(char *text, const char array[][10], int last)
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
114 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
115 int i;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
116 for(i = 0; i < last; i++)
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
117 if(!strcmp(array[i], text)) return i;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
118 return last;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
119 }
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
120
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
121 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
122 {
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
123 char *ret = dest;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
124 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
125 while((*dest++ = *src++));
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
126 return ret;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
127 }
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
128
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
129 static Block *allocp = NULL; //the location of the last item allocated by malloc
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
130
22
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
131 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
132 {
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
133 allocp = (void*)0x200000;
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
134 allocp->next = allocp;
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
135 allocp->size = memSize/sizeof(u64);
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
136 }
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
137
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
138 void* malloc(size_t size)
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
139 {
22
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
140 unsigned int nUnits = ((size + sizeof(Block)) + sizeof(u64) - 1)/sizeof(u64);
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
141 Block *cur, *prev, *temp;
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
142 if(allocp == NULL) return NULL;
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
143 prev = allocp;
22
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
144 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
145 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
146 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
147 prev->next = cur->next;
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
148 } else {
22
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
149 temp = cur + nUnits;
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
150 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
151 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
152 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
153 cur->size = nUnits;
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
154 }
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
155 allocp = prev;
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
156 return (void*)(cur + 1);
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
157 } 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
158 return NULL;
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
159 }
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
160 }
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
161 }
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
162
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
163 /*
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
164 void free(void *ptr)
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
165 {
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 */