1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-11-23 08:32:39 +00:00

Implemented calloc()

This commit is contained in:
jespergravgaard 2019-07-11 00:04:18 +02:00
parent 57bbe42711
commit 57cdb8dab4
4 changed files with 23 additions and 2 deletions

View File

@ -0,0 +1,5 @@
ldy #0
lda ({z2}),y
sta {z1}
lda #0
sta {z1}+1

View File

@ -0,0 +1,4 @@
lda {z1}
beq {la1}
lda {z1}+1
beq {la1}

View File

@ -1,5 +1,7 @@
// Implementation of functions found int C stdlib.h / stdlib.c // Implementation of functions found int C stdlib.h / stdlib.c
import "string"
// Top of the heap used by malloc() // Top of the heap used by malloc()
unsigned char* HEAP_TOP = 0xa000; unsigned char* HEAP_TOP = 0xa000;
@ -20,6 +22,15 @@ void free(void* ptr) {
// TODO: So far no support for freeing stuff // 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. // 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 // - key - The value to look for
// - items - Pointer to the start of the array to search in // - items - Pointer to the start of the array to search in

View File

@ -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. // 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) { void *memset(void *str, char c, size_t num) {
char* end = (char*)str + num; char* end = (char*)str + num;
for(char* dst = str; dst!=end; dst++) if(num>0)
*dst = c; for(char* dst = str; dst!=end; dst++)
*dst = c;
return str; return str;
} }