-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib.c
More file actions
318 lines (267 loc) · 9.45 KB
/
Copy pathstdlib.c
File metadata and controls
318 lines (267 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//-------------------------------------------------------------------------------
// Copyright 2022 Mark Seminatore. All rights reserved.
//-------------------------------------------------------------------------------
#include "ctype.h"
#include "string.h"
#include "assert.h"
#include "stdlib.h"
#include "malloc/mm.h"
#if defined(_WIN32)
#include <windows.h>
#elif defined (__APPLE_CC__) || defined(__linux__)
#include <sys/syscall.h>
#include "unistd.h"
#endif
#include "stdarg.h"
//-------------------------------------------------------------------------------
// platform specific process exit and atexit implementation
//-------------------------------------------------------------------------------
typedef void (*atexit_func_t)(void);
static atexit_func_t atexit_funcs[CRTL_MAX_ATEXIT_FUNCS];
static int atexit_count = 0;
static unsigned int rand_seed = 1;
// heap memory and management structures
// 16-byte alignment ensures SIMD-safe pointers from the allocator on all targets.
static char g_heap_mem[CRTL_HEAP_SIZE] __attribute__((aligned(16)));
static MM_LIST g_heap;
static int g_heap_initialized = 0;
static void heap_init(void)
{
if (!g_heap_initialized)
{
mm_list_init(&g_heap, g_heap_mem, sizeof(g_heap_mem));
g_heap_initialized = 1;
}
}
//-------------------------------------------------------------------------------
// platform specific process exit
//-------------------------------------------------------------------------------
NORETURN void exit(int status)
{
// call the registered atexit functions
for (int i = atexit_count - 1; i >= 0; i--)
{
atexit_funcs[i]();
}
// platform specific process exit
#if defined(_WIN32)
ExitProcess(status);
#elif defined (__APPLE_CC__) || defined(__linux__)
syscall(SYS_exit, status);
#endif
while (1) {}
}
//-------------------------------------------------------------------------------
// abort the program
//-------------------------------------------------------------------------------
NORETURN void abort()
{
exit(0);
}
//-------------------------------------------------------------------------------
// Registers a function to be called at normal process termination
//-------------------------------------------------------------------------------
int atexit(void (*func)(void))
{
if (atexit_count >= CRTL_MAX_ATEXIT_FUNCS)
return -1;
atexit_funcs[atexit_count++] = func;
return 0;
}
//-------------------------------------------------------------------------------
// convert a string to an integer
//-------------------------------------------------------------------------------
int atoi(const char *str)
{
int result = 0, places;
int placeValue = 1;
int sign = 1;
// check for negative number
if (str[0] == '-')
{
str++;
sign = -1;
}
places = strlen(str);
for (int i = places - 1; i >= 0; i--)
{
if (!isdigit(str[i]))
return 0;
result += (str[i] - '0') * placeValue;
placeValue *= 10;
}
return sign * result;
}
//-------------------------------------------------------------------------------
// absolute value
//-------------------------------------------------------------------------------
int abs(int x)
{
return x < 0 ? -x : x;
}
//-------------------------------------------------------------------------------
// absolute value for long int
//-------------------------------------------------------------------------------
long int labs(long int x)
{
return x < 0 ? -x : x;
}
#ifdef CRTL_INC_DIV
//-------------------------------------------------------------------------------
// compute quotient and remainder of integer division
//-------------------------------------------------------------------------------
div_t div(int numer, int denom)
{
div_t result;
result.quot = numer / denom;
result.rem = numer - result.quot * denom;
return result;
}
#endif
#ifdef CRTL_INC_LDIV
//-------------------------------------------------------------------------------
// compute quotient and remainder of long integer division
//-------------------------------------------------------------------------------
ldiv_t ldiv(long int numer, long int denom)
{
ldiv_t result;
result.quot = numer / denom;
result.rem = numer - result.quot * denom;
return result;
}
#endif
//-------------------------------------------------------------------------------
// return a psuedo random number
//-------------------------------------------------------------------------------
int rand(void)
{
int result;
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"
unsigned int x = rand_seed;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
rand_seed = x;
return (int)(x/2);
}
//-------------------------------------------------------------------------------
// set the seed for random number generation
//-------------------------------------------------------------------------------
void srand(unsigned int seed)
{
rand_seed = seed;
}
//-------------------------------------------------------------------------------
// free the memory block pointed to by ptr
//-------------------------------------------------------------------------------
void free(void *ptr)
{
heap_init();
mm_free(&g_heap, ptr);
}
//-------------------------------------------------------------------------------
// alloc memory of size bytes and return a pointer to the allocated memory
//-------------------------------------------------------------------------------
void *malloc(size_t size)
{
if (0 == size)
return NULL;
// mm_alloc uses the freed block's space to store a next pointer (sizeof(void*)),
// so the minimum safe allocation is sizeof(void*) bytes.
if (size < sizeof(void*))
size = sizeof(void*);
heap_init();
return mm_alloc(&g_heap, size);
}
//-------------------------------------------------------------------------------
// alloc memory for an array of num elements of size bytes each and returns a
// pointer to the allocated memory. The memory is set to zero.
//-------------------------------------------------------------------------------
void *calloc(size_t num, size_t size)
{
if (0 == num || 0 == size)
return NULL;
heap_init();
return mm_calloc(&g_heap, num, size);
}
//-------------------------------------------------------------------------------
// change the size of the memory block pointed to by ptr to newSize bytes
//-------------------------------------------------------------------------------
void *realloc(void *ptr, size_t newSize)
{
heap_init();
return mm_realloc(&g_heap, ptr, newSize);
}
//-------------------------------------------------------------------------------
// convert a string to a long integer
//-------------------------------------------------------------------------------
long int atol(const char *str)
{
return (long int)atoi(str);
}
#if 0
//-------------------------------------------------------------------------------
// convert a string to a long integer with error checking
//-------------------------------------------------------------------------------
long int strtol(const char *str, char **endptr, int base)
{
// TODO - convert str to a long int and return it. If endptr is not NULL, store the address of the first invalid character in *endptr. The base can be 2, 8, 10, or 16
return 0;
}
//-------------------------------------------------------------------------------
// convert a string to an unsigned long integer with error checking
//-------------------------------------------------------------------------------
unsigned long int strtoul(const char *str, char **endptr, int base)
{
// TODO - convert str to an unsigned long int and return it. If endptr is not NULL,
// store the address of the first invalid character in *endptr.
// The base can be 2, 8, 10, or 16
return 0;
}
#endif
//-------------------------------------------------------------------------------
// sort an array of num elements of size bytes each, using the compar function
// to compare elements
//-------------------------------------------------------------------------------
void qsort(void *base, size_t num, size_t size, int (*compar)(const void *, const void *))
{
int j;
// for simplicity, we'll use an insertion sort, which is efficient for small arrays and is stable.
// A more efficient algorithm like quicksort or mergesort could be implemented for larger arrays.
for (int i = 1; i < num; i++)
{
j = i;
for (; j > 0 && compar((char *)base + (j - 1) * size, (char *)base + j * size) > 0; j--)
{
char temp[size];
memcpy(temp, (char *)base + j * size, size);
memcpy((char *)base + j * size, (char *)base + (j - 1) * size, size);
memcpy((char *)base + (j - 1) * size, temp, size);
}
}
}
//-------------------------------------------------------------------------------
// perform a binary search for key in the array pointed to by base, which contains
// num elements of size bytes each, using the compar function to compare elements
//-------------------------------------------------------------------------------
void *bsearch(const void *key, const void *base, size_t num, size_t size, int (*compar)(const void *, const void *))
{
if (num == 0)
return NULL;
// perform a binary search for key in the array pointed to by base, which contains num elements of size bytes each, using the compar function to compare elements
void *low = (void *)base;
void *high = (char *)base + (num - 1) * size;
void *mid;
while (low <= high)
{
mid = (char *)low + (((char *)high - (char *)low) / size / 2) * size;
int cmp = compar(key, mid);
if (cmp < 0)
high = (char *)mid - size;
else if (cmp > 0)
low = (char *)mid + size;
else
return mid;
}
return NULL;
}