diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2017-07-05 13:53:42 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2017-07-05 13:53:42 +0200 |
commit | c55bc80baa51dc1028cbbae8a02540cfe4c56557 (patch) | |
tree | cc8f9e327d9e68eb6aee9ca781838c473074fa60 /src/libc | |
parent | b281252aac949fe54057608edb20cdfbe20eaed0 (diff) | |
download | abaos-c55bc80baa51dc1028cbbae8a02540cfe4c56557.tar.gz abaos-c55bc80baa51dc1028cbbae8a02540cfe4c56557.tar.bz2 |
'mem' command shows memory usage of kernel heap now
implemented a malloc/free and memory manager (simplest possible implementation,
just allocating linearly and never freeing)
Diffstat (limited to 'src/libc')
-rw-r--r-- | src/libc/stdio.c | 40 | ||||
-rw-r--r-- | src/libc/stdio.h | 4 | ||||
-rw-r--r-- | src/libc/stdlib.c | 19 | ||||
-rw-r--r-- | src/libc/stdlib.h | 9 |
4 files changed, 49 insertions, 23 deletions
diff --git a/src/libc/stdio.c b/src/libc/stdio.c index 1d53f7a..07a047e 100644 --- a/src/libc/stdio.c +++ b/src/libc/stdio.c @@ -3,16 +3,16 @@ #include "stdlib.h" #include "string.h" -console_t *global_console = NULL; +console_t *stdio_console = NULL; int puts( const char *s ) { - if( global_console == NULL ) { + if( stdio_console == NULL ) { return EOF; } - console_put_string( global_console, s ); - console_put_newline( global_console ); + console_put_string( stdio_console, s ); + console_put_newline( stdio_console ); return 1; } @@ -33,34 +33,34 @@ int vprintf( const char *format, va_list args ) const char *s = format; int n = 0; - if( global_console == NULL ) { + if( stdio_console == NULL ) { return -1; } while( *s != '\0' ) { switch( *s ) { case '\n': - console_put_newline( global_console ); + console_put_newline( stdio_console ); n++; break; case '%': s++; if( *s == '\0' ) { - console_put_string( global_console, "<truncated % found at end of format string>" ); - console_put_newline( global_console ); + console_put_string( stdio_console, "<truncated % found at end of format string>" ); + console_put_newline( stdio_console ); return -1; } switch( *s ) { case '%': - console_put_char( global_console, '%' ); + console_put_char( stdio_console, '%' ); break; case 'X': { char buf[19]; itoa( va_arg( args, int ), (char *)buf, 16 ); - console_put_string( global_console, buf ); + console_put_string( stdio_console, buf ); n += strlen( buf ); } break; @@ -68,30 +68,30 @@ int vprintf( const char *format, va_list args ) case 'd': { char buf[19]; itoa( va_arg( args, int ), (char *)buf, 10 ); - console_put_string( global_console, buf ); + console_put_string( stdio_console, buf ); n += strlen( buf ); } break; case 'c': - console_put_char( global_console, va_arg( args, int ) ); + console_put_char( stdio_console, va_arg( args, int ) ); break; case 's': - console_put_string( global_console, va_arg( args, const char * ) ); + console_put_string( stdio_console, va_arg( args, const char * ) ); break; default: - console_put_string( global_console, "<illegal format string %" ); - console_put_char( global_console, *s ); - console_put_string( global_console, ">" ); - console_put_newline( global_console ); + console_put_string( stdio_console, "<illegal format string %" ); + console_put_char( stdio_console, *s ); + console_put_string( stdio_console, ">" ); + console_put_newline( stdio_console ); } break; default: - console_put_char( global_console, *s ); + console_put_char( stdio_console, *s ); n++; } s++; @@ -100,7 +100,7 @@ int vprintf( const char *format, va_list args ) return n; } -void stdio_set_console( console_t *console ) +void __stdio_set_console( console_t *console ) { - global_console = console; + stdio_console = console; } diff --git a/src/libc/stdio.h b/src/libc/stdio.h index bc7fac0..dbf7ac7 100644 --- a/src/libc/stdio.h +++ b/src/libc/stdio.h @@ -9,12 +9,10 @@ #define EOF (-1) -extern console_t *global_console; - int puts( const char *s ); int printf( const char *format, ... ); int vprintf( const char *format, va_list args ); -void stdio_set_console( console_t *console ); +void __stdio_set_console( console_t *console ); #endif //STDIO_H diff --git a/src/libc/stdlib.c b/src/libc/stdlib.c index 46bdce8..9150431 100644 --- a/src/libc/stdlib.c +++ b/src/libc/stdlib.c @@ -45,4 +45,23 @@ char *itoa( int v, char *s, int base ) return s; } + +// 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 ); +} + +void free( void *p ) +{ + memory_manager_deallocate( stdlib_memory_manager, &p ); +} + +void __stdlib_set_memory_manager( memory_manager_t *memory_manager ) +{ + stdlib_memory_manager = memory_manager; +} + diff --git a/src/libc/stdlib.h b/src/libc/stdlib.h index 331012e..ff6b67f 100644 --- a/src/libc/stdlib.h +++ b/src/libc/stdlib.h @@ -1,6 +1,15 @@ #ifndef STDLIB_H #define STDLIB_H +#include "stddef.h" + +#include "memorymanagement.h" + char *itoa( int v, char *s, int base ); +void *malloc( size_t size ); +void free( void *p ); + +void __stdlib_set_memory_manager( memory_manager_t *memory_manager ); + #endif // STDLIB_H |