lstring.c 3.5 KB

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