lstring.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. ** $Id: lstring.c,v 1.54 2001/02/01 13:56:49 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. luint32 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 > (luint32)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 char *str, size_t l) {
  51. TString *ts;
  52. luint32 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)+(unsigned char)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, ts->str, 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(ts->str, str, l);
  69. ts->str[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. union L_UTString *uts = (union L_UTString *)luaM_malloc(L, sizeudata(s));
  75. TString *ts = &uts->ts;
  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) ? uts+1 : 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. TString *luaS_createudata (lua_State *L, void *udata, int tag) {
  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 && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
  90. return ts;
  91. }
  92. /* not found */
  93. ts = luaS_newudata(L, 0, udata);
  94. if (tag != LUA_ANYTAG)
  95. ts->u.d.tag = tag;
  96. return ts;
  97. }