lstate.c 4.0 KB

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