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
|
#include <stdbool.h>
#include "string.h"
#include "stdlib.h"
#include <stddef.h>
#include "kernel.h"
static void strreverse( char *s )
{
char *end = s + strlen( s ) - 1;
while( s < end ) {
// XOR swap;
*s ^= *end;
*end ^= *s;
*s ^= *end;
s++;
end--;
}
}
char *itoa( int v, char *s, int base )
{
static char digit[] = "0123456789ABCDEF";
bool sign = false;
char *p = s;
if( base < 2 || base > 16 ) {
return NULL;
}
if( v < 0 ) {
v = -v;
sign = true;
}
do {
*p++ = digit[v % base];
} while( ( v /= base ) > 0 );
if( sign ) {
*p++ = '-';
}
*p = '\0';
strreverse( s );
return s;
}
void __attribute__( ( noreturn ) ) exit( int status )
{
#ifdef OS_ABAOS
// TODO: this should be done on process level, terminating
// the process (by signalling SIGABRT for instance)
kernel_panic( "exited with exit code %d", status );
#endif
#ifdef OS_LINUX
syscall1( __NR_exit, status );
#endif
// make gcc happy ("error: ‘noreturn’ function does return")
for( ;; );
}
void __attribute__( ( noreturn ) ) abort( void )
{
// TODO: this should be done on process level, terminating
// the process (by signalling SIGABRT for instance)
#ifdef OS_ABAOS
kernel_panic( "aborted" );
#endif
#ifdef OS_LINUX
// TODO: this clearly wrong, but for now we exit with a 255 exit code
// on abort..
syscall1( __NR_exit, 255 );
#endif
// make gcc happy ("error: ‘noreturn’ function does return")
for( ;; );
}
// TODO: we should have a global memory manager and one per
// user process later
static memory_manager_t *stdlib_memory_manager = NULL;
void *malloc( size_t size )
{
return memory_manager_allocate( stdlib_memory_manager, size, 1 );
}
void free( void *p )
{
memory_manager_deallocate( stdlib_memory_manager, &p );
}
void *aligned_alloc( size_t alignment, size_t size )
{
if( size % alignment != 0 ) {
kernel_panic( "Illegal call to 'aligned_alloc' with size %d and alignment %d!",
size, alignment );
}
return memory_manager_allocate( stdlib_memory_manager, size, alignment );
}
void __stdlib_set_memory_manager( memory_manager_t *memory_manager )
{
stdlib_memory_manager = memory_manager;
}
|