hash.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. ** hash.c
  3. ** hash manager for lua
  4. */
  5. char *rcs_hash="$Id: hash.c,v 2.9 1994/10/17 19:03:23 celes Exp roberto $";
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include "opcode.h"
  9. #include "hash.h"
  10. #include "inout.h"
  11. #include "table.h"
  12. #include "lua.h"
  13. #define streq(s1,s2) (s1 == s2 || (*(s1) == *(s2) && strcmp(s1,s2)==0))
  14. #define new(s) ((s *)malloc(sizeof(s)))
  15. #define newvector(n,s) ((s *)calloc(n,sizeof(s)))
  16. #define nhash(t) ((t)->nhash)
  17. #define nuse(t) ((t)->nuse)
  18. #define markarray(t) ((t)->mark)
  19. #define nodevector(t) ((t)->node)
  20. #define node(t,i) (&(t)->node[i])
  21. #define ref(n) (&(n)->ref)
  22. #define val(n) (&(n)->val)
  23. #define REHASH_LIMIT 0.70 /* avoid more than this % full */
  24. static Hash *listhead = NULL;
  25. /* hash dimensions values */
  26. static int dimensions[] =
  27. {3, 5, 7, 11, 23, 47, 97, 197, 397, 797, 1597, 3203, 6421,
  28. 12853, 25717, 51437, 0};
  29. static int redimension (int nhash)
  30. {
  31. int i;
  32. for (i=0; dimensions[i]!=0; i++)
  33. {
  34. if (dimensions[i] > nhash)
  35. return dimensions[i];
  36. }
  37. return nhash*2+1;
  38. }
  39. static int hashindex (Hash *t, Object *ref) /* hash function */
  40. {
  41. switch (tag(ref))
  42. {
  43. case T_NUMBER:
  44. return (((int)nvalue(ref))%nhash(t));
  45. case T_STRING:
  46. {
  47. int h;
  48. char *name = svalue(ref);
  49. for (h=0; *name!=0; name++) /* interpret name as binary number */
  50. {
  51. h <<= 8;
  52. h += (unsigned char) *name; /* avoid sign extension */
  53. h %= nhash(t); /* make it a valid index */
  54. }
  55. return h;
  56. }
  57. case T_FUNCTION:
  58. return (((int)bvalue(ref))%nhash(t));
  59. case T_CFUNCTION:
  60. return (((int)fvalue(ref))%nhash(t));
  61. case T_ARRAY:
  62. return (((int)avalue(ref))%nhash(t));
  63. case T_USERDATA:
  64. return (((int)uvalue(ref))%nhash(t));
  65. default:
  66. lua_reportbug ("unexpected type to index table");
  67. return -1;
  68. }
  69. }
  70. static int equalObj (Object *t1, Object *t2)
  71. {
  72. if (tag(t1) != tag(t2)) return 0;
  73. switch (tag(t1))
  74. {
  75. case T_NUMBER: return nvalue(t1) == nvalue(t2);
  76. case T_STRING: return streq(svalue(t1), svalue(t2));
  77. default: return uvalue(t1) == uvalue(t2);
  78. }
  79. }
  80. static int present (Hash *t, Object *ref)
  81. {
  82. int h = hashindex(t, ref);
  83. if (h < 0) return h;
  84. while (tag(ref(node(t, h))) != T_NIL)
  85. {
  86. if (equalObj(ref, ref(node(t, h))))
  87. return h;
  88. h = (h+1) % nhash(t);
  89. }
  90. return h;
  91. }
  92. /*
  93. ** Alloc a vector node
  94. */
  95. static Node *hashnodecreate (int nhash)
  96. {
  97. int i;
  98. Node *v = newvector (nhash, Node);
  99. if (v == NULL)
  100. {
  101. lua_error ("not enough memory");
  102. return NULL;
  103. }
  104. for (i=0; i<nhash; i++)
  105. tag(ref(&v[i])) = T_NIL;
  106. return v;
  107. }
  108. /*
  109. ** Create a new hash. Return the hash pointer or NULL on error.
  110. */
  111. static Hash *hashcreate (int nhash)
  112. {
  113. Hash *t = new (Hash);
  114. if (t == NULL)
  115. {
  116. lua_error ("not enough memory");
  117. return NULL;
  118. }
  119. nhash = redimension((int)((float)nhash/REHASH_LIMIT));
  120. nodevector(t) = hashnodecreate(nhash);
  121. if (nodevector(t) == NULL)
  122. return NULL;
  123. nhash(t) = nhash;
  124. nuse(t) = 0;
  125. markarray(t) = 0;
  126. return t;
  127. }
  128. /*
  129. ** Delete a hash
  130. */
  131. static void hashdelete (Hash *t)
  132. {
  133. free (nodevector(t));
  134. free(t);
  135. }
  136. /*
  137. ** Mark a hash and check its elements
  138. */
  139. void lua_hashmark (Hash *h)
  140. {
  141. if (markarray(h) == 0)
  142. {
  143. int i;
  144. markarray(h) = 1;
  145. for (i=0; i<nhash(h); i++)
  146. {
  147. Node *n = node(h,i);
  148. if (tag(ref(n)) != T_NIL)
  149. {
  150. lua_markobject(&n->ref);
  151. lua_markobject(&n->val);
  152. }
  153. }
  154. }
  155. }
  156. /*
  157. ** Garbage collection to arrays
  158. ** Delete all unmarked arrays.
  159. */
  160. void lua_hashcollector (void)
  161. {
  162. Hash *curr_array = listhead, *prev = NULL;
  163. while (curr_array != NULL)
  164. {
  165. Hash *next = curr_array->next;
  166. if (markarray(curr_array) != 1)
  167. {
  168. if (prev == NULL) listhead = next;
  169. else prev->next = next;
  170. hashdelete(curr_array);
  171. ++lua_recovered;
  172. }
  173. else
  174. {
  175. markarray(curr_array) = 0;
  176. prev = curr_array;
  177. }
  178. curr_array = next;
  179. }
  180. }
  181. /*
  182. ** Create a new array
  183. ** This function inserts the new array in the array list. It also
  184. ** executes garbage collection if the number of arrays created
  185. ** exceed a pre-defined range.
  186. */
  187. Hash *lua_createarray (int nhash)
  188. {
  189. Hash *array = hashcreate(nhash);
  190. if (array == NULL)
  191. {
  192. lua_error ("not enough memory");
  193. return NULL;
  194. }
  195. if (lua_nentity == lua_block)
  196. lua_pack();
  197. lua_nentity++;
  198. array->next = listhead;
  199. listhead = array;
  200. return array;
  201. }
  202. /*
  203. ** Re-hash
  204. */
  205. static void rehash (Hash *t)
  206. {
  207. int i;
  208. int nold = nhash(t);
  209. Node *vold = nodevector(t);
  210. nhash(t) = redimension(nhash(t));
  211. nodevector(t) = hashnodecreate(nhash(t));
  212. for (i=0; i<nold; i++)
  213. {
  214. Node *n = vold+i;
  215. if (tag(ref(n)) != T_NIL && tag(val(n)) != T_NIL)
  216. *node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */
  217. }
  218. free(vold);
  219. }
  220. /*
  221. ** If the hash node is present, return its pointer, otherwise search
  222. ** the node at parent table, recursively, if there is parent.
  223. ** If no parent and the node is not present, return a static nil object.
  224. */
  225. Object *lua_hashget (Hash *t, Object *ref)
  226. {
  227. static int count = 1000;
  228. static Object nil_obj = {T_NIL, {NULL}};
  229. int h = present(t, ref);
  230. if (h < 0) return &nil_obj;
  231. if (tag(ref(node(t, h))) != T_NIL) return val(node(t, h));
  232. if (--count == 0)
  233. {
  234. lua_reportbug ("hierarchy too deep (maybe there is an inheritance loop)");
  235. return &nil_obj;
  236. }
  237. { /* check "parent" or "godparent" field */
  238. Hash *p;
  239. Object parent;
  240. Object godparent;
  241. tag(&parent) = T_STRING; svalue(&parent) = "parent";
  242. tag(&godparent) = T_STRING; svalue(&godparent) = "godparent";
  243. h = present(t, &parent); /* assert(h >= 0); */
  244. p = tag(ref(node(t, h))) != T_NIL && tag(val(node(t, h))) == T_ARRAY ?
  245. avalue(val(node(t, h))) : NULL;
  246. if (p != NULL)
  247. {
  248. Object *r = lua_hashget(p, ref);
  249. if (tag(r) != T_NIL) { count++; return r; }
  250. }
  251. h = present(t, &godparent); /* assert(h >= 0); */
  252. p = tag(ref(node(t, h))) != T_NIL && tag(val(node(t, h))) == T_ARRAY ?
  253. avalue(val(node(t, h))) : NULL;
  254. if (p != NULL)
  255. {
  256. Object *r = lua_hashget(p, ref);
  257. if (tag(r) != T_NIL) { count++; return r; }
  258. }
  259. }
  260. count++;
  261. return &nil_obj;
  262. }
  263. /*
  264. ** If the hash node is present, return its pointer, otherwise create a new
  265. ** node for the given reference and also return its pointer.
  266. ** On error, return NULL.
  267. */
  268. Object *lua_hashdefine (Hash *t, Object *ref)
  269. {
  270. int h;
  271. Node *n;
  272. h = present(t, ref);
  273. if (h < 0) return NULL;
  274. n = node(t, h);
  275. if (tag(ref(n)) == T_NIL)
  276. {
  277. nuse(t)++;
  278. if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT)
  279. {
  280. rehash(t);
  281. h = present(t, ref);
  282. n = node(t, h);
  283. }
  284. *ref(n) = *ref;
  285. tag(val(n)) = T_NIL;
  286. }
  287. return (val(n));
  288. }
  289. /*
  290. ** Internal function to manipulate arrays.
  291. ** Given an array object and a reference value, return the next element
  292. ** in the hash.
  293. ** This function pushs the element value and its reference to the stack.
  294. */
  295. static void hashnext (Hash *t, int i)
  296. {
  297. if (i >= nhash(t))
  298. {
  299. lua_pushnil(); lua_pushnil();
  300. return;
  301. }
  302. while (tag(ref(node(t,i))) == T_NIL || tag(val(node(t,i))) == T_NIL)
  303. {
  304. if (++i >= nhash(t))
  305. {
  306. lua_pushnil(); lua_pushnil();
  307. return;
  308. }
  309. }
  310. lua_pushobject(ref(node(t,i)));
  311. lua_pushobject(val(node(t,i)));
  312. }
  313. void lua_next (void)
  314. {
  315. Hash *t;
  316. Object *o = lua_getparam (1);
  317. Object *r = lua_getparam (2);
  318. if (o == NULL || r == NULL)
  319. { lua_error ("too few arguments to function `next'"); return; }
  320. if (lua_getparam (3) != NULL)
  321. { lua_error ("too many arguments to function `next'"); return; }
  322. if (tag(o) != T_ARRAY)
  323. { lua_error ("first argument of function `next' is not a table"); return; }
  324. t = avalue(o);
  325. if (tag(r) == T_NIL)
  326. {
  327. hashnext(t, 0);
  328. }
  329. else
  330. {
  331. int h = present (t, r);
  332. if (h >= 0)
  333. hashnext(t, h+1);
  334. else
  335. lua_error ("error in function 'next': reference not found");
  336. }
  337. }