table.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. ** table.c
  3. ** Module to control static tables
  4. */
  5. char *rcs_table="$Id: table.c,v 2.73 1997/07/07 16:44:26 roberto Exp roberto $";
  6. #include "luamem.h"
  7. #include "auxlib.h"
  8. #include "func.h"
  9. #include "opcode.h"
  10. #include "tree.h"
  11. #include "hash.h"
  12. #include "table.h"
  13. #include "inout.h"
  14. #include "lua.h"
  15. #include "fallback.h"
  16. #include "luadebug.h"
  17. #define BUFFER_BLOCK 256
  18. Symbol *lua_table = NULL;
  19. Word lua_ntable = 0;
  20. static Long lua_maxsymbol = 0;
  21. #define GARBAGE_BLOCK 100
  22. static TaggedString *luaI_createfixedstring (char *name)
  23. {
  24. TaggedString *ts = luaI_createstring(name);
  25. luaI_fixstring(ts);
  26. return ts;
  27. }
  28. void luaI_initsymbol (void)
  29. {
  30. lua_maxsymbol = BUFFER_BLOCK;
  31. lua_table = newvector(lua_maxsymbol, Symbol);
  32. luaI_predefine();
  33. }
  34. void luaI_initconstant (void)
  35. {
  36. /* pre-register mem error messages, to avoid loop when error arises */
  37. luaI_createfixedstring(tableEM);
  38. luaI_createfixedstring(memEM);
  39. }
  40. /*
  41. ** Given a name, search it at symbol table and return its index. If not
  42. ** found, allocate it.
  43. */
  44. Word luaI_findsymbol (TaggedString *t)
  45. {
  46. if (t->u.s.varindex == NOT_USED)
  47. {
  48. if (lua_ntable == lua_maxsymbol)
  49. lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
  50. symbolEM, MAX_WORD);
  51. t->u.s.varindex = lua_ntable;
  52. lua_table[lua_ntable].varname = t;
  53. s_ttype(lua_ntable) = LUA_T_NIL;
  54. lua_ntable++;
  55. }
  56. return t->u.s.varindex;
  57. }
  58. Word luaI_findsymbolbyname (char *name)
  59. {
  60. return luaI_findsymbol(luaI_createfixedstring(name));
  61. }
  62. void luaI_releasestring (TaggedString *t)
  63. {
  64. if (t->marked == 2) /* string has temporary mark? */
  65. t->marked = 0;
  66. }
  67. void luaI_fixstring (TaggedString *t)
  68. {
  69. if (t->marked < 3)
  70. t->marked = 3; /* avoid GC */
  71. }
  72. TaggedString *luaI_createtempstring (char *name)
  73. {
  74. TaggedString *ts = luaI_createstring(name);
  75. if (!ts->marked)
  76. ts->marked = 2; /* avoid (temporarily) GC */
  77. return ts;
  78. }
  79. int luaI_globaldefined (char *name)
  80. {
  81. return ttype(&lua_table[luaI_findsymbolbyname(name)].object) != LUA_T_NIL;
  82. }
  83. /*
  84. ** Traverse symbol table objects
  85. */
  86. static char *lua_travsymbol (int (*fn)(TObject *))
  87. {
  88. Word i;
  89. for (i=0; i<lua_ntable; i++)
  90. if (fn(&s_object(i)))
  91. return lua_table[i].varname->str;
  92. return NULL;
  93. }
  94. int lua_markobject (TObject *o)
  95. {/* if already marked, does not change mark value */
  96. if (ttype(o) == LUA_T_USERDATA ||
  97. (ttype(o) == LUA_T_STRING && !tsvalue(o)->marked))
  98. tsvalue(o)->marked = 1;
  99. else if (ttype(o) == LUA_T_ARRAY)
  100. lua_hashmark (avalue(o));
  101. else if ((o->ttype == LUA_T_FUNCTION || o->ttype == LUA_T_MARK)
  102. && !o->value.tf->marked)
  103. luaI_funcmark(o->value.tf);
  104. return 0;
  105. }
  106. /*
  107. * returns 0 if the object is going to be (garbage) collected
  108. */
  109. int luaI_ismarked (TObject *o)
  110. {
  111. switch (o->ttype)
  112. {
  113. case LUA_T_STRING: case LUA_T_USERDATA:
  114. return o->value.ts->marked;
  115. case LUA_T_FUNCTION:
  116. return o->value.tf->marked;
  117. case LUA_T_ARRAY:
  118. return o->value.a->mark;
  119. default: /* nil, number, cfunction, or user data */
  120. return 1;
  121. }
  122. }
  123. static void call_nilIM (void)
  124. { /* signals end of garbage collection */
  125. TObject t;
  126. ttype(&t) = LUA_T_NIL;
  127. luaI_gcIM(&t); /* end of list */
  128. }
  129. /*
  130. ** Garbage collection.
  131. ** Delete all unused strings and arrays.
  132. */
  133. static long gc_block = GARBAGE_BLOCK;
  134. static long gc_nentity = 0; /* total of strings, arrays, etc */
  135. static void markall (void)
  136. {
  137. lua_travstack(lua_markobject); /* mark stack objects */
  138. lua_travsymbol(lua_markobject); /* mark symbol table objects */
  139. luaI_travlock(lua_markobject); /* mark locked objects */
  140. luaI_travfallbacks(lua_markobject); /* mark fallbacks */
  141. }
  142. long lua_collectgarbage (long limit)
  143. {
  144. long recovered = 0;
  145. Hash *freetable;
  146. TaggedString *freestr;
  147. TFunc *freefunc;
  148. markall();
  149. luaI_invalidaterefs();
  150. freetable = luaI_hashcollector(&recovered);
  151. freestr = luaI_strcollector(&recovered);
  152. freefunc = luaI_funccollector(&recovered);
  153. gc_nentity -= recovered;
  154. gc_block = (limit == 0) ? 2*(gc_block-recovered) : gc_nentity+limit;
  155. luaI_hashcallIM(freetable);
  156. luaI_strcallIM(freestr);
  157. call_nilIM();
  158. luaI_hashfree(freetable);
  159. luaI_strfree(freestr);
  160. luaI_funcfree(freefunc);
  161. /*printf("total %d coletados %d\n", (int)gc_nentity, (int)recovered);*/
  162. return recovered;
  163. }
  164. void lua_pack (void)
  165. {
  166. if (++gc_nentity >= gc_block)
  167. lua_collectgarbage(0);
  168. }
  169. /*
  170. ** Internal function: return next global variable
  171. */
  172. void luaI_nextvar (void)
  173. {
  174. Word next = lua_isnil(lua_getparam(1)) ? 0 :
  175. luaI_findsymbolbyname(luaL_check_string(1))+1;
  176. if (next != 0)
  177. luaL_arg_check(s_ttype(next-1)!=LUA_T_NIL, 1, "undefined global name");
  178. while (next < lua_ntable && s_ttype(next) == LUA_T_NIL)
  179. next++;
  180. if (next < lua_ntable) {
  181. lua_pushstring(lua_table[next].varname->str);
  182. luaI_pushobject(&s_object(next));
  183. }
  184. }
  185. static TObject *functofind;
  186. static int checkfunc (TObject *o)
  187. {
  188. if (o->ttype == LUA_T_FUNCTION)
  189. return
  190. ((functofind->ttype == LUA_T_FUNCTION || functofind->ttype == LUA_T_MARK)
  191. && (functofind->value.tf == o->value.tf));
  192. if (o->ttype == LUA_T_CFUNCTION)
  193. return
  194. ((functofind->ttype == LUA_T_CFUNCTION ||
  195. functofind->ttype == LUA_T_CMARK) &&
  196. (functofind->value.f == o->value.f));
  197. return 0;
  198. }
  199. char *lua_getobjname (lua_Object o, char **name)
  200. { /* try to find a name for given function */
  201. functofind = luaI_Address(o);
  202. if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
  203. return "tag-method";
  204. else if ((*name = lua_travsymbol(checkfunc)) != NULL)
  205. return "global";
  206. else return "";
  207. }