lstring.c 3.1 KB

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