lstring.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. ** $Id: lstring.c,v 1.60 2001/02/22 17:15:18 roberto Exp roberto $
  3. ** String table (keeps all strings handled by Lua)
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #include "lua.h"
  8. #include "lmem.h"
  9. #include "lobject.h"
  10. #include "lstate.h"
  11. #include "lstring.h"
  12. void luaS_init (lua_State *L) {
  13. luaS_resize(L, &G(L)->strt, MINPOWER2);
  14. luaS_resize(L, &G(L)->udt, MINPOWER2);
  15. }
  16. void luaS_freeall (lua_State *L) {
  17. lua_assert(G(L)->strt.nuse==0);
  18. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
  19. lua_assert(G(L)->udt.nuse==0);
  20. luaM_freearray(L, G(L)->udt.hash, G(L)->udt.size, TString *);
  21. }
  22. void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
  23. TString **newhash = luaM_newvector(L, newsize, TString *);
  24. int i;
  25. for (i=0; i<newsize; i++) newhash[i] = NULL;
  26. /* rehash */
  27. for (i=0; i<tb->size; i++) {
  28. TString *p = tb->hash[i];
  29. while (p) { /* for each node in the list */
  30. TString *next = p->nexthash; /* save next */
  31. lu_hash h = (tb == &G(L)->strt) ? p->u.s.hash : IntPoint(p->u.d.value);
  32. int h1 = lmod(h, newsize); /* new position */
  33. lua_assert((int)(h%newsize) == lmod(h, newsize));
  34. p->nexthash = newhash[h1]; /* chain it in new position */
  35. newhash[h1] = p;
  36. p = next;
  37. }
  38. }
  39. luaM_freearray(L, tb->hash, tb->size, TString *);
  40. tb->size = newsize;
  41. tb->hash = newhash;
  42. }
  43. static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) {
  44. ts->nexthash = tb->hash[h]; /* chain new entry */
  45. tb->hash[h] = ts;
  46. tb->nuse++;
  47. if (tb->nuse > (ls_nstr)tb->size && tb->size <= MAX_INT/2) /* too crowded? */
  48. luaS_resize(L, tb, tb->size*2);
  49. }
  50. TString *luaS_newlstr (lua_State *L, const l_char *str, size_t l) {
  51. TString *ts;
  52. lu_hash h = l; /* seed */
  53. size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
  54. size_t l1;
  55. for (l1=l; l1>=step; l1-=step) /* compute hash */
  56. h = h ^ ((h<<5)+(h>>2)+uchar(str[l1-1]));
  57. for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; ts; ts = ts->nexthash) {
  58. if (ts->len == l && (memcmp(str, getstr(ts), l) == 0))
  59. return ts;
  60. }
  61. /* not found */
  62. ts = (TString *)luaM_malloc(L, sizestring(l));
  63. ts->marked = 0;
  64. ts->nexthash = NULL;
  65. ts->len = l;
  66. ts->u.s.hash = h;
  67. ts->u.s.constindex = 0;
  68. memcpy(getstr(ts), str, l*sizeof(l_char));
  69. getstr(ts)[l] = 0; /* ending 0 */
  70. newentry(L, &G(L)->strt, ts, lmod(h, G(L)->strt.size)); /* insert it */
  71. return ts;
  72. }
  73. TString *luaS_newudata (lua_State *L, size_t s, void *udata) {
  74. TString *ts = (TString *)luaM_malloc(L, sizeudata(s));
  75. ts->marked = 0;
  76. ts->nexthash = NULL;
  77. ts->len = s;
  78. ts->u.d.tag = 0;
  79. ts->u.d.value = (s > 0) ? getstr(ts) : udata;
  80. /* insert it on table */
  81. newentry(L, &G(L)->udt, ts, lmod(IntPoint(ts->u.d.value), G(L)->udt.size));
  82. return ts;
  83. }
  84. int luaS_createudata (lua_State *L, void *udata, TObject *o) {
  85. int h1 = lmod(IntPoint(udata), G(L)->udt.size);
  86. TString *ts;
  87. for (ts = G(L)->udt.hash[h1]; ts; ts = ts->nexthash) {
  88. if (udata == ts->u.d.value) {
  89. setuvalue(o, ts);
  90. return 0;
  91. }
  92. }
  93. /* not found */
  94. setuvalue(o, luaS_newudata(L, 0, udata));
  95. return 1;
  96. }