|
6 | 6 | #include "lauxlib.h"
|
7 | 7 | #include "sproto.h"
|
8 | 8 |
|
| 9 | +#define MAX_GLOBALSPROTO 16 |
9 | 10 | #define ENCODE_BUFFERSIZE 2050
|
10 | 11 |
|
11 | 12 | //#define ENCODE_BUFFERSIZE 2050
|
@@ -458,27 +459,46 @@ lprotocol(lua_State *L) {
|
458 | 459 | }
|
459 | 460 |
|
460 | 461 | /* global sproto pointer for multi states */
|
461 |
| -static void * G_sproto = NULL; |
462 |
| -static size_t G_sproto_sz = 0; |
| 462 | +struct sproto_bin { |
| 463 | + void *ptr; |
| 464 | + size_t sz; |
| 465 | +}; |
| 466 | + |
| 467 | +static struct sproto_bin G_sproto[MAX_GLOBALSPROTO]; |
463 | 468 |
|
464 | 469 | static int
|
465 | 470 | lsaveproto(lua_State *L) {
|
466 | 471 | size_t sz;
|
467 | 472 | void * buffer = (void *)luaL_checklstring(L,1,&sz);
|
468 |
| - void * tmp = malloc(sz); |
| 473 | + int index = luaL_optinteger(L, 2, 0); |
| 474 | + void * tmp; |
| 475 | + struct sproto_bin * sbin = &G_sproto[index]; |
| 476 | + if (index < 0 || index >= MAX_GLOBALSPROTO) { |
| 477 | + return luaL_error(L, "Invalid global slot index %d", index); |
| 478 | + } |
| 479 | + tmp = malloc(sz); |
469 | 480 | memcpy(tmp, buffer, sz);
|
470 |
| - if (G_sproto) { |
471 |
| - free(G_sproto); |
| 481 | + if (sbin->ptr) { |
| 482 | + free(sbin->ptr); |
472 | 483 | }
|
473 |
| - G_sproto = tmp; |
474 |
| - G_sproto_sz = sz; |
| 484 | + sbin->ptr = tmp; |
| 485 | + sbin->sz = sz; |
475 | 486 | return 0;
|
476 | 487 | }
|
477 | 488 |
|
478 | 489 | static int
|
479 | 490 | lloadproto(lua_State *L) {
|
480 |
| - lua_pushlightuserdata(L, G_sproto); |
481 |
| - lua_pushinteger(L, G_sproto_sz); |
| 491 | + int index = luaL_optinteger(L, 1, 0); |
| 492 | + struct sproto_bin * sbin = &G_sproto[index]; |
| 493 | + if (index < 0 || index >= MAX_GLOBALSPROTO) { |
| 494 | + return luaL_error(L, "Invalid global slot index %d", index); |
| 495 | + } |
| 496 | + if (sbin->ptr == NULL) { |
| 497 | + return luaL_error(L, "nil sproto at index %d", index); |
| 498 | + } |
| 499 | + |
| 500 | + lua_pushlightuserdata(L, sbin->ptr); |
| 501 | + lua_pushinteger(L, sbin->sz); |
482 | 502 | return 2;
|
483 | 503 | }
|
484 | 504 |
|
|
0 commit comments