lstate.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. ** $Id: lstate.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
  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. 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. setobj(defaultet(L), defaultet(so->L)); /* share default event table */
  39. setobj(gt(L), gt(so->L)); /* share table of globals */
  40. setobj(registry(L), registry(so->L)); /* share registry */
  41. }
  42. else { /* create a new global state */
  43. L->_G = luaM_new(L, global_State);
  44. G(L)->strt.size = 0;
  45. G(L)->strt.nuse = 0;
  46. G(L)->strt.hash = NULL;
  47. G(L)->Mbuffer = NULL;
  48. G(L)->Mbuffsize = 0;
  49. G(L)->rootproto = NULL;
  50. G(L)->rootcl = NULL;
  51. G(L)->roottable = NULL;
  52. G(L)->rootupval = NULL;
  53. G(L)->rootudata = NULL;
  54. G(L)->tmudata = NULL;
  55. G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
  56. luaD_init(L, so->stacksize); /* init stack */
  57. /* create default event table with a dummy table, and then close the loop */
  58. sethvalue(defaultet(L), NULL);
  59. sethvalue(defaultet(L), luaH_new(L, 0, 4));
  60. hvalue(defaultet(L))->eventtable = hvalue(defaultet(L));
  61. sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */
  62. sethvalue(registry(L), luaH_new(L, 0, 0)); /* registry */
  63. luaS_resize(L, 4); /* initial size of string table */
  64. luaT_init(L);
  65. luaX_init(L);
  66. G(L)->GCthreshold = 4*G(L)->nblocks;
  67. }
  68. }
  69. LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
  70. struct Sopen so;
  71. lua_State *L;
  72. if (OL) lua_lock(OL);
  73. L = luaM_new(OL, lua_State);
  74. if (L) { /* allocation OK? */
  75. L->_G = NULL;
  76. L->stack = NULL;
  77. L->stacksize = 0;
  78. L->errorJmp = NULL;
  79. L->callhook = NULL;
  80. L->linehook = NULL;
  81. L->openupval = NULL;
  82. L->size_ci = 0;
  83. L->base_ci = NULL;
  84. L->allowhooks = 1;
  85. L->next = L->previous = L;
  86. so.stacksize = stacksize;
  87. so.L = OL;
  88. if (luaD_runprotected(L, f_luaopen, &so) != 0) {
  89. /* memory allocation error: free partial state */
  90. close_state(L, OL);
  91. L = NULL;
  92. }
  93. }
  94. if (OL) lua_unlock(OL);
  95. return L;
  96. }
  97. static void close_state (lua_State *L, lua_State *OL) {
  98. luaF_close(L, L->stack); /* close all upvalues for this thread */
  99. lua_assert(L->openupval == NULL);
  100. if (OL != NULL) { /* are there other threads? */
  101. lua_assert(L->previous != L);
  102. L->previous->next = L->next;
  103. L->next->previous = L->previous;
  104. }
  105. else if (G(L)) { /* last thread; close global state */
  106. if (G(L)->rootudata) /* (avoid problems with incomplete states) */
  107. luaC_callallgcTM(L); /* call GC tag methods for all udata */
  108. lua_assert(G(L)->tmudata == NULL);
  109. luaC_collect(L, 1); /* collect all elements */
  110. lua_assert(G(L)->rootproto == NULL);
  111. lua_assert(G(L)->rootudata == NULL);
  112. lua_assert(G(L)->rootcl == NULL);
  113. lua_assert(G(L)->rootupval == NULL);
  114. lua_assert(G(L)->roottable == NULL);
  115. luaS_freeall(L);
  116. luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char);
  117. luaM_freelem(NULL, L->_G);
  118. }
  119. luaM_freearray(OL, L->base_ci, L->size_ci, CallInfo);
  120. luaM_freearray(OL, L->stack, L->stacksize, TObject);
  121. luaM_freelem(OL, L);
  122. }
  123. LUA_API void lua_close (lua_State *L) {
  124. lua_State *OL;
  125. lua_assert(L != lua_state || lua_gettop(L) == 0);
  126. lua_lock(L);
  127. OL = L->next; /* any surviving thread (if there is one) */
  128. if (OL == L) OL = NULL; /* no surviving threads */
  129. close_state(L, OL);
  130. if (OL) lua_unlock(OL); /* cannot unlock over a freed state */
  131. lua_assert(L != lua_state || memdebug_numblocks == 0);
  132. lua_assert(L != lua_state || memdebug_total == 0);
  133. }