annotate src/lua/lstate.c @ 320:9637a0f52e7b

located item-list related addresses.
author Robert McIntyre <rlm@mit.edu>
date Tue, 03 Apr 2012 23:17:33 -0500
parents 27763b933818
children
rev   line source
rlm@1 1 /*
rlm@1 2 ** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $
rlm@1 3 ** Global State
rlm@1 4 ** See Copyright Notice in lua.h
rlm@1 5 */
rlm@1 6
rlm@1 7
rlm@1 8 #include <stddef.h>
rlm@1 9
rlm@1 10 #define lstate_c
rlm@1 11 #define LUA_CORE
rlm@1 12
rlm@1 13 #include "lua.h"
rlm@1 14
rlm@1 15 #include "ldebug.h"
rlm@1 16 #include "ldo.h"
rlm@1 17 #include "lfunc.h"
rlm@1 18 #include "lgc.h"
rlm@1 19 #include "llex.h"
rlm@1 20 #include "lmem.h"
rlm@1 21 #include "lstate.h"
rlm@1 22 #include "lstring.h"
rlm@1 23 #include "ltable.h"
rlm@1 24 #include "ltm.h"
rlm@1 25
rlm@1 26
rlm@1 27 #define state_size(x) (sizeof(x) + LUAI_EXTRASPACE)
rlm@1 28 #define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE)
rlm@1 29 #define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))
rlm@1 30
rlm@1 31
rlm@1 32 /*
rlm@1 33 ** Main thread combines a thread state and the global state
rlm@1 34 */
rlm@1 35 typedef struct LG {
rlm@1 36 lua_State l;
rlm@1 37 global_State g;
rlm@1 38 } LG;
rlm@1 39
rlm@1 40
rlm@1 41
rlm@1 42 static void stack_init (lua_State *L1, lua_State *L) {
rlm@1 43 /* initialize CallInfo array */
rlm@1 44 L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
rlm@1 45 L1->ci = L1->base_ci;
rlm@1 46 L1->size_ci = BASIC_CI_SIZE;
rlm@1 47 L1->end_ci = L1->base_ci + L1->size_ci - 1;
rlm@1 48 /* initialize stack array */
rlm@1 49 L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);
rlm@1 50 L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
rlm@1 51 L1->top = L1->stack;
rlm@1 52 L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
rlm@1 53 /* initialize first ci */
rlm@1 54 L1->ci->func = L1->top;
rlm@1 55 setnilvalue(L1->top++); /* `function' entry for this `ci' */
rlm@1 56 L1->base = L1->ci->base = L1->top;
rlm@1 57 L1->ci->top = L1->top + LUA_MINSTACK;
rlm@1 58 }
rlm@1 59
rlm@1 60
rlm@1 61 static void freestack (lua_State *L, lua_State *L1) {
rlm@1 62 luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);
rlm@1 63 luaM_freearray(L, L1->stack, L1->stacksize, TValue);
rlm@1 64 }
rlm@1 65
rlm@1 66
rlm@1 67 /*
rlm@1 68 ** open parts that may cause memory-allocation errors
rlm@1 69 */
rlm@1 70 static void f_luaopen (lua_State *L, void *ud) {
rlm@1 71 global_State *g = G(L);
rlm@1 72 UNUSED(ud);
rlm@1 73 stack_init(L, L); /* init stack */
rlm@1 74 sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */
rlm@1 75 sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */
rlm@1 76 luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
rlm@1 77 luaT_init(L);
rlm@1 78 luaX_init(L);
rlm@1 79 luaS_fix(luaS_newliteral(L, MEMERRMSG));
rlm@1 80 g->GCthreshold = 4*g->totalbytes;
rlm@1 81 }
rlm@1 82
rlm@1 83
rlm@1 84 static void preinit_state (lua_State *L, global_State *g) {
rlm@1 85 G(L) = g;
rlm@1 86 L->stack = NULL;
rlm@1 87 L->stacksize = 0;
rlm@1 88 L->errorJmp = NULL;
rlm@1 89 L->hook = NULL;
rlm@1 90 L->hookmask = 0;
rlm@1 91 L->basehookcount = 0;
rlm@1 92 L->allowhook = 1;
rlm@1 93 resethookcount(L);
rlm@1 94 L->openupval = NULL;
rlm@1 95 L->size_ci = 0;
rlm@1 96 L->nCcalls = L->baseCcalls = 0;
rlm@1 97 L->status = 0;
rlm@1 98 L->base_ci = L->ci = NULL;
rlm@1 99 L->savedpc = NULL;
rlm@1 100 L->errfunc = 0;
rlm@1 101 setnilvalue(gt(L));
rlm@1 102 }
rlm@1 103
rlm@1 104
rlm@1 105 static void close_state (lua_State *L) {
rlm@1 106 global_State *g = G(L);
rlm@1 107 luaF_close(L, L->stack); /* close all upvalues for this thread */
rlm@1 108 luaC_freeall(L); /* collect all objects */
rlm@1 109 lua_assert(g->rootgc == obj2gco(L));
rlm@1 110 lua_assert(g->strt.nuse == 0);
rlm@1 111 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
rlm@1 112 luaZ_freebuffer(L, &g->buff);
rlm@1 113 freestack(L, L);
rlm@1 114 lua_assert(g->totalbytes == sizeof(LG));
rlm@1 115 (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
rlm@1 116 }
rlm@1 117
rlm@1 118
rlm@1 119 lua_State *luaE_newthread (lua_State *L) {
rlm@1 120 lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
rlm@1 121 luaC_link(L, obj2gco(L1), LUA_TTHREAD);
rlm@1 122 preinit_state(L1, G(L));
rlm@1 123 stack_init(L1, L); /* init stack */
rlm@1 124 setobj2n(L, gt(L1), gt(L)); /* share table of globals */
rlm@1 125 L1->hookmask = L->hookmask;
rlm@1 126 L1->basehookcount = L->basehookcount;
rlm@1 127 L1->hook = L->hook;
rlm@1 128 resethookcount(L1);
rlm@1 129 lua_assert(iswhite(obj2gco(L1)));
rlm@1 130 return L1;
rlm@1 131 }
rlm@1 132
rlm@1 133
rlm@1 134 void luaE_freethread (lua_State *L, lua_State *L1) {
rlm@1 135 luaF_close(L1, L1->stack); /* close all upvalues for this thread */
rlm@1 136 lua_assert(L1->openupval == NULL);
rlm@1 137 luai_userstatefree(L1);
rlm@1 138 freestack(L, L1);
rlm@1 139 luaM_freemem(L, fromstate(L1), state_size(lua_State));
rlm@1 140 }
rlm@1 141
rlm@1 142
rlm@1 143 LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
rlm@1 144 int i;
rlm@1 145 lua_State *L;
rlm@1 146 global_State *g;
rlm@1 147 void *l = (*f)(ud, NULL, 0, state_size(LG));
rlm@1 148 if (l == NULL) return NULL;
rlm@1 149 L = tostate(l);
rlm@1 150 g = &((LG *)L)->g;
rlm@1 151 L->next = NULL;
rlm@1 152 L->tt = LUA_TTHREAD;
rlm@1 153 g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
rlm@1 154 L->marked = luaC_white(g);
rlm@1 155 set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
rlm@1 156 preinit_state(L, g);
rlm@1 157 g->frealloc = f;
rlm@1 158 g->ud = ud;
rlm@1 159 g->mainthread = L;
rlm@1 160 g->uvhead.u.l.prev = &g->uvhead;
rlm@1 161 g->uvhead.u.l.next = &g->uvhead;
rlm@1 162 g->GCthreshold = 0; /* mark it as unfinished state */
rlm@1 163 g->strt.size = 0;
rlm@1 164 g->strt.nuse = 0;
rlm@1 165 g->strt.hash = NULL;
rlm@1 166 setnilvalue(registry(L));
rlm@1 167 luaZ_initbuffer(L, &g->buff);
rlm@1 168 g->panic = NULL;
rlm@1 169 g->gcstate = GCSpause;
rlm@1 170 g->rootgc = obj2gco(L);
rlm@1 171 g->sweepstrgc = 0;
rlm@1 172 g->sweepgc = &g->rootgc;
rlm@1 173 g->gray = NULL;
rlm@1 174 g->grayagain = NULL;
rlm@1 175 g->weak = NULL;
rlm@1 176 g->tmudata = NULL;
rlm@1 177 g->totalbytes = sizeof(LG);
rlm@1 178 g->gcpause = LUAI_GCPAUSE;
rlm@1 179 g->gcstepmul = LUAI_GCMUL;
rlm@1 180 g->gcdept = 0;
rlm@1 181 for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL;
rlm@1 182 if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
rlm@1 183 /* memory allocation error: free partial state */
rlm@1 184 close_state(L);
rlm@1 185 L = NULL;
rlm@1 186 }
rlm@1 187 else
rlm@1 188 luai_userstateopen(L);
rlm@1 189 return L;
rlm@1 190 }
rlm@1 191
rlm@1 192
rlm@1 193 static void callallgcTM (lua_State *L, void *ud) {
rlm@1 194 UNUSED(ud);
rlm@1 195 luaC_callGCTM(L); /* call GC metamethods for all udata */
rlm@1 196 }
rlm@1 197
rlm@1 198
rlm@1 199 LUA_API void lua_close (lua_State *L) {
rlm@1 200 L = G(L)->mainthread; /* only the main thread can be closed */
rlm@1 201 lua_lock(L);
rlm@1 202 luaF_close(L, L->stack); /* close all upvalues for this thread */
rlm@1 203 luaC_separateudata(L, 1); /* separate udata that have GC metamethods */
rlm@1 204 L->errfunc = 0; /* no error function during GC metamethods */
rlm@1 205 do { /* repeat until no more errors */
rlm@1 206 L->ci = L->base_ci;
rlm@1 207 L->base = L->top = L->ci->base;
rlm@1 208 L->nCcalls = L->baseCcalls = 0;
rlm@1 209 } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);
rlm@1 210 lua_assert(G(L)->tmudata == NULL);
rlm@1 211 luai_userstateclose(L);
rlm@1 212 close_state(L);
rlm@1 213 }
rlm@1 214