From 57cdb8dab4af3e6f5f3608a97a8f01b7ed2c0727 Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Thu, 11 Jul 2019 00:04:18 +0200 Subject: [PATCH] Implemented calloc() --- src/main/fragment/vwuz1=_deref_pbuz2.asm | 5 +++++ src/main/fragment/vwuz1_le_0_then_la1.asm | 4 ++++ src/main/kc/stdlib/stdlib.kc | 11 +++++++++++ src/main/kc/stdlib/string.kc | 5 +++-- 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/main/fragment/vwuz1=_deref_pbuz2.asm create mode 100644 src/main/fragment/vwuz1_le_0_then_la1.asm diff --git a/src/main/fragment/vwuz1=_deref_pbuz2.asm b/src/main/fragment/vwuz1=_deref_pbuz2.asm new file mode 100644 index 000000000..9bb52804e --- /dev/null +++ b/src/main/fragment/vwuz1=_deref_pbuz2.asm @@ -0,0 +1,5 @@ +ldy #0 +lda ({z2}),y +sta {z1} +lda #0 +sta {z1}+1 \ No newline at end of file diff --git a/src/main/fragment/vwuz1_le_0_then_la1.asm b/src/main/fragment/vwuz1_le_0_then_la1.asm new file mode 100644 index 000000000..18921f075 --- /dev/null +++ b/src/main/fragment/vwuz1_le_0_then_la1.asm @@ -0,0 +1,4 @@ +lda {z1} +beq {la1} +lda {z1}+1 +beq {la1} \ No newline at end of file diff --git a/src/main/kc/stdlib/stdlib.kc b/src/main/kc/stdlib/stdlib.kc index 35e847513..5b9fe2dd3 100644 --- a/src/main/kc/stdlib/stdlib.kc +++ b/src/main/kc/stdlib/stdlib.kc @@ -1,5 +1,7 @@ // Implementation of functions found int C stdlib.h / stdlib.c +import "string" + // Top of the heap used by malloc() unsigned char* HEAP_TOP = 0xa000; @@ -20,6 +22,15 @@ void free(void* ptr) { // TODO: So far no support for freeing stuff } +// Allocates memory and returns a pointer to it. Sets allocated memory to zero. +// - nitems − This is the number of elements to be allocated. +// - size − This is the size of elements. +void *calloc(size_t nitems, size_t size) { + void* mem = malloc(nitems*size); + memset(mem, 0, nitems*size); + return mem; +} + // Searches an array of nitems unsigned words, the initial member of which is pointed to by base, for a member that matches the value key. // - key - The value to look for // - items - Pointer to the start of the array to search in diff --git a/src/main/kc/stdlib/string.kc b/src/main/kc/stdlib/string.kc index 12c3b6a85..02e50c607 100644 --- a/src/main/kc/stdlib/string.kc +++ b/src/main/kc/stdlib/string.kc @@ -29,8 +29,9 @@ void* memmove( void* destination, void* source, size_t num ) { // Copies the character c (an unsigned char) to the first num characters of the object pointed to by the argument str. void *memset(void *str, char c, size_t num) { char* end = (char*)str + num; - for(char* dst = str; dst!=end; dst++) - *dst = c; + if(num>0) + for(char* dst = str; dst!=end; dst++) + *dst = c; return str; }