summaryrefslogtreecommitdiff
path: root/src/lj_gc.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lj_gc.h102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/lj_gc.h b/src/lj_gc.h
new file mode 100644
index 00000000..192066d3
--- /dev/null
+++ b/src/lj_gc.h
@@ -0,0 +1,102 @@
1/*
2** Garbage collector.
3** Copyright (C) 2005-2009 Mike Pall. See Copyright Notice in luajit.h
4*/
5
6#ifndef _LJ_GC_H
7#define _LJ_GC_H
8
9#include "lj_obj.h"
10
11/* Garbage collector states. Order matters. */
12enum { GCSpause, GCSpropagate, GCSsweepstring, GCSsweep, GCSfinalize };
13
14/* Bitmasks for marked field of GCobj. */
15#define LJ_GC_WHITE0 0x01
16#define LJ_GC_WHITE1 0x02
17#define LJ_GC_BLACK 0x04
18#define LJ_GC_FINALIZED 0x08
19#define LJ_GC_WEAKKEY 0x08
20#define LJ_GC_WEAKVAL 0x10
21#define LJ_GC_FIXED 0x20
22#define LJ_GC_SFIXED 0x40
23
24#define LJ_GC_WHITES (LJ_GC_WHITE0 | LJ_GC_WHITE1)
25#define LJ_GC_COLORS (LJ_GC_WHITES | LJ_GC_BLACK)
26#define LJ_GC_WEAK (LJ_GC_WEAKKEY | LJ_GC_WEAKVAL)
27
28/* Macros to test and set GCobj colors. */
29#define iswhite(x) ((x)->gch.marked & LJ_GC_WHITES)
30#define isblack(x) ((x)->gch.marked & LJ_GC_BLACK)
31#define isgray(x) (!((x)->gch.marked & (LJ_GC_BLACK|LJ_GC_WHITES)))
32#define tviswhite(x) (tvisgcv(x) && iswhite(gcV(x)))
33#define otherwhite(g) (g->gc.currentwhite ^ LJ_GC_WHITES)
34#define isdead(g, v) ((v)->gch.marked & otherwhite(g) & LJ_GC_WHITES)
35
36#define curwhite(g) ((g)->gc.currentwhite & LJ_GC_WHITES)
37#define newwhite(g, x) (obj2gco(x)->gch.marked = (uint8_t)curwhite(g))
38#define flipwhite(x) ((x)->gch.marked ^= LJ_GC_WHITES)
39#define fixstring(s) ((s)->marked |= LJ_GC_FIXED)
40
41/* Collector. */
42LJ_FUNC size_t lj_gc_separateudata(global_State *g, int all);
43LJ_FUNC void lj_gc_finalizeudata(lua_State *L);
44LJ_FUNC void lj_gc_freeall(global_State *g);
45LJ_FUNCA int lj_gc_step(lua_State *L);
46LJ_FUNCA void lj_gc_step_fixtop(lua_State *L);
47LJ_FUNCA void lj_gc_step_jit(lua_State *L, const BCIns *pc, MSize steps);
48LJ_FUNC void lj_gc_fullgc(lua_State *L);
49
50/* GC check: drive collector forward if the GC threshold has been reached. */
51#define lj_gc_check(L) \
52 { if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) \
53 lj_gc_step(L); }
54#define lj_gc_check_fixtop(L) \
55 { if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) \
56 lj_gc_step_fixtop(L); }
57
58/* Write barriers. */
59LJ_FUNC void lj_gc_barrierback(global_State *g, GCtab *t);
60LJ_FUNC void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v);
61LJ_FUNCA void lj_gc_barrieruv(global_State *g, GCobj *o, GCobj *v);
62LJ_FUNC void lj_gc_closeuv(global_State *g, GCupval *uv);
63LJ_FUNC void lj_gc_barriertrace(global_State *g, void *T);
64
65/* Barrier for stores to table objects. TValue and GCobj variant. */
66#define lj_gc_barriert(L, t, tv) \
67 { if (tviswhite(tv) && isblack(obj2gco(t))) \
68 lj_gc_barrierback(G(L), (t)); }
69#define lj_gc_objbarriert(L, t, o) \
70 { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) \
71 lj_gc_barrierback(G(L), (t)); }
72
73/* Barrier for stores to any other object. TValue and GCobj variant. */
74#define lj_gc_barrier(L, p, tv) \
75 { if (tviswhite(tv) && isblack(obj2gco(p))) \
76 lj_gc_barrierf(G(L), obj2gco(p), gcV(tv)); }
77#define lj_gc_objbarrier(L, p, o) \
78 { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
79 lj_gc_barrierf(G(L), obj2gco(p), obj2gco(o)); }
80
81/* Allocator. */
82LJ_FUNC void *lj_mem_realloc(lua_State *L, void *p, MSize osz, MSize nsz);
83LJ_FUNC void *lj_mem_newgco(lua_State *L, MSize size);
84LJ_FUNC void *lj_mem_grow(lua_State *L, void *p,
85 MSize *szp, MSize lim, MSize esz);
86
87#define lj_mem_new(L, s) lj_mem_realloc(L, NULL, 0, (s))
88#define lj_mem_free(g, p, osize) \
89 (g->gc.total -= (MSize)(osize), g->allocf(g->allocd, (p), (osize), 0))
90
91#define lj_mem_newvec(L, n, t) ((t *)lj_mem_new(L, (MSize)((n)*sizeof(t))))
92#define lj_mem_reallocvec(L, p, on, n, t) \
93 ((p) = (t *)lj_mem_realloc(L, p, (on)*sizeof(t), (MSize)((n)*sizeof(t))))
94#define lj_mem_growvec(L, p, n, m, t) \
95 ((p) = (t *)lj_mem_grow(L, (p), &(n), (m), (MSize)sizeof(t)))
96#define lj_mem_freevec(g, p, n, t) lj_mem_free(g, (p), (n)*sizeof(t))
97
98#define lj_mem_newobj(L, t) ((t *)lj_mem_newgco(L, sizeof(t)))
99#define lj_mem_newt(L, s, t) ((t *)lj_mem_new(L, (s)))
100#define lj_mem_freet(g, p) lj_mem_free(g, (p), sizeof(*(p)))
101
102#endif