Minor fixes for x64 interpreter.
[luajit-2.0/celess22.git] / src / lj_gc.h
blob63d6ec6cad6097771de34cb9c2cafa9b9bdd8293
1 /*
2 ** Garbage collector.
3 ** Copyright (C) 2005-2009 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #ifndef _LJ_GC_H
7 #define _LJ_GC_H
9 #include "lj_obj.h"
11 /* Garbage collector states. Order matters. */
12 enum { GCSpause, GCSpropagate, GCSsweepstring, GCSsweep, GCSfinalize };
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
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)
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)
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)
41 /* Collector. */
42 LJ_FUNC size_t lj_gc_separateudata(global_State *g, int all);
43 LJ_FUNC void lj_gc_finalizeudata(lua_State *L);
44 LJ_FUNC void lj_gc_freeall(global_State *g);
45 LJ_FUNCA int LJ_FASTCALL lj_gc_step(lua_State *L);
46 LJ_FUNCA void LJ_FASTCALL lj_gc_step_fixtop(lua_State *L);
47 LJ_FUNC void LJ_FASTCALL lj_gc_step_jit(lua_State *L, MSize steps);
48 LJ_FUNC void lj_gc_fullgc(lua_State *L);
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); }
58 /* Write barriers. */
59 LJ_FUNC void lj_gc_barrierback(global_State *g, GCtab *t);
60 LJ_FUNC void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v);
61 LJ_FUNCA void LJ_FASTCALL lj_gc_barrieruv(global_State *g, TValue *tv);
62 LJ_FUNC void lj_gc_closeuv(global_State *g, GCupval *uv);
63 LJ_FUNC void lj_gc_barriertrace(global_State *g, void *T);
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)); }
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)); }
81 /* Allocator. */
82 LJ_FUNC void *lj_mem_realloc(lua_State *L, void *p, MSize osz, MSize nsz);
83 LJ_FUNC void *lj_mem_newgco(lua_State *L, MSize size);
84 LJ_FUNC void *lj_mem_grow(lua_State *L, void *p,
85 MSize *szp, MSize lim, MSize esz);
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))
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))
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)))
102 #endif