lstate.c 3.7 KB

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