Browse Source

ckdb - buffer init macro

master
kanoi 11 years ago
parent
commit
5aeca8e566
  1. 42
      src/ckdb.c

42
src/ckdb.c

@ -114,10 +114,22 @@ ASSERT2(sizeof(int64_t) == 8);
trg[siz - 1] = '\0'; \ trg[siz - 1] = '\0'; \
} while (0) } while (0)
#define AR_SIZ 1024
#define APPEND_REALLOC_INIT(_buf, _off, _len) do { \
_len = AR_SIZ; \
(_buf) = malloc(_len); \
if (!(_buf)) \
quithere(1, "malloc (%d) OOM", (int)_len); \
(_buf)[0] = '\0'; \
_off = 0; \
} while(0)
#define APPEND_REALLOC(_dst, _dstoff, _dstsiz, _src) do { \ #define APPEND_REALLOC(_dst, _dstoff, _dstsiz, _src) do { \
size_t _srclen = strlen(_src); \ size_t _newlen, _srclen = strlen(_src); \
if ((_dstoff) + _srclen >= (_dstsiz)) { \ _newlen = (_dstoff) + _srclen; \
_dstsiz += 1024; \ if (_newlen >= (_dstsiz)) { \
_dstsiz = _newlen + AR_SIZ - (_newlen % AR_SIZ); \
_dst = realloc(_dst, _dstsiz); \ _dst = realloc(_dst, _dstsiz); \
if (!(_dst)) \ if (!(_dst)) \
quithere(1, "realloc (%d) OOM", (int)_dstsiz); \ quithere(1, "realloc (%d) OOM", (int)_dstsiz); \
@ -4140,12 +4152,8 @@ static char *cmd_payments(char *cmd, char *id, __maybe_unused tv_t *now, __maybe
row->paydate.tv_sec = 0; row->paydate.tv_sec = 0;
row->paydate.tv_usec = 0; row->paydate.tv_usec = 0;
p_item = find_after_in_ktree(payments_root, look, cmp_payments, ctx); p_item = find_after_in_ktree(payments_root, look, cmp_payments, ctx);
len = 1024; APPEND_REALLOC_INIT(buf, off, len);
buf = malloc(len); APPEND_REALLOC(buf, off, len, "ok.");
if (!buf)
quithere(1, "malloc buf (%d) OOM", (int)len);
strcpy(buf, "ok.");
off = strlen(buf);
rows = 0; rows = 0;
while (p_item && DATA_PAYMENTS(p_item)->userid == DATA_USERS(u_item)->userid) { while (p_item && DATA_PAYMENTS(p_item)->userid == DATA_USERS(u_item)->userid) {
tv_to_buf(&(DATA_PAYMENTS(p_item)->paydate), reply, sizeof(reply)); tv_to_buf(&(DATA_PAYMENTS(p_item)->paydate), reply, sizeof(reply));
@ -4540,13 +4548,8 @@ static char *cmd_homepage(char *cmd, char *id, __maybe_unused tv_t *now, __maybe
i_username = optional_name("username", 1, NULL); i_username = optional_name("username", 1, NULL);
len = 1024; APPEND_REALLOC_INIT(buf, off, len);
buf = malloc(len); APPEND_REALLOC(buf, off, len, "ok.");
if (!buf)
quithere(1, "malloc buf (%d) OOM", (int)len);
strcpy(buf, "ok.");
off = strlen(buf);
if (last_bc) { if (last_bc) {
tvs_to_buf(last_bc, reply, sizeof(reply)); tvs_to_buf(last_bc, reply, sizeof(reply));
snprintf(tmp, sizeof(tmp), "lastbc=%s%c", reply, FLDSEP); snprintf(tmp, sizeof(tmp), "lastbc=%s%c", reply, FLDSEP);
@ -4839,12 +4842,7 @@ static enum cmd_values breakdown(char *buf, int *which_cmds, char *cmd, char *id
json_t *json_element; json_t *json_element;
bool first = true; bool first = true;
len = 1024; APPEND_REALLOC_INIT(DATA_TRANSFER(item)->data, off, len);
DATA_TRANSFER(item)->data = malloc(len);
if (!(DATA_TRANSFER(item)->data))
quithere(1, "malloc data (%d) OOM", (int)len);
off = 0;
DATA_TRANSFER(item)->data[0] = '\0';
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
json_element = json_array_get(json_value, i); json_element = json_array_get(json_value, i);
if (json_is_string(json_element)) { if (json_is_string(json_element)) {

Loading…
Cancel
Save