calloc.c (353B)
1 /* 2 * calloc.c - allocate and clear memory 3 * Copyright (C) 2026. All rights reserved. 4 */ 5 6 #include "libc.h" 7 8 void* calloc(size_t nmemb, size_t size) { 9 if (nmemb != 0 && size > (size_t)-1 / nmemb) { 10 return NULL; 11 } 12 13 size_t total = nmemb * size; 14 15 void *ptr = malloc(total); 16 if (ptr != NULL) { 17 memset(ptr, 0, total); 18 } 19 return ptr; 20 }