-
Notifications
You must be signed in to change notification settings - Fork 41
/
ngs.h
86 lines (70 loc) · 2.29 KB
/
ngs.h
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
#ifndef NGS_H
#define NGS_H
#include <stdint.h>
#include <stdio.h>
#include <pthread.h>
#include "version.h"
// GC - start
// http://www.hboehm.info/gc/scale.html
#define GC_THREADS
#define PARALLEL_MARK
#define THREAD_LOCAL_ALLOC
#include <gc.h>
#define NGS_GC_THR_INIT() GC_thr_init()
#ifdef NGS_STUPID_MALLOC_AFTER_FORK
extern int_fast8_t ngs_use_stupid_malloc;
void ngs_malloc_init();
void *ngs_malloc(size_t size);
void *ngs_malloc_atomic(size_t size);
void *ngs_realloc(void *ptr, size_t size);
# define NGS_GC_INIT() GC_set_handle_fork(1); GC_INIT(); ngs_use_stupid_malloc = 0;
# define NGS_MALLOC(n) ngs_malloc(n)
# define NGS_MALLOC_ATOMIC(n) ngs_malloc_atomic(n)
// There is a problem to know which chunks were allocated with gc malloc and which with standard malloc
# define NGS_REALLOC(p, n) ngs_realloc(p, n)
# define NGS_GCOLLECT() GC_gcollect()
# define NGS_SIZE(p) GC_size(p)
# define NGS_NOTIFY_MALLOC_ABOUT_FORK() ngs_use_stupid_malloc = 1; ngs_malloc_init();
#else
# define NGS_GC_INIT() GC_set_handle_fork(1); GC_INIT()
# define NGS_MALLOC(n) GC_MALLOC(n)
# define NGS_MALLOC_ATOMIC(n) GC_MALLOC_ATOMIC(n)
# define NGS_REALLOC(p, n) GC_REALLOC(p, n)
# define NGS_GCOLLECT() ngs_gc_collect()
# define NGS_SIZE(p) GC_size(p)
# define NGS_NOTIFY_MALLOC_ABOUT_FORK() (void)0
#endif
#define NGS_MALLOC_OBJ(dst) dst = NGS_MALLOC(sizeof(*dst)); assert(dst);
#define YY_MALLOC(ctx, size) NGS_MALLOC(size)
#define YY_REALLOC(ctx, ptr, size) NGS_REALLOC(ptr, size)
#define YY_FREE(ctx, ptr) (void)(ptr)
// GC - end
typedef struct VM VM;
// XXX: don't do NGS_REALLOC all the time. Maybe re-use some
// other code here instead of reinventing the wheel
#define ENSURE_ARRAY_ROOM(dst, allocated, len, min_len) { \
assert(min_len); \
if(!allocated) { \
allocated = min_len; \
dst = NGS_MALLOC(sizeof(*dst) * allocated); \
} \
while(allocated < len) { \
allocated *= 2; \
dst = NGS_REALLOC(dst, sizeof(*dst) * allocated); \
} \
assert(dst); \
}
#define PUSH_ARRAY_ELT(dst, len, elt) { \
(dst)[(len)++] = elt; \
}
#include "debug.h"
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
typedef enum {
METHOD_OK,
METHOD_ARGS_MISMATCH,
METHOD_IMPL_MISSING,
METHOD_EXCEPTION,
} METHOD_RESULT;
extern pthread_key_t thread_local_key;
#endif