diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-08-21 16:21:16 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-08-21 16:21:16 -0300 |
commit | ae800656c9f82b54f6fae1497022d3484ad0c920 (patch) | |
tree | e3124387f4e29fc452ebb3c15b7e6cd0c1f4bbb7 | |
parent | 8c688639605f3ec0b5a57d906cacc27981b066da (diff) | |
download | lua-ae800656c9f82b54f6fae1497022d3484ad0c920.tar.gz lua-ae800656c9f82b54f6fae1497022d3484ad0c920.tar.bz2 lua-ae800656c9f82b54f6fae1497022d3484ad0c920.zip |
change in string table: string table is now independent of GC lists; all
strings live in 'normal' GC lists
-rw-r--r-- | lgc.c | 46 | ||||
-rw-r--r-- | lgc.h | 11 | ||||
-rw-r--r-- | lstate.c | 5 | ||||
-rw-r--r-- | lstate.h | 12 | ||||
-rw-r--r-- | lstring.c | 140 | ||||
-rw-r--r-- | lstring.h | 3 | ||||
-rw-r--r-- | ltests.c | 22 |
7 files changed, 125 insertions, 114 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lgc.c,v 2.147 2013/08/19 14:18:43 roberto Exp roberto $ | 2 | ** $Id: lgc.c,v 2.148 2013/08/20 17:46:34 roberto Exp roberto $ |
3 | ** Garbage Collector | 3 | ** Garbage Collector |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -667,7 +667,7 @@ static void freeobj (lua_State *L, GCObject *o) { | |||
667 | case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break; | 667 | case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break; |
668 | case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break; | 668 | case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break; |
669 | case LUA_TSHRSTR: | 669 | case LUA_TSHRSTR: |
670 | G(L)->strt.nuse--; | 670 | luaS_remove(L, rawgco2ts(o)); /* remove it from hash table */ |
671 | /* go through */ | 671 | /* go through */ |
672 | case LUA_TLNGSTR: { | 672 | case LUA_TLNGSTR: { |
673 | luaM_freemem(L, o, sizestring(gco2ts(o))); | 673 | luaM_freemem(L, o, sizestring(gco2ts(o))); |
@@ -749,14 +749,10 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) { | |||
749 | ** ======================================================= | 749 | ** ======================================================= |
750 | */ | 750 | */ |
751 | 751 | ||
752 | static void checkSizes (lua_State *L) { | 752 | static void checkBuffer (lua_State *L) { |
753 | global_State *g = G(L); | 753 | global_State *g = G(L); |
754 | if (g->gckind != KGC_EMERGENCY) { /* do not change sizes in emergency */ | 754 | if (g->gckind != KGC_EMERGENCY) |
755 | int hs = g->strt.size / 2; /* half the size of the string table */ | ||
756 | if (g->strt.nuse < cast(lu_int32, hs)) /* using less than that half? */ | ||
757 | luaS_resize(L, hs); /* halve its size */ | ||
758 | luaZ_freebuffer(L, &g->buff); /* free concatenation buffer */ | 755 | luaZ_freebuffer(L, &g->buff); /* free concatenation buffer */ |
759 | } | ||
760 | } | 756 | } |
761 | 757 | ||
762 | 758 | ||
@@ -855,7 +851,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { | |||
855 | } | 851 | } |
856 | /* search for pointer pointing to 'o' */ | 852 | /* search for pointer pointing to 'o' */ |
857 | for (p = &g->allgc; *p != o; p = &gch(*p)->next) { /* empty */ } | 853 | for (p = &g->allgc; *p != o; p = &gch(*p)->next) { /* empty */ } |
858 | *p = ho->next; /* remove 'o' from root list */ | 854 | *p = ho->next; /* remove 'o' from 'allgc' list */ |
859 | ho->next = g->finobj; /* link it in list 'finobj' */ | 855 | ho->next = g->finobj; /* link it in list 'finobj' */ |
860 | g->finobj = o; | 856 | g->finobj = o; |
861 | l_setbit(ho->marked, FINALIZEDBIT); /* mark it as such */ | 857 | l_setbit(ho->marked, FINALIZEDBIT); /* mark it as such */ |
@@ -889,25 +885,23 @@ static void setpause (global_State *g, l_mem estimate) { | |||
889 | } | 885 | } |
890 | 886 | ||
891 | 887 | ||
892 | #define sweepphases \ | 888 | #define sweepphases (bitmask(GCSsweepudata) | bitmask(GCSsweep)) |
893 | (bitmask(GCSsweepstring) | bitmask(GCSsweepudata) | bitmask(GCSsweep)) | ||
894 | 889 | ||
895 | 890 | ||
896 | /* | 891 | /* |
897 | ** enter first sweep phase (strings) and prepare pointers for other | 892 | ** enter first sweep phase and prepare pointers for other sweep phases. |
898 | ** sweep phases. The calls to 'sweeptolive' make pointers point to an | 893 | ** The calls to 'sweeptolive' make pointers point to an object inside |
899 | ** object inside the list (instead of to the header), so that the real | 894 | ** the list (instead of to the header), so that the real sweep do not |
900 | ** sweep do not need to skip objects created between "now" and the start | 895 | ** need to skip objects created between "now" and the start of the real |
901 | ** of the real sweep. | 896 | ** sweep. |
902 | ** Returns how many objects it swept. | 897 | ** Returns how many objects it swept. |
903 | */ | 898 | */ |
904 | static int entersweep (lua_State *L) { | 899 | static int entersweep (lua_State *L) { |
905 | global_State *g = G(L); | 900 | global_State *g = G(L); |
906 | int n = 0; | 901 | int n = 0; |
907 | g->gcstate = GCSsweepstring; | 902 | g->gcstate = GCSsweepudata; |
908 | lua_assert(g->sweepgc == NULL && g->sweepfin == NULL); | 903 | lua_assert(g->sweepgc == NULL && g->sweepfin == NULL); |
909 | /* prepare to sweep strings, finalizable objects, and regular objects */ | 904 | /* prepare to sweep finalizable objects and regular objects */ |
910 | g->sweepstrgc = 0; | ||
911 | g->sweepfin = sweeptolive(L, &g->finobj, &n); | 905 | g->sweepfin = sweeptolive(L, &g->finobj, &n); |
912 | g->sweepgc = sweeptolive(L, &g->allgc, &n); | 906 | g->sweepgc = sweeptolive(L, &g->allgc, &n); |
913 | return n; | 907 | return n; |
@@ -926,7 +920,6 @@ static void callallpendingfinalizers (lua_State *L, int propagateerrors) { | |||
926 | 920 | ||
927 | void luaC_freeallobjects (lua_State *L) { | 921 | void luaC_freeallobjects (lua_State *L) { |
928 | global_State *g = G(L); | 922 | global_State *g = G(L); |
929 | int i; | ||
930 | separatetobefnz(L, 1); /* separate all objects with finalizers */ | 923 | separatetobefnz(L, 1); /* separate all objects with finalizers */ |
931 | lua_assert(g->finobj == NULL); | 924 | lua_assert(g->finobj == NULL); |
932 | callallpendingfinalizers(L, 0); | 925 | callallpendingfinalizers(L, 0); |
@@ -934,8 +927,6 @@ void luaC_freeallobjects (lua_State *L) { | |||
934 | g->gckind = KGC_NORMAL; | 927 | g->gckind = KGC_NORMAL; |
935 | sweepwholelist(L, &g->finobj); /* finalizers can create objs. in 'finobj' */ | 928 | sweepwholelist(L, &g->finobj); /* finalizers can create objs. in 'finobj' */ |
936 | sweepwholelist(L, &g->allgc); | 929 | sweepwholelist(L, &g->allgc); |
937 | for (i = 0; i < g->strt.size; i++) /* free all string lists */ | ||
938 | sweepwholelist(L, &g->strt.hash[i]); | ||
939 | lua_assert(g->strt.nuse == 0); | 930 | lua_assert(g->strt.nuse == 0); |
940 | } | 931 | } |
941 | 932 | ||
@@ -1009,15 +1000,6 @@ static lu_mem singlestep (lua_State *L) { | |||
1009 | sw = entersweep(L); | 1000 | sw = entersweep(L); |
1010 | return work + sw * GCSWEEPCOST; | 1001 | return work + sw * GCSWEEPCOST; |
1011 | } | 1002 | } |
1012 | case GCSsweepstring: { | ||
1013 | int i; | ||
1014 | for (i = 0; i < GCSWEEPMAX && g->sweepstrgc + i < g->strt.size; i++) | ||
1015 | sweepwholelist(L, &g->strt.hash[g->sweepstrgc + i]); | ||
1016 | g->sweepstrgc += i; | ||
1017 | if (g->sweepstrgc >= g->strt.size) /* no more strings to sweep? */ | ||
1018 | g->gcstate = GCSsweepudata; | ||
1019 | return i * GCSWEEPCOST; | ||
1020 | } | ||
1021 | case GCSsweepudata: { | 1003 | case GCSsweepudata: { |
1022 | if (g->sweepfin) { | 1004 | if (g->sweepfin) { |
1023 | g->sweepfin = sweeplist(L, g->sweepfin, GCSWEEPMAX); | 1005 | g->sweepfin = sweeplist(L, g->sweepfin, GCSWEEPMAX); |
@@ -1037,7 +1019,7 @@ static lu_mem singlestep (lua_State *L) { | |||
1037 | /* sweep main thread */ | 1019 | /* sweep main thread */ |
1038 | GCObject *mt = obj2gco(g->mainthread); | 1020 | GCObject *mt = obj2gco(g->mainthread); |
1039 | sweeplist(L, &mt, 1); | 1021 | sweeplist(L, &mt, 1); |
1040 | checkSizes(L); | 1022 | checkBuffer(L); |
1041 | g->gcstate = GCSpause; /* finish collection */ | 1023 | g->gcstate = GCSpause; /* finish collection */ |
1042 | return GCSWEEPCOST; | 1024 | return GCSWEEPCOST; |
1043 | } | 1025 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lgc.h,v 2.62 2013/08/19 14:18:43 roberto Exp roberto $ | 2 | ** $Id: lgc.h,v 2.63 2013/08/20 17:46:34 roberto Exp roberto $ |
3 | ** Garbage Collector | 3 | ** Garbage Collector |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -38,14 +38,13 @@ | |||
38 | */ | 38 | */ |
39 | #define GCSpropagate 0 | 39 | #define GCSpropagate 0 |
40 | #define GCSatomic 1 | 40 | #define GCSatomic 1 |
41 | #define GCSsweepstring 2 | 41 | #define GCSsweepudata 2 |
42 | #define GCSsweepudata 3 | 42 | #define GCSsweep 3 |
43 | #define GCSsweep 4 | 43 | #define GCSpause 4 |
44 | #define GCSpause 5 | ||
45 | 44 | ||
46 | 45 | ||
47 | #define issweepphase(g) \ | 46 | #define issweepphase(g) \ |
48 | (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep) | 47 | (GCSsweepudata <= (g)->gcstate && (g)->gcstate <= GCSsweep) |
49 | 48 | ||
50 | 49 | ||
51 | /* | 50 | /* |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.c,v 2.101 2013/08/07 12:18:11 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 2.102 2013/08/16 18:55:49 roberto Exp roberto $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -280,8 +280,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | |||
280 | g->seed = makeseed(L); | 280 | g->seed = makeseed(L); |
281 | g->gcrunning = 0; /* no GC while building state */ | 281 | g->gcrunning = 0; /* no GC while building state */ |
282 | g->GCestimate = 0; | 282 | g->GCestimate = 0; |
283 | g->strt.size = 0; | 283 | g->strt.size = g->strt.nuse = g->strt.empty = 0; |
284 | g->strt.nuse = 0; | ||
285 | g->strt.hash = NULL; | 284 | g->strt.hash = NULL; |
286 | setnilvalue(&g->l_registry); | 285 | setnilvalue(&g->l_registry); |
287 | luaZ_initbuffer(L, &g->buff); | 286 | luaZ_initbuffer(L, &g->buff); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.h,v 2.84 2013/08/07 12:18:11 roberto Exp roberto $ | 2 | ** $Id: lstate.h,v 2.85 2013/08/20 17:46:34 roberto Exp roberto $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -24,8 +24,6 @@ | |||
24 | ** at the end of the 'allgc' list, after the 'l_registry' (which is | 24 | ** at the end of the 'allgc' list, after the 'l_registry' (which is |
25 | ** the first object to be added to the list). | 25 | ** the first object to be added to the list). |
26 | ** | 26 | ** |
27 | ** Short strings are kept in several lists headed by the array g->strt.hash. | ||
28 | ** | ||
29 | ** Open upvalues are not subject to independent garbage collection. They | 27 | ** Open upvalues are not subject to independent garbage collection. They |
30 | ** are collected together with their respective threads. (They are | 28 | ** are collected together with their respective threads. (They are |
31 | ** always gray, so they must be remarked in the atomic step. Usually | 29 | ** always gray, so they must be remarked in the atomic step. Usually |
@@ -57,9 +55,10 @@ struct lua_longjmp; /* defined in ldo.c */ | |||
57 | 55 | ||
58 | 56 | ||
59 | typedef struct stringtable { | 57 | typedef struct stringtable { |
60 | GCObject **hash; | 58 | TString **hash; |
61 | lu_int32 nuse; /* number of elements */ | 59 | unsigned int nuse; /* number of elements */ |
62 | int size; | 60 | unsigned int empty; /* number of available empty slots */ |
61 | unsigned int size; | ||
63 | } stringtable; | 62 | } stringtable; |
64 | 63 | ||
65 | 64 | ||
@@ -123,7 +122,6 @@ typedef struct global_State { | |||
123 | lu_byte gcstate; /* state of garbage collector */ | 122 | lu_byte gcstate; /* state of garbage collector */ |
124 | lu_byte gckind; /* kind of GC running */ | 123 | lu_byte gckind; /* kind of GC running */ |
125 | lu_byte gcrunning; /* true if GC is running */ | 124 | lu_byte gcrunning; /* true if GC is running */ |
126 | int sweepstrgc; /* position of sweep in `strt' */ | ||
127 | GCObject *allgc; /* list of all collectable objects */ | 125 | GCObject *allgc; /* list of all collectable objects */ |
128 | GCObject *finobj; /* list of collectable objects with finalizers */ | 126 | GCObject *finobj; /* list of collectable objects with finalizers */ |
129 | GCObject **sweepgc; /* current position of sweep in list 'allgc' */ | 127 | GCObject **sweepgc; /* current position of sweep in list 'allgc' */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 2.27 2013/06/19 14:27:00 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 2.28 2013/08/05 16:58:28 roberto Exp roberto $ |
3 | ** String table (keeps all strings handled by Lua) | 3 | ** String table (keeps all strings handled by Lua) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -12,12 +12,21 @@ | |||
12 | 12 | ||
13 | #include "lua.h" | 13 | #include "lua.h" |
14 | 14 | ||
15 | #include "ldebug.h" | ||
15 | #include "lmem.h" | 16 | #include "lmem.h" |
16 | #include "lobject.h" | 17 | #include "lobject.h" |
17 | #include "lstate.h" | 18 | #include "lstate.h" |
18 | #include "lstring.h" | 19 | #include "lstring.h" |
19 | 20 | ||
20 | 21 | ||
22 | /* mark for vacant places in hash table */ | ||
23 | #define VACANTK cast(TString *, cast(size_t, -1)) | ||
24 | |||
25 | |||
26 | /* second hash (for double hash) */ | ||
27 | #define h2(h1,hash,size) lmod(h1 + ((hash % 31) | 1), size) | ||
28 | |||
29 | |||
21 | /* | 30 | /* |
22 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to | 31 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to |
23 | ** compute its hash | 32 | ** compute its hash |
@@ -64,30 +73,27 @@ unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { | |||
64 | void luaS_resize (lua_State *L, int newsize) { | 73 | void luaS_resize (lua_State *L, int newsize) { |
65 | int i; | 74 | int i; |
66 | stringtable *tb = &G(L)->strt; | 75 | stringtable *tb = &G(L)->strt; |
67 | /* cannot resize while GC is traversing strings */ | 76 | TString **oldhash = tb->hash; |
68 | luaC_runtilstate(L, ~bitmask(GCSsweepstring)); | 77 | int oldsize = tb->size; |
69 | if (newsize > tb->size) { | 78 | tb->hash = luaM_newvector(L, newsize, TString *); |
70 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); | 79 | tb->size = newsize; |
71 | for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL; | 80 | /* keep load factor below 75% */ |
72 | } | 81 | tb->empty = newsize/2 + newsize/4 - tb->nuse; |
82 | for (i = 0; i < newsize; i++) tb->hash[i] = NULL; | ||
83 | tb->nuse = 0; | ||
73 | /* rehash */ | 84 | /* rehash */ |
74 | for (i=0; i<tb->size; i++) { | 85 | for (i = 0; i < oldsize; i++) { |
75 | GCObject *p = tb->hash[i]; | 86 | TString *ts = oldhash[i]; |
76 | tb->hash[i] = NULL; | 87 | if (ts != NULL && ts != VACANTK) { |
77 | while (p) { /* for each node in the list */ | 88 | unsigned int hash = ts->tsv.hash; |
78 | GCObject *next = gch(p)->next; /* save next */ | 89 | int h1 = lmod(hash, tb->size); |
79 | unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */ | 90 | while (tb->hash[h1] != NULL) |
80 | gch(p)->next = tb->hash[h]; /* chain it */ | 91 | h1 = h2(h1, hash, tb->size); |
81 | tb->hash[h] = p; | 92 | tb->hash[h1] = ts; |
82 | p = next; | 93 | tb->nuse++; |
83 | } | 94 | } |
84 | } | 95 | } |
85 | if (newsize < tb->size) { | 96 | luaM_freearray(L, oldhash, oldsize); |
86 | /* shrinking slice must be empty */ | ||
87 | lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); | ||
88 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); | ||
89 | } | ||
90 | tb->size = newsize; | ||
91 | } | 97 | } |
92 | 98 | ||
93 | 99 | ||
@@ -95,11 +101,11 @@ void luaS_resize (lua_State *L, int newsize) { | |||
95 | ** creates a new string object | 101 | ** creates a new string object |
96 | */ | 102 | */ |
97 | static TString *createstrobj (lua_State *L, const char *str, size_t l, | 103 | static TString *createstrobj (lua_State *L, const char *str, size_t l, |
98 | int tag, unsigned int h, GCObject **list) { | 104 | int tag, unsigned int h) { |
99 | TString *ts; | 105 | TString *ts; |
100 | size_t totalsize; /* total size of TString object */ | 106 | size_t totalsize; /* total size of TString object */ |
101 | totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); | 107 | totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); |
102 | ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts; | 108 | ts = &luaC_newobj(L, tag, totalsize, NULL, 0)->ts; |
103 | ts->tsv.len = l; | 109 | ts->tsv.len = l; |
104 | ts->tsv.hash = h; | 110 | ts->tsv.hash = h; |
105 | ts->tsv.extra = 0; | 111 | ts->tsv.extra = 0; |
@@ -109,20 +115,33 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l, | |||
109 | } | 115 | } |
110 | 116 | ||
111 | 117 | ||
112 | /* | 118 | static void rehash (lua_State *L, stringtable *tb) { |
113 | ** creates a new short string, inserting it into string table | 119 | int newsize; |
114 | */ | 120 | if (tb->nuse < tb->size / 2) { /* using less than half the size? */ |
115 | static TString *newshrstr (lua_State *L, const char *str, size_t l, | 121 | if (tb->nuse < tb->size / 4) /* using less than half of that? */ |
116 | unsigned int h) { | 122 | newsize = tb->size / 2; /* shrink table */ |
117 | GCObject **list; /* (pointer to) list where it will be inserted */ | 123 | else |
124 | newsize = tb->size; /* keep size (but reorganize table) */ | ||
125 | } | ||
126 | else { /* table must grow */ | ||
127 | if (tb->size >= MAX_INT/2) | ||
128 | luaG_runerror(L, "string-table overflow: too many strings"); | ||
129 | newsize = tb->size * 2; | ||
130 | } | ||
131 | luaS_resize(L, newsize); | ||
132 | } | ||
133 | |||
134 | |||
135 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts) { | ||
118 | stringtable *tb = &G(L)->strt; | 136 | stringtable *tb = &G(L)->strt; |
119 | TString *s; | 137 | unsigned int hash = ts->tsv.hash; |
120 | if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) | 138 | int h1 = lmod(hash, tb->size); |
121 | luaS_resize(L, tb->size*2); /* too crowded */ | 139 | while (tb->hash[h1] != ts) { |
122 | list = &tb->hash[lmod(h, tb->size)]; | 140 | lua_assert(tb->hash[h1] != NULL); |
123 | s = createstrobj(L, str, l, LUA_TSHRSTR, h, list); | 141 | h1 = h2(h1, hash, tb->size); |
124 | tb->nuse++; | 142 | } |
125 | return s; | 143 | tb->hash[h1] = VACANTK; |
144 | tb->nuse--; | ||
126 | } | 145 | } |
127 | 146 | ||
128 | 147 | ||
@@ -130,22 +149,39 @@ static TString *newshrstr (lua_State *L, const char *str, size_t l, | |||
130 | ** checks whether short string exists and reuses it or creates a new one | 149 | ** checks whether short string exists and reuses it or creates a new one |
131 | */ | 150 | */ |
132 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { | 151 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { |
133 | GCObject *o; | 152 | TString *ts; |
134 | global_State *g = G(L); | 153 | unsigned int hash = luaS_hash(str, l, G(L)->seed); |
135 | unsigned int h = luaS_hash(str, l, g->seed); | 154 | stringtable *tb = &G(L)->strt; |
136 | for (o = g->strt.hash[lmod(h, g->strt.size)]; | 155 | int vacant = -1; |
137 | o != NULL; | 156 | int h1; |
138 | o = gch(o)->next) { | 157 | if (tb->empty <= 0) |
139 | TString *ts = rawgco2ts(o); | 158 | rehash(L, tb); |
140 | if (h == ts->tsv.hash && | 159 | h1 = lmod(hash, tb->size); /* previous call can changed 'size' */ |
141 | l == ts->tsv.len && | 160 | while ((ts = tb->hash[h1]) != NULL) { /* search the string in hash table */ |
142 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { | 161 | if (ts == VACANTK) { |
143 | if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ | 162 | if (vacant < 0) vacant = h1; /* keep track of a vacant place */ |
144 | changewhite(o); /* resurrect it */ | 163 | } |
145 | return ts; | 164 | else if (l == ts->tsv.len && |
165 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { | ||
166 | /* found! */ | ||
167 | if (isdead(G(L), obj2gco(ts))) /* dead (but was not collected yet)? */ | ||
168 | changewhite(obj2gco(ts)); /* resurrect it */ | ||
169 | if (vacant >= 0) { /* is there a better place for this string? */ | ||
170 | tb->hash[vacant] = ts; /* move it up the line */ | ||
171 | tb->hash[h1] = VACANTK; | ||
172 | } | ||
173 | return ts; /* found */ | ||
146 | } | 174 | } |
175 | h1 = h2(h1, hash, tb->size); | ||
147 | } | 176 | } |
148 | return newshrstr(L, str, l, h); /* not found; create a new string */ | 177 | ts = createstrobj(L, str, l, LUA_TSHRSTR, hash); |
178 | tb->nuse++; | ||
179 | if (vacant < 0) /* found no vacant place? */ | ||
180 | tb->empty--; /* will have to use the empty place */ | ||
181 | else | ||
182 | h1 = vacant; /* insert string into vacant place */ | ||
183 | tb->hash[h1] = ts; | ||
184 | return ts; | ||
149 | } | 185 | } |
150 | 186 | ||
151 | 187 | ||
@@ -158,7 +194,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | |||
158 | else { | 194 | else { |
159 | if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char)) | 195 | if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char)) |
160 | luaM_toobig(L); | 196 | luaM_toobig(L); |
161 | return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL); | 197 | return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed); |
162 | } | 198 | } |
163 | } | 199 | } |
164 | 200 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.h,v 1.49 2012/02/01 21:57:15 roberto Exp roberto $ | 2 | ** $Id: lstring.h,v 1.50 2013/08/16 18:55:49 roberto Exp roberto $ |
3 | ** String table (keep all strings handled by Lua) | 3 | ** String table (keep all strings handled by Lua) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -38,6 +38,7 @@ LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); | |||
38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); | 38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); |
39 | LUAI_FUNC int luaS_eqstr (TString *a, TString *b); | 39 | LUAI_FUNC int luaS_eqstr (TString *a, TString *b); |
40 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); | 40 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); |
41 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); | ||
41 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); | 42 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); |
42 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); | 43 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); |
43 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); | 44 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltests.c,v 2.145 2013/08/19 14:16:33 roberto Exp roberto $ | 2 | ** $Id: ltests.c,v 2.146 2013/08/20 17:46:34 roberto Exp roberto $ |
3 | ** Internal Module for Debugging of the Lua Implementation | 3 | ** Internal Module for Debugging of the Lua Implementation |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -661,7 +661,7 @@ static int gc_local (lua_State *L) { | |||
661 | 661 | ||
662 | static int gc_state (lua_State *L) { | 662 | static int gc_state (lua_State *L) { |
663 | static const char *statenames[] = {"propagate", "atomic", | 663 | static const char *statenames[] = {"propagate", "atomic", |
664 | "sweepstring", "sweepudata", "sweep", "pause", ""}; | 664 | "sweepudata", "sweep", "pause", ""}; |
665 | int option = luaL_checkoption(L, 1, "", statenames); | 665 | int option = luaL_checkoption(L, 1, "", statenames); |
666 | if (option == GCSpause + 1) { | 666 | if (option == GCSpause + 1) { |
667 | lua_pushstring(L, statenames[G(L)->gcstate]); | 667 | lua_pushstring(L, statenames[G(L)->gcstate]); |
@@ -742,22 +742,18 @@ static int table_query (lua_State *L) { | |||
742 | static int string_query (lua_State *L) { | 742 | static int string_query (lua_State *L) { |
743 | stringtable *tb = &G(L)->strt; | 743 | stringtable *tb = &G(L)->strt; |
744 | int s = luaL_optint(L, 2, 0) - 1; | 744 | int s = luaL_optint(L, 2, 0) - 1; |
745 | if (s==-1) { | 745 | if (s < 0) { |
746 | lua_pushinteger(L ,tb->nuse); | 746 | lua_pushinteger(L ,tb->nuse); |
747 | lua_pushinteger(L ,tb->size); | 747 | lua_pushinteger(L ,tb->size); |
748 | return 2; | 748 | return 2; |
749 | } | 749 | } |
750 | else if (s < tb->size) { | 750 | else if ((unsigned int)s < tb->size) { |
751 | GCObject *ts; | 751 | TString *ts = tb->hash[s]; |
752 | int n = 0; | 752 | setsvalue2s(L, L->top, ts); |
753 | for (ts = tb->hash[s]; ts; ts = gch(ts)->next) { | 753 | api_incr_top(L); |
754 | setsvalue2s(L, L->top, rawgco2ts(ts)); | 754 | return 1; |
755 | api_incr_top(L); | ||
756 | n++; | ||
757 | } | ||
758 | return n; | ||
759 | } | 755 | } |
760 | return 0; | 756 | else return 0; |
761 | } | 757 | } |
762 | 758 | ||
763 | 759 | ||