lstate.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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);
  22. static void stack_init (lua_State *L, lua_State *OL, int maxstacksize) {
  23. if (maxstacksize == 0)
  24. maxstacksize = DEFAULT_MAXSTACK;
  25. else
  26. maxstacksize += 2*LUA_MINSTACK;
  27. L->stack = luaM_newvector(OL, BASIC_STACK_SIZE, TObject);
  28. L->maxstacksize = maxstacksize;
  29. L->stacksize = BASIC_STACK_SIZE;
  30. L->top = L->stack + RESERVED_STACK_PREFIX;
  31. L->stack_last = L->stack+(BASIC_STACK_SIZE-EXTRA_STACK)-1;
  32. L->base_ci = luaM_newvector(OL, BASIC_CI_SIZE, CallInfo);
  33. L->ci = L->base_ci;
  34. L->ci->base = L->top;
  35. L->ci->pc = NULL;
  36. L->size_ci = BASIC_CI_SIZE;
  37. L->end_ci = L->base_ci + L->size_ci;
  38. }
  39. /*
  40. ** open parts that may cause memory-allocation errors
  41. */
  42. static void f_luaopen (lua_State *L, void *ud) {
  43. struct Sopen *so = cast(struct Sopen *, ud);
  44. /* create a new global state */
  45. L->_G = luaM_new(L, global_State);
  46. G(L)->strt.size = 0;
  47. G(L)->strt.nuse = 0;
  48. G(L)->strt.hash = NULL;
  49. G(L)->Mbuffer = NULL;
  50. G(L)->Mbuffsize = 0;
  51. G(L)->rootproto = NULL;
  52. G(L)->rootcl = NULL;
  53. G(L)->roottable = NULL;
  54. G(L)->rootupval = NULL;
  55. G(L)->rootudata = NULL;
  56. G(L)->tmudata = NULL;
  57. G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
  58. stack_init(L, L, so->stacksize); /* init stack */
  59. /* create default meta table with a dummy table, and then close the loop */
  60. sethvalue(defaultmeta(L), NULL);
  61. sethvalue(defaultmeta(L), luaH_new(L, 0, 4));
  62. hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L));
  63. sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */
  64. sethvalue(registry(L), luaH_new(L, 0, 0)); /* registry */
  65. luaS_resize(L, 4); /* initial size of string table */
  66. luaT_init(L);
  67. luaX_init(L);
  68. G(L)->GCthreshold = 4*G(L)->nblocks;
  69. }
  70. static void preinit_state (lua_State *L) {
  71. L->stack = NULL;
  72. L->stacksize = 0;
  73. L->maxstacksize = 1;
  74. L->errorJmp = NULL;
  75. L->callhook = NULL;
  76. L->linehook = NULL;
  77. L->openupval = NULL;
  78. L->size_ci = 0;
  79. L->base_ci = NULL;
  80. L->allowhooks = 1;
  81. }
  82. LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
  83. lua_State *L;
  84. lua_lock(OL);
  85. L = luaM_new(OL, lua_State);
  86. preinit_state(L);
  87. L->_G = OL->_G;
  88. OL->next->previous = L; /* insert L into linked list */
  89. L->next = OL->next;
  90. OL->next = L;
  91. L->previous = OL;
  92. stack_init(L, OL, stacksize); /* init stack */
  93. setobj(defaultmeta(L), defaultmeta(OL)); /* share default meta table */
  94. setobj(gt(L), gt(OL)); /* share table of globals */
  95. setobj(registry(L), registry(OL)); /* share registry */
  96. lua_unlock(OL);
  97. lua_userstateopen(L);
  98. return L;
  99. }
  100. LUA_API lua_State *lua_open (int stacksize) {
  101. struct Sopen so;
  102. lua_State *L;
  103. L = luaM_new(NULL, lua_State);
  104. if (L) { /* allocation OK? */
  105. preinit_state(L);
  106. L->_G = NULL;
  107. L->next = L->previous = L;
  108. so.stacksize = stacksize;
  109. so.L = NULL;
  110. if (luaD_runprotected(L, f_luaopen, &so) != 0) {
  111. /* memory allocation error: free partial state */
  112. close_state(L);
  113. L = NULL;
  114. }
  115. }
  116. lua_userstateopen(L);
  117. return L;
  118. }
  119. void luaE_closethread (lua_State *OL, lua_State *L) {
  120. luaF_close(L, L->stack); /* close all upvalues for this thread */
  121. lua_assert(L->openupval == NULL);
  122. L->previous->next = L->next;
  123. L->next->previous = L->previous;
  124. luaM_freearray(OL, L->base_ci, L->size_ci, CallInfo);
  125. luaM_freearray(OL, L->stack, L->stacksize, TObject);
  126. luaM_freelem(OL, L);
  127. }
  128. static void close_state (lua_State *L) {
  129. luaF_close(L, L->stack); /* close all upvalues for this thread */
  130. if (G(L)) { /* close global state */
  131. luaC_collect(L, 1); /* collect all elements */
  132. lua_assert(G(L)->rootproto == NULL);
  133. lua_assert(G(L)->rootudata == NULL);
  134. lua_assert(G(L)->rootcl == NULL);
  135. lua_assert(G(L)->rootupval == NULL);
  136. lua_assert(G(L)->roottable == NULL);
  137. luaS_freeall(L);
  138. luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char);
  139. luaM_freelem(NULL, L->_G);
  140. }
  141. luaE_closethread(NULL, L);
  142. }
  143. LUA_API void lua_closethread (lua_State *L, lua_State *thread) {
  144. if (L == thread) lua_error(L, "cannot close only thread of a state");
  145. lua_lock(L);
  146. luaE_closethread(L, thread);
  147. lua_unlock(L);
  148. }
  149. LUA_API void lua_close (lua_State *L) {
  150. lua_lock(L);
  151. luaC_callallgcTM(L); /* call GC tag methods for all udata */
  152. lua_assert(G(L)->tmudata == NULL);
  153. while (L->next != L) /* then, close all other threads */
  154. luaE_closethread(L, L->next);
  155. close_state(L);
  156. }