diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-07-18 10:36:14 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-07-18 10:36:14 -0300 |
commit | ca41b43f53562e64abe433d6346d174c92548603 (patch) | |
tree | 03f7a99f76359fc1e0bbc45fc13e579ff2aafabb /lstring.c | |
parent | 3511e186cde4b78f268d17199d0f46fb3eaa9638 (diff) | |
download | lua-ca41b43f53562e64abe433d6346d174c92548603.tar.gz lua-ca41b43f53562e64abe433d6346d174c92548603.tar.bz2 lua-ca41b43f53562e64abe433d6346d174c92548603.zip |
type 'TString' refers directly to the structure inside the union
(union used only for size purposes)
Diffstat (limited to 'lstring.c')
-rw-r--r-- | lstring.c | 44 |
1 files changed, 22 insertions, 22 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 2.40 2014/06/18 22:59:29 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 2.41 2014/07/18 12:17:54 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 | */ |
@@ -34,10 +34,10 @@ | |||
34 | ** equality for long strings | 34 | ** equality for long strings |
35 | */ | 35 | */ |
36 | int luaS_eqlngstr (TString *a, TString *b) { | 36 | int luaS_eqlngstr (TString *a, TString *b) { |
37 | size_t len = a->tsv.len; | 37 | size_t len = a->len; |
38 | lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR); | 38 | lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR); |
39 | return (a == b) || /* same instance or... */ | 39 | return (a == b) || /* same instance or... */ |
40 | ((len == b->tsv.len) && /* equal length and ... */ | 40 | ((len == b->len) && /* equal length and ... */ |
41 | (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ | 41 | (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ |
42 | } | 42 | } |
43 | 43 | ||
@@ -67,9 +67,9 @@ void luaS_resize (lua_State *L, int newsize) { | |||
67 | TString *p = tb->hash[i]; | 67 | TString *p = tb->hash[i]; |
68 | tb->hash[i] = NULL; | 68 | tb->hash[i] = NULL; |
69 | while (p) { /* for each node in the list */ | 69 | while (p) { /* for each node in the list */ |
70 | TString *hnext = p->tsv.hnext; /* save next */ | 70 | TString *hnext = p->hnext; /* save next */ |
71 | unsigned int h = lmod(p->tsv.hash, newsize); /* new position */ | 71 | unsigned int h = lmod(p->hash, newsize); /* new position */ |
72 | p->tsv.hnext = tb->hash[h]; /* chain it */ | 72 | p->hnext = tb->hash[h]; /* chain it */ |
73 | tb->hash[h] = p; | 73 | tb->hash[h] = p; |
74 | p = hnext; | 74 | p = hnext; |
75 | } | 75 | } |
@@ -92,24 +92,24 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l, | |||
92 | TString *ts; | 92 | TString *ts; |
93 | GCObject *o; | 93 | GCObject *o; |
94 | size_t totalsize; /* total size of TString object */ | 94 | size_t totalsize; /* total size of TString object */ |
95 | totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); | 95 | totalsize = sizelstring(l); |
96 | o = luaC_newobj(L, tag, totalsize); | 96 | o = luaC_newobj(L, tag, totalsize); |
97 | ts = rawgco2ts(o); | 97 | ts = gco2ts(o); |
98 | ts->tsv.len = l; | 98 | ts->len = l; |
99 | ts->tsv.hash = h; | 99 | ts->hash = h; |
100 | ts->tsv.extra = 0; | 100 | ts->extra = 0; |
101 | memcpy(ts+1, str, l*sizeof(char)); | 101 | memcpy(getaddrstr(ts), str, l * sizeof(char)); |
102 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ | 102 | getaddrstr(ts)[l] = '\0'; /* ending 0 */ |
103 | return ts; | 103 | return ts; |
104 | } | 104 | } |
105 | 105 | ||
106 | 106 | ||
107 | void luaS_remove (lua_State *L, TString *ts) { | 107 | void luaS_remove (lua_State *L, TString *ts) { |
108 | stringtable *tb = &G(L)->strt; | 108 | stringtable *tb = &G(L)->strt; |
109 | TString **p = &tb->hash[lmod(ts->tsv.hash, tb->size)]; | 109 | TString **p = &tb->hash[lmod(ts->hash, tb->size)]; |
110 | while (*p != ts) /* find previous element */ | 110 | while (*p != ts) /* find previous element */ |
111 | p = &(*p)->tsv.hnext; | 111 | p = &(*p)->hnext; |
112 | *p = (*p)->tsv.hnext; /* remove element from its list */ | 112 | *p = (*p)->hnext; /* remove element from its list */ |
113 | tb->nuse--; | 113 | tb->nuse--; |
114 | } | 114 | } |
115 | 115 | ||
@@ -122,12 +122,12 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { | |||
122 | global_State *g = G(L); | 122 | global_State *g = G(L); |
123 | unsigned int h = luaS_hash(str, l, g->seed); | 123 | unsigned int h = luaS_hash(str, l, g->seed); |
124 | TString **list = &g->strt.hash[lmod(h, g->strt.size)]; | 124 | TString **list = &g->strt.hash[lmod(h, g->strt.size)]; |
125 | for (ts = *list; ts != NULL; ts = ts->tsv.hnext) { | 125 | for (ts = *list; ts != NULL; ts = ts->hnext) { |
126 | if (l == ts->tsv.len && | 126 | if (l == ts->len && |
127 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { | 127 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { |
128 | /* found! */ | 128 | /* found! */ |
129 | if (isdead(g, ts2gco(ts))) /* dead (but not collected yet)? */ | 129 | if (isdead(g, obj2gco(ts))) /* dead (but not collected yet)? */ |
130 | changewhite(ts2gco(ts)); /* resurrect it */ | 130 | changewhite(obj2gco(ts)); /* resurrect it */ |
131 | return ts; | 131 | return ts; |
132 | } | 132 | } |
133 | } | 133 | } |
@@ -136,7 +136,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { | |||
136 | list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ | 136 | list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ |
137 | } | 137 | } |
138 | ts = createstrobj(L, str, l, LUA_TSHRSTR, h); | 138 | ts = createstrobj(L, str, l, LUA_TSHRSTR, h); |
139 | ts->tsv.hnext = *list; | 139 | ts->hnext = *list; |
140 | *list = ts; | 140 | *list = ts; |
141 | g->strt.nuse++; | 141 | g->strt.nuse++; |
142 | return ts; | 142 | return ts; |