lstate.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. ** $Id: lstate.c,v 1.18 1999/11/29 19:12:07 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->errorJmp = NULL;
  20. L->Mbuffer = NULL;
  21. L->Mbuffbase = 0;
  22. L->Mbuffsize = 0;
  23. L->Mbuffnext = 0;
  24. L->Cblocks = NULL;
  25. L->numCblocks = 0;
  26. L->debug = 0;
  27. L->callhook = NULL;
  28. L->linehook = NULL;
  29. L->rootproto = NULL;
  30. L->rootcl = NULL;
  31. L->rootglobal = NULL;
  32. L->roottable = NULL;
  33. L->IMtable = NULL;
  34. L->refArray = NULL;
  35. L->refSize = 0;
  36. L->refFree = NONEXT;
  37. L->nblocks = 0;
  38. L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
  39. luaD_init(L);
  40. luaS_init(L);
  41. luaX_init(L);
  42. luaT_init(L);
  43. luaB_predefine(L);
  44. L->GCthreshold = L->nblocks*4;
  45. return L;
  46. }
  47. void lua_close (lua_State *L) {
  48. luaC_collect(L, 1); /* collect all elements */
  49. LUA_ASSERT(L, L->rootproto == NULL, "list should be empty");
  50. LUA_ASSERT(L, L->rootcl == NULL, "list should be empty");
  51. LUA_ASSERT(L, L->rootglobal == NULL, "list should be empty");
  52. LUA_ASSERT(L, L->roottable == NULL, "list should be empty");
  53. luaS_freeall(L);
  54. luaM_free(L, L->stack);
  55. luaM_free(L, L->IMtable);
  56. luaM_free(L, L->refArray);
  57. luaM_free(L, L->Mbuffer);
  58. luaM_free(L, L->Cblocks);
  59. LUA_ASSERT(L, L->nblocks == 0, "wrong count for nblocks");
  60. luaM_free(L, L);
  61. LUA_ASSERT(L, numblocks == 0, "memory leak!");
  62. LUA_ASSERT(L, totalmem == 0,"memory leak!");
  63. L = NULL;
  64. }