summaryrefslogtreecommitdiff
path: root/ecomp-c/libc-freestanding.c
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2020-03-14 20:00:10 +0100
committerAndreas Baumann <mail@andreasbaumann.cc>2020-03-14 20:00:10 +0100
commitcaa171eb64d90d3428190dfaa76a45c111ded7e8 (patch)
tree679eff874ff22e51affb68aa1d196f3d571a6786 /ecomp-c/libc-freestanding.c
parent5c038f1937b97405c1610d1b52bb2d70807128ae (diff)
downloadcompilertests-caa171eb64d90d3428190dfaa76a45c111ded7e8.tar.gz
compilertests-caa171eb64d90d3428190dfaa76a45c111ded7e8.tar.bz2
fixed tests for hex constants
more debug output and checks in freestanding malloc library started added handling of symbols in asm-i386
Diffstat (limited to 'ecomp-c/libc-freestanding.c')
-rw-r--r--ecomp-c/libc-freestanding.c19
1 files changed, 16 insertions, 3 deletions
diff --git a/ecomp-c/libc-freestanding.c b/ecomp-c/libc-freestanding.c
index a638a89..3d09366 100644
--- a/ecomp-c/libc-freestanding.c
+++ b/ecomp-c/libc-freestanding.c
@@ -258,6 +258,8 @@ static void *initial_brk = NULL;
static int mem_total_use = 0;
static int mem_in_use = 0;
static int mem_max_use = 0;
+static int mem_nof_mallocs = 0;
+static int mem_nof_frees = 0;
int mem_align( int n )
{
@@ -278,8 +280,9 @@ void *malloc( int size )
while( header != NULL ) {
if( !header->used && header->size >= size ) {
header->used = 1;
- mem_in_use += size;
- mem_total_use += size;
+ mem_in_use += header->size;
+ mem_total_use += header->size;
+ mem_nof_mallocs++;
return (void *)( header + 1 );
}
header = header->next;
@@ -310,22 +313,31 @@ void *malloc( int size )
mem_max_use += size;
mem_total_use += size;
mem_in_use += size;
+ mem_nof_mallocs++;
return (void *)( header + 1 );
}
+static void print_string( int fd, const char *s );
+
void free( void *ptr )
{
mem_header *header, *tmp;
void *actual_brk;
if( ptr == NULL ) {
+ print_string( 2, "warning: freeing a NULL pointer\n" );
return;
}
header = (mem_header *)ptr - 1;
+ if( !header->used ) {
+ print_string( 2, "warning: double free in linked list detected\n" );
+ return;
+ }
header->used = 0;
mem_in_use -= header->size;
+ mem_nof_frees++;
actual_brk = sbrk( 0 );
if( (char *)ptr + header->size == actual_brk ) {
@@ -587,6 +599,7 @@ void malloc_stats( void )
{
void *actual_brk = sbrk( 0 );
- fprintf( stderr, "Total %d bytes allocated, %d bytes maximal, %d bytes in use, brked memory %d bytes\n",
+ fprintf( stderr, "Total %d bytes allocated, %d bytes peak allocation, %d bytes in use, brked memory %d bytes\n",
mem_total_use, mem_max_use, mem_in_use, (int)actual_brk - (int)initial_brk );
+ fprintf( stderr, "Total %d mallocs, %d frees\n", mem_nof_mallocs, mem_nof_frees );
}