misc/liblua/lgc.c
author smxx
Sat, 27 Mar 2010 21:04:38 +0000
changeset 3127 9ebdc21aba87
parent 2812 0a24853de796
child 3697 d5b30d6373fc
permissions -rw-r--r--
CMake * I must not eat melon bombs * I must not eat melon bombs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     1
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     2
** $Id: lgc.c,v 2.38.1.1 2007/12/27 13:02:25 roberto Exp $
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     3
** Garbage Collector
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     4
** See Copyright Notice in lua.h
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     5
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     6
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     7
#include <string.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     8
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     9
#define lgc_c
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    10
#define LUA_CORE
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    11
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    12
#include "lua.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    13
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    14
#include "ldebug.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    15
#include "ldo.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    16
#include "lfunc.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    17
#include "lgc.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    18
#include "lmem.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    19
#include "lobject.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    20
#include "lstate.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    21
#include "lstring.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    22
#include "ltable.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    23
#include "ltm.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    24
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    25
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    26
#define GCSTEPSIZE	1024u
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    27
#define GCSWEEPMAX	40
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    28
#define GCSWEEPCOST	10
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    29
#define GCFINALIZECOST	100
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    30
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    31
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    32
#define maskmarks	cast_byte(~(bitmask(BLACKBIT)|WHITEBITS))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    33
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    34
#define makewhite(g,x)	\
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    35
   ((x)->gch.marked = cast_byte(((x)->gch.marked & maskmarks) | luaC_white(g)))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    36
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    37
#define white2gray(x)	reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    38
#define black2gray(x)	resetbit((x)->gch.marked, BLACKBIT)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    39
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    40
#define stringmark(s)	reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    41
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    42
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    43
#define isfinalized(u)		testbit((u)->marked, FINALIZEDBIT)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    44
#define markfinalized(u)	l_setbit((u)->marked, FINALIZEDBIT)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    45
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    46
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    47
#define KEYWEAK         bitmask(KEYWEAKBIT)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    48
#define VALUEWEAK       bitmask(VALUEWEAKBIT)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    49
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    50
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    51
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    52
#define markvalue(g,o) { checkconsistency(o); \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    53
  if (iscollectable(o) && iswhite(gcvalue(o))) reallymarkobject(g,gcvalue(o)); }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    54
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    55
#define markobject(g,t) { if (iswhite(obj2gco(t))) \
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    56
		reallymarkobject(g, obj2gco(t)); }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    57
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    58
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    59
#define setthreshold(g)  (g->GCthreshold = (g->estimate/100) * g->gcpause)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    60
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    61
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    62
static void removeentry (Node *n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    63
  lua_assert(ttisnil(gval(n)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    64
  if (iscollectable(gkey(n)))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    65
    setttype(gkey(n), LUA_TDEADKEY);  /* dead key; remove it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    66
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    67
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    68
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    69
static void reallymarkobject (global_State *g, GCObject *o) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    70
  lua_assert(iswhite(o) && !isdead(g, o));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    71
  white2gray(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    72
  switch (o->gch.tt) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    73
    case LUA_TSTRING: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    74
      return;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    75
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    76
    case LUA_TUSERDATA: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    77
      Table *mt = gco2u(o)->metatable;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    78
      gray2black(o);  /* udata are never gray */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    79
      if (mt) markobject(g, mt);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    80
      markobject(g, gco2u(o)->env);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    81
      return;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    82
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    83
    case LUA_TUPVAL: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    84
      UpVal *uv = gco2uv(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    85
      markvalue(g, uv->v);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    86
      if (uv->v == &uv->u.value)  /* closed? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    87
        gray2black(o);  /* open upvalues are never black */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    88
      return;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    89
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    90
    case LUA_TFUNCTION: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    91
      gco2cl(o)->c.gclist = g->gray;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    92
      g->gray = o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    93
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    94
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    95
    case LUA_TTABLE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    96
      gco2h(o)->gclist = g->gray;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    97
      g->gray = o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    98
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    99
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   100
    case LUA_TTHREAD: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   101
      gco2th(o)->gclist = g->gray;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   102
      g->gray = o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   103
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   104
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   105
    case LUA_TPROTO: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   106
      gco2p(o)->gclist = g->gray;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   107
      g->gray = o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   108
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   109
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   110
    default: lua_assert(0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   111
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   112
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   113
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   114
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   115
static void marktmu (global_State *g) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   116
  GCObject *u = g->tmudata;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   117
  if (u) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   118
    do {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   119
      u = u->gch.next;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   120
      makewhite(g, u);  /* may be marked, if left from previous GC */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   121
      reallymarkobject(g, u);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   122
    } while (u != g->tmudata);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   123
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   124
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   125
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   126
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   127
/* move `dead' udata that need finalization to list `tmudata' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   128
size_t luaC_separateudata (lua_State *L, int all) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   129
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   130
  size_t deadmem = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   131
  GCObject **p = &g->mainthread->next;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   132
  GCObject *curr;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   133
  while ((curr = *p) != NULL) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   134
    if (!(iswhite(curr) || all) || isfinalized(gco2u(curr)))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   135
      p = &curr->gch.next;  /* don't bother with them */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   136
    else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   137
      markfinalized(gco2u(curr));  /* don't need finalization */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   138
      p = &curr->gch.next;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   139
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   140
    else {  /* must call its gc method */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   141
      deadmem += sizeudata(gco2u(curr));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   142
      markfinalized(gco2u(curr));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   143
      *p = curr->gch.next;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   144
      /* link `curr' at the end of `tmudata' list */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   145
      if (g->tmudata == NULL)  /* list is empty? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   146
        g->tmudata = curr->gch.next = curr;  /* creates a circular list */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   147
      else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   148
        curr->gch.next = g->tmudata->gch.next;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   149
        g->tmudata->gch.next = curr;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   150
        g->tmudata = curr;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   151
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   152
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   153
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   154
  return deadmem;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   155
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   156
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   157
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   158
static int traversetable (global_State *g, Table *h) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   159
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   160
  int weakkey = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   161
  int weakvalue = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   162
  const TValue *mode;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   163
  if (h->metatable)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   164
    markobject(g, h->metatable);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   165
  mode = gfasttm(g, h->metatable, TM_MODE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   166
  if (mode && ttisstring(mode)) {  /* is there a weak mode? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   167
    weakkey = (strchr(svalue(mode), 'k') != NULL);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   168
    weakvalue = (strchr(svalue(mode), 'v') != NULL);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   169
    if (weakkey || weakvalue) {  /* is really weak? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   170
      h->marked &= ~(KEYWEAK | VALUEWEAK);  /* clear bits */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   171
      h->marked |= cast_byte((weakkey << KEYWEAKBIT) |
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   172
                             (weakvalue << VALUEWEAKBIT));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   173
      h->gclist = g->weak;  /* must be cleared after GC, ... */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   174
      g->weak = obj2gco(h);  /* ... so put in the appropriate list */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   175
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   176
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   177
  if (weakkey && weakvalue) return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   178
  if (!weakvalue) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   179
    i = h->sizearray;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   180
    while (i--)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   181
      markvalue(g, &h->array[i]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   182
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   183
  i = sizenode(h);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   184
  while (i--) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   185
    Node *n = gnode(h, i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   186
    lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   187
    if (ttisnil(gval(n)))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   188
      removeentry(n);  /* remove empty entries */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   189
    else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   190
      lua_assert(!ttisnil(gkey(n)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   191
      if (!weakkey) markvalue(g, gkey(n));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   192
      if (!weakvalue) markvalue(g, gval(n));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   193
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   194
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   195
  return weakkey || weakvalue;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   196
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   197
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   198
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   199
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   200
** All marks are conditional because a GC may happen while the
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   201
** prototype is still being created
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   202
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   203
static void traverseproto (global_State *g, Proto *f) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   204
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   205
  if (f->source) stringmark(f->source);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   206
  for (i=0; i<f->sizek; i++)  /* mark literals */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   207
    markvalue(g, &f->k[i]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   208
  for (i=0; i<f->sizeupvalues; i++) {  /* mark upvalue names */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   209
    if (f->upvalues[i])
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   210
      stringmark(f->upvalues[i]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   211
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   212
  for (i=0; i<f->sizep; i++) {  /* mark nested protos */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   213
    if (f->p[i])
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   214
      markobject(g, f->p[i]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   215
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   216
  for (i=0; i<f->sizelocvars; i++) {  /* mark local-variable names */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   217
    if (f->locvars[i].varname)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   218
      stringmark(f->locvars[i].varname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   219
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   220
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   221
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   222
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   223
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   224
static void traverseclosure (global_State *g, Closure *cl) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   225
  markobject(g, cl->c.env);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   226
  if (cl->c.isC) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   227
    int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   228
    for (i=0; i<cl->c.nupvalues; i++)  /* mark its upvalues */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   229
      markvalue(g, &cl->c.upvalue[i]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   230
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   231
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   232
    int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   233
    lua_assert(cl->l.nupvalues == cl->l.p->nups);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   234
    markobject(g, cl->l.p);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   235
    for (i=0; i<cl->l.nupvalues; i++)  /* mark its upvalues */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   236
      markobject(g, cl->l.upvals[i]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   237
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   238
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   239
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   240
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   241
static void checkstacksizes (lua_State *L, StkId max) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   242
  int ci_used = cast_int(L->ci - L->base_ci);  /* number of `ci' in use */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   243
  int s_used = cast_int(max - L->stack);  /* part of stack in use */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   244
  if (L->size_ci > LUAI_MAXCALLS)  /* handling overflow? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   245
    return;  /* do not touch the stacks */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   246
  if (4*ci_used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   247
    luaD_reallocCI(L, L->size_ci/2);  /* still big enough... */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   248
  condhardstacktests(luaD_reallocCI(L, ci_used + 1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   249
  if (4*s_used < L->stacksize &&
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   250
      2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   251
    luaD_reallocstack(L, L->stacksize/2);  /* still big enough... */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   252
  condhardstacktests(luaD_reallocstack(L, s_used));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   253
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   254
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   255
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   256
static void traversestack (global_State *g, lua_State *l) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   257
  StkId o, lim;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   258
  CallInfo *ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   259
  markvalue(g, gt(l));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   260
  lim = l->top;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   261
  for (ci = l->base_ci; ci <= l->ci; ci++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   262
    lua_assert(ci->top <= l->stack_last);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   263
    if (lim < ci->top) lim = ci->top;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   264
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   265
  for (o = l->stack; o < l->top; o++)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   266
    markvalue(g, o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   267
  for (; o <= lim; o++)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   268
    setnilvalue(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   269
  checkstacksizes(l, lim);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   270
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   271
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   272
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   273
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   274
** traverse one gray object, turning it to black.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   275
** Returns `quantity' traversed.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   276
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   277
static l_mem propagatemark (global_State *g) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   278
  GCObject *o = g->gray;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   279
  lua_assert(isgray(o));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   280
  gray2black(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   281
  switch (o->gch.tt) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   282
    case LUA_TTABLE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   283
      Table *h = gco2h(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   284
      g->gray = h->gclist;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   285
      if (traversetable(g, h))  /* table is weak? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   286
        black2gray(o);  /* keep it gray */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   287
      return sizeof(Table) + sizeof(TValue) * h->sizearray +
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   288
                             sizeof(Node) * sizenode(h);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   289
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   290
    case LUA_TFUNCTION: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   291
      Closure *cl = gco2cl(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   292
      g->gray = cl->c.gclist;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   293
      traverseclosure(g, cl);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   294
      return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) :
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   295
                           sizeLclosure(cl->l.nupvalues);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   296
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   297
    case LUA_TTHREAD: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   298
      lua_State *th = gco2th(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   299
      g->gray = th->gclist;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   300
      th->gclist = g->grayagain;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   301
      g->grayagain = o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   302
      black2gray(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   303
      traversestack(g, th);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   304
      return sizeof(lua_State) + sizeof(TValue) * th->stacksize +
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   305
                                 sizeof(CallInfo) * th->size_ci;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   306
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   307
    case LUA_TPROTO: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   308
      Proto *p = gco2p(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   309
      g->gray = p->gclist;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   310
      traverseproto(g, p);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   311
      return sizeof(Proto) + sizeof(Instruction) * p->sizecode +
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   312
                             sizeof(Proto *) * p->sizep +
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   313
                             sizeof(TValue) * p->sizek + 
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   314
                             sizeof(int) * p->sizelineinfo +
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   315
                             sizeof(LocVar) * p->sizelocvars +
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   316
                             sizeof(TString *) * p->sizeupvalues;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   317
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   318
    default: lua_assert(0); return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   319
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   320
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   321
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   322
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   323
static size_t propagateall (global_State *g) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   324
  size_t m = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   325
  while (g->gray) m += propagatemark(g);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   326
  return m;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   327
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   328
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   329
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   330
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   331
** The next function tells whether a key or value can be cleared from
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   332
** a weak table. Non-collectable objects are never removed from weak
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   333
** tables. Strings behave as `values', so are never removed too. for
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   334
** other objects: if really collected, cannot keep them; for userdata
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   335
** being finalized, keep them in keys, but not in values
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   336
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   337
static int iscleared (const TValue *o, int iskey) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   338
  if (!iscollectable(o)) return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   339
  if (ttisstring(o)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   340
    stringmark(rawtsvalue(o));  /* strings are `values', so are never weak */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   341
    return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   342
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   343
  return iswhite(gcvalue(o)) ||
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   344
    (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o))));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   345
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   346
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   347
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   348
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   349
** clear collected entries from weaktables
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   350
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   351
static void cleartable (GCObject *l) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   352
  while (l) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   353
    Table *h = gco2h(l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   354
    int i = h->sizearray;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   355
    lua_assert(testbit(h->marked, VALUEWEAKBIT) ||
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   356
               testbit(h->marked, KEYWEAKBIT));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   357
    if (testbit(h->marked, VALUEWEAKBIT)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   358
      while (i--) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   359
        TValue *o = &h->array[i];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   360
        if (iscleared(o, 0))  /* value was collected? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   361
          setnilvalue(o);  /* remove value */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   362
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   363
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   364
    i = sizenode(h);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   365
    while (i--) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   366
      Node *n = gnode(h, i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   367
      if (!ttisnil(gval(n)) &&  /* non-empty entry? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   368
          (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   369
        setnilvalue(gval(n));  /* remove value ... */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   370
        removeentry(n);  /* remove entry from table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   371
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   372
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   373
    l = h->gclist;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   374
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   375
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   376
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   377
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   378
static void freeobj (lua_State *L, GCObject *o) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   379
  switch (o->gch.tt) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   380
    case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   381
    case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   382
    case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   383
    case LUA_TTABLE: luaH_free(L, gco2h(o)); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   384
    case LUA_TTHREAD: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   385
      lua_assert(gco2th(o) != L && gco2th(o) != G(L)->mainthread);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   386
      luaE_freethread(L, gco2th(o));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   387
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   388
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   389
    case LUA_TSTRING: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   390
      G(L)->strt.nuse--;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   391
      luaM_freemem(L, o, sizestring(gco2ts(o)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   392
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   393
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   394
    case LUA_TUSERDATA: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   395
      luaM_freemem(L, o, sizeudata(gco2u(o)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   396
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   397
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   398
    default: lua_assert(0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   399
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   400
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   401
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   402
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   403
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   404
#define sweepwholelist(L,p)	sweeplist(L,p,MAX_LUMEM)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   405
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   406
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   407
static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   408
  GCObject *curr;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   409
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   410
  int deadmask = otherwhite(g);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   411
  while ((curr = *p) != NULL && count-- > 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   412
    if (curr->gch.tt == LUA_TTHREAD)  /* sweep open upvalues of each thread */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   413
      sweepwholelist(L, &gco2th(curr)->openupval);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   414
    if ((curr->gch.marked ^ WHITEBITS) & deadmask) {  /* not dead? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   415
      lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   416
      makewhite(g, curr);  /* make it white (for next cycle) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   417
      p = &curr->gch.next;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   418
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   419
    else {  /* must erase `curr' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   420
      lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   421
      *p = curr->gch.next;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   422
      if (curr == g->rootgc)  /* is the first element of the list? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   423
        g->rootgc = curr->gch.next;  /* adjust first */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   424
      freeobj(L, curr);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   425
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   426
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   427
  return p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   428
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   429
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   430
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   431
static void checkSizes (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   432
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   433
  /* check size of string hash */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   434
  if (g->strt.nuse < cast(lu_int32, g->strt.size/4) &&
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   435
      g->strt.size > MINSTRTABSIZE*2)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   436
    luaS_resize(L, g->strt.size/2);  /* table is too big */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   437
  /* check size of buffer */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   438
  if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) {  /* buffer too big? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   439
    size_t newsize = luaZ_sizebuffer(&g->buff) / 2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   440
    luaZ_resizebuffer(L, &g->buff, newsize);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   441
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   442
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   443
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   444
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   445
static void GCTM (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   446
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   447
  GCObject *o = g->tmudata->gch.next;  /* get first element */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   448
  Udata *udata = rawgco2u(o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   449
  const TValue *tm;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   450
  /* remove udata from `tmudata' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   451
  if (o == g->tmudata)  /* last element? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   452
    g->tmudata = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   453
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   454
    g->tmudata->gch.next = udata->uv.next;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   455
  udata->uv.next = g->mainthread->next;  /* return it to `root' list */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   456
  g->mainthread->next = o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   457
  makewhite(g, o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   458
  tm = fasttm(L, udata->uv.metatable, TM_GC);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   459
  if (tm != NULL) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   460
    lu_byte oldah = L->allowhook;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   461
    lu_mem oldt = g->GCthreshold;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   462
    L->allowhook = 0;  /* stop debug hooks during GC tag method */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   463
    g->GCthreshold = 2*g->totalbytes;  /* avoid GC steps */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   464
    setobj2s(L, L->top, tm);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   465
    setuvalue(L, L->top+1, udata);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   466
    L->top += 2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   467
    luaD_call(L, L->top - 2, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   468
    L->allowhook = oldah;  /* restore hooks */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   469
    g->GCthreshold = oldt;  /* restore threshold */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   470
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   471
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   472
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   473
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   474
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   475
** Call all GC tag methods
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   476
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   477
void luaC_callGCTM (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   478
  while (G(L)->tmudata)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   479
    GCTM(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   480
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   481
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   482
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   483
void luaC_freeall (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   484
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   485
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   486
  g->currentwhite = WHITEBITS | bitmask(SFIXEDBIT);  /* mask to collect all elements */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   487
  sweepwholelist(L, &g->rootgc);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   488
  for (i = 0; i < g->strt.size; i++)  /* free all string lists */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   489
    sweepwholelist(L, &g->strt.hash[i]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   490
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   491
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   492
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   493
static void markmt (global_State *g) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   494
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   495
  for (i=0; i<NUM_TAGS; i++)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   496
    if (g->mt[i]) markobject(g, g->mt[i]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   497
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   498
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   499
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   500
/* mark root set */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   501
static void markroot (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   502
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   503
  g->gray = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   504
  g->grayagain = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   505
  g->weak = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   506
  markobject(g, g->mainthread);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   507
  /* make global table be traversed before main stack */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   508
  markvalue(g, gt(g->mainthread));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   509
  markvalue(g, registry(L));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   510
  markmt(g);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   511
  g->gcstate = GCSpropagate;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   512
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   513
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   514
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   515
static void remarkupvals (global_State *g) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   516
  UpVal *uv;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   517
  for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   518
    lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   519
    if (isgray(obj2gco(uv)))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   520
      markvalue(g, uv->v);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   521
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   522
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   523
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   524
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   525
static void atomic (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   526
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   527
  size_t udsize;  /* total size of userdata to be finalized */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   528
  /* remark occasional upvalues of (maybe) dead threads */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   529
  remarkupvals(g);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   530
  /* traverse objects cautch by write barrier and by 'remarkupvals' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   531
  propagateall(g);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   532
  /* remark weak tables */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   533
  g->gray = g->weak;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   534
  g->weak = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   535
  lua_assert(!iswhite(obj2gco(g->mainthread)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   536
  markobject(g, L);  /* mark running thread */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   537
  markmt(g);  /* mark basic metatables (again) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   538
  propagateall(g);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   539
  /* remark gray again */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   540
  g->gray = g->grayagain;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   541
  g->grayagain = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   542
  propagateall(g);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   543
  udsize = luaC_separateudata(L, 0);  /* separate userdata to be finalized */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   544
  marktmu(g);  /* mark `preserved' userdata */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   545
  udsize += propagateall(g);  /* remark, to propagate `preserveness' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   546
  cleartable(g->weak);  /* remove collected objects from weak tables */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   547
  /* flip current white */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   548
  g->currentwhite = cast_byte(otherwhite(g));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   549
  g->sweepstrgc = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   550
  g->sweepgc = &g->rootgc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   551
  g->gcstate = GCSsweepstring;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   552
  g->estimate = g->totalbytes - udsize;  /* first estimate */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   553
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   554
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   555
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   556
static l_mem singlestep (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   557
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   558
  /*lua_checkmemory(L);*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   559
  switch (g->gcstate) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   560
    case GCSpause: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   561
      markroot(L);  /* start a new collection */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   562
      return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   563
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   564
    case GCSpropagate: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   565
      if (g->gray)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   566
        return propagatemark(g);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   567
      else {  /* no more `gray' objects */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   568
        atomic(L);  /* finish mark phase */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   569
        return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   570
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   571
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   572
    case GCSsweepstring: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   573
      lu_mem old = g->totalbytes;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   574
      sweepwholelist(L, &g->strt.hash[g->sweepstrgc++]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   575
      if (g->sweepstrgc >= g->strt.size)  /* nothing more to sweep? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   576
        g->gcstate = GCSsweep;  /* end sweep-string phase */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   577
      lua_assert(old >= g->totalbytes);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   578
      g->estimate -= old - g->totalbytes;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   579
      return GCSWEEPCOST;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   580
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   581
    case GCSsweep: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   582
      lu_mem old = g->totalbytes;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   583
      g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   584
      if (*g->sweepgc == NULL) {  /* nothing more to sweep? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   585
        checkSizes(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   586
        g->gcstate = GCSfinalize;  /* end sweep phase */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   587
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   588
      lua_assert(old >= g->totalbytes);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   589
      g->estimate -= old - g->totalbytes;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   590
      return GCSWEEPMAX*GCSWEEPCOST;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   591
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   592
    case GCSfinalize: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   593
      if (g->tmudata) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   594
        GCTM(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   595
        if (g->estimate > GCFINALIZECOST)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   596
          g->estimate -= GCFINALIZECOST;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   597
        return GCFINALIZECOST;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   598
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   599
      else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   600
        g->gcstate = GCSpause;  /* end collection */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   601
        g->gcdept = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   602
        return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   603
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   604
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   605
    default: lua_assert(0); return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   606
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   607
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   608
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   609
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   610
void luaC_step (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   611
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   612
  l_mem lim = (GCSTEPSIZE/100) * g->gcstepmul;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   613
  if (lim == 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   614
    lim = (MAX_LUMEM-1)/2;  /* no limit */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   615
  g->gcdept += g->totalbytes - g->GCthreshold;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   616
  do {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   617
    lim -= singlestep(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   618
    if (g->gcstate == GCSpause)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   619
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   620
  } while (lim > 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   621
  if (g->gcstate != GCSpause) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   622
    if (g->gcdept < GCSTEPSIZE)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   623
      g->GCthreshold = g->totalbytes + GCSTEPSIZE;  /* - lim/g->gcstepmul;*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   624
    else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   625
      g->gcdept -= GCSTEPSIZE;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   626
      g->GCthreshold = g->totalbytes;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   627
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   628
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   629
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   630
    lua_assert(g->totalbytes >= g->estimate);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   631
    setthreshold(g);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   632
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   633
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   634
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   635
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   636
void luaC_fullgc (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   637
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   638
  if (g->gcstate <= GCSpropagate) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   639
    /* reset sweep marks to sweep all elements (returning them to white) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   640
    g->sweepstrgc = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   641
    g->sweepgc = &g->rootgc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   642
    /* reset other collector lists */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   643
    g->gray = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   644
    g->grayagain = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   645
    g->weak = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   646
    g->gcstate = GCSsweepstring;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   647
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   648
  lua_assert(g->gcstate != GCSpause && g->gcstate != GCSpropagate);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   649
  /* finish any pending sweep phase */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   650
  while (g->gcstate != GCSfinalize) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   651
    lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   652
    singlestep(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   653
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   654
  markroot(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   655
  while (g->gcstate != GCSpause) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   656
    singlestep(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   657
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   658
  setthreshold(g);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   659
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   660
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   661
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   662
void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   663
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   664
  lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   665
  lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   666
  lua_assert(ttype(&o->gch) != LUA_TTABLE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   667
  /* must keep invariant? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   668
  if (g->gcstate == GCSpropagate)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   669
    reallymarkobject(g, v);  /* restore invariant */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   670
  else  /* don't mind */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   671
    makewhite(g, o);  /* mark as white just to avoid other barriers */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   672
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   673
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   674
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   675
void luaC_barrierback (lua_State *L, Table *t) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   676
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   677
  GCObject *o = obj2gco(t);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   678
  lua_assert(isblack(o) && !isdead(g, o));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   679
  lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   680
  black2gray(o);  /* make table gray (again) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   681
  t->gclist = g->grayagain;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   682
  g->grayagain = o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   683
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   684
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   685
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   686
void luaC_link (lua_State *L, GCObject *o, lu_byte tt) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   687
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   688
  o->gch.next = g->rootgc;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   689
  g->rootgc = o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   690
  o->gch.marked = luaC_white(g);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   691
  o->gch.tt = tt;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   692
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   693
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   694
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   695
void luaC_linkupval (lua_State *L, UpVal *uv) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   696
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   697
  GCObject *o = obj2gco(uv);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   698
  o->gch.next = g->rootgc;  /* link upvalue into `rootgc' list */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   699
  g->rootgc = o;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   700
  if (isgray(o)) { 
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   701
    if (g->gcstate == GCSpropagate) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   702
      gray2black(o);  /* closed upvalues need barrier */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   703
      luaC_barrier(L, uv, uv->v);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   704
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   705
    else {  /* sweep phase: sweep it (turning it into white) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   706
      makewhite(g, o);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   707
      lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   708
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   709
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   710
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   711