lstring.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. ** $Id: lstring.c,v 1.64 2001/06/07 15:01:21 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_freeall (lua_State *L) {
  14. lua_assert(G(L)->strt.nuse==0);
  15. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
  16. }
  17. void luaS_resize (lua_State *L, int newsize) {
  18. TString **newhash = luaM_newvector(L, newsize, TString *);
  19. stringtable *tb = &G(L)->strt;
  20. int i;
  21. for (i=0; i<newsize; i++) newhash[i] = NULL;
  22. /* rehash */
  23. for (i=0; i<tb->size; i++) {
  24. TString *p = tb->hash[i];
  25. while (p) { /* for each node in the list */
  26. TString *next = p->tsv.nexthash; /* save next */
  27. lu_hash h = p->tsv.hash;
  28. int h1 = lmod(h, newsize); /* new position */
  29. lua_assert((int)(h%newsize) == lmod(h, newsize));
  30. p->tsv.nexthash = newhash[h1]; /* chain it in new position */
  31. newhash[h1] = p;
  32. p = next;
  33. }
  34. }
  35. luaM_freearray(L, tb->hash, tb->size, TString *);
  36. tb->size = newsize;
  37. tb->hash = newhash;
  38. }
  39. static TString *newlstr (lua_State *L, const l_char *str, size_t l, lu_hash h) {
  40. TString *ts = (TString *)luaM_malloc(L, sizestring(l));
  41. stringtable *tb;
  42. ts->tsv.nexthash = NULL;
  43. ts->tsv.len = l;
  44. ts->tsv.hash = h;
  45. ts->tsv.marked = 0;
  46. ts->tsv.constindex = 0;
  47. memcpy(getstr(ts), str, l*sizeof(l_char));
  48. getstr(ts)[l] = l_c('\0'); /* ending 0 */
  49. tb = &G(L)->strt;
  50. h = lmod(h, tb->size);
  51. ts->tsv.nexthash = tb->hash[h]; /* chain new entry */
  52. tb->hash[h] = ts;
  53. tb->nuse++;
  54. if (tb->nuse > (ls_nstr)tb->size && tb->size <= MAX_INT/2)
  55. luaS_resize(L, tb->size*2); /* too crowded */
  56. return ts;
  57. }
  58. TString *luaS_newlstr (lua_State *L, const l_char *str, size_t l) {
  59. TString *ts;
  60. lu_hash h = l; /* seed */
  61. size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
  62. size_t l1;
  63. for (l1=l; l1>=step; l1-=step) /* compute hash */
  64. h = h ^ ((h<<5)+(h>>2)+uchar(str[l1-1]));
  65. for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
  66. ts != NULL;
  67. ts = ts->tsv.nexthash) {
  68. if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0))
  69. return ts;
  70. }
  71. return newlstr(L, str, l, h); /* not found */
  72. }
  73. Udata *luaS_newudata (lua_State *L, size_t s) {
  74. Udata *u = (Udata *)luaM_malloc(L, sizeudata(s));
  75. u->uv.len = s;
  76. u->uv.tag = 0;
  77. u->uv.value = u + 1;
  78. /* chain it on udata list */
  79. u->uv.next = G(L)->rootudata;
  80. G(L)->rootudata = u;
  81. return u;
  82. }