lstring.c 2.6 KB

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