lstate.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. ** $Id: lstate.c,v 1.72 2001/11/06 21:40:51 roberto Exp $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include "lua.h"
  8. #include "ldo.h"
  9. #include "lfunc.h"
  10. #include "lgc.h"
  11. #include "llex.h"
  12. #include "lmem.h"
  13. #include "lstate.h"
  14. #include "lstring.h"
  15. #include "ltable.h"
  16. #include "ltm.h"
  17. struct Sopen {
  18. int stacksize;
  19. lua_State *L;
  20. };
  21. static void close_state (lua_State *L, lua_State *OL);
  22. /*
  23. ** open parts that may cause memory-allocation errors
  24. */
  25. static void f_luaopen (lua_State *L, void *ud) {
  26. struct Sopen *so = cast(struct Sopen *, ud);
  27. if (so->stacksize == 0)
  28. so->stacksize = DEFAULT_STACK_SIZE;
  29. else
  30. so->stacksize += LUA_MINSTACK;
  31. if (so->L != NULL) { /* shared global state? */
  32. L->_G = G(so->L);
  33. L->gt = so->L->gt; /* share table of globals */
  34. so->L->next->previous = L; /* insert L into linked list */
  35. L->next = so->L->next;
  36. so->L->next = L;
  37. L->previous = so->L;
  38. luaD_init(L, so->stacksize); /* init stack */
  39. }
  40. else { /* create a new global state */
  41. L->_G = luaM_new(L, global_State);
  42. G(L)->strt.size = 0;
  43. G(L)->strt.nuse = 0;
  44. G(L)->strt.hash = NULL;
  45. G(L)->Mbuffer = NULL;
  46. G(L)->Mbuffsize = 0;
  47. G(L)->rootproto = NULL;
  48. G(L)->rootcl = NULL;
  49. G(L)->roottable = NULL;
  50. G(L)->rootudata = NULL;
  51. G(L)->rootupval = NULL;
  52. G(L)->TMtable = NULL;
  53. G(L)->sizeTM = 0;
  54. G(L)->ntag = 0;
  55. G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
  56. luaD_init(L, so->stacksize); /* init stack */
  57. sethvalue(&L->gt, luaH_new(L, 0, 4)); /* table of globals */
  58. G(L)->type2tag = luaH_new(L, 0, 3);
  59. sethvalue(&G(L)->registry, luaH_new(L, 0, 0));
  60. luaS_resize(L, 4); /* initial size of string table */
  61. luaX_init(L);
  62. luaT_init(L);
  63. G(L)->GCthreshold = 4*G(L)->nblocks;
  64. }
  65. }
  66. LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
  67. struct Sopen so;
  68. lua_State *L;
  69. if (OL) lua_lock(OL);
  70. L = luaM_new(OL, lua_State);
  71. if (L) { /* allocation OK? */
  72. L->_G = NULL;
  73. L->stack = NULL;
  74. L->stacksize = 0;
  75. L->ci = &L->basefunc;
  76. L->basefunc.prev = NULL;
  77. L->errorJmp = NULL;
  78. L->callhook = NULL;
  79. L->linehook = NULL;
  80. L->openupval = NULL;
  81. L->allowhooks = 1;
  82. L->next = L->previous = L;
  83. so.stacksize = stacksize;
  84. so.L = OL;
  85. if (luaD_runprotected(L, f_luaopen, &so) != 0) {
  86. /* memory allocation error: free partial state */
  87. close_state(L, OL);
  88. L = NULL;
  89. }
  90. }
  91. if (OL) lua_unlock(OL);
  92. return L;
  93. }
  94. static void close_state (lua_State *L, lua_State *OL) {
  95. luaF_close(L, L->stack); /* close all upvalues for this thread */
  96. lua_assert(L->openupval == NULL);
  97. if (OL != NULL) { /* are there other threads? */
  98. lua_assert(L->previous != L);
  99. L->previous->next = L->next;
  100. L->next->previous = L->previous;
  101. }
  102. else if (G(L)) { /* last thread; close global state */
  103. luaC_callallgcTM(L); /* call GC tag methods for all udata */
  104. luaC_collect(L, 1); /* collect all elements */
  105. lua_assert(G(L)->rootproto == NULL);
  106. lua_assert(G(L)->rootudata == NULL);
  107. lua_assert(G(L)->rootcl == NULL);
  108. lua_assert(G(L)->rootupval == NULL);
  109. lua_assert(G(L)->roottable == NULL);
  110. luaS_freeall(L);
  111. luaM_freearray(L, G(L)->TMtable, G(L)->sizeTM, struct TM);
  112. luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char);
  113. luaM_freelem(NULL, L->_G);
  114. }
  115. luaM_freearray(OL, L->stack, L->stacksize, TObject);
  116. luaM_freelem(OL, L);
  117. }
  118. LUA_API void lua_close (lua_State *L) {
  119. lua_State *OL;
  120. lua_assert(L != lua_state || lua_gettop(L) == 0);
  121. lua_lock(L);
  122. OL = L->next; /* any surviving thread (if there is one) */
  123. if (OL == L) OL = NULL; /* no surviving threads */
  124. close_state(L, OL);
  125. if (OL) lua_unlock(OL); /* cannot unlock over a freed state */
  126. lua_assert(L != lua_state || memdebug_numblocks == 0);
  127. lua_assert(L != lua_state || memdebug_total == 0);
  128. }