-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.c
More file actions
23 lines (19 loc) · 878 Bytes
/
Copy pathmemory.c
File metadata and controls
23 lines (19 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdlib.h>
#include "memory.h"
/*
https://craftinginterpreters.com/chunks-of-bytecode.html
This reallocate() function is the single function we’ll use for all dynamic memory management in clox—allocating memory, freeing it, and changing the size of an existing allocation. Routing all of those operations through a single function will be important later when we add a garbage collector that needs to keep track of how much memory is in use.
*/
void* reallocate(void* pointer, size_t oldSize, size_t newSize) {
if (newSize == 0) {
// handle deallocation ourselves
free(pointer);
return NULL;
}
// when oldSize is zero, realloc() is the same as malloc()
void* result = realloc(pointer, newSize);
// allocation can fail if there isn't enough memory and relloc()
// can return null, so handle it:
if (result == NULL) exit(1);
return result;
}