Skip to content

Commit 3e66f5f

Browse files
committed
some code style fix & use selfdefine type constants instead of abusing Lua's types
1 parent cd483ec commit 3e66f5f

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

src/ngx_http_lua_shdict.c

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static ngx_inline ngx_shm_zone_t *ngx_http_lua_shdict_get_zone(lua_State *L,
5151
#define NGX_HTTP_LUA_SHDICT_ADD 0x0001
5252
#define NGX_HTTP_LUA_SHDICT_REPLACE 0x0002
5353
#define NGX_HTTP_LUA_SHDICT_SAFE_STORE 0x0004
54+
5455
#define NGX_HTTP_LUA_SHDICT_LEFT 0x0001
5556
#define NGX_HTTP_LUA_SHDICT_RIGHT 0x0002
5657

@@ -59,6 +60,14 @@ enum {
5960
SHDICT_USERDATA_INDEX = 1,
6061
};
6162

63+
enum {
64+
SHDICT_TNIL = 0, // same as LUA_TNIL
65+
SHDICT_TBOOLEAN = 1, // same as LUA_TBOOLEAN
66+
SHDICT_TNUMBER = 3, // same as LUA_TNUMBER
67+
SHDICT_TSTRING = 4, // same as LUA_TSTRING
68+
SHDICT_TLIST = 5,
69+
};
70+
6271

6372
#define ngx_http_lua_shdict_get_list_head(sd, key_len) \
6473
(ngx_queue_t *) ngx_align_ptr(((u_char *) &sd->data + key_len), \
@@ -305,7 +314,7 @@ ngx_http_lua_shdict_expire(ngx_http_lua_shdict_ctx_t *ctx, ngx_uint_t n)
305314
}
306315
}
307316

308-
if (sd->value_type == LUA_TTABLE) {
317+
if (sd->value_type == SHDICT_TLIST) {
309318

310319
list_queue = ngx_http_lua_shdict_get_list_head(sd, sd->key_len);
311320

@@ -553,12 +562,12 @@ ngx_http_lua_shdict_get_helper(lua_State *L, int get_stale)
553562

554563
switch (value_type) {
555564

556-
case LUA_TSTRING:
565+
case SHDICT_TSTRING:
557566

558567
lua_pushlstring(L, (char *) value.data, value.len);
559568
break;
560569

561-
case LUA_TNUMBER:
570+
case SHDICT_TNUMBER:
562571

563572
if (value.len != sizeof(double)) {
564573

@@ -574,7 +583,7 @@ ngx_http_lua_shdict_get_helper(lua_State *L, int get_stale)
574583
lua_pushnumber(L, num);
575584
break;
576585

577-
case LUA_TBOOLEAN:
586+
case SHDICT_TBOOLEAN:
578587

579588
if (value.len != sizeof(u_char)) {
580589

@@ -590,7 +599,7 @@ ngx_http_lua_shdict_get_helper(lua_State *L, int get_stale)
590599
lua_pushboolean(L, c ? 1 : 0);
591600
break;
592601

593-
case LUA_TTABLE:
602+
case SHDICT_TLIST:
594603

595604
ngx_shmtx_unlock(&ctx->shpool->mutex);
596605

@@ -750,7 +759,7 @@ ngx_http_lua_shdict_flush_expired(lua_State *L)
750759

751760
if (sd->expires != 0 && sd->expires <= now) {
752761

753-
if (sd->value_type == LUA_TTABLE) {
762+
if (sd->value_type == SHDICT_TLIST) {
754763

755764
list_queue = ngx_http_lua_shdict_get_list_head(sd, sd->key_len);
756765

@@ -991,17 +1000,17 @@ ngx_http_lua_shdict_set_helper(lua_State *L, int flags)
9911000

9921001
switch (value_type) {
9931002

994-
case LUA_TSTRING:
1003+
case SHDICT_TSTRING:
9951004
value.data = (u_char *) lua_tolstring(L, 3, &value.len);
9961005
break;
9971006

998-
case LUA_TNUMBER:
1007+
case SHDICT_TNUMBER:
9991008
value.len = sizeof(double);
10001009
num = lua_tonumber(L, 3);
10011010
value.data = (u_char *) &num;
10021011
break;
10031012

1004-
case LUA_TBOOLEAN:
1013+
case SHDICT_TBOOLEAN:
10051014
value.len = sizeof(u_char);
10061015
c = lua_toboolean(L, 3) ? 1 : 0;
10071016
value.data = &c;
@@ -1094,7 +1103,7 @@ ngx_http_lua_shdict_set_helper(lua_State *L, int flags)
10941103

10951104
if (value.data
10961105
&& value.len == (size_t) sd->value_len
1097-
&& sd->value_type != LUA_TTABLE)
1106+
&& sd->value_type != SHDICT_TLIST)
10981107
{
10991108

11001109
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
@@ -1140,7 +1149,7 @@ ngx_http_lua_shdict_set_helper(lua_State *L, int flags)
11401149

11411150
remove:
11421151

1143-
if (sd->value_type == LUA_TTABLE) {
1152+
if (sd->value_type == SHDICT_TLIST) {
11441153
queue = ngx_http_lua_shdict_get_list_head(sd, key.len);
11451154

11461155
for (q = ngx_queue_head(queue);
@@ -1342,7 +1351,7 @@ ngx_http_lua_shdict_incr(lua_State *L)
13421351

13431352
/* rc == NGX_OK */
13441353

1345-
if (sd->value_type != LUA_TNUMBER || sd->value_len != sizeof(double)) {
1354+
if (sd->value_type != SHDICT_TNUMBER || sd->value_len != sizeof(double)) {
13461355
ngx_shmtx_unlock(&ctx->shpool->mutex);
13471356

13481357
lua_pushnil(L);
@@ -1412,7 +1421,7 @@ ngx_http_lua_shared_dict_get(ngx_shm_zone_t *zone, u_char *key_data,
14121421

14131422
switch (value->type) {
14141423

1415-
case LUA_TSTRING:
1424+
case SHDICT_TSTRING:
14161425

14171426
if (value->value.s.data == NULL || value->value.s.len == 0) {
14181427
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "no string buffer "
@@ -1431,7 +1440,7 @@ ngx_http_lua_shared_dict_get(ngx_shm_zone_t *zone, u_char *key_data,
14311440
ngx_memcpy(value->value.s.data, data, len);
14321441
break;
14331442

1434-
case LUA_TNUMBER:
1443+
case SHDICT_TNUMBER:
14351444

14361445
if (len != sizeof(double)) {
14371446
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "bad lua number "
@@ -1445,7 +1454,7 @@ ngx_http_lua_shared_dict_get(ngx_shm_zone_t *zone, u_char *key_data,
14451454
ngx_memcpy(&value->value.b, data, len);
14461455
break;
14471456

1448-
case LUA_TBOOLEAN:
1457+
case SHDICT_TBOOLEAN:
14491458

14501459
if (len != sizeof(u_char)) {
14511460
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "bad lua boolean "
@@ -1548,11 +1557,11 @@ ngx_http_lua_shdict_push_helper(lua_State *L, int flags)
15481557

15491558
switch (value_type) {
15501559

1551-
case LUA_TSTRING:
1560+
case SHDICT_TSTRING:
15521561
value.data = (u_char *) lua_tolstring(L, 3, &value.len);
15531562
break;
15541563

1555-
case LUA_TNUMBER:
1564+
case SHDICT_TNUMBER:
15561565
value.len = sizeof(double);
15571566
num = lua_tonumber(L, 3);
15581567
value.data = (u_char *) &num;
@@ -1577,7 +1586,7 @@ ngx_http_lua_shdict_push_helper(lua_State *L, int flags)
15771586
if (rc == NGX_DONE) {
15781587
/* exists but expired */
15791588

1580-
if (sd->value_type != LUA_TTABLE) {
1589+
if (sd->value_type != SHDICT_TLIST) {
15811590
/* TODO: reuse when length matched */
15821591

15831592
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
@@ -1626,7 +1635,7 @@ ngx_http_lua_shdict_push_helper(lua_State *L, int flags)
16261635

16271636
} else if (rc == NGX_OK) {
16281637

1629-
if (sd->value_type != LUA_TTABLE) {
1638+
if (sd->value_type != SHDICT_TLIST) {
16301639
ngx_shmtx_unlock(&ctx->shpool->mutex);
16311640

16321641
lua_pushnil(L);
@@ -1683,9 +1692,9 @@ ngx_http_lua_shdict_push_helper(lua_State *L, int flags)
16831692

16841693
sd->value_len = 0;
16851694

1686-
dd("setting value type to %d", (int) LUA_TTABLE);
1695+
dd("setting value type to %d", (int) SHDICT_TLIST);
16871696

1688-
sd->value_type = (uint8_t) LUA_TTABLE;
1697+
sd->value_type = (uint8_t) SHDICT_TLIST;
16891698

16901699
ngx_memcpy(sd->data, key.data, key.len);
16911700

@@ -1851,7 +1860,7 @@ ngx_http_lua_shdict_pop_helper(lua_State *L, int flags)
18511860

18521861
/* rc == NGX_OK */
18531862

1854-
if (sd->value_type != LUA_TTABLE) {
1863+
if (sd->value_type != SHDICT_TLIST) {
18551864
ngx_shmtx_unlock(&ctx->shpool->mutex);
18561865

18571866
lua_pushnil(L);
@@ -1888,12 +1897,12 @@ ngx_http_lua_shdict_pop_helper(lua_State *L, int flags)
18881897

18891898
switch (value_type) {
18901899

1891-
case LUA_TSTRING:
1900+
case SHDICT_TSTRING:
18921901

18931902
lua_pushlstring(L, (char *) value.data, value.len);
18941903
break;
18951904

1896-
case LUA_TNUMBER:
1905+
case SHDICT_TNUMBER:
18971906

18981907
if (value.len != sizeof(double)) {
18991908

@@ -2014,7 +2023,7 @@ ngx_http_lua_shdict_llen(lua_State *L)
20142023

20152024
if (rc == NGX_OK) {
20162025

2017-
if (sd->value_type != LUA_TTABLE) {
2026+
if (sd->value_type != SHDICT_TLIST) {
20182027
ngx_shmtx_unlock(&ctx->shpool->mutex);
20192028

20202029
lua_pushnil(L);
@@ -2200,17 +2209,17 @@ ngx_http_lua_ffi_shdict_store(ngx_shm_zone_t *zone, int op, u_char *key,
22002209

22012210
switch (value_type) {
22022211

2203-
case LUA_TSTRING:
2212+
case SHDICT_TSTRING:
22042213
/* do nothing */
22052214
break;
22062215

2207-
case LUA_TNUMBER:
2216+
case SHDICT_TNUMBER:
22082217
dd("num value: %lf", num_value);
22092218
str_value_buf = (u_char *) &num_value;
22102219
str_value_len = sizeof(double);
22112220
break;
22122221

2213-
case LUA_TBOOLEAN:
2222+
case SHDICT_TBOOLEAN:
22142223
c = num_value ? 1 : 0;
22152224
str_value_buf = &c;
22162225
str_value_len = sizeof(u_char);
@@ -2285,7 +2294,7 @@ ngx_http_lua_ffi_shdict_store(ngx_shm_zone_t *zone, int op, u_char *key,
22852294

22862295
if (str_value_buf
22872296
&& str_value_len == (size_t) sd->value_len
2288-
&& sd->value_type != LUA_TTABLE)
2297+
&& sd->value_type != SHDICT_TLIST)
22892298
{
22902299

22912300
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
@@ -2328,7 +2337,7 @@ ngx_http_lua_ffi_shdict_store(ngx_shm_zone_t *zone, int op, u_char *key,
23282337

23292338
remove:
23302339

2331-
if (sd->value_type == LUA_TTABLE) {
2340+
if (sd->value_type == SHDICT_TLIST) {
23322341

23332342
queue = ngx_http_lua_shdict_get_list_head(sd, key_len);
23342343

@@ -2496,12 +2505,12 @@ ngx_http_lua_ffi_shdict_get(ngx_shm_zone_t *zone, u_char *key,
24962505
value.len = (size_t) sd->value_len;
24972506

24982507
if (*str_value_len < (size_t) value.len) {
2499-
if (*value_type == LUA_TBOOLEAN) {
2508+
if (*value_type == SHDICT_TBOOLEAN) {
25002509
ngx_shmtx_unlock(&ctx->shpool->mutex);
25012510
return NGX_ERROR;
25022511
}
25032512

2504-
if (*value_type == LUA_TSTRING) {
2513+
if (*value_type == SHDICT_TSTRING) {
25052514
*str_value_buf = malloc(value.len);
25062515
if (*str_value_buf == NULL) {
25072516
ngx_shmtx_unlock(&ctx->shpool->mutex);
@@ -2512,12 +2521,12 @@ ngx_http_lua_ffi_shdict_get(ngx_shm_zone_t *zone, u_char *key,
25122521

25132522
switch (*value_type) {
25142523

2515-
case LUA_TSTRING:
2524+
case SHDICT_TSTRING:
25162525
*str_value_len = value.len;
25172526
ngx_memcpy(*str_value_buf, value.data, value.len);
25182527
break;
25192528

2520-
case LUA_TNUMBER:
2529+
case SHDICT_TNUMBER:
25212530

25222531
if (value.len != sizeof(double)) {
25232532
ngx_shmtx_unlock(&ctx->shpool->mutex);
@@ -2532,7 +2541,7 @@ ngx_http_lua_ffi_shdict_get(ngx_shm_zone_t *zone, u_char *key,
25322541
ngx_memcpy(num_value, value.data, sizeof(double));
25332542
break;
25342543

2535-
case LUA_TBOOLEAN:
2544+
case SHDICT_TBOOLEAN:
25362545

25372546
if (value.len != sizeof(u_char)) {
25382547
ngx_shmtx_unlock(&ctx->shpool->mutex);
@@ -2606,7 +2615,7 @@ ngx_http_lua_ffi_shdict_incr(ngx_shm_zone_t *zone, u_char *key,
26062615

26072616
/* rc == NGX_OK */
26082617

2609-
if (sd->value_type != LUA_TNUMBER || sd->value_len != sizeof(double)) {
2618+
if (sd->value_type != SHDICT_TNUMBER || sd->value_len != sizeof(double)) {
26102619
ngx_shmtx_unlock(&ctx->shpool->mutex);
26112620
*err = "not a number";
26122621
return NGX_ERROR;

0 commit comments

Comments
 (0)