lstring.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. ** $Id: lstring.c,v 2.12 2009/04/17 14:40: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. #define lstring_c
  8. #define LUA_CORE
  9. #include "lua.h"
  10. #include "lmem.h"
  11. #include "lobject.h"
  12. #include "lstate.h"
  13. #include "lstring.h"
  14. void luaS_resize (lua_State *L, int newsize) {
  15. int i;
  16. stringtable *tb = &G(L)->strt;
  17. if (G(L)->gcstate == GCSsweepstring)
  18. return; /* cannot resize during GC traverse */
  19. if (newsize > tb->size) {
  20. luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
  21. for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL;
  22. }
  23. /* rehash */
  24. for (i=0; i<tb->size; i++) {
  25. GCObject *p = tb->hash[i];
  26. tb->hash[i] = NULL;
  27. while (p) { /* for each node in the list */
  28. GCObject *next = gch(p)->next; /* save next */
  29. unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */
  30. gch(p)->next = tb->hash[h]; /* chain it */
  31. tb->hash[h] = p;
  32. p = next;
  33. }
  34. }
  35. if (newsize < tb->size) {
  36. /* shrinking slice must be empty */
  37. lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
  38. luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
  39. }
  40. tb->size = newsize;
  41. }
  42. static TString *newlstr (lua_State *L, const char *str, size_t l,
  43. unsigned int h) {
  44. TString *ts;
  45. stringtable *tb = &G(L)->strt;
  46. if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
  47. luaM_toobig(L);
  48. if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
  49. luaS_resize(L, tb->size*2); /* too crowded */
  50. ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
  51. ts->tsv.len = l;
  52. ts->tsv.hash = h;
  53. ts->tsv.marked = luaC_white(G(L));
  54. ts->tsv.tt = LUA_TSTRING;
  55. ts->tsv.reserved = 0;
  56. memcpy(ts+1, str, l*sizeof(char));
  57. ((char *)(ts+1))[l] = '\0'; /* ending 0 */
  58. h = lmod(h, tb->size);
  59. ts->tsv.next = tb->hash[h]; /* chain new entry */
  60. tb->hash[h] = obj2gco(ts);
  61. tb->nuse++;
  62. return ts;
  63. }
  64. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  65. GCObject *o;
  66. unsigned int h = cast(unsigned int, l); /* seed */
  67. size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
  68. size_t l1;
  69. for (l1=l; l1>=step; l1-=step) /* compute hash */
  70. h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
  71. for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
  72. o != NULL;
  73. o = gch(o)->next) {
  74. TString *ts = rawgco2ts(o);
  75. if (h == ts->tsv.hash && ts->tsv.len == l &&
  76. (memcmp(str, getstr(ts), l) == 0)) {
  77. if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */
  78. changewhite(o); /* resurrect it */
  79. return ts;
  80. }
  81. }
  82. return newlstr(L, str, l, h); /* not found; create a new string */
  83. }
  84. Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
  85. Udata *u;
  86. if (s > MAX_SIZET - sizeof(Udata))
  87. luaM_toobig(L);
  88. u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
  89. luaC_link(L, obj2gco(u), LUA_TUSERDATA);
  90. u->uv.len = s;
  91. u->uv.metatable = NULL;
  92. u->uv.env = e;
  93. return u;
  94. }