-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloc.c
139 lines (113 loc) · 3.49 KB
/
malloc.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "mylib.h"
void *my_malloc(size_t _size)
{
void *_memory = dmalloc(_size);
// void *_memory = malloc(_size);
if (!_memory)
{
fprintf(stderr, "malloc error\n");
exit(1);
}
return _memory;
}
void my_free(void *_memory)
{
dfree(_memory);
// free(_memory);
_memory = NULL;
}
void *my_malloc_str(size_t _size)
{
void *memory = my_malloc(_size);
// c++のmapにmallocのポインターをキーにして保存。
my_malloc_check_t *value = my_malloc(sizeof(my_malloc_check_t));
value->length = _size;
cpp_map_set(map_voidkey_kind, g_map_malloc_str, memory, value);
return memory;
}
void my_free_str(void *_memory)
{
if (!_memory)
return;
my_malloc_check_t *value = cpp_map_get(map_voidkey_kind, g_map_malloc_str, _memory);
if (!value)
return;
// freeする前に文字列の長さ(strlen)とmallocサイズが一致するかチェック。文字列の結合しかしていない。
int s_len = strlen((char *)_memory);
if (s_len != value->length - 1)
{
fprintf(stderr, "error at my_free_str, %d(strlen) != %d(value->length-1)\n", s_len, value->length - 1);
exit(1);
}
cpp_map_erase(map_voidkey_kind, g_map_malloc_str, _memory);
my_free(_memory);
my_free(value);
}
void *my_malloc_node(size_t _size)
{
void *_memory = my_malloc(_size);
// 追加mallocしてNode用のmapに追加
my_malloc_check_t *value = my_malloc(sizeof(my_malloc_check_t));
value->length = _size;
cpp_map_set(map_voidkey_kind, g_map_malloc_debug_node, _memory, value);
return _memory;
}
void my_free_node(void *_memory, int8_t free_env_var)
{
// node->env_varをfreeするならfree_env_var=1;
Node *node = (Node *)_memory;
// run前のnodeならfreeしない
if (!node->after_run)
{
return;
}
// return nodeならfreeしない(がenv_varを0にする)
if (node->return_value)
{
node->env_var = 0;
return;
}
// envにセットされたvarなら、envをfreeするときしかfreeしない
if (node->env_var && !free_env_var)
{
return;
}
// freeするので意味はないがデバッグ用。
node->freed = 1;
// ポインターのアドレスがmallocしたnodeか(freeされていないか)をチェックする。
my_malloc_check_t *value = cpp_map_get(map_voidkey_kind, g_map_malloc_debug_node, _memory);
if (!value)
{
fprintf(stderr, "free check error at my_free_node\n");
exit(1);
}
cpp_map_erase(map_voidkey_kind, g_map_malloc_debug_node, _memory);
my_free(value);
my_free(_memory);
}
void *my_malloc_env(size_t _size)
{
void *_memory = my_malloc(_size);
// 追加mallocしてEnv用のmapに追加
my_malloc_check_t *value = my_malloc(sizeof(my_malloc_check_t));
value->length = _size;
cpp_map_set(map_voidkey_kind, g_map_malloc_debug_env, _memory, value);
return _memory;
}
void my_free_env(void *_memory)
{
// ポインターのアドレスがmallocしたenvか(freeされていないか)をチェックする。
my_malloc_check_t *value = cpp_map_get(map_voidkey_kind, g_map_malloc_debug_env, _memory);
if (!value)
{
fprintf(stderr, "free check error at my_free_env\n");
exit(1);
}
cpp_map_erase(map_voidkey_kind, g_map_malloc_debug_env, _memory);
my_free(value);
my_free(_memory);
}