-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.c
61 lines (54 loc) · 1.71 KB
/
mem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "mem.h"
#include <assert.h> // assert
#include <stdio.h> // fputs, stderr
#include <stdlib.h> // free, realloc, abort
#include "safemath.h"
#ifdef SLANG_DEBUG
MemStats mem_stats;
#endif
void mem_error(const char *msg) {
(void)msg;
#ifndef SLANG_COREDUMP
fputs(msg, stderr);
putc('\n', stderr);
abort();
#endif
}
void *mem_reallocate(void *p, size_t old_size, size_t new_size) {
#ifdef SLANG_DEBUG
mem_stats.calls++;
#endif
if (new_size == 0) {
#ifdef SLANG_DEBUG
if (size_t_sub_over(mem_stats.bytes, old_size, &mem_stats.bytes)) {
mem_error("freeing more bytes than allocated");
}
#endif
free(p);
return 0;
}
if (new_size == old_size) {
return p;
}
#ifdef SLANG_DEBUG
if (size_t_sub_over(mem_stats.bytes, old_size, &mem_stats.bytes)) {
mem_error("freeing more bytes than allocated");
}
if (size_t_add_over(mem_stats.bytes, new_size, &mem_stats.bytes)) {
mem_error("allocating more than SIZE_T_MAX");
}
#endif
void *res = realloc(p, new_size);
if (!res) {
mem_error("out of memory");
}
return res;
}
extern inline void *mem_allocate(size_t);
extern inline void mem_free(void *, size_t);
extern inline void *mem_resize_array(void *, size_t item_size, size_t old_len, size_t new_len);
extern inline void mem_free_array(void *, size_t item_size, size_t old_len);
extern inline void *mem_allocate_flex(size_t type_size, size_t item_size, size_t len);
extern inline void *mem_resize_flex(void *, size_t type_size, size_t item_size, size_t old_len,
size_t new_len);
extern inline void mem_free_flex(void *, size_t type_size, size_t item_size, size_t len);