lstring.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. ** $Id: lstring.c,v 2.18 2010/05/10 18:23:45 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. /* cannot resize while GC is traversing strings */
  18. luaC_runtilstate(L, ~bitmask(GCSsweepstring));
  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. resetoldbit(p); /* see MOVE OLD rule */
  33. p = next;
  34. }
  35. }
  36. if (newsize < tb->size) {
  37. /* shrinking slice must be empty */
  38. lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
  39. luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
  40. }
  41. tb->size = newsize;
  42. }
  43. static TString *newlstr (lua_State *L, const char *str, size_t l,
  44. unsigned int h) {
  45. size_t totalsize; /* total size of TString object */
  46. GCObject **list; /* (pointer to) list where it will be inserted */
  47. TString *ts;
  48. stringtable *tb = &G(L)->strt;
  49. if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
  50. luaM_toobig(L);
  51. if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
  52. luaS_resize(L, tb->size*2); /* too crowded */
  53. totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
  54. list = &tb->hash[lmod(h, tb->size)];
  55. ts = &luaC_newobj(L, LUA_TSTRING, totalsize, list, 0)->ts;
  56. ts->tsv.len = l;
  57. ts->tsv.hash = h;
  58. ts->tsv.reserved = 0;
  59. memcpy(ts+1, str, l*sizeof(char));
  60. ((char *)(ts+1))[l] = '\0'; /* ending 0 */
  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 &&
  76. ts->tsv.len == l &&
  77. (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
  78. if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */
  79. changewhite(o); /* resurrect it */
  80. return ts;
  81. }
  82. }
  83. return newlstr(L, str, l, h); /* not found; create a new string */
  84. }
  85. TString *luaS_new (lua_State *L, const char *str) {
  86. return luaS_newlstr(L, str, strlen(str));
  87. }
  88. Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
  89. Udata *u;
  90. if (s > MAX_SIZET - sizeof(Udata))
  91. luaM_toobig(L);
  92. u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u;
  93. u->uv.len = s;
  94. u->uv.metatable = NULL;
  95. u->uv.env = e;
  96. return u;
  97. }