use sign n for SSM

This commit is contained in:
Cloud Wu
2019-04-02 19:32:44 +08:00
parent 56d24f46d6
commit f8d55affba
2 changed files with 24 additions and 24 deletions

View File

@@ -281,10 +281,10 @@ struct shrmap_slot {
struct shrmap { struct shrmap {
struct rwlock lock; struct rwlock lock;
unsigned int n; int n;
unsigned int mask; int mask;
unsigned int total; int total;
unsigned int roslots; int roslots;
struct shrmap_slot * readwrite; struct shrmap_slot * readwrite;
struct shrmap_slot * readonly; struct shrmap_slot * readonly;
}; };
@@ -292,8 +292,8 @@ struct shrmap {
static struct shrmap SSM; static struct shrmap SSM;
static struct shrmap_slot * static struct shrmap_slot *
shrstr_newpage(unsigned int sz) { shrstr_newpage(int sz) {
unsigned int i; int i;
struct shrmap_slot * s = (struct shrmap_slot *)malloc(sz * sizeof(*s)); struct shrmap_slot * s = (struct shrmap_slot *)malloc(sz * sizeof(*s));
for (i=0;i<sz;i++) { for (i=0;i<sz;i++) {
rwlock_init(&s[i].lock); rwlock_init(&s[i].lock);
@@ -303,9 +303,9 @@ shrstr_newpage(unsigned int sz) {
} }
static void static void
shrstr_deletepage(struct shrmap_slot *s, unsigned int sz) { shrstr_deletepage(struct shrmap_slot *s, int sz) {
if (s) { if (s) {
unsigned int i; int i;
for (i=0;i<sz;i++) { for (i=0;i<sz;i++) {
TString *str = s[i].str; TString *str = s[i].str;
while (str) { while (str) {
@@ -319,7 +319,7 @@ shrstr_deletepage(struct shrmap_slot *s, unsigned int sz) {
} }
static int static int
shrstr_allocpage(struct shrmap * s, unsigned int osz, unsigned int sz, struct shrmap_slot * newpage) { shrstr_allocpage(struct shrmap * s, int osz, int sz, struct shrmap_slot * newpage) {
if (s->readonly != NULL) if (s->readonly != NULL)
return 0; return 0;
if ((s->mask + 1) != osz) if ((s->mask + 1) != osz)
@@ -333,13 +333,13 @@ shrstr_allocpage(struct shrmap * s, unsigned int osz, unsigned int sz, struct sh
} }
static void static void
shrstr_rehash(struct shrmap *s, unsigned int slotid) { shrstr_rehash(struct shrmap *s, int slotid) {
struct shrmap_slot *slot = &s->readonly[slotid]; struct shrmap_slot *slot = &s->readonly[slotid];
rwlock_wlock(&slot->lock); rwlock_wlock(&slot->lock);
TString *str = slot->str; TString *str = slot->str;
while (str) { while (str) {
TString * next = str->u.hnext; TString * next = str->u.hnext;
unsigned int newslotid = str->hash & s->mask; int newslotid = str->hash & s->mask;
struct shrmap_slot *newslot = &s->readwrite[newslotid]; struct shrmap_slot *newslot = &s->readwrite[newslotid];
rwlock_wlock(&newslot->lock); rwlock_wlock(&newslot->lock);
str->u.hnext = newslot->str; str->u.hnext = newslot->str;
@@ -361,14 +361,14 @@ shrstr_rehash(struct shrmap *s, unsigned int slotid) {
6. remove temporary readonly (writelock SSM) 6. remove temporary readonly (writelock SSM)
*/ */
static void static void
shrstr_expandpage(unsigned int cap) { shrstr_expandpage(int cap) {
struct shrmap * s = &SSM; struct shrmap * s = &SSM;
if (s->readonly) if (s->readonly)
return; return;
unsigned int osz = s->mask + 1; int osz = s->mask + 1;
unsigned int sz = osz * 2; int sz = osz * 2;
// overflow check // overflow check
if (sz == 0) if (sz <= 0)
return; return;
while (sz < cap) while (sz < cap)
sz = sz * 2; sz = sz * 2;
@@ -380,7 +380,7 @@ shrstr_expandpage(unsigned int cap) {
shrstr_deletepage(newpage, sz); shrstr_deletepage(newpage, sz);
return; return;
} }
unsigned int i; int i;
for (i=0;i<osz;i++) { for (i=0;i<osz;i++) {
shrstr_rehash(s, i); shrstr_rehash(s, i);
} }
@@ -406,7 +406,7 @@ LUA_API void
luaS_exitshr() { luaS_exitshr() {
struct shrmap * s = &SSM; struct shrmap * s = &SSM;
rwlock_wlock(&s->lock); rwlock_wlock(&s->lock);
unsigned int sz = s->mask + 1; int sz = s->mask + 1;
shrstr_deletepage(s->readwrite, sz); shrstr_deletepage(s->readwrite, sz);
shrstr_deletepage(s->readonly, s->roslots); shrstr_deletepage(s->readonly, s->roslots);
s->readwrite = NULL; s->readwrite = NULL;
@@ -451,7 +451,7 @@ query_string(TString *t, unsigned int h, const char *str, lu_byte l) {
ts = find_string(t, slot, h, str, l); ts = find_string(t, slot, h, str, l);
rwlock_runlock(&slot->lock); rwlock_runlock(&slot->lock);
if (ts == NULL && s->readonly != NULL) { if (ts == NULL && s->readonly != NULL) {
unsigned int mask = s->roslots - 1; int mask = s->roslots - 1;
slot = &s->readonly[h & mask]; slot = &s->readonly[h & mask];
rwlock_rlock(&slot->lock); rwlock_rlock(&slot->lock);
ts = find_string(t, slot, h, str, l); ts = find_string(t, slot, h, str, l);
@@ -542,11 +542,11 @@ internshrstr (lua_State *L, const char *str, size_t l) {
} }
LUA_API void LUA_API void
luaS_expandshr(unsigned int n) { luaS_expandshr(int n) {
struct shrmap * s = &SSM; struct shrmap * s = &SSM;
if (s->n < n) { if (s->n < n) {
ATOM_ADD(&s->n, n); ATOM_ADD(&s->n, n);
unsigned int t = (s->total + s->n) * 5 / 4; int t = (s->total + s->n) * 5 / 4;
if (t > s->mask) { if (t > s->mask) {
shrstr_expandpage(t); shrstr_expandpage(t);
} }
@@ -622,10 +622,10 @@ luaS_shrinfo(lua_State *L) {
memset(&total, 0, sizeof(total)); memset(&total, 0, sizeof(total));
struct shrmap * s = &SSM; struct shrmap * s = &SSM;
struct variance v = { 0,0,0 }; struct variance v = { 0,0,0 };
unsigned int slots = 0; int slots = 0;
rwlock_rlock(&s->lock); rwlock_rlock(&s->lock);
unsigned int i; int i;
unsigned int sz = s->mask + 1; int sz = s->mask + 1;
for (i=0;i<sz;i++) { for (i=0;i<sz;i++) {
struct shrmap_slot *slot = &s->readwrite[i]; struct shrmap_slot *slot = &s->readwrite[i];
getslot(slot, &tmp); getslot(slot, &tmp);

View File

@@ -49,7 +49,7 @@ LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l);
LUA_API void luaS_initshr(); LUA_API void luaS_initshr();
LUA_API void luaS_exitshr(); LUA_API void luaS_exitshr();
LUA_API void luaS_expandshr(unsigned int n); LUA_API void luaS_expandshr(int n);
LUAI_FUNC TString *luaS_clonestring(lua_State *L, TString *); LUAI_FUNC TString *luaS_clonestring(lua_State *L, TString *);
LUA_API int luaS_shrinfo(lua_State *L); LUA_API int luaS_shrinfo(lua_State *L);