summaryrefslogtreecommitdiff
path: root/erts/lib_src/yielding_c_fun/lib/simple_c_gc/simple_c_gc.c
blob: 0eeb050282b81a57c684170ac5802bab0e999bd6 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * %CopyrightBegin%
 *
 * Copyright 2019 Kjell Winblad (kjellwinblad@gmail.com, http://winsh.me).
 * All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * %CopyrightEnd%
 */

/*
 *
 * Author: Kjell Winblad
 *
 */

#include "simple_c_gc.h"
#include "chained_hash_set.h"

#include <setjmp.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

static bool scgc_print_gc_info = false;
static ChainedHashSet *scgc_objects;
static void *scgc_stack_top;

#define SCGC_MIN_ALLOCS_UNTIL_FREE 100
static int scgc_allocs_until_gc = SCGC_MIN_ALLOCS_UNTIL_FREE;
static void *(*scgc_user_malloc)(size_t size);
static void (*scgc_user_free)(void *ptr);

typedef enum { scgc_blank_state, scgc_marked } scgc_object_state;

typedef struct {
  unsigned long magic_number;
  size_t size;
  scgc_object_state state;
  void *data[1];
} scgc_object;

typedef struct {
  void *address;
  scgc_object *base;
  unsigned long magic_number;
} scgc_object_ref;

static void *scgc_malloc(size_t size) {
  void *(*my_malloc)(size_t size);
  if (scgc_user_malloc == NULL) {
    my_malloc = malloc;
  } else {
    my_malloc = scgc_user_malloc;
  }
  void *res = my_malloc(size);
  if (res == NULL) {
    printf("GCGC: Allocator returned NULL.\n");
    exit(1);
  }
  return res;
}

static void scgc_free(void *ptr) {
  void (*my_free)(void *ptr);
  if (scgc_user_free == NULL) {
    my_free = free;
  } else {
    my_free = scgc_user_free;
  }
  my_free(ptr);
}

static void *scgc_extract_key(void *v, int keyPos) {
  (void)keyPos;
  return v;
}

static unsigned int scgc_hash_key(void *keyPtr) {
  /* From
   * https://lemire.me/blog/2018/08/15/fast-strongly-universal-64-bit-hashing-everywhere*/
  int64_t x = (intptr_t)*((void **)keyPtr);
  int64_t a = 2348923;
  int64_t b = 3292;
  int64_t c = 9893487421;
  int32_t low = (int)x;
  int32_t high = (int)(x >> 32);
  return (unsigned int)((a * low + b * high + c) >> 32);
}

static bool scgc_are_equal(void *v1p, void *v2p) {
  scgc_object_ref *v1 = ((scgc_object_ref *)v1p);
  scgc_object_ref *v2 = ((scgc_object_ref *)v2p);
  return v1->address == v2->address;
}

static char *scgc_to_string(void *vp) {
  scgc_object_ref *v = ((scgc_object_ref *)vp);
  char *str = scgc_malloc(200);
  sprintf(str, "{.address=%p, .base_address=%p, magic_number=%lu}", v->address,
          (void *)v->base, v->magic_number);
  return str;
}

static void scgc_initialize_global_state() {
  scgc_objects =
      ch_set_create(0, scgc_extract_key, scgc_hash_key, scgc_are_equal,
                    scgc_to_string, scgc_malloc, scgc_free);
  srand((int)(intptr_t)scgc_objects * 2654435761);
}

static void scgc_do_gc(bool no_stack);

static void scgc_destroy_global_state() {
  scgc_do_gc(true);
  ch_set_free(scgc_objects);
}

static int scgc_start_gced_code_2(int (*main)(int, char *[]), int argc,
                                  char **argv[]) {
  volatile int noinline = 1;
  volatile char **my_argv = (volatile char **)*argv;
  scgc_stack_top = (void *)my_argv;
  int (*next)(int, char *[]) = noinline ? main : (int (*)(int, char *[]))(NULL);
  {
    int to_return;
    scgc_initialize_global_state();
    to_return = next(argc, (char **)my_argv);
    scgc_destroy_global_state();
    return to_return;
  }
}

static void scgc_global_set_put(scgc_object_ref ref) {
  ch_set_insert(scgc_objects, &ref, sizeof(scgc_object_ref));
}

static void scgc_global_set_del(void *key) {
  ch_set_delete(scgc_objects, &key, sizeof(void *));
}

static scgc_object_ref *scgc_global_set_get(void *key) {
  scgc_object_ref *ret = ch_set_lookup(scgc_objects, &key);
  return ret;
}

static void scgc_mark_reachable_objects_in_region(void *start, void *end);

static void *scgc_min(void *a, void *b) { return a <= b ? a : b; }

static void *scgc_max(void *a, void *b) { return a > b ? a : b; }

static void ycf_find_stack_bottom_and_mark_conservative_helper(void) {
  volatile void *p = NULL;
  volatile intptr_t stack_bottom = (intptr_t)&p;
  scgc_mark_reachable_objects_in_region(
      scgc_min(scgc_stack_top, (void *)&stack_bottom),
      scgc_max((void *)&stack_bottom, scgc_stack_top));
}

static void scgc_get_stack_bottom_and_mark() {
  jmp_buf env;
  setjmp(env);

  volatile int noinline = 1;

  void (*bottom)(void) =
      noinline ? ycf_find_stack_bottom_and_mark_conservative_helper
               : (void (*)(void))(NULL);

  bottom();
}

static void scgc_mark_reachable_objects_in_region(void *start, void *end) {
  void **iter = start;
  void **iter_end = end;
  scgc_object *object;
  while (iter <= iter_end) {
    scgc_object_ref *ref = scgc_global_set_get(*iter);
    if (ref != NULL && ref->base->data == *iter &&
        ref->magic_number == ref->base->magic_number &&
        ref->base->state == scgc_blank_state) {
      object = ref->base;
      object->state = scgc_marked;
      scgc_mark_reachable_objects_in_region(
          &object->data[0], &((char *)object->data)[object->size - 1]);
    }
    iter = iter + 1;
  }
}

static void scgc_mark_reachable_objects(bool no_stack) {
  void *tmp = NULL;
  if (no_stack) {
    scgc_mark_reachable_objects_in_region(&tmp, &tmp);
  } else {
    scgc_get_stack_bottom_and_mark();
  }
}

static void scgc_collect_unmarked_traverser(size_t index, void *v,
                                            void *context) {
  void **objects_to_remove = context;
  scgc_object_ref *ref = v;
  if (ref->base->state == scgc_blank_state) {
    scgc_free(ref->base);
    objects_to_remove[index] = ref->address;
  } else {
    objects_to_remove[index] = NULL;
  }
}

static void scgc_remove_unmarked_objects() {
  size_t nr_of_candidates = scgc_objects->size;
  void **objects_to_remove = scgc_malloc(sizeof(void *) * nr_of_candidates);
  for (size_t i = 0; i < nr_of_candidates; i++) {
    objects_to_remove[i] = NULL;
  }
  ch_set_traverse(scgc_objects, scgc_collect_unmarked_traverser,
                  objects_to_remove);
  for (size_t i = 0; i < nr_of_candidates; i++) {
    if (objects_to_remove[i] != NULL) {
      scgc_global_set_del(objects_to_remove[i]);
    }
  }
  scgc_free(objects_to_remove);
}

static void scgc_unmark_traverser(size_t index, void *v, void *context) {
  scgc_object_ref *ref = v;
  ref->base->state = scgc_blank_state;
}

static void scgc_unmark_objects() {
  ch_set_traverse(scgc_objects, scgc_unmark_traverser, NULL);
}

static void scgc_do_gc(bool no_stack) {
  unsigned int objects_before = scgc_objects->size;
  scgc_mark_reachable_objects(no_stack);
  scgc_remove_unmarked_objects();
  scgc_unmark_objects();
  if (scgc_print_gc_info) {
    unsigned int objects_after = scgc_objects->size;
    unsigned int objects_removed = objects_before - objects_after;
    fprintf(stderr, "GC: before=%u, after=%u, removed=%u\n", objects_before,
            objects_after, objects_removed);
  }
}

static void scgc_gc() {
  scgc_allocs_until_gc--;
  if (scgc_allocs_until_gc <= 0) {
    scgc_do_gc(false);
    unsigned int objects_after = scgc_objects->size;
    scgc_allocs_until_gc = objects_after * 2;
    if (scgc_allocs_until_gc < SCGC_MIN_ALLOCS_UNTIL_FREE) {
      scgc_allocs_until_gc = SCGC_MIN_ALLOCS_UNTIL_FREE;
    }
  }
}

/* Public interface */

int scgc_start_gced_code(int (*main)(int, char *[]), int argc, char *argv[],
                         void *(*my_malloc)(size_t), void (*my_free)(void *)) {
  volatile int noinline = 1;
  int (*next)(int (*)(int, char *[]), int, char **[]) =
      (noinline ? scgc_start_gced_code_2
                : (int (*)(int (*)(int, char *[]), int, char **[]))(NULL));
  volatile char **my_argv = (volatile char **)argv;
  int res;
  scgc_user_malloc = my_malloc;
  scgc_user_free = my_free;
  if (my_argv == NULL) {
    fprintf(stderr,
            "scgc_start_gced_code: the argv parameter should not be NULL!");
    exit(1);
  }
  res = next(main, argc, (char ***)&my_argv);
  return res;
}

void *scgc_new(size_t size) {
  scgc_gc();
  scgc_object *new = scgc_malloc(size + sizeof(scgc_object));
  scgc_object_ref new_ref;
  unsigned long magic_number = (unsigned long)rand();
  new->state = scgc_blank_state;
  new->magic_number = magic_number;
  new->size = size;
  new_ref.address = new->data;
  new_ref.base = new;
  new_ref.magic_number = magic_number;
  scgc_global_set_put(new_ref);
  return new->data;
}

void scgc_enable_print_gc_info() { scgc_print_gc_info = true; }