PatchworkOS
Loading...
Searching...
No Matches
calloc.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include <string.h>
3
4#include "common/heap.h"
5
6void* calloc(size_t nmemb, size_t size)
7{
8 size_t totalSize = nmemb * size;
9 if (size != 0 && totalSize / size != nmemb)
10 {
11 return NULL;
12 }
13
15
16 _heap_header_t* block = _heap_alloc(totalSize);
17 if (block == NULL)
18 {
20 return NULL;
21 }
22
23 if (!(block->flags & _HEAP_ZEROED))
24 {
25 memset(block->data, 0, totalSize);
26 }
27 // When this function returns we have no way of knowing whether the caller
28 // will fill the memory or not, so we clear the zeroed flag.
29 block->flags &= ~_HEAP_ZEROED;
30
32 return block->data;
33}
void * calloc(size_t nmemb, size_t size)
Definition calloc.c:6
void _heap_acquire(void)
Acquire the heap lock.
Definition heap.c:81
_heap_header_t * _heap_alloc(uint64_t size)
Allocates a block of memory of given size.
Definition heap.c:199
void _heap_release(void)
Release the heap lock.
Definition heap.c:90
@ _HEAP_ZEROED
Block is zeroed.
Definition heap.h:55
#define NULL
Pointer error value.
Definition NULL.h:23
_PUBLIC void * memset(void *s, int c, size_t n)
Definition memset.c:4
uint8_t data[]
Definition heap.h:73
_heap_flags_t flags
Definition heap.h:69