lstate.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. ** $Id: lstate.c,v 1.59 2001/03/07 18:09:25 roberto Exp roberto $
  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 "lgc.h"
  10. #include "llex.h"
  11. #include "lmem.h"
  12. #include "lstate.h"
  13. #include "lstring.h"
  14. #include "ltable.h"
  15. #include "ltm.h"
  16. struct Sopen {
  17. int stacksize;
  18. lua_State *L;
  19. };
  20. static void close_state (lua_State *L, lua_State *OL);
  21. /*
  22. ** initialize ref array and registry
  23. */
  24. #define INIT_REFSIZE 4
  25. static void ref_init (lua_State *L) {
  26. G(L)->refArray = luaM_newvector(L, INIT_REFSIZE, struct Ref);
  27. G(L)->sizeref = INIT_REFSIZE;
  28. sethvalue(&G(L)->refArray[0].o, luaH_new(L, 0));
  29. G(L)->refArray[0].st = LOCK;
  30. G(L)->nref = 1;
  31. }
  32. /*
  33. ** open parts that may cause memory-allocation errors
  34. */
  35. static void f_luaopen (lua_State *L, void *ud) {
  36. struct Sopen *so = (struct Sopen *)ud;
  37. if (so->stacksize == 0)
  38. so->stacksize = DEFAULT_STACK_SIZE;
  39. else
  40. so->stacksize += LUA_MINSTACK;
  41. if (so->L != NULL) { /* shared global state? */
  42. L->G = G(so->L);
  43. L->gt = so->L->gt; /* share table of globals */
  44. so->L->next->previous = L; /* insert L into linked list */
  45. L->next = so->L->next;
  46. so->L->next = L;
  47. L->previous = so->L;
  48. luaD_init(L, so->stacksize); /* init stack */
  49. }
  50. else { /* create a new global state */
  51. L->G = luaM_new(L, global_State);
  52. G(L)->strt.size = G(L)->udt.size = 0;
  53. G(L)->strt.nuse = G(L)->udt.nuse = 0;
  54. G(L)->strt.hash = G(L)->udt.hash = NULL;
  55. G(L)->Mbuffer = NULL;
  56. G(L)->Mbuffsize = 0;
  57. G(L)->rootproto = NULL;
  58. G(L)->rootcl = NULL;
  59. G(L)->roottable = NULL;
  60. G(L)->TMtable = NULL;
  61. G(L)->sizeTM = 0;
  62. G(L)->ntag = 0;
  63. G(L)->refArray = NULL;
  64. G(L)->nref = 0;
  65. G(L)->sizeref = 0;
  66. G(L)->refFree = NONEXT;
  67. G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
  68. luaD_init(L, so->stacksize); /* init stack */
  69. L->gt = luaH_new(L, 10); /* table of globals */
  70. G(L)->type2tag = luaH_new(L, 10);
  71. luaS_init(L);
  72. luaX_init(L);
  73. luaT_init(L);
  74. ref_init(L);
  75. G(L)->GCthreshold = 4*G(L)->nblocks;
  76. }
  77. }
  78. LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
  79. struct Sopen so;
  80. lua_State *L;
  81. if (OL) lua_lock(OL);
  82. L = luaM_new(OL, lua_State);
  83. if (L) { /* allocation OK? */
  84. L->G = NULL;
  85. L->stack = NULL;
  86. L->stacksize = 0;
  87. L->ci = &L->basefunc;
  88. L->basefunc.prev = NULL;
  89. L->errorJmp = NULL;
  90. L->callhook = NULL;
  91. L->linehook = NULL;
  92. L->allowhooks = 1;
  93. L->next = L->previous = L;
  94. so.stacksize = stacksize;
  95. so.L = OL;
  96. if (luaD_runprotected(L, f_luaopen, &so) != 0) {
  97. /* memory allocation error: free partial state */
  98. close_state(L, OL);
  99. L = NULL;
  100. }
  101. }
  102. if (OL) lua_unlock(OL);
  103. return L;
  104. }
  105. static void close_state (lua_State *L, lua_State *OL) {
  106. if (OL != NULL) { /* are there other threads? */
  107. lua_assert(L->previous != L);
  108. L->previous->next = L->next;
  109. L->next->previous = L->previous;
  110. }
  111. else if (G(L)) { /* last thread; close global state */
  112. luaC_collect(L, 1); /* collect all elements */
  113. lua_assert(G(L)->rootproto == NULL);
  114. lua_assert(G(L)->rootcl == NULL);
  115. lua_assert(G(L)->roottable == NULL);
  116. luaS_freeall(L);
  117. luaM_freearray(L, G(L)->TMtable, G(L)->sizeTM, struct TM);
  118. luaM_freearray(L, G(L)->refArray, G(L)->sizeref, struct Ref);
  119. luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, l_char);
  120. luaM_freelem(NULL, L->G, global_State);
  121. }
  122. luaM_freearray(OL, L->stack, L->stacksize, TObject);
  123. luaM_freelem(OL, L, lua_State);
  124. }
  125. LUA_API void lua_close (lua_State *L) {
  126. lua_State *OL;
  127. lua_assert(L != lua_state || lua_gettop(L) == 0);
  128. lua_lock(L);
  129. OL = L->next; /* any surviving thread (if there is one) */
  130. if (OL == L) OL = NULL; /* no surviving threads */
  131. close_state(L, OL);
  132. if (OL) lua_unlock(OL); /* cannot unlock over a freed state */
  133. lua_assert(L != lua_state || memdebug_numblocks == 0);
  134. lua_assert(L != lua_state || memdebug_total == 0);
  135. }