/scripting/lua/lua/lstring.c

https://bitbucket.org/Tsiannian/cocos2d-x · C · 111 lines · 88 code · 15 blank · 8 comment · 18 complexity · 0ffb701fcc7fc6aeae1d2779b47a1694 MD5 · raw file

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