Skip to content

Commit 0bb730c

Browse files
committed
lib: monkey: Test to add allocator trace
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent ebcba8a commit 0bb730c

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

lib/monkey/deps/flb_libco/amd64.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,56 @@ static void (*co_swap)(cothread_t, cothread_t) = 0;
117117
}
118118
#endif
119119

120+
121+
struct co_allocation {
122+
cothread_t handle;
123+
struct co_allocation *next;
124+
};
125+
126+
static thread_local struct co_allocation *co_allocations;
127+
128+
static int co_allocation_track(cothread_t handle) {
129+
struct co_allocation *entry;
130+
131+
entry = (struct co_allocation *)malloc(sizeof(struct co_allocation));
132+
if (entry == 0) {
133+
return 0;
134+
}
135+
136+
entry->handle = handle;
137+
entry->next = co_allocations;
138+
co_allocations = entry;
139+
140+
return 1;
141+
}
142+
143+
static int co_allocation_untrack(cothread_t handle) {
144+
struct co_allocation *entry;
145+
struct co_allocation *prev;
146+
147+
prev = 0;
148+
entry = co_allocations;
149+
150+
while (entry != 0) {
151+
if (entry->handle == handle) {
152+
if (prev != 0) {
153+
prev->next = entry->next;
154+
}
155+
else {
156+
co_allocations = entry->next;
157+
}
158+
159+
free(entry);
160+
return 1;
161+
}
162+
163+
prev = entry;
164+
entry = entry->next;
165+
}
166+
167+
return 0;
168+
}
169+
120170
static void crash() {
121171
assert(0); /* called only if cothread_t entrypoint returns */
122172
}
@@ -144,12 +194,25 @@ cothread_t co_create(unsigned int size, void (*entrypoint)(void),
144194
*--p = (long long)crash; /* crash if entrypoint returns */
145195
*--p = (long long)entrypoint; /* start of function */
146196
*(long long*)handle = (long long)p; /* stack pointer */
197+
198+
if (!co_allocation_track(handle)) {
199+
free(handle);
200+
return 0;
201+
}
147202
}
148203

149204
return handle;
150205
}
151206

152207
void co_delete(cothread_t handle) {
208+
if (handle == 0) {
209+
return;
210+
}
211+
212+
if (!co_allocation_untrack(handle)) {
213+
return;
214+
}
215+
153216
free(handle);
154217
}
155218

0 commit comments

Comments
 (0)