lstring.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. ** $Id: lstring.c,v 1.75 2002/08/16 14:45:55 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. GCObject **newhash = luaM_newvector(L, newsize, GCObject *);
  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. GCObject *p = tb->hash[i];
  24. while (p) { /* for each node in the list */
  25. GCObject *next = p->gch.next; /* save next */
  26. lu_hash h = (&p->ts)->tsv.hash;
  27. int h1 = lmod(h, newsize); /* new position */
  28. lua_assert(cast(int, h%newsize) == lmod(h, newsize));
  29. p->gch.next = newhash[h1]; /* chain it */
  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.len = l;
  42. ts->tsv.hash = h;
  43. ts->tsv.marked = 0;
  44. ts->tsv.tt = LUA_TSTRING;
  45. ts->tsv.reserved = 0;
  46. memcpy(ts+1, str, l*sizeof(char));
  47. ((char *)(ts+1))[l] = '\0'; /* ending 0 */
  48. tb = &G(L)->strt;
  49. h = lmod(h, tb->size);
  50. ts->tsv.next = tb->hash[h]; /* chain new entry */
  51. tb->hash[h] = cast(GCObject *, ts);
  52. tb->nuse++;
  53. if (tb->nuse > cast(ls_nstr, tb->size) && tb->size <= MAX_INT/2)
  54. luaS_resize(L, tb->size*2); /* too crowded */
  55. return ts;
  56. }
  57. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  58. GCObject *ts;
  59. lu_hash h = (lu_hash)l; /* seed */
  60. size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
  61. size_t l1;
  62. for (l1=l; l1>=step; l1-=step) /* compute hash */
  63. h = h ^ ((h<<5)+(h>>2)+(unsigned char)(str[l1-1]));
  64. for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
  65. ts != NULL;
  66. ts = ts->gch.next) {
  67. if ((&ts->ts)->tsv.len == l && (memcmp(str, getstr(&ts->ts), l) == 0))
  68. return &ts->ts;
  69. }
  70. return newlstr(L, str, l, h); /* not found */
  71. }
  72. Udata *luaS_newudata (lua_State *L, size_t s) {
  73. Udata *u;
  74. u = cast(Udata *, luaM_malloc(L, sizeudata(s)));
  75. u->uv.marked = (1<<1); /* is not finalized */
  76. u->uv.tt = LUA_TUSERDATA;
  77. u->uv.len = s;
  78. u->uv.metatable = hvalue(defaultmeta(L));
  79. /* chain it on udata list */
  80. u->uv.next = G(L)->rootudata;
  81. G(L)->rootudata = cast(GCObject *, u);
  82. return u;
  83. }