lstring.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. ** $Id: lstring.c,v 1.41 2000/08/04 19:38:35 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. L->strt.hash = luaM_newvector(L, 1, TString *);
  14. L->udt.hash = luaM_newvector(L, 1, TString *);
  15. L->strt.size = L->udt.size = 1;
  16. L->strt.nuse = L->udt.nuse = 0;
  17. L->strt.hash[0] = L->udt.hash[0] = NULL;
  18. }
  19. void luaS_freeall (lua_State *L) {
  20. LUA_ASSERT(L->strt.nuse==0, "non-empty string table");
  21. luaM_free(L, L->strt.hash);
  22. LUA_ASSERT(L->udt.nuse==0, "non-empty udata table");
  23. luaM_free(L, L->udt.hash);
  24. }
  25. static unsigned long hash_s (const char *s, size_t l) {
  26. unsigned long h = l; /* seed */
  27. size_t step = (l>>5)|1; /* if string is too long, don't hash all its chars */
  28. for (; l>=step; l-=step)
  29. h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
  30. return h;
  31. }
  32. void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
  33. TString **newhash = luaM_newvector(L, newsize, TString *);
  34. int i;
  35. for (i=0; i<newsize; i++) newhash[i] = NULL;
  36. /* rehash */
  37. for (i=0; i<tb->size; i++) {
  38. TString *p = tb->hash[i];
  39. while (p) { /* for each node in the list */
  40. TString *next = p->nexthash; /* save next */
  41. unsigned long h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value);
  42. int h1 = h&(newsize-1); /* new position */
  43. LUA_ASSERT(h%newsize == (h&(newsize-1)),
  44. "a&(x-1) == a%x, for x power of 2");
  45. p->nexthash = newhash[h1]; /* chain it in new position */
  46. newhash[h1] = p;
  47. p = next;
  48. }
  49. }
  50. luaM_free(L, tb->hash);
  51. tb->size = newsize;
  52. tb->hash = newhash;
  53. }
  54. static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) {
  55. ts->nexthash = tb->hash[h]; /* chain new entry */
  56. tb->hash[h] = ts;
  57. tb->nuse++;
  58. if (tb->nuse > (lint32)tb->size && tb->size < MAX_INT/2) /* too crowded? */
  59. luaS_resize(L, tb, tb->size*2);
  60. }
  61. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  62. unsigned long h = hash_s(str, l);
  63. int h1 = h&(L->strt.size-1);
  64. TString *ts;
  65. for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash) {
  66. if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0))
  67. return ts;
  68. }
  69. /* not found */
  70. ts = (TString *)luaM_malloc(L, sizeof(TString)+(lint32)l*sizeof(char));
  71. ts->marked = 0;
  72. ts->nexthash = NULL;
  73. ts->u.s.len = l;
  74. ts->u.s.hash = h;
  75. ts->u.s.constindex = 0;
  76. memcpy(ts->str, str, l);
  77. ts->str[l] = 0; /* ending 0 */
  78. L->nblocks += gcsizestring(L, l);
  79. newentry(L, &L->strt, ts, h1); /* insert it on table */
  80. return ts;
  81. }
  82. TString *luaS_createudata (lua_State *L, void *udata, int tag) {
  83. unsigned long h = IntPoint(udata);
  84. int h1 = h&(L->udt.size-1);
  85. TString *ts;
  86. for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) {
  87. if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
  88. return ts;
  89. }
  90. /* not found */
  91. ts = luaM_new(L, TString);
  92. ts->marked = 0;
  93. ts->nexthash = NULL;
  94. ts->u.d.value = udata;
  95. ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
  96. L->nblocks += gcsizeudata;
  97. newentry(L, &L->udt, ts, h1); /* insert it on table */
  98. return ts;
  99. }
  100. TString *luaS_new (lua_State *L, const char *str) {
  101. return luaS_newlstr(L, str, strlen(str));
  102. }
  103. TString *luaS_newfixed (lua_State *L, const char *str) {
  104. TString *ts = luaS_new(L, str);
  105. if (ts->marked == 0) ts->marked = FIXMARK; /* avoid GC */
  106. return ts;
  107. }