* Fixed the build of the boot loader's heap test.

* Added realloc() test that actually succeeds.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34413 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2009-12-01 15:40:34 +00:00
parent 943bec1287
commit 625990fbb9
2 changed files with 74 additions and 43 deletions
+6 -19
View File
@@ -1,31 +1,18 @@
SubDir HAIKU_TOP src tests system boot heap ; SubDir HAIKU_TOP src tests system boot heap ;
UsePublicHeaders support ; UsePrivateKernelHeaders ;
UsePrivateHeaders kernel ;
SubDirHdrs $(HAIKU_TOP) headers private kernel arch $(TARGET_ARCH) ; ObjectDefines heap.cpp : malloc=heap_malloc free=heap_free realloc=heap_realloc
SubDirHdrs $(HAIKU_TOP) headers private kernel boot platform $(TARGET_BOOT_PLATFORM) ; HEAP_TEST=1 ;
ObjectDefines heap.cpp : malloc=heap_malloc free=heap_free realloc=heap_realloc HEAP_TEST=1 ; SimpleTest heapTest :
# This lets the correct stdio.h header be available
# on the build platform.
# This will be fixed with the move to GNU's stdio header
if $(OS) = "LINUX" {
SubDirC++Flags -include /usr/include/stdio.h ;
} else {
SubDirC++Flags -include /boot/develop/headers/posix/stdio.h ;
}
BuildPlatformTest heapTest :
heapTest.cpp heapTest.cpp
# from the boot loader:
heap.cpp heap.cpp
: ;
;
# Tell Jam where to find the utility sources # Tell Jam where to find the utility sources
SEARCH on [ FGristFiles SEARCH on [ FGristFiles
heap.cpp heap.cpp
] = [ FDirName $(HAIKU_TOP) src system boot loader ] ; ] = [ FDirName $(HAIKU_TOP) src system boot loader ] ;
+63 -19
View File
@@ -1,7 +1,7 @@
/* /*
** Copyright 2003, Axel Dörfler, [email protected]. All rights reserved. * Copyright 2003-2009, Axel Dörfler, [email protected].
** Distributed under the terms of the OpenBeOS License. * Distributed under the terms of the MIT License.
*/ */
#include <boot/platform.h> #include <boot/platform.h>
@@ -13,8 +13,9 @@
#include <stdarg.h> #include <stdarg.h>
extern "C" void *heap_malloc(size_t size); extern "C" void* heap_malloc(size_t size);
extern "C" void heap_free(void *buffer); extern "C" void* heap_realloc(void* oldBuffer, size_t size);
extern "C" void heap_free(void* buffer);
extern void dump_chunks(void); extern void dump_chunks(void);
extern uint32 heap_available(void); extern uint32 heap_available(void);
@@ -25,28 +26,28 @@ int32 gVerbosity = 1;
void void
platform_release_heap(struct stage2_args *args, void *base) platform_release_heap(struct stage2_args* args, void* base)
{ {
free(base); free(base);
} }
status_t status_t
platform_init_heap(struct stage2_args *args, void **_base, void **_top) platform_init_heap(struct stage2_args* args, void** _base, void** _top)
{ {
void *base = malloc(kHeapSize); void* base = malloc(kHeapSize);
if (base == NULL) if (base == NULL)
return B_NO_MEMORY; return B_NO_MEMORY;
*_base = base; *_base = base;
*_top = (void *)((uint8 *)base + kHeapSize); *_top = (void*)((uint8*)base + kHeapSize);
return B_OK; return B_OK;
} }
void void
panic(const char *format, ...) panic(const char* format, ...)
{ {
va_list args; va_list args;
@@ -58,32 +59,51 @@ panic(const char *format, ...)
} }
// #pragma mark - void
dprintf(const char* format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
va_end(args);
}
// #pragma mark -
static void static void
dump_allocated_chunk(int32 index, void *buffer) dump_allocated_chunk(int32 index, void* buffer)
{ {
if (buffer == NULL || gVerbosity < 3) if (buffer == NULL || gVerbosity < 3)
return; return;
size_t *size = (size_t *)((uint8 *)buffer - sizeof(uint32)); size_t* size = (size_t*)((uint8*)buffer - sizeof(uint32));
printf("\t%ld. allocation at %p, chunk at %p, size = %ld\n", index, buffer, size, *size); printf("\t%ld. allocation at %p, chunk at %p, size = %ld\n", index, buffer,
size, *size);
if (gVerbosity > 3) if (gVerbosity > 3)
dump_chunks(); dump_chunks();
} }
static void * static void*
test_malloc(size_t bytes) test_malloc(size_t bytes)
{ {
return heap_malloc(bytes); return heap_malloc(bytes);
} }
static void*
test_realloc(void* oldBuffer, size_t size)
{
return heap_realloc(oldBuffer, size);
}
static void static void
test_free(void *buffer) test_free(void* buffer)
{ {
if (gVerbosity > 4) { if (gVerbosity > 4) {
printf("\tfreeing buffer at %p\n", buffer); printf("\tfreeing buffer at %p\n", buffer);
@@ -100,7 +120,7 @@ test_free(void *buffer)
static int32 static int32
random_allocations(void *array[], size_t maxSize) random_allocations(void* array[], size_t maxSize)
{ {
printf("* random allocations (up to %ld bytes)\n", maxSize); printf("* random allocations (up to %ld bytes)\n", maxSize);
@@ -134,7 +154,7 @@ random_allocations(void *array[], size_t maxSize)
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
if (argc > 1) if (argc > 1)
gVerbosity = atoi(argv[1]); gVerbosity = atoi(argv[1]);
@@ -153,7 +173,7 @@ main(int argc, char **argv)
dump_chunks(); dump_chunks();
puts("* simple allocation of 100 * 128 bytes"); puts("* simple allocation of 100 * 128 bytes");
void *array[100]; void* array[100];
for (int32 i = 0; i < 100; i++) { for (int32 i = 0; i < 100; i++) {
array[i] = test_malloc(128); array[i] = test_malloc(128);
dump_allocated_chunk(i, array[i]); dump_allocated_chunk(i, array[i]);
@@ -244,6 +264,30 @@ main(int argc, char **argv)
} }
} }
puts("* realloc() test");
uint8* buffer = (uint8*)test_malloc(1);
buffer[0] = 'h';
uint8* newBuffer = (uint8*)test_realloc(buffer, 2);
if (newBuffer != buffer)
panic(" could not reuse buffer");
newBuffer[1] = 'a';
newBuffer = (uint8*)test_realloc(buffer, 3);
if (newBuffer != buffer)
panic(" could not reuse buffer");
newBuffer[2] = 'i';
newBuffer = (uint8*)test_realloc(buffer, 4);
if (newBuffer != buffer)
panic(" could not reuse buffer");
newBuffer[3] = 'k';
newBuffer = (uint8*)test_realloc(buffer, 5);
if (newBuffer == buffer)
panic(" could reuse buffer!");
newBuffer[4] = 'u';
if (memcmp(newBuffer, "haiku", 5))
panic(" contents differ!");
heap_release(&args); heap_release(&args);
return 0; return 0;
} }