calloc.c (348B)
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 size_t total = nmemb * size; 10 if (nmemb != 0 && total / nmemb != size) { 11 return NULL; 12 } 13 14 void *ptr = malloc(total); 15 if (ptr != NULL) { 16 memset(ptr, 0, total); 17 } 18 return ptr; 19 }