Add: calloc

This commit is contained in:
xamidev
2024-09-27 20:03:24 +02:00
parent 1085222d98
commit 8a68cf3b30
8 changed files with 28 additions and 6 deletions

View File

@@ -56,6 +56,23 @@ void* malloc(size_t size)
return NULL;
}
void* calloc(size_t num, size_t size)
{
size_t total_size = num*size;
void* ptr = malloc(total_size);
if (ptr != NULL)
{
uint8_t* byte_ptr = (uint8_t*)ptr;
for (size_t i=0; i<total_size; i++)
{
byte_ptr[i] = 0;
}
}
return ptr;
}
void free(void* ptr)
{
if (ptr == NULL) return;

View File

@@ -20,5 +20,6 @@ typedef struct block
void init_alloc();
void* malloc(size_t size);
void free(void* ptr);
void* calloc(size_t num, size_t size);
#endif

View File

@@ -118,6 +118,10 @@ void kmain(multiboot2_info *mb_info)
printf("[debug] malloc test ptr1=0x%x, ptr2=0x%x\n", (unsigned int)ptr1, (unsigned int)ptr2);
free(ptr1); free(ptr2);
void* ptr3 = calloc(1024, 2);
printf("[debug] calloc test ptr3=0x%x\n", (unsigned int)ptr3);
free (ptr3);
// usually the place where i do testing
timer_install();