2
0

lstring.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. ** $Id: lstring.c,v 1.77 2002/11/13 11:32:26 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 lstring_c
  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. GCObject **newhash = luaM_newvector(L, newsize, GCObject *);
  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. GCObject *p = tb->hash[i];
  25. while (p) { /* for each node in the list */
  26. GCObject *next = p->gch.next; /* save next */
  27. lu_hash h = gcotots(p)->tsv.hash;
  28. int h1 = lmod(h, newsize); /* new position */
  29. lua_assert(cast(int, h%newsize) == lmod(h, newsize));
  30. p->gch.next = newhash[h1]; /* chain it */
  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 char *str, size_t l, lu_hash h) {
  40. TString *ts = cast(TString *, luaM_malloc(L, sizestring(l)));
  41. stringtable *tb;
  42. ts->tsv.len = l;
  43. ts->tsv.hash = h;
  44. ts->tsv.marked = 0;
  45. ts->tsv.tt = LUA_TSTRING;
  46. ts->tsv.reserved = 0;
  47. memcpy(ts+1, str, l*sizeof(char));
  48. ((char *)(ts+1))[l] = '\0'; /* ending 0 */
  49. tb = &G(L)->strt;
  50. h = lmod(h, tb->size);
  51. ts->tsv.next = tb->hash[h]; /* chain new entry */
  52. tb->hash[h] = valtogco(ts);
  53. tb->nuse++;
  54. if (tb->nuse > cast(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 char *str, size_t l) {
  59. GCObject *o;
  60. lu_hash h = (lu_hash)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)+(unsigned char)(str[l1-1]));
  65. for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
  66. o != NULL;
  67. o = o->gch.next) {
  68. TString *ts = gcotots(o);
  69. if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0))
  70. return ts;
  71. }
  72. return newlstr(L, str, l, h); /* not found */
  73. }
  74. Udata *luaS_newudata (lua_State *L, size_t s) {
  75. Udata *u;
  76. u = cast(Udata *, luaM_malloc(L, sizeudata(s)));
  77. u->uv.marked = (1<<1); /* is not finalized */
  78. u->uv.tt = LUA_TUSERDATA;
  79. u->uv.len = s;
  80. u->uv.metatable = hvalue(defaultmeta(L));
  81. /* chain it on udata list */
  82. u->uv.next = G(L)->rootudata;
  83. G(L)->rootudata = valtogco(u);
  84. return u;
  85. }