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
62
63
|
#include "MemoryManager.h"
#include "Types.h"
#include <stdlib.h>
#include <string.h>
Result
std_alloc(MemoryManager mm, void **ptr, usize size) {
if (NULL == ptr)
return R_NullArgument;
*ptr = malloc(size);
return (*ptr == NULL) ? R_Err : R_Ok;
}
Result
std_realloc(MemoryManager mm, void **ptr, usize size) {
if (NULL == ptr)
return R_NullArgument;
*ptr = realloc(*ptr, size);
return (*ptr == NULL) ? R_Err : R_Ok;
}
Result
std_free(MemoryManager mm, void **ptr) {
if (NULL == ptr)
return R_NullArgument;
free(*ptr);
*ptr = NULL;
return R_Ok;
}
Result
std_destroy(MemoryManager *mm) {
return R_Unavailable;
}
MemoryManager
M_std() {
return (MemoryManager){
.alloc = &std_alloc,
.realloc = &std_realloc,
.free = &std_free,
.destroy = &std_destroy,
};
}
char*
M_strdup(MemoryManager mm, const char* str) {
if (NULL == str)
return NULL;
char* newstr = NULL;
if (R_Ok != mm.alloc(mm, (void**)&newstr, strlen(str) + 1) ||
NULL == newstr)
{
return NULL;
}
strcpy(newstr, str);
return newstr;
}
|