lstate.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. ** $Id: lstate.c,v 1.16 1999/11/10 15:39:35 roberto Exp roberto $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define LUA_REENTRANT
  7. #include "lbuiltin.h"
  8. #include "ldo.h"
  9. #include "lgc.h"
  10. #include "llex.h"
  11. #include "lmem.h"
  12. #include "lref.h"
  13. #include "lstate.h"
  14. #include "lstring.h"
  15. #include "ltm.h"
  16. lua_State *lua_state = NULL;
  17. lua_State *lua_newstate (void) {
  18. lua_State *L = luaM_new(NULL, lua_State);
  19. L->Cstack.base = 0;
  20. L->Cstack.lua2C = 0;
  21. L->Cstack.num = 0;
  22. L->errorJmp = NULL;
  23. L->Mbuffer = NULL;
  24. L->Mbuffbase = 0;
  25. L->Mbuffsize = 0;
  26. L->Mbuffnext = 0;
  27. L->Cblocks = NULL;
  28. L->numCblocks = 0;
  29. L->debug = 0;
  30. L->callhook = NULL;
  31. L->linehook = NULL;
  32. L->rootproto = NULL;
  33. L->rootcl = NULL;
  34. L->rootglobal = NULL;
  35. L->roottable = NULL;
  36. L->IMtable = NULL;
  37. L->refArray = NULL;
  38. L->refSize = 0;
  39. L->refFree = NONEXT;
  40. L->nblocks = 0;
  41. L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
  42. luaD_init(L);
  43. luaS_init(L);
  44. luaX_init(L);
  45. luaT_init(L);
  46. luaB_predefine(L);
  47. return L;
  48. L->GCthreshold = L->nblocks*4;
  49. }
  50. void lua_close (lua_State *L) {
  51. luaC_collect(L, 1); /* collect all elements */
  52. LUA_ASSERT(L, L->rootproto == NULL, "list should be empty");
  53. LUA_ASSERT(L, L->rootcl == NULL, "list should be empty");
  54. LUA_ASSERT(L, L->rootglobal == NULL, "list should be empty");
  55. LUA_ASSERT(L, L->roottable == NULL, "list should be empty");
  56. luaS_freeall(L);
  57. luaM_free(L, L->stack.stack);
  58. luaM_free(L, L->IMtable);
  59. luaM_free(L, L->refArray);
  60. luaM_free(L, L->Mbuffer);
  61. luaM_free(L, L->Cblocks);
  62. LUA_ASSERT(L, L->nblocks == 0, "wrong count for nblocks");
  63. luaM_free(L, L);
  64. LUA_ASSERT(L, numblocks == 0, "memory leak!");
  65. LUA_ASSERT(L, totalmem == 0,"memory leak!");
  66. L = NULL;
  67. }