• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tarantool / luajit / 11774765987

11 Nov 2024 08:22AM UTC coverage: 92.931% (-0.008%) from 92.939%
11774765987

push

github

mandesero
tmp commit for ci check

5694 of 6033 branches covered (94.38%)

Branch coverage included in aggregate %.

21704 of 23449 relevant lines covered (92.56%)

2961521.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.16
/src/lj_gc.c
1
/*
2
** Garbage collector.
3
** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
4
**
5
** Major portions taken verbatim or adapted from the Lua interpreter.
6
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
7
*/
8

9
#define lj_gc_c
10
#define LUA_CORE
11

12
#include "lj_obj.h"
13
#include "lj_gc.h"
14
#include "lj_err.h"
15
#include "lj_buf.h"
16
#include "lj_str.h"
17
#include "lj_tab.h"
18
#include "lj_func.h"
19
#include "lj_udata.h"
20
#include "lj_meta.h"
21
#include "lj_state.h"
22
#include "lj_frame.h"
23
#if LJ_HASFFI
24
#include "lj_ctype.h"
25
#include "lj_cdata.h"
26
#endif
27
#include "lj_trace.h"
28
#include "lj_dispatch.h"
29
#include "lj_vm.h"
30
#include "lj_vmevent.h"
31

32
#define GCSTEPSIZE        1024u
33
#define GCSWEEPMAX        40
34
#define GCSWEEPCOST        10
35
#define GCFINALIZECOST        100
36

37
/* Macros to set GCobj colors and flags. */
38
#define white2gray(x)                ((x)->gch.marked &= (uint8_t)~LJ_GC_WHITES)
39
#define gray2black(x)                ((x)->gch.marked |= LJ_GC_BLACK)
40
#define isfinalized(u)                ((u)->marked & LJ_GC_FINALIZED)
41

42
/* -- Mark phase ---------------------------------------------------------- */
43

44
/* Mark a TValue (if needed). */
45
#define gc_marktv(g, tv) \
46
  { lj_assertG(!tvisgcv(tv) || (~itype(tv) == gcval(tv)->gch.gct), \
47
               "TValue and GC type mismatch"); \
48
    if (tviswhite(tv)) gc_mark(g, gcV(tv)); }
49

50
/* Mark a GCobj (if needed). */
51
#define gc_markobj(g, o) \
52
  { if (iswhite(obj2gco(o))) gc_mark(g, obj2gco(o)); }
53

54
/* Mark a string object. */
55
#define gc_mark_str(s)                ((s)->marked &= (uint8_t)~LJ_GC_WHITES)
56

57
/* Mark a white GCobj. */
58
static void gc_mark(global_State *g, GCobj *o)
191,202,629✔
59
{
60
  int gct = o->gch.gct;
191,209,340✔
61
  lj_assertG(iswhite(o), "mark of non-white object");
191,209,340✔
62
  lj_assertG(!isdead(g, o), "mark of dead object");
191,209,340✔
63
  white2gray(o);
191,209,340✔
64
  if (LJ_UNLIKELY(gct == ~LJ_TUDATA)) {
191,209,340✔
65
    GCtab *mt = tabref(gco2ud(o)->metatable);
1,055,362✔
66
    gray2black(o);  /* Userdata are never gray. */
1,055,362✔
67
    if (mt) gc_markobj(g, mt);
1,055,362✔
68
    gc_markobj(g, tabref(gco2ud(o)->env));
1,055,362✔
69
  } else if (LJ_UNLIKELY(gct == ~LJ_TUPVAL)) {
190,153,978✔
70
    GCupval *uv = gco2uv(o);
1,718,766✔
71
    gc_marktv(g, uvval(uv));
1,718,766✔
72
    if (uv->closed)
1,718,766✔
73
      gray2black(o);  /* Closed upvalues are never gray. */
152,120✔
74
  } else if (gct != ~LJ_TSTR && gct != ~LJ_TCDATA) {
188,435,212✔
75
    lj_assertG(gct == ~LJ_TFUNC || gct == ~LJ_TTAB ||
8,764,955✔
76
               gct == ~LJ_TTHREAD || gct == ~LJ_TPROTO || gct == ~LJ_TTRACE,
77
               "bad GC type %d", gct);
78
    setgcrefr(o->gch.gclist, g->gc.gray);
8,764,955✔
79
    setgcref(g->gc.gray, o);
8,764,955✔
80
  }
81
}
191,202,629✔
82

83
/* Mark GC roots. */
84
static void gc_mark_gcroot(global_State *g)
13,489✔
85
{
86
  ptrdiff_t i;
13,489✔
87
  for (i = 0; i < GCROOT_MAX; i++)
539,560✔
88
    if (gcref(g->gcroot[i]) != NULL)
526,071✔
89
      gc_markobj(g, gcref(g->gcroot[i]));
362,150✔
90
}
13,489✔
91

92
/* Start a GC cycle and mark the root set. */
93
static void gc_mark_start(global_State *g)
6,764✔
94
{
95
  setgcrefnull(g->gc.gray);
6,764✔
96
  setgcrefnull(g->gc.grayagain);
6,764✔
97
  setgcrefnull(g->gc.weak);
6,764✔
98
  gc_markobj(g, mainthread(g));
6,764✔
99
  gc_markobj(g, tabref(mainthread(g)->env));
6,764✔
100
  gc_marktv(g, &g->registrytv);
6,764✔
101
  gc_mark_gcroot(g);
6,764✔
102
  g->gc.state = GCSpropagate;
6,764✔
103
}
6,764✔
104

105
/* Mark open upvalues. */
106
static void gc_mark_uv(global_State *g)
6,725✔
107
{
108
  GCupval *uv;
6,725✔
109
  for (uv = uvnext(&g->uvhead); uv != &g->uvhead; uv = uvnext(uv)) {
1,573,314✔
110
    lj_assertG(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv,
1,566,589✔
111
               "broken upvalue chain");
112
    if (isgray(obj2gco(uv)))
1,566,589✔
113
      gc_marktv(g, uvval(uv));
1,566,589✔
114
  }
115
}
6,725✔
116

117
/* Mark userdata in mmudata list. */
118
static void gc_mark_mmudata(global_State *g)
6,725✔
119
{
120
  GCobj *root = gcref(g->gc.mmudata);
6,725✔
121
  GCobj *u = root;
6,725✔
122
  if (u) {
6,725✔
123
    do {
1,002,866✔
124
      u = gcnext(u);
1,002,866✔
125
      makewhite(g, u);  /* Could be from previous GC. */
1,002,866✔
126
      gc_mark(g, u);
1,002,866✔
127
    } while (u != root);
1,002,866✔
128
  }
129
}
6,725✔
130

131
/* Separate userdata objects to be finalized to mmudata list. */
132
size_t lj_gc_separateudata(global_State *g, int all)
7,427✔
133
{
134
  size_t m = 0;
7,427✔
135
  GCRef *p = &mainthread(g)->nextgc;
7,427✔
136
  GCobj *o;
7,427✔
137
  while ((o = gcref(*p)) != NULL) {
2,071,890✔
138
    if (!(iswhite(o) || all) || isfinalized(gco2ud(o))) {
2,064,463✔
139
      p = &o->gch.nextgc;  /* Nothing to do. */
1,057,644✔
140
    } else if (!lj_meta_fastg(g, tabref(gco2ud(o)->metatable), MM_gc)) {
1,006,819✔
141
      markfinalized(o);  /* Done, as there's no __gc metamethod. */
2,358✔
142
      p = &o->gch.nextgc;
2,358✔
143
    } else {  /* Otherwise move userdata to be finalized to mmudata list. */
144
      m += sizeudata(gco2ud(o));
1,004,461✔
145
      markfinalized(o);
1,004,461✔
146
      *p = o->gch.nextgc;
1,004,461✔
147
      if (gcref(g->gc.mmudata)) {  /* Link to end of mmudata list. */
1,004,461✔
148
        GCobj *root = gcref(g->gc.mmudata);
998,147✔
149
        setgcrefr(o->gch.nextgc, root->gch.nextgc);
998,147✔
150
        setgcref(root->gch.nextgc, o);
998,147✔
151
        setgcref(g->gc.mmudata, o);
998,147✔
152
      } else {  /* Create circular list. */
153
        setgcref(o->gch.nextgc, o);
6,314✔
154
        setgcref(g->gc.mmudata, o);
6,314✔
155
      }
156
    }
157
  }
158
  return m;
7,427✔
159
}
160

161
/* -- Propagation phase --------------------------------------------------- */
162

163
/* Traverse a table. */
164
static int gc_traverse_tab(global_State *g, GCtab *t)
3,778,944✔
165
{
166
  int weak = 0;
3,778,944✔
167
  cTValue *mode;
3,778,944✔
168
  GCtab *mt = tabref(t->metatable);
3,778,944✔
169
  if (mt)
3,778,944✔
170
    gc_markobj(g, mt);
35,934✔
171
  mode = lj_meta_fastg(g, mt, MM_mode);
3,778,944✔
172
  if (mode && tvisstr(mode)) {  /* Valid __mode field? */
20,515✔
173
    const char *modestr = strVdata(mode);
20,378✔
174
    int c;
20,378✔
175
    while ((c = *modestr++)) {
54,277✔
176
      if (c == 'k') weak |= LJ_GC_WEAKKEY;
33,899✔
177
      else if (c == 'v') weak |= LJ_GC_WEAKVAL;
13,543✔
178
    }
179
    if (weak) {  /* Weak tables are cleared in the atomic phase. */
20,378✔
180
#if LJ_HASFFI
181
      if (gcref(g->gcroot[GCROOT_FFI_FIN]) == obj2gco(t)) {
20,378✔
182
        weak = (int)(~0u & ~LJ_GC_WEAKVAL);
183
      } else
184
#endif
185
      {
186
        t->marked = (uint8_t)((t->marked & ~LJ_GC_WEAK) | weak);
13,545✔
187
        setgcrefr(t->gclist, g->gc.weak);
13,545✔
188
        setgcref(g->gc.weak, obj2gco(t));
13,545✔
189
      }
190
    }
191
  }
192
  if (weak == LJ_GC_WEAK)  /* Nothing to mark if both keys/values are weak. */
28,964✔
193
    return 1;
194
  if (!(weak & LJ_GC_WEAKVAL)) {  /* Mark array part. */
3,765,423✔
195
    MSize i, asize = t->asize;
3,765,401✔
196
    for (i = 0; i < asize; i++)
685,699,149✔
197
      gc_marktv(g, arrayslot(t, i));
681,933,748✔
198
  }
199
  if (t->hmask > 0) {  /* Mark hash part. */
3,765,423✔
200
    Node *node = noderef(t->node);
1,789,227✔
201
    MSize i, hmask = t->hmask;
1,789,227✔
202
    for (i = 0; i <= hmask; i++) {
758,735,841✔
203
      Node *n = &node[i];
756,946,614✔
204
      if (!tvisnil(&n->val)) {  /* Mark non-empty slot. */
756,946,614✔
205
        lj_assertG(!tvisnil(&n->key), "mark of nil key in non-empty slot");
444,918,862✔
206
        if (!(weak & LJ_GC_WEAKKEY)) gc_marktv(g, &n->key);
444,918,862✔
207
        if (!(weak & LJ_GC_WEAKVAL)) gc_marktv(g, &n->val);
756,946,614✔
208
      }
209
    }
210
  }
211
  return weak;
212
}
213

214
/* Traverse a function. */
215
static void gc_traverse_func(global_State *g, GCfunc *fn)
3,618,540✔
216
{
217
  gc_markobj(g, tabref(fn->c.env));
3,618,540✔
218
  if (isluafunc(fn)) {
3,618,540✔
219
    uint32_t i;
2,247,448✔
220
    lj_assertG(fn->l.nupvalues <= funcproto(fn)->sizeuv,
2,247,448✔
221
               "function upvalues out of range");
222
    gc_markobj(g, funcproto(fn));
2,247,448✔
223
    for (i = 0; i < fn->l.nupvalues; i++)  /* Mark Lua function upvalues. */
4,239,798✔
224
      gc_markobj(g, &gcref(fn->l.uvptr[i])->uv);
1,992,350✔
225
  } else {
226
    uint32_t i;
227
    for (i = 0; i < fn->c.nupvalues; i++)  /* Mark C function upvalues. */
1,506,042✔
228
      gc_marktv(g, &fn->c.upvalue[i]);
134,950✔
229
  }
230
}
3,618,540✔
231

232
#if LJ_HASJIT
233
/* Mark a trace. */
234
static void gc_marktrace(global_State *g, TraceNo traceno)
110,386✔
235
{
236
  GCobj *o = obj2gco(traceref(G2J(g), traceno));
110,386✔
237
  lj_assertG(traceno != G2J(g)->cur.traceno, "active trace escaped");
110,386✔
238
  if (iswhite(o)) {
110,386✔
239
    white2gray(o);
62,018✔
240
    setgcrefr(o->gch.gclist, g->gc.gray);
62,018✔
241
    setgcref(g->gc.gray, o);
62,018✔
242
  }
243
}
244

245
/* Traverse a trace. */
246
static void gc_traverse_trace(global_State *g, GCtrace *T)
68,760✔
247
{
248
  IRRef ref;
68,760✔
249
  if (T->traceno == 0) return;
68,760✔
250
  for (ref = T->nk; ref < REF_TRUE; ref++) {
882,670✔
251
    IRIns *ir = &T->ir[ref];
820,663✔
252
    if (ir->o == IR_KGC)
820,663✔
253
      gc_markobj(g, ir_kgc(ir));
296,505✔
254
    if (irt_is64(ir->t) && ir->o != IR_KNULL)
820,663✔
255
      ref++;
448,820✔
256
  }
257
  if (T->link) gc_marktrace(g, T->link);
62,007✔
258
  if (T->nextroot) gc_marktrace(g, T->nextroot);
62,007✔
259
  if (T->nextside) gc_marktrace(g, T->nextside);
62,007✔
260
  gc_markobj(g, gcref(T->startpt));
62,007✔
261
}
262

263
/* The current trace is a GC root while not anchored in the prototype (yet). */
264
#define gc_traverse_curtrace(g)        gc_traverse_trace(g, &G2J(g)->cur)
265
#else
266
#define gc_traverse_curtrace(g)        UNUSED(g)
267
#endif
268

269
/* Traverse a prototype. */
270
static void gc_traverse_proto(global_State *g, GCproto *pt)
590,483✔
271
{
272
  ptrdiff_t i;
590,483✔
273
  gc_mark_str(proto_chunkname(pt));
590,483✔
274
  for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++)  /* Mark collectable consts. */
3,113,522✔
275
    gc_markobj(g, proto_kgc(pt, i));
2,523,039✔
276
#if LJ_HASJIT
277
  if (pt->trace) gc_marktrace(g, pt->trace);
590,483✔
278
#endif
279
}
590,483✔
280

281
/* Traverse the frame structure of a stack. */
282
static MSize gc_traverse_frames(global_State *g, lua_State *th)
283
{
284
  TValue *frame, *top = th->top-1, *bot = tvref(th->stack);
285
  /* Note: extra vararg frame not skipped, marks function twice (harmless). */
286
  for (frame = th->base-1; frame > bot+LJ_FR2; frame = frame_prev(frame)) {
287
    GCfunc *fn = frame_func(frame);
288
    TValue *ftop = frame;
289
    if (isluafunc(fn)) ftop += funcproto(fn)->framesize;
290
    if (ftop > top) top = ftop;
291
    if (!LJ_FR2) gc_markobj(g, fn);  /* Need to mark hidden function (or L). */
292
  }
293
  top++;  /* Correct bias of -1 (frame == base-1). */
294
  if (top > tvref(th->maxstack)) top = tvref(th->maxstack);
295
  return (MSize)(top - bot);  /* Return minimum needed stack size. */
296
}
297

298
/* Traverse a thread object. */
299
static void gc_traverse_thread(global_State *g, lua_State *th)
1,570,766✔
300
{
301
  TValue *o, *top = th->top;
1,570,766✔
302
  for (o = tvref(th->stack)+1+LJ_FR2; o < top; o++)
11,563,691✔
303
    gc_marktv(g, o);
9,992,925✔
304
  if (g->gc.state == GCSatomic) {
1,570,766✔
305
    top = tvref(th->stack) + th->stacksize;
784,917✔
306
    for (; o < top; o++)  /* Clear unmarked slots. */
31,722,688✔
307
      setnilV(o);
30,937,771✔
308
  }
309
  gc_markobj(g, tabref(th->env));
1,570,766✔
310
  lj_state_shrinkstack(th, gc_traverse_frames(g, th));
1,570,766✔
311
}
1,570,766✔
312

313
/* Propagate one gray object. Traverse it and turn it black. */
314
static size_t propagatemark(global_State *g)
9,620,768✔
315
{
316
  GCobj *o = gcref(g->gc.gray);
9,620,768✔
317
  int gct = o->gch.gct;
9,620,768✔
318
  lj_assertG(isgray(o), "propagation of non-gray object");
9,620,768✔
319
  gray2black(o);
9,620,768✔
320
  setgcrefr(g->gc.gray, o->gch.gclist);  /* Remove from gray list. */
9,620,768✔
321
  if (LJ_LIKELY(gct == ~LJ_TTAB)) {
9,620,768✔
322
    GCtab *t = gco2tab(o);
3,778,944✔
323
    if (gc_traverse_tab(g, t) > 0)
3,778,944✔
324
      black2gray(o);  /* Keep weak tables gray. */
13,545✔
325
    return sizeof(GCtab) + sizeof(TValue) * t->asize +
3,778,944✔
326
                           (t->hmask ? sizeof(Node) * (t->hmask + 1) : 0);
3,778,944✔
327
  } else if (LJ_LIKELY(gct == ~LJ_TFUNC)) {
5,841,824✔
328
    GCfunc *fn = gco2func(o);
3,618,540✔
329
    gc_traverse_func(g, fn);
3,618,540✔
330
    return isluafunc(fn) ? sizeLfunc((MSize)fn->l.nupvalues) :
3,618,540✔
331
                           sizeCfunc((MSize)fn->c.nupvalues);
1,371,092✔
332
  } else if (LJ_LIKELY(gct == ~LJ_TPROTO)) {
2,223,284✔
333
    GCproto *pt = gco2pt(o);
590,483✔
334
    gc_traverse_proto(g, pt);
590,483✔
335
    return pt->sizept;
590,483✔
336
  } else if (LJ_LIKELY(gct == ~LJ_TTHREAD)) {
1,632,801✔
337
    lua_State *th = gco2th(o);
1,570,766✔
338
    setgcrefr(th->gclist, g->gc.grayagain);
1,570,766✔
339
    setgcref(g->gc.grayagain, o);
1,570,766✔
340
    black2gray(o);  /* Threads are never black. */
1,570,766✔
341
    gc_traverse_thread(g, th);
1,570,766✔
342
    return sizeof(lua_State) + sizeof(TValue) * th->stacksize;
1,570,766✔
343
  } else {
344
#if LJ_HASJIT
345
    GCtrace *T = gco2trace(o);
62,035✔
346
    gc_traverse_trace(g, T);
62,035✔
347
    return ((sizeof(GCtrace)+7)&~7) + (T->nins-T->nk)*sizeof(IRIns) +
62,035✔
348
           T->nsnap*sizeof(SnapShot) + T->nsnapmap*sizeof(SnapEntry);
62,035✔
349
#else
350
    lj_assertG(0, "bad GC type %d", gct);
351
    return 0;
352
#endif
353
  }
354
}
355

356
/* Propagate all gray objects. */
357
static size_t gc_propagate_gray(global_State *g)
26,900✔
358
{
359
  size_t m = 0;
13,450✔
360
  while (gcref(g->gc.gray) != NULL)
2,955,834✔
361
    m += propagatemark(g);
2,928,934✔
362
  return m;
26,900✔
363
}
364

365
/* -- Sweep phase --------------------------------------------------------- */
366

367
/* Type of GC free functions. */
368
typedef void (LJ_FASTCALL *GCFreeFunc)(global_State *g, GCobj *o);
369

370
/* GC free functions for LJ_TSTR .. LJ_TUDATA. ORDER LJ_T */
371
static const GCFreeFunc gc_freefunc[] = {
372
  (GCFreeFunc)lj_str_free,
373
  (GCFreeFunc)lj_func_freeuv,
374
  (GCFreeFunc)lj_state_free,
375
  (GCFreeFunc)lj_func_freeproto,
376
  (GCFreeFunc)lj_func_free,
377
#if LJ_HASJIT
378
  (GCFreeFunc)lj_trace_free,
379
#else
380
  (GCFreeFunc)0,
381
#endif
382
#if LJ_HASFFI
383
  (GCFreeFunc)lj_cdata_free,
384
#else
385
  (GCFreeFunc)0,
386
#endif
387
  (GCFreeFunc)lj_tab_free,
388
  (GCFreeFunc)lj_udata_free
389
};
390

391
/* Full sweep of a GC list. */
392
#define gc_fullsweep(g, p)        gc_sweep(g, (p), ~(uint32_t)0)
393

394
/* Partial sweep of a GC list. */
395
static GCRef *gc_sweep(global_State *g, GCRef *p, uint32_t lim)
8,374,549✔
396
{
397
  /* Mask with other white and LJ_GC_FIXED. Or LJ_GC_SFIXED on shutdown. */
398
  int ow = otherwhite(g);
8,374,549✔
399
  GCobj *o;
8,374,549✔
400
  while ((o = gcref(*p)) != NULL && lim-- > 0) {
342,244,832✔
401
    if (o->gch.gct == ~LJ_TTHREAD)  /* Need to sweep open upvalues, too. */
333,870,283✔
402
      gc_fullsweep(g, &gco2th(o)->openupval);
1,478,511✔
403
    if (((o->gch.marked ^ LJ_GC_WHITES) & ow)) {  /* Black or current white? */
333,870,283✔
404
      lj_assertG(!isdead(g, o) || (o->gch.marked & LJ_GC_FIXED),
192,654,866✔
405
                 "sweep of undead object");
406
      makewhite(g, o);  /* Value is alive, change to the current white. */
192,654,866✔
407
      p = &o->gch.nextgc;
192,654,866✔
408
    } else {  /* Otherwise value is dead, free it. */
409
      lj_assertG(isdead(g, o) || ow == LJ_GC_SFIXED,
141,215,417✔
410
                 "sweep of unlive object");
411
      setgcrefr(*p, o->gch.nextgc);
141,215,417✔
412
      if (o == gcref(g->gc.root))
141,215,417✔
413
        setgcrefr(g->gc.root, o->gch.nextgc);  /* Adjust list anchor. */
×
414
      gc_freefunc[o->gch.gct - ~LJ_TSTR](g, o);
141,215,417✔
415
    }
416
  }
417
  return p;
8,374,549✔
418
}
419

420
/* Full sweep of a string chain. */
421
static GCRef *gc_sweep_str_chain(global_State *g, GCRef *p)
13,585,610✔
422
{
423
  /* Mask with other white and LJ_GC_FIXED. Or LJ_GC_SFIXED on shutdown. */
424
  int ow = otherwhite(g);
13,585,610✔
425
  GCobj *o;
13,585,610✔
426
  while ((o = gcref(*p)) != NULL) {
20,638,600✔
427
    if (((o->gch.marked ^ LJ_GC_WHITES) & ow)) {  /* Black or current white? */
7,052,990✔
428
      lj_assertG(!isdead(g, o) || (o->gch.marked & LJ_GC_FIXED),
6,410,767✔
429
                 "sweep of undead string");
430
      makewhite(g, o);  /* Value is alive, change to the current white. */
6,410,767✔
431
#if LUAJIT_SMART_STRINGS
432
      if (strsmart(&o->str)) {
6,410,767✔
433
        /* must match lj_str_new */
434
        bloomset(g->strbloom.next[0], o->str.hash >> (sizeof(o->str.hash)*8-6));
21,148✔
435
        bloomset(g->strbloom.next[1], o->str.strflags);
21,148✔
436
      }
437
#endif
438
      p = &o->gch.nextgc;
6,410,767✔
439
    } else {  /* Otherwise value is dead, free it. */
440
      lj_assertG(isdead(g, o) || ow == LJ_GC_SFIXED,
642,223✔
441
                 "sweep of unlive string");
442
      setgcrefr(*p, o->gch.nextgc);
642,223✔
443
      lj_str_free(g, &o->str);
642,223✔
444
    }
445
  }
446
  return p;
13,585,610✔
447
}
448

449
/* Check whether we can clear a key or a value slot from a table. */
450
static int gc_mayclear(cTValue *o, int val)
451
{
452
  if (tvisgcv(o)) {  /* Only collectable objects can be weak references. */
453
    if (tvisstr(o)) {  /* But strings cannot be used as weak references. */
454
      gc_mark_str(strV(o));  /* And need to be marked. */
455
      return 0;
456
    }
457
    if (iswhite(gcV(o)))
458
      return 1;  /* Object is about to be collected. */
459
    if (tvisudata(o) && val && isfinalized(udataV(o)))
460
      return 1;  /* Finalized userdata is dropped only from values. */
461
  }
462
  return 0;  /* Cannot clear. */
463
}
464

465
/* Clear collected entries from weak tables. */
466
static void gc_clearweak(global_State *g, GCobj *o)
467
{
468
  UNUSED(g);
469
  while (o) {
470
    GCtab *t = gco2tab(o);
471
    lj_assertG((t->marked & LJ_GC_WEAK), "clear of non-weak table");
472
    if ((t->marked & LJ_GC_WEAKVAL)) {
473
      MSize i, asize = t->asize;
474
      for (i = 0; i < asize; i++) {
475
        /* Clear array slot when value is about to be collected. */
476
        TValue *tv = arrayslot(t, i);
477
        if (gc_mayclear(tv, 1))
478
          setnilV(tv);
479
      }
480
    }
481
    if (t->hmask > 0) {
482
      Node *node = noderef(t->node);
483
      MSize i, hmask = t->hmask;
484
      for (i = 0; i <= hmask; i++) {
485
        Node *n = &node[i];
486
        /* Clear hash slot when key or value is about to be collected. */
487
        if (!tvisnil(&n->val) && (gc_mayclear(&n->key, 0) ||
488
                                  gc_mayclear(&n->val, 1)))
489
          setnilV(&n->val);
490
      }
491
    }
492
    o = gcref(t->gclist);
493
  }
494
}
495

496
/* Call a userdata or cdata finalizer. */
497
static void gc_call_finalizer(global_State *g, lua_State *L,
498
                              cTValue *mo, GCobj *o)
499
{
500
  /* Save and restore lots of state around the __gc callback. */
501
  uint8_t oldh = hook_save(g);
502
  GCSize oldt = g->gc.threshold;
503
  int errcode;
504
  TValue *top;
505
  lj_trace_abort(g);
506
  hook_entergc(g);  /* Disable hooks and new traces during __gc. */
507
  if (LJ_HASPROFILE && (oldh & HOOK_PROFILE)) lj_dispatch_update(g);
508
  g->gc.threshold = LJ_MAX_MEM;  /* Prevent GC steps. */
509
  top = L->top;
510
  copyTV(L, top++, mo);
511
  if (LJ_FR2) setnilV(top++);
512
  setgcV(L, top, o, ~o->gch.gct);
513
  L->top = top+1;
514
  errcode = lj_vm_pcall(L, top, 1+0, -1);  /* Stack: |mo|o| -> | */
515
  hook_restore(g, oldh);
516
  if (LJ_HASPROFILE && (oldh & HOOK_PROFILE)) lj_dispatch_update(g);
517
  g->gc.threshold = oldt;  /* Restore GC threshold. */
518
  if (errcode) {
519
    ptrdiff_t errobj = savestack(L, L->top-1);  /* Stack may be resized. */
520
    lj_vmevent_send(L, ERRFIN,
521
      copyTV(L, L->top++, restorestack(L, errobj));
522
    );
523
    L->top--;
524
  }
525
}
526

527
/* Finalize one userdata or cdata object from the mmudata list. */
528
static void gc_finalize(lua_State *L)
1,044,128✔
529
{
530
  global_State *g = G(L);
1,044,128✔
531
  GCobj *o = gcnext(gcref(g->gc.mmudata));
1,044,128✔
532
  cTValue *mo;
1,044,128✔
533
  lj_assertG(tvref(g->jit_base) == NULL, "finalizer called on trace");
1,044,128✔
534
  /* Unchain from list of userdata to be finalized. */
535
  if (o == gcref(g->gc.mmudata))
1,044,128✔
536
    setgcrefnull(g->gc.mmudata);
6,384✔
537
  else
538
    setgcrefr(gcref(g->gc.mmudata)->gch.nextgc, o->gch.nextgc);
1,037,744✔
539
#if LJ_HASFFI
540
  if (o->gch.gct == ~LJ_TCDATA) {
1,044,128✔
541
    TValue tmp, *tv;
39,667✔
542
    /* Add cdata back to the GC list and make it white. */
543
    setgcrefr(o->gch.nextgc, g->gc.root);
39,667✔
544
    setgcref(g->gc.root, o);
39,667✔
545
    makewhite(g, o);
39,667✔
546
    o->gch.marked &= (uint8_t)~LJ_GC_CDATA_FIN;
39,667✔
547
    /* Resolve finalizer. */
548
    setcdataV(L, &tmp, gco2cd(o));
39,667✔
549
    tv = lj_tab_set(L, tabref(g->gcroot[GCROOT_FFI_FIN]), &tmp);
39,667✔
550
    if (!tvisnil(tv)) {
39,667✔
551
      copyTV(L, &tmp, tv);
39,667✔
552
      setnilV(tv);  /* Clear entry in finalizer table. */
39,667✔
553
      gc_call_finalizer(g, L, &tmp, o);
39,667✔
554
    }
555
    return;
39,667✔
556
  }
557
#endif
558
  /* Add userdata back to the main userdata list and make it white. */
559
  setgcrefr(o->gch.nextgc, mainthread(g)->nextgc);
1,004,461✔
560
  setgcref(mainthread(g)->nextgc, o);
1,004,461✔
561
  makewhite(g, o);
1,004,461✔
562
  /* Resolve the __gc metamethod. */
563
  mo = lj_meta_fastg(g, tabref(gco2ud(o)->metatable), MM_gc);
1,004,461✔
564
  if (mo)
1,004,461✔
565
    gc_call_finalizer(g, L, mo, o);
1,004,461✔
566
}
567

568
/* Finalize all userdata objects from mmudata list. */
569
void lj_gc_finalize_udata(lua_State *L)
356✔
570
{
571
  while (gcref(G(L)->gc.mmudata) != NULL)
1,971✔
572
    gc_finalize(L);
1,615✔
573
}
356✔
574

575
#if LJ_HASFFI
576
/* Finalize all cdata objects from finalizer table. */
577
void lj_gc_finalize_cdata(lua_State *L)
356✔
578
{
579
  global_State *g = G(L);
356✔
580
  GCtab *t = tabref(g->gcroot[GCROOT_FFI_FIN]);
356✔
581
  Node *node = noderef(t->node);
356✔
582
  ptrdiff_t i;
356✔
583
  setgcrefnull(t->metatable);  /* Mark finalizer table as disabled. */
356✔
584
  for (i = (ptrdiff_t)t->hmask; i >= 0; i--)
67,143,594✔
585
    if (!tvisnil(&node[i].val) && tviscdata(&node[i].key)) {
67,143,238✔
586
      GCobj *o = gcV(&node[i].key);
67,109,425✔
587
      TValue tmp;
67,109,425✔
588
      makewhite(g, o);
67,109,425✔
589
      o->gch.marked &= (uint8_t)~LJ_GC_CDATA_FIN;
67,109,425✔
590
      copyTV(L, &tmp, &node[i].val);
67,109,425✔
591
      setnilV(&node[i].val);
67,109,425✔
592
      gc_call_finalizer(g, L, &tmp, o);
67,109,425✔
593
    }
594
}
356✔
595
#endif
596

597
/* Free all remaining GC objects. */
598
void lj_gc_freeall(global_State *g)
347✔
599
{
600
  MSize i, strmask;
347✔
601
  /* Free everything, except super-fixed objects (the main thread). */
602
  g->gc.currentwhite = LJ_GC_WHITES | LJ_GC_SFIXED;
347✔
603
  gc_fullsweep(g, &g->gc.root);
347✔
604
  strmask = g->strmask;
347✔
605
  for (i = 0; i <= strmask; i++)  /* Free all string hash chains. */
325,467✔
606
    gc_fullsweep(g, &g->strhash[i]);
325,120✔
607
}
347✔
608

609
/* -- Collector ----------------------------------------------------------- */
610

611
/* Atomic part of the GC cycle, transitioning from mark to sweep phase. */
612
static void atomic(global_State *g, lua_State *L)
6,725✔
613
{
614
  size_t udsize;
6,725✔
615

616
  gc_mark_uv(g);  /* Need to remark open upvalues (the thread may be dead). */
6,725✔
617
  gc_propagate_gray(g);  /* Propagate any left-overs. */
6,725✔
618

619
  setgcrefr(g->gc.gray, g->gc.weak);  /* Empty the list of weak tables. */
6,725✔
620
  setgcrefnull(g->gc.weak);
6,725✔
621
  lj_assertG(!iswhite(obj2gco(mainthread(g))), "main thread turned white");
6,725✔
622
  gc_markobj(g, L);  /* Mark running thread. */
6,725✔
623
  gc_traverse_curtrace(g);  /* Traverse current trace. */
6,725✔
624
  gc_mark_gcroot(g);  /* Mark GC roots (again). */
6,725✔
625
  gc_propagate_gray(g);  /* Propagate all of the above. */
6,725✔
626

627
  setgcrefr(g->gc.gray, g->gc.grayagain);  /* Empty the 2nd chance list. */
6,725✔
628
  setgcrefnull(g->gc.grayagain);
6,725✔
629
  gc_propagate_gray(g);  /* Propagate it. */
6,725✔
630

631
  udsize = lj_gc_separateudata(g, 0);  /* Separate userdata to be finalized. */
6,725✔
632
  gc_mark_mmudata(g);  /* Mark them. */
6,725✔
633
  udsize += gc_propagate_gray(g);  /* And propagate the marks. */
6,725✔
634

635
  /* All marking done, clear weak tables. */
636
  gc_clearweak(g, gcref(g->gc.weak));
6,725✔
637

638
  lj_buf_shrink(L, &g->tmpbuf);  /* Shrink temp buffer. */
6,725✔
639

640
  /* Prepare for sweep phase. */
641
  g->gc.currentwhite = (uint8_t)otherwhite(g);  /* Flip current white. */
6,725✔
642
  g->strempty.marked = g->gc.currentwhite;
6,725✔
643
  setmref(g->gc.sweep, &g->gc.root);
6,725✔
644
  g->gc.estimate = g->gc.total - (GCSize)udsize;  /* Initial estimate. */
6,725✔
645
}
6,725✔
646

647
/* GC state machine. Returns a cost estimate for each step performed. */
648
static size_t gc_onestep(lua_State *L)
27,917,217✔
649
{
650
  global_State *g = G(L);
27,917,217✔
651
  g->gc.state_count[g->gc.state]++;
27,917,217✔
652
  switch (g->gc.state) {
27,917,217✔
653
  case GCSpause:
6,764✔
654
    gc_mark_start(g);  /* Start a new GC cycle by marking all GC roots. */
6,764✔
655
    return 0;
6,764✔
656
  case GCSpropagate:
6,698,559✔
657
    if (gcref(g->gc.gray) != NULL)
6,698,559✔
658
      return propagatemark(g);  /* Propagate one gray object. */
6,691,834✔
659
    g->gc.state = GCSatomic;  /* End of mark phase. */
6,725✔
660
    return 0;
6,725✔
661
  case GCSatomic:
6,891✔
662
    if (tvref(g->jit_base))  /* Don't run atomic phase on trace. */
6,891✔
663
      return LJ_MAX_MEM;
664
    atomic(g, L);
6,725✔
665
    g->gc.state = GCSsweepstring;  /* Start of sweep phase. */
6,725✔
666
    g->gc.sweepstr = 0;
6,725✔
667
#if LUAJIT_SMART_STRINGS
668
    g->strbloom.next[0] = 0;
6,725✔
669
    g->strbloom.next[1] = 0;
6,725✔
670
#endif
671
    return 0;
6,725✔
672
  case GCSsweepstring: {
13,585,610✔
673
    GCSize old = g->gc.total;
13,585,610✔
674
    gc_sweep_str_chain(g, &g->strhash[g->gc.sweepstr++]);  /* Sweep one chain. */
13,585,610✔
675
    if (g->gc.sweepstr > g->strmask) {
13,585,610✔
676
      g->gc.state = GCSsweep;  /* All string hash chains sweeped. */
7,688✔
677
#if LUAJIT_SMART_STRINGS
678
      g->strbloom.cur[0] = g->strbloom.next[0];
7,688✔
679
      g->strbloom.cur[1] = g->strbloom.next[1];
7,688✔
680
#endif
681
    }
682
    lj_assertG(old >= g->gc.total, "sweep increased memory");
13,585,610✔
683
    g->gc.estimate -= old - g->gc.total;
13,585,610✔
684
    return GCSWEEPCOST;
13,585,610✔
685
    }
686
  case GCSsweep: {
6,570,571✔
687
    GCSize old = g->gc.total;
6,570,571✔
688
    setmref(g->gc.sweep, gc_sweep(g, mref(g->gc.sweep, GCRef), GCSWEEPMAX));
6,570,571✔
689
    lj_assertG(old >= g->gc.total, "sweep increased memory");
6,570,571✔
690
    g->gc.estimate -= old - g->gc.total;
6,570,571✔
691
    if (gcref(*mref(g->gc.sweep, GCRef)) == NULL) {
6,570,571✔
692
      if (g->strnum <= (g->strmask >> 2) && g->strmask > LJ_MIN_STRTAB*2-1)
7,686✔
693
        lj_str_resize(L, g->strmask >> 1);  /* Shrink string table. */
94✔
694
      if (gcref(g->gc.mmudata)) {  /* Need any finalizations? */
7,686✔
695
        g->gc.state = GCSfinalize;
6,039✔
696
      } else {  /* Otherwise skip this phase to help the JIT. */
697
        g->gc.state = GCSpause;  /* End of GC cycle. */
1,647✔
698
        g->gc.debt = 0;
1,647✔
699
      }
700
    }
701
    return GCSWEEPMAX*GCSWEEPCOST;
702
    }
703
  case GCSfinalize:
1,048,822✔
704
    if (gcref(g->gc.mmudata) != NULL) {
1,048,822✔
705
      if (tvref(g->jit_base))  /* Don't call finalizers on trace. */
1,042,787✔
706
        return LJ_MAX_MEM;
707
      gc_finalize(L);  /* Finalize one userdata object. */
1,042,513✔
708
      if (g->gc.estimate > GCFINALIZECOST)
1,042,513✔
709
        g->gc.estimate -= GCFINALIZECOST;
1,042,513✔
710
      return GCFINALIZECOST;
1,042,513✔
711
    }
712
    g->gc.state = GCSpause;  /* End of GC cycle. */
6,035✔
713
    g->gc.debt = 0;
6,035✔
714
    return 0;
6,035✔
715
  default:
716
    lj_assertG(0, "bad GC state");
717
    return 0;
718
  }
719
}
720

721
/* Perform a limited amount of incremental GC steps. */
722
int LJ_FASTCALL lj_gc_step(lua_State *L)
891,096✔
723
{
724
  global_State *g = G(L);
891,096✔
725
  GCSize lim;
891,096✔
726
  int32_t ostate = g->vmstate;
891,096✔
727
  setvmstate(g, GC);
891,096✔
728
  lim = (GCSTEPSIZE/100) * g->gc.stepmul;
891,096✔
729
  if (lim == 0)
891,096✔
730
    lim = LJ_MAX_MEM;
×
731
  if (g->gc.total > g->gc.threshold)
891,096✔
732
    g->gc.debt += g->gc.total - g->gc.threshold;
865,235✔
733
  do {
11,446,762✔
734
    lim -= (GCSize)gc_onestep(L);
11,446,762✔
735
    if (g->gc.state == GCSpause) {
11,446,762✔
736
      g->gc.threshold = (g->gc.estimate/100) * g->gc.pause;
5,727✔
737
      g->vmstate = ostate;
5,727✔
738
      return 1;  /* Finished a GC cycle. */
5,727✔
739
    }
740
  } while (sizeof(lim) == 8 ? ((int64_t)lim > 0) : ((int32_t)lim > 0));
11,441,035✔
741
  if (g->gc.debt < GCSTEPSIZE) {
885,369✔
742
    g->gc.threshold = g->gc.total + GCSTEPSIZE;
299,682✔
743
    g->vmstate = ostate;
299,682✔
744
    return -1;
299,682✔
745
  } else {
746
    g->gc.debt -= GCSTEPSIZE;
585,687✔
747
    g->gc.threshold = g->gc.total;
585,687✔
748
    g->vmstate = ostate;
585,687✔
749
    return 0;
585,687✔
750
  }
751
}
752

753
/* Ditto, but fix the stack top first. */
754
void LJ_FASTCALL lj_gc_step_fixtop(lua_State *L)
72,291✔
755
{
756
  if (curr_funcisL(L)) L->top = curr_topL(L);
72,291✔
757
  lj_gc_step(L);
72,291✔
758
}
72,291✔
759

760
#if LJ_HASJIT
761
/* Perform multiple GC steps. Called from JIT-compiled code. */
762
int LJ_FASTCALL lj_gc_step_jit(global_State *g, MSize steps)
615,133✔
763
{
764
  lua_State *L = gco2th(gcref(g->cur_L));
615,133✔
765
  L->base = tvref(G(L)->jit_base);
615,133✔
766
  L->top = curr_topL(L);
615,133✔
767
  while (steps-- > 0 && lj_gc_step(L) == 0)
1,231,877✔
768
    ;
1,081,271✔
769
  /* Return 1 to force a trace exit. */
770
  return (G(L)->gc.state == GCSatomic || G(L)->gc.state == GCSfinalize);
615,133✔
771
}
772
#endif
773

774
/* Perform a full GC cycle. */
775
void lj_gc_fullgc(lua_State *L)
979✔
776
{
777
  global_State *g = G(L);
979✔
778
  int32_t ostate = g->vmstate;
979✔
779
  setvmstate(g, GC);
979✔
780
  if (g->gc.state <= GCSatomic) {  /* Caught somewhere in the middle. */
979✔
781
    setmref(g->gc.sweep, &g->gc.root);  /* Sweep everything (preserving it). */
971✔
782
    setgcrefnull(g->gc.gray);  /* Reset lists from partial propagation. */
971✔
783
    setgcrefnull(g->gc.grayagain);
971✔
784
    setgcrefnull(g->gc.weak);
971✔
785
    g->gc.state = GCSsweepstring;  /* Fast forward to the sweep phase. */
971✔
786
    g->gc.sweepstr = 0;
971✔
787
  }
788
  while (g->gc.state == GCSsweepstring || g->gc.state == GCSsweep)
6,159,022✔
789
    gc_onestep(L);  /* Finish sweep. */
6,158,043✔
790
  lj_assertG(g->gc.state == GCSfinalize || g->gc.state == GCSpause,
979✔
791
             "bad GC state");
792
  /* Now perform a full GC. */
793
  g->gc.state = GCSpause;
979✔
794
  do { gc_onestep(L); } while (g->gc.state != GCSpause);
10,312,412✔
795
  g->gc.threshold = (g->gc.estimate/100) * g->gc.pause;
979✔
796
  g->vmstate = ostate;
979✔
797
}
979✔
798

799
/* -- Write barriers ------------------------------------------------------ */
800

801
/* Move the GC propagation frontier forward. */
802
void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v)
5,011✔
803
{
804
  lj_assertG(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o),
5,011✔
805
             "bad object states for forward barrier");
806
  lj_assertG(g->gc.state != GCSfinalize && g->gc.state != GCSpause,
5,011✔
807
             "bad GC state");
808
  lj_assertG(o->gch.gct != ~LJ_TTAB, "barrier object is not a table");
5,011✔
809
  /* Preserve invariant during propagation. Otherwise it doesn't matter. */
810
  if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic)
5,011✔
811
    gc_mark(g, v);  /* Move frontier forward. */
2,477✔
812
  else
813
    makewhite(g, o);  /* Make it white to avoid the following barrier. */
2,534✔
814
}
×
815

816
/* Specialized barrier for closed upvalue. Pass &uv->tv. */
817
void LJ_FASTCALL lj_gc_barrieruv(global_State *g, TValue *tv)
21,017✔
818
{
819
#define TV2MARKED(x) \
820
  (*((uint8_t *)(x) - offsetof(GCupval, tv) + offsetof(GCupval, marked)))
821
  if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic)
21,017✔
822
    gc_mark(g, gcV(tv));
21,012✔
823
  else
824
    TV2MARKED(tv) = (TV2MARKED(tv) & (uint8_t)~LJ_GC_COLORS) | curwhite(g);
5✔
825
#undef TV2MARKED
826
}
21,017✔
827

828
/* Close upvalue. Also needs a write barrier. */
829
void lj_gc_closeuv(global_State *g, GCupval *uv)
15,246✔
830
{
831
  GCobj *o = obj2gco(uv);
15,246✔
832
  /* Copy stack slot to upvalue itself and point to the copy. */
833
  copyTV(mainthread(g), &uv->tv, uvval(uv));
15,246✔
834
  setmref(uv->v, &uv->tv);
15,246✔
835
  uv->closed = 1;
15,246✔
836
  setgcrefr(o->gch.nextgc, g->gc.root);
15,246✔
837
  setgcref(g->gc.root, o);
15,246✔
838
  if (isgray(o)) {  /* A closed upvalue is never gray, so fix this. */
15,246✔
839
    if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) {
544✔
840
      gray2black(o);  /* Make it black and preserve invariant. */
225✔
841
      if (tviswhite(&uv->tv))
225✔
842
        lj_gc_barrierf(g, o, gcV(&uv->tv));
×
843
    } else {
844
      makewhite(g, o);  /* Make it white, i.e. sweep the upvalue. */
319✔
845
      lj_assertG(g->gc.state != GCSfinalize && g->gc.state != GCSpause,
15,246✔
846
                 "bad GC state");
847
    }
848
  }
849
}
15,246✔
850

851
#if LJ_HASJIT
852
/* Mark a trace if it's saved during the propagation phase. */
853
void lj_gc_barriertrace(global_State *g, uint32_t traceno)
20,436✔
854
{
855
  if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic)
20,436✔
856
    gc_marktrace(g, traceno);
1,574✔
857
}
20,436✔
858
#endif
859

860
/* -- Allocator ----------------------------------------------------------- */
861

862
/* Call pluggable memory allocator to allocate or resize a fragment. */
863
void *lj_mem_realloc(lua_State *L, void *p, GCSize osz, GCSize nsz)
4,639,762✔
864
{
865
  global_State *g = G(L);
4,639,762✔
866
  lj_assertG((osz == 0) == (p == NULL), "realloc API violation");
4,639,762✔
867

868
  setgcref(g->mem_L, obj2gco(L));
4,639,762✔
869
  p = g->allocf(g->allocd, p, osz, nsz);
4,639,762✔
870
  if (p == NULL && nsz > 0)
4,639,762✔
871
    lj_err_mem(L);
3✔
872
  lj_assertG((nsz == 0) == (p == NULL), "allocf API violation");
4,639,759✔
873
  lj_assertG(checkptrGC(p),
4,639,759✔
874
             "allocated memory address %p outside required range", p);
875
  g->gc.total = (g->gc.total - osz) + nsz;
4,639,759✔
876
  g->gc.allocated += nsz;
4,639,759✔
877
  g->gc.freed += osz;
4,639,759✔
878
  return p;
4,639,759✔
879
}
880

881
/* Allocate new GC object and link it to the root set. */
882
void * LJ_FASTCALL lj_mem_newgco(lua_State *L, GCSize size)
139,884,973✔
883
{
884
  global_State *g = G(L);
139,884,973✔
885
  GCobj *o;
139,884,973✔
886

887
  setgcref(g->mem_L, obj2gco(L));
139,884,973✔
888
  o = (GCobj *)g->allocf(g->allocd, NULL, 0, size);
139,884,973✔
889
  if (o == NULL)
139,884,973✔
890
    lj_err_mem(L);
×
891
  lj_assertG(checkptrGC(o),
139,884,973✔
892
             "allocated memory address %p outside required range", o);
893
  g->gc.total += size;
139,884,973✔
894
  g->gc.allocated += size;
139,884,973✔
895
  setgcrefr(o->gch.nextgc, g->gc.root);
139,884,973✔
896
  setgcref(g->gc.root, o);
139,884,973✔
897
  newwhite(g, o);
139,884,973✔
898
  return o;
139,884,973✔
899
}
900

901
/* Resize growable vector. */
902
void *lj_mem_grow(lua_State *L, void *p, MSize *szp, MSize lim, MSize esz)
181,969✔
903
{
904
  MSize sz = (*szp) << 1;
181,969✔
905
  if (sz < LJ_MIN_VECSZ)
181,969✔
906
    sz = LJ_MIN_VECSZ;
907
  if (sz > lim)
181,969✔
908
    sz = lim;
909
  p = lj_mem_realloc(L, p, (*szp)*esz, sz*esz);
181,969✔
910
  *szp = sz;
181,968✔
911
  return p;
181,968✔
912
}
913

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc