Browse Source

!4 fix num变量冲突

tags/v1.1.0
微希夷 yitter 4 years ago
parent
commit
60854e5a2e
2 changed files with 18 additions and 19 deletions
  1. +5
    -4
      PHP/config.w32
  2. +13
    -15
      PHP/snowdrift.c

+ 5
- 4
PHP/config.w32 View File

@@ -8,10 +8,11 @@
ARG_ENABLE("snowdrift", "enable snowdrift support", "no"); ARG_ENABLE("snowdrift", "enable snowdrift support", "no");


if (PHP_SNOWDRIFT != "no") { if (PHP_SNOWDRIFT != "no") {
THIS_DIR=`dirname $0`
snowdrift_source_file="snowdrift.c\
$THIS_DIR/src/snowflake/snowflake.c
"
snowdrift_source_file="snowdrift.c\
src/snowflake/snowflake.c\
src/snowflake/shm.c\
src/snowflake/spinlock.c
"
EXTENSION("snowdrift", $snowdrift_source_file, PHP_EXTNAME_SHARED, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1"); EXTENSION("snowdrift", $snowdrift_source_file, PHP_EXTNAME_SHARED, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
} }



+ 13
- 15
PHP/snowdrift.c View File

@@ -35,7 +35,7 @@ static struct shm shmctx;
static snowflake *sf; static snowflake *sf;
zend_class_entry snowdrift_ce; zend_class_entry snowdrift_ce;


uint8_t num = 0;
static uint8_t wid_num = 0;


/* {{{ PHP_INI */ /* {{{ PHP_INI */


@@ -43,7 +43,7 @@ PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("snowdrift.Method", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, Method, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.Method", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, Method, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.BaseTime", "1582136402000", PHP_INI_SYSTEM, OnUpdateLongGEZero, BaseTime, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.BaseTime", "1582136402000", PHP_INI_SYSTEM, OnUpdateLongGEZero, BaseTime, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.WorkerId", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerId, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.WorkerId", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerId, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.WorkerIdNum", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerIdNum, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.WorkerIdNum", "63", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerIdNum, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.WorkerIdBitLength", "6", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerIdBitLength, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.WorkerIdBitLength", "6", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerIdBitLength, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.SeqBitLength", "6", PHP_INI_SYSTEM, OnUpdateLongGEZero, SeqBitLength, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.SeqBitLength", "6", PHP_INI_SYSTEM, OnUpdateLongGEZero, SeqBitLength, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.MaxSeqNumber", "0", PHP_INI_SYSTEM, OnUpdateLongGEZero, MaxSeqNumber, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.MaxSeqNumber", "0", PHP_INI_SYSTEM, OnUpdateLongGEZero, MaxSeqNumber, zend_snowdrift_globals, snowdrift_globals)
@@ -55,25 +55,23 @@ PHP_INI_END()


static int snowdrift_init() static int snowdrift_init()
{ {
num = (-1L << SD_G(WorkerIdBitLength)) ^ -1L;
if (SD_G(WorkerIdNum) < num)
wid_num = (-1L << SD_G(WorkerIdBitLength)) ^ -1L;
if (SD_G(WorkerIdNum) < wid_num)
{ {
num = SD_G(WorkerIdNum);
wid_num = SD_G(WorkerIdNum);
} }
shmctx.size = num * sizeof(snowflake);
shmctx.size = wid_num * sizeof(snowflake);
if (shm_alloc(&shmctx) == -1) if (shm_alloc(&shmctx) == -1)
{ {
zend_throw_exception_ex(NULL, 0, "shared memory malloc failed");
return FAILURE; return FAILURE;
} }
if (SD_G(MaxSeqNumber) <= SD_G(MinSeqNumber))
if (SD_G(MaxSeqNumber) < SD_G(MinSeqNumber))
{ {
zend_throw_exception_ex(NULL, 0, "MaxSeqNumber must GE then MinSeqNumber");
return FAILURE; return FAILURE;
} }
bzero(shmctx.addr, num * sizeof(snowflake));
bzero(shmctx.addr, wid_num * sizeof(snowflake));
sf = (snowflake *)shmctx.addr; sf = (snowflake *)shmctx.addr;
for (int i = 0; i < num; i++)
for (int i = 0; i < wid_num; i++)
{ {
snowflake *tmp = (sf + i); snowflake *tmp = (sf + i);
tmp->Method = SD_G(Method); tmp->Method = SD_G(Method);
@@ -97,9 +95,9 @@ PHP_METHOD(snowdrift, NextId)
RETURN_FALSE; RETURN_FALSE;
} }
wid--; wid--;
if (wid < 0 || wid > num - 1)
if (wid < 0 || wid > wid_num)
{ {
zend_throw_exception_ex(NULL, 0, "wid error! wid between 0 and %d", num - 1);
zend_throw_exception_ex(NULL, 0, "wid error! wid between 1 and %d", wid_num);
RETURN_NULL(); RETURN_NULL();
} }
snowflake *flake = (sf + wid); snowflake *flake = (sf + wid);
@@ -117,9 +115,9 @@ PHP_METHOD(snowdrift, NextNumId)
RETURN_FALSE; RETURN_FALSE;
} }
wid--; wid--;
if (wid < 0 || wid > num - 1)
if (wid < 0 || wid > wid_num)
{ {
zend_throw_exception_ex(NULL, 0, "wid error! wid between 0 and %d", num - 1);
zend_throw_exception_ex(NULL, 0, "wid error! wid between 1 and %d", wid_num);
RETURN_NULL(); RETURN_NULL();
} }
snowflake *flake = (sf + wid); snowflake *flake = (sf + wid);


Loading…
Cancel
Save