lstate.c 1.5 KB

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