summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2017-07-13 11:23:03 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2017-07-13 11:23:03 +0200
commitebb0e5f9fe6707133dacbe70f80831cd1aa5c974 (patch)
tree23ccd04644fcab179812a093545213dec9907943
parented869aa3867d134fb37f586f1db029006677cebb (diff)
downloadabaos-ebb0e5f9fe6707133dacbe70f80831cd1aa5c974.tar.gz
abaos-ebb0e5f9fe6707133dacbe70f80831cd1aa5c974.tar.bz2
added a simple test for malloc/free
added a memorymanager initilializer which works on a buffer instead on an offset (thus we can but the heap area also onto the stack for testing) added a kernel_stub module to test modules using kernel_panic (for now, maybe have a callback later)
-rw-r--r--BUGS3
-rw-r--r--src/kernel/memorymanagement.c12
-rw-r--r--src/kernel/memorymanagement.h1
-rw-r--r--src/libc/stdlib.c17
-rw-r--r--src/libc/stdlib.h6
-rw-r--r--tests/libc/Makefile27
-rw-r--r--tests/libc/kernel_stub.c18
7 files changed, 64 insertions, 20 deletions
diff --git a/BUGS b/BUGS
index 0f618d8..8e5ebbe 100644
--- a/BUGS
+++ b/BUGS
@@ -21,4 +21,5 @@
outside of the library. Then in the libc test directory we have
to implement a stub module for instance using some host functions
like standard io of the host C library
-
+- memorymanagement should be a library of the libc or even a standalone
+ library, not part of the kernel directory
diff --git a/src/kernel/memorymanagement.c b/src/kernel/memorymanagement.c
index b8a84b2..bc7252e 100644
--- a/src/kernel/memorymanagement.c
+++ b/src/kernel/memorymanagement.c
@@ -10,7 +10,17 @@ void memory_manager_init( memory_manager_t *memory_manager, size_t offset, size_
memory_manager->offset = offset;
memory_manager->size = size;
- memory_manager->free_ptr = offset;
+ memory_manager->free_ptr = memory_manager->offset;
+ memory_manager->prev_ptr = 0xFFFFFFFF;
+}
+
+void memory_manager_init_with_buf( memory_manager_t *memory_manager, void *buf, size_t size )
+{
+ memset( memory_manager, 0, sizeof( memory_manager_t ) );
+
+ memory_manager->offset = (uint32_t)buf;
+ memory_manager->size = size;
+ memory_manager->free_ptr = memory_manager->offset;
memory_manager->prev_ptr = 0xFFFFFFFF;
}
diff --git a/src/kernel/memorymanagement.h b/src/kernel/memorymanagement.h
index 647c05d..5ea2618 100644
--- a/src/kernel/memorymanagement.h
+++ b/src/kernel/memorymanagement.h
@@ -11,6 +11,7 @@ typedef struct {
} memory_manager_t;
void memory_manager_init( memory_manager_t *memory_manager, size_t offset, size_t size );
+void memory_manager_init_with_buf( memory_manager_t *memory_manager, void *buf, size_t size );
void *memory_manager_allocate( memory_manager_t *memory_manager, size_t size );
void memory_manager_deallocate( memory_manager_t *memory_manager, void **p );
diff --git a/src/libc/stdlib.c b/src/libc/stdlib.c
index d6bb8b2..d7d4331 100644
--- a/src/libc/stdlib.c
+++ b/src/libc/stdlib.c
@@ -3,6 +3,8 @@
#include "stdlib.h"
#include "stddef.h"
+#include "kernel.h"
+
static void strreverse( char *s )
{
char *end = s + strlen( s ) - 1;
@@ -46,29 +48,28 @@ char *itoa( int v, char *s, int base )
return s;
}
-#ifdef OS_ABAOS
+void abort( void )
+{
+ // TODO: this should be done on process level, terminating
+ // the process (by signalling SIGABRT for instance)
+ kernel_panic( "aborted" );
+}
+
// TODO: we should have a global memory manager and one per
// user process later
static memory_manager_t *stdlib_memory_manager = NULL;
-#endif
void *malloc( size_t size )
{
-#ifdef OS_ABAOS
return memory_manager_allocate( stdlib_memory_manager, size );
-#endif
}
void free( void *p )
{
-#ifdef OS_ABAOS
memory_manager_deallocate( stdlib_memory_manager, &p );
-#endif
}
-#ifdef OS_ABAOS
void __stdlib_set_memory_manager( memory_manager_t *memory_manager )
{
stdlib_memory_manager = memory_manager;
}
-#endif
diff --git a/src/libc/stdlib.h b/src/libc/stdlib.h
index 9507fa3..9ff28a3 100644
--- a/src/libc/stdlib.h
+++ b/src/libc/stdlib.h
@@ -3,17 +3,15 @@
#include "stddef.h"
-#ifdef OS_ABAOS
#include "memorymanagement.h"
-#endif
char *itoa( int v, char *s, int base );
+void abort( void );
+
void *malloc( size_t size );
void free( void *p );
-#ifdef OS_ABAOS
void __stdlib_set_memory_manager( memory_manager_t *memory_manager );
-#endif
#endif // STDLIB_H
diff --git a/tests/libc/Makefile b/tests/libc/Makefile
index ce7e121..43620d7 100644
--- a/tests/libc/Makefile
+++ b/tests/libc/Makefile
@@ -1,6 +1,6 @@
CC := gcc
CFLAGS := -std=c99 -m32 -ffreestanding -O0 -g -Wall -Werror
-INCLUDES = -I../../src/libc
+INCLUDES = -I../../src/libc -I../../src/kernel
LD := ld
all: test
@@ -17,22 +17,37 @@ test_strlcat: test_strlcat.o ../../src/libc/string.o
test_strlcat.o: test_strlcat.c
$(CC) $(CFLAGS) $(INCLUDES) -c -o test_strlcat.o test_strlcat.c
-test_itoa: test_itoa.o ../../src/libc/stdlib.o ../../src/libc/string.o
- $(CC) -o test_itoa test_itoa.o ../../src/libc/stdlib.o ../../src/libc/string.o
+test_itoa: test_itoa.o ../../src/libc/stdlib.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o
+ $(CC) -o test_itoa test_itoa.o ../../src/libc/stdlib.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o
-test_itoa.o: test_itoa.c ../../src/libc/stdlib.o
+test_itoa.o: test_itoa.c ../../src/libc/stdlib.h
$(CC) $(CFLAGS) $(INCLUDES) -c -o test_itoa.o test_itoa.c
+test_malloc: test_malloc.o ../../src/libc/stdlib.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o
+ $(CC) -o test_malloc test_malloc.o ../../src/libc/stdlib.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o
+
+test_malloc.o: test_malloc.c ../../src/libc/stdlib.h
+ $(CC) $(CFLAGS) $(INCLUDES) -c -o test_malloc.o test_malloc.c
+
../../src/libc/string.o: ../../src/libc/string.c
$(CC) $(CFLAGS) $(INCLUDES) -c -o ../../src/libc/string.o ../../src/libc/string.c
../../src/libc/stdlib.o: ../../src/libc/stdlib.c
$(CC) $(CFLAGS) $(INCLUDES) -c -o ../../src/libc/stdlib.o ../../src/libc/stdlib.c
-test: test_strlcpy test_strlcat test_itoa
+../../src/kernel/memorymanagement.o: ../../src/kernel/memorymanagement.c
+ $(CC) $(CFLAGS) $(INCLUDES) -c -o ../../src/kernel/memorymanagement.o ../../src/kernel/memorymanagement.c
+
+kernel_stub.o: kernel_stub.c
+ $(CC) $(CFLAGS) $(INCLUDES) -c -o kernel_stub.o kernel_stub.c
+
+test: test_strlcpy test_strlcat test_itoa test_malloc
./test_strlcpy
./test_strlcat
./test_itoa
+ ./test_malloc
clean:
- -rm -f test_strlcpy test_strlcat test_itoa ../../src/libc/stdlib.o ../../src/libc/string.o *.o
+ -rm -f test_strlcpy test_strlcat test_itoa test_malloc
+ -rm -f ../../src/libc/stdlib.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o
+ -rm -f *.o
diff --git a/tests/libc/kernel_stub.c b/tests/libc/kernel_stub.c
new file mode 100644
index 0000000..c859be0
--- /dev/null
+++ b/tests/libc/kernel_stub.c
@@ -0,0 +1,18 @@
+#include <stdarg.h>
+
+#include "stdio.h"
+#include "stdlib.h"
+
+void kernel_panic( const char *format, ... )
+{
+ (void)printf( "\n*** KERNEL PANIC ***\n" );
+
+ va_list args;
+ va_start( args, format );
+ (void)vprintf( format, args );
+ va_end( args );
+ puts( "" );
+
+ abort( );
+}
+