MCPcopy Create free account
hub / github.com/IntegralPilot/wasm_os / realloc

Function realloc

stdlib/c/stdlib.c:25–45  ·  view source on GitHub ↗

make our own realloc function

Source from the content-addressed store, hash-verified

23
24// make our own realloc function
25void* realloc(void* ptr, size_t new_size) {
26 if (ptr == NULL) {
27 return malloc(new_size);
28 }
29
30 // You need to keep track of the old size to properly copy the data
31 int old_size = ptrsize(ptr);
32
33 void* new_ptr = malloc(new_size);
34 if (new_ptr == NULL) {
35 return NULL; // Allocation failed
36 }
37
38 // Copy the old data to the new location
39 memcpy(new_ptr, ptr, old_size < new_size ? old_size : new_size);
40
41 // Free the old location
42 free(ptr);
43
44 return new_ptr;
45}
46
47// A simple implementation of atoi
48int atoi(const char* str) {

Callers 1

_startFunction · 0.85

Calls 3

mallocFunction · 0.85
memcpyFunction · 0.85
freeFunction · 0.85

Tested by

no test coverage detected