Browse Source

ckdb - macro to simplify pointer reallocation

master
kanoi 9 years ago
parent
commit
f1966ae648
  1. 23
      src/ckdb.h
  2. 12
      src/ckdb_cmd.c
  3. 14
      src/ckdb_crypt.c
  4. 10
      src/ckdb_data.c
  5. 47
      src/ckdb_dbio.c

23
src/ckdb.h

@ -55,7 +55,7 @@
#define DB_VLOCK "1" #define DB_VLOCK "1"
#define DB_VERSION "1.0.1" #define DB_VERSION "1.0.1"
#define CKDB_VERSION DB_VERSION"-1.210" #define CKDB_VERSION DB_VERSION"-1.211"
#define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL " - from %s %s() line %d"
#define WHERE_FFL_HERE __FILE__, __func__, __LINE__ #define WHERE_FFL_HERE __FILE__, __func__, __LINE__
@ -483,11 +483,28 @@ enum cmd_values {
LIST_MEM_ADD(_list, _val); \ LIST_MEM_ADD(_list, _val); \
_fld = strdup(_val); \ _fld = strdup(_val); \
if (!(_fld)) \ if (!(_fld)) \
quithere(1, "malloc OOM"); \ quithere(1, "strdup OOM"); \
} \ } \
} \ } \
} while (0) } while (0)
#define DUP_POINTER(_list, _fld, _val) do { \
if ((_fld) && ((_fld) != EMPTY)) { \
if (_list) \
LIST_MEM_SUB(_list, _fld); \
free(_fld); \
} \
if (!(_val) || !(*(_val))) \
(_fld) = EMPTY; \
else { \
if (_list) \
LIST_MEM_ADD(_list, _val); \
_fld = strdup(_val); \
if (!(_fld)) \
quithere(1, "strdup OOM"); \
} \
} while (0)
#define SET_CREATEBY(_list, _fld, _val) SET_POINTER(_list, _fld, _val, by_default) #define SET_CREATEBY(_list, _fld, _val) SET_POINTER(_list, _fld, _val, by_default)
#define SET_CREATECODE(_list, _fld, _val) SET_POINTER(_list, _fld, _val, EMPTY) #define SET_CREATECODE(_list, _fld, _val) SET_POINTER(_list, _fld, _val, EMPTY)
#define SET_CREATEINET(_list, _fld, _val) SET_POINTER(_list, _fld, _val, inet_default) #define SET_CREATEINET(_list, _fld, _val) SET_POINTER(_list, _fld, _val, inet_default)
@ -2276,7 +2293,7 @@ extern K_ITEM *find_markersummary_userid(int64_t userid, char *workername,
_find_markersummary(_markerid, 0, KANO, EMPTY, true) _find_markersummary(_markerid, 0, KANO, EMPTY, true)
#define POOL_MS(_row) do { \ #define POOL_MS(_row) do { \
(_row)->userid = KANO; \ (_row)->userid = KANO; \
(_row)->workername = strdup(EMPTY); \ (_row)->workername = EMPTY; \
} while (0) } while (0)
extern K_ITEM *_find_markersummary(int64_t markerid, int64_t workinfoid, extern K_ITEM *_find_markersummary(int64_t markerid, int64_t workinfoid,
int64_t userid, char *workername, bool pool); int64_t userid, char *workername, bool pool);

12
src/ckdb_cmd.c

@ -3794,10 +3794,8 @@ static char *cmd_setopts(PGconn *conn, char *cmd, char *id,
gotvalue = false; gotvalue = false;
} }
if (strcmp(dot, "value") == 0) { if (strcmp(dot, "value") == 0) {
optioncontrol->optionvalue = strdup(data); DUP_POINTER(optioncontrol_free,
if (!(optioncontrol->optionvalue)) optioncontrol->optionvalue, data);
quithere(1, "malloc (%d) OOM", (int)strlen(data));
LIST_MEM_ADD(optioncontrol_free, optioncontrol->optionvalue);
gotvalue = true; gotvalue = true;
} else if (strcmp(dot, "date") == 0) { } else if (strcmp(dot, "date") == 0) {
att_to_date(&(optioncontrol->activationdate), data, now); att_to_date(&(optioncontrol->activationdate), data, now);
@ -4695,8 +4693,7 @@ static char *cmd_payouts(PGconn *conn, char *cmd, char *id, tv_t *now,
payouts2->diffused = payouts->diffused; payouts2->diffused = payouts->diffused;
payouts2->shareacc = payouts->shareacc; payouts2->shareacc = payouts->shareacc;
copy_tv(&(payouts2->lastshareacc), &(payouts->lastshareacc)); copy_tv(&(payouts2->lastshareacc), &(payouts->lastshareacc));
payouts2->stats = strdup(payouts->stats); DUP_POINTER(payouts_free, payouts2->stats, payouts->stats);
LIST_MEM_ADD(payouts_free, payouts2->stats);
ok = payouts_add(conn, true, p2_item, &old_p2_item, ok = payouts_add(conn, true, p2_item, &old_p2_item,
by, code, inet, now, NULL, false); by, code, inet, now, NULL, false);
@ -4760,8 +4757,7 @@ static char *cmd_payouts(PGconn *conn, char *cmd, char *id, tv_t *now,
payouts2->diffused = payouts->diffused; payouts2->diffused = payouts->diffused;
payouts2->shareacc = payouts->shareacc; payouts2->shareacc = payouts->shareacc;
copy_tv(&(payouts2->lastshareacc), &(payouts->lastshareacc)); copy_tv(&(payouts2->lastshareacc), &(payouts->lastshareacc));
payouts2->stats = strdup(payouts->stats); DUP_POINTER(payouts_free, payouts2->stats, payouts->stats);
LIST_MEM_ADD(payouts_free, payouts2->stats);
ok = payouts_add(conn, true, p2_item, &old_p2_item, ok = payouts_add(conn, true, p2_item, &old_p2_item,
by, code, inet, now, NULL, false); by, code, inet, now, NULL, false);

14
src/ckdb_crypt.c

@ -118,12 +118,7 @@ K_ITEM *gen_2fa_key(K_ITEM *old_u_item, int32_t entropy, char *by, char *code,
K_WUNLOCK(users_free); K_WUNLOCK(users_free);
DATA_USERS(users, u_item); DATA_USERS(users, u_item);
memcpy(users, old_users, sizeof(*users)); memcpy(users, old_users, sizeof(*users));
if (users->userdata != EMPTY) { DUP_POINTER(users_free, users->userdata, old_users->userdata);
users->userdata = strdup(users->userdata);
if (!users->userdata)
quithere(1, "strdup OOM");
LIST_MEM_ADD(users_free, users->userdata);
}
users_userdata_add_bin(users, USER_TOTPAUTH_NAME, users_userdata_add_bin(users, USER_TOTPAUTH_NAME,
USER_TOTPAUTH, key, sizeof(key)); USER_TOTPAUTH, key, sizeof(key));
users_userdata_add_txt(users, USER_TEST2FA_NAME, users_userdata_add_txt(users, USER_TEST2FA_NAME,
@ -226,12 +221,7 @@ bool tst_2fa(K_ITEM *old_u_item, int32_t value, char *by, char *code,
K_WUNLOCK(users_free); K_WUNLOCK(users_free);
DATA_USERS(users, u_item); DATA_USERS(users, u_item);
memcpy(users, old_users, sizeof(*users)); memcpy(users, old_users, sizeof(*users));
if (users->userdata != EMPTY) { DUP_POINTER(users_free, users->userdata, old_users->userdata);
users->userdata = strdup(users->userdata);
if (!users->userdata)
quithere(1, "strdup OOM");
LIST_MEM_ADD(users_free, users->userdata);
}
users_userdata_del(users, USER_TEST2FA_NAME, USER_TEST2FA); users_userdata_del(users, USER_TEST2FA_NAME, USER_TEST2FA);
ok = users_replace(NULL, u_item, old_u_item, by, code, inet, cd, ok = users_replace(NULL, u_item, old_u_item, by, code, inet, cd,
trf_root); trf_root);

10
src/ckdb_data.c

@ -3672,8 +3672,7 @@ bool process_pplns(int32_t height, char *blockhash, tv_t *addr_cd)
diff_times, FLDSEP, diff_add, FLDSEP, total_share_count, diff_times, FLDSEP, diff_add, FLDSEP, total_share_count,
FLDSEP, ss_count, FLDSEP, wm_count, FLDSEP, ms_count, FLDSEP, ss_count, FLDSEP, wm_count, FLDSEP, ms_count,
FLDSEP, cd_buf); FLDSEP, cd_buf);
payouts->stats = buf; DUP_POINTER(payouts_free, payouts->stats, &buf[0]);
LIST_MEM_ADD(payouts_free, payouts->stats);
conned = CKPQConn(&conn); conned = CKPQConn(&conn);
begun = CKPQBegin(conn); begun = CKPQBegin(conn);
@ -3887,10 +3886,6 @@ bool process_pplns(int32_t height, char *blockhash, tv_t *addr_cd)
ss_count, wm_count, ms_count, usercount, diff_times, ss_count, wm_count, ms_count, usercount, diff_times,
diff_add, cd_buf); diff_add, cd_buf);
// convert the stack memory to heap memeory
payouts->stats = strdup(payouts->stats);
LIST_MEM_ADD(payouts_free, payouts->stats);
K_WLOCK(payouts_free); K_WLOCK(payouts_free);
p2_item = k_unlink_head(payouts_free); p2_item = k_unlink_head(payouts_free);
K_WUNLOCK(payouts_free); K_WUNLOCK(payouts_free);
@ -3908,8 +3903,7 @@ bool process_pplns(int32_t height, char *blockhash, tv_t *addr_cd)
payouts2->diffused = payouts->diffused; payouts2->diffused = payouts->diffused;
payouts2->shareacc = payouts->shareacc; payouts2->shareacc = payouts->shareacc;
copy_tv(&(payouts2->lastshareacc), &(payouts->lastshareacc)); copy_tv(&(payouts2->lastshareacc), &(payouts->lastshareacc));
payouts2->stats = strdup(payouts->stats); DUP_POINTER(payouts_free, payouts2->stats, payouts->stats);
LIST_MEM_ADD(payouts_free, payouts2->stats);
setnow(&now); setnow(&now);
/* N.B. the PROCESSING payouts could have expirydate = createdate /* N.B. the PROCESSING payouts could have expirydate = createdate

47
src/ckdb_dbio.c

@ -428,12 +428,7 @@ bool users_update(PGconn *conn, K_ITEM *u_item, char *oldhash,
STRNCPY(row->emailaddress, email); STRNCPY(row->emailaddress, email);
if (status) if (status)
STRNCPY(row->status, status); STRNCPY(row->status, status);
if (row->userdata != EMPTY) { DUP_POINTER(users_free, row->userdata, users->userdata);
row->userdata = strdup(users->userdata);
if (!row->userdata)
quithere(1, "strdup OOM");
LIST_MEM_ADD(users_free, row->userdata);
}
HISTORYDATEINIT(row, cd, by, code, inet); HISTORYDATEINIT(row, cd, by, code, inet);
HISTORYDATETRANSFER(trf_root, row); HISTORYDATETRANSFER(trf_root, row);
@ -2649,14 +2644,8 @@ int64_t workinfo_add(PGconn *conn, char *workinfoidstr, char *poolinstance,
TXT_TO_BIGINT("workinfoid", workinfoidstr, row->workinfoid); TXT_TO_BIGINT("workinfoid", workinfoidstr, row->workinfoid);
STRNCPY(row->poolinstance, poolinstance); STRNCPY(row->poolinstance, poolinstance);
row->transactiontree = strdup(transactiontree); DUP_POINTER(workinfo_free, row->transactiontree, transactiontree);
if (!(row->transactiontree)) DUP_POINTER(workinfo_free, row->merklehash, merklehash);
quithere(1, "malloc (%d) OOM", (int)strlen(transactiontree));
LIST_MEM_ADD(workinfo_free, row->transactiontree);
row->merklehash = strdup(merklehash);
if (!(row->merklehash))
quithere(1, "malloc (%d) OOM", (int)strlen(merklehash));
LIST_MEM_ADD(workinfo_free, row->merklehash);
STRNCPY(row->prevhash, prevhash); STRNCPY(row->prevhash, prevhash);
STRNCPY(row->coinbase1, coinbase1); STRNCPY(row->coinbase1, coinbase1);
STRNCPY(row->coinbase2, coinbase2); STRNCPY(row->coinbase2, coinbase2);
@ -3680,9 +3669,11 @@ bool sharesummaries_to_markersummaries(PGconn *conn, WORKMARKERS *workmarkers,
bzero(markersummary, sizeof(*markersummary)); bzero(markersummary, sizeof(*markersummary));
markersummary->markerid = workmarkers->markerid; markersummary->markerid = workmarkers->markerid;
markersummary->userid = sharesummary->userid; markersummary->userid = sharesummary->userid;
markersummary->workername = strdup(sharesummary->workername); DUP_POINTER(markersummary_free,
LIST_MEM_ADD(markersummary_free, markersummary->workername); markersummary->workername,
ms_root = add_to_ktree(ms_root, ms_item, cmp_markersummary); sharesummary->workername);
ms_root = add_to_ktree(ms_root, ms_item,
cmp_markersummary);
LOGDEBUG("%s() new ms %"PRId64"/%"PRId64"/%s", LOGDEBUG("%s() new ms %"PRId64"/%"PRId64"/%s",
shortname, markersummary->markerid, shortname, markersummary->markerid,
@ -3820,7 +3811,6 @@ flail:
bzero(p_markersummary, sizeof(*p_markersummary)); bzero(p_markersummary, sizeof(*p_markersummary));
p_markersummary->markerid = markersummary->markerid; p_markersummary->markerid = markersummary->markerid;
POOL_MS(p_markersummary); POOL_MS(p_markersummary);
LIST_MEM_ADD(markersummary_free, p_markersummary->workername);
markersummary_pool_root = add_to_ktree(markersummary_pool_root, markersummary_pool_root = add_to_ktree(markersummary_pool_root,
p_ms_item, p_ms_item,
cmp_markersummary); cmp_markersummary);
@ -4065,8 +4055,8 @@ bool _sharesummary_update(SHARES *s_row, SHAREERRORS *e_row, K_ITEM *ss_item,
DATA_SHARESUMMARY(row, item); DATA_SHARESUMMARY(row, item);
bzero(row, sizeof(*row)); bzero(row, sizeof(*row));
row->userid = userid; row->userid = userid;
row->workername = strdup(workername); DUP_POINTER(sharesummary_free, row->workername,
LIST_MEM_ADD(sharesummary_free, row->workername); workername);
row->workinfoid = workinfoid; row->workinfoid = workinfoid;
} }
@ -4136,7 +4126,6 @@ bool _sharesummary_update(SHARES *s_row, SHAREERRORS *e_row, K_ITEM *ss_item,
DATA_SHARESUMMARY(p_row, p_item); DATA_SHARESUMMARY(p_row, p_item);
bzero(p_row, sizeof(*p_row)); bzero(p_row, sizeof(*p_row));
POOL_SS(p_row); POOL_SS(p_row);
LIST_MEM_ADD(sharesummary_free, p_row->workername);
p_row->workinfoid = workinfoid; p_row->workinfoid = workinfoid;
} }
@ -6416,7 +6405,6 @@ bool markersummary_fill(PGconn *conn)
bzero(p_row, sizeof(*p_row)); bzero(p_row, sizeof(*p_row));
p_row->markerid = row->markerid; p_row->markerid = row->markerid;
POOL_MS(p_row); POOL_MS(p_row);
LIST_MEM_ADD(markersummary_free, p_row->workername);
markersummary_pool_root = add_to_ktree(markersummary_pool_root, markersummary_pool_root = add_to_ktree(markersummary_pool_root,
p_item, p_item,
cmp_markersummary); cmp_markersummary);
@ -6596,12 +6584,10 @@ bool _workmarkers_process(PGconn *conn, bool already, bool add,
} }
} }
row->poolinstance = strdup(poolinstance); DUP_POINTER(workmarkers_free, row->poolinstance, poolinstance);
LIST_MEM_ADD(workmarkers_free, poolinstance);
row->workinfoidend = workinfoidend; row->workinfoidend = workinfoidend;
row->workinfoidstart = workinfoidstart; row->workinfoidstart = workinfoidstart;
row->description = strdup(description); DUP_POINTER(workmarkers_free, row->description, description);
LIST_MEM_ADD(workmarkers_free, description);
STRNCPY(row->status, status); STRNCPY(row->status, status);
HISTORYDATEINIT(row, cd, by, code, inet); HISTORYDATEINIT(row, cd, by, code, inet);
HISTORYDATETRANSFER(trf_root, row); HISTORYDATETRANSFER(trf_root, row);
@ -6897,13 +6883,10 @@ bool _marks_process(PGconn *conn, bool add, char *poolinstance,
K_WUNLOCK(marks_free); K_WUNLOCK(marks_free);
DATA_MARKS(row, m_item); DATA_MARKS(row, m_item);
bzero(row, sizeof(*row)); bzero(row, sizeof(*row));
row->poolinstance = strdup(poolinstance); DUP_POINTER(marks_free, row->poolinstance, poolinstance);
LIST_MEM_ADD(marks_free, poolinstance);
row->workinfoid = workinfoid; row->workinfoid = workinfoid;
row->description = strdup(description); DUP_POINTER(marks_free, row->description, description);
LIST_MEM_ADD(marks_free, description); DUP_POINTER(marks_free, row->extra, extra);
row->extra = strdup(extra);
LIST_MEM_ADD(marks_free, extra);
STRNCPY(row->marktype, marktype); STRNCPY(row->marktype, marktype);
STRNCPY(row->status, status); STRNCPY(row->status, status);
HISTORYDATEINIT(row, cd, by, code, inet); HISTORYDATEINIT(row, cd, by, code, inet);

Loading…
Cancel
Save