Skip to content

Commit

Permalink
Remove hi_zalloc()
Browse files Browse the repository at this point in the history
To have a common ground with hiredis regarding allocators the
not so commonly used hi_zalloc() is replaced with a hi_malloc() and
a memset().
Also making sure that hi_malloc() returning NULL is handled.
  • Loading branch information
bjosv committed Dec 3, 2020
1 parent e94a672 commit 36dec32
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
19 changes: 14 additions & 5 deletions hircluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -2564,11 +2564,12 @@ static int command_pre_fragment(redisClusterContext *cc, struct cmd *command,

key_count = hiarray_n(command->keys);

sub_commands = hi_zalloc(REDIS_CLUSTER_SLOTS * sizeof(*sub_commands));
sub_commands = hi_malloc(REDIS_CLUSTER_SLOTS * sizeof(*sub_commands));
if (sub_commands == NULL) {
__redisClusterSetError(cc, REDIS_ERR_OOM, "Out of memory");
goto done;
}
memset(sub_commands, 0, REDIS_CLUSTER_SLOTS * sizeof(*sub_commands));

command->frag_seq = hi_malloc(key_count * sizeof(*command->frag_seq));
if (command->frag_seq == NULL) {
Expand Down Expand Up @@ -2662,12 +2663,14 @@ static int command_pre_fragment(redisClusterContext *cc, struct cmd *command,
sub_command->clen += 13 + num_str_len;

sub_command->cmd =
hi_zalloc(sub_command->clen * sizeof(*sub_command->cmd));
hi_malloc(sub_command->clen * sizeof(*sub_command->cmd));
if (sub_command->cmd == NULL) {
__redisClusterSetError(cc, REDIS_ERR_OOM, "Out of memory");
slot_num = -1;
goto done;
}
memset(sub_command->cmd, 0,
sub_command->clen * sizeof(*sub_command->cmd));

sub_command->cmd[idx++] = '*';
memcpy(sub_command->cmd + idx, num_str, num_str_len);
Expand Down Expand Up @@ -2704,12 +2707,14 @@ static int command_pre_fragment(redisClusterContext *cc, struct cmd *command,
sub_command->clen += 12 + num_str_len;

sub_command->cmd =
hi_zalloc(sub_command->clen * sizeof(*sub_command->cmd));
hi_malloc(sub_command->clen * sizeof(*sub_command->cmd));
if (sub_command->cmd == NULL) {
__redisClusterSetError(cc, REDIS_ERR_OOM, "Out of memory");
slot_num = -1;
goto done;
}
memset(sub_command->cmd, 0,
sub_command->clen * sizeof(*sub_command->cmd));

sub_command->cmd[idx++] = '*';
memcpy(sub_command->cmd + idx, num_str, num_str_len);
Expand Down Expand Up @@ -2746,12 +2751,14 @@ static int command_pre_fragment(redisClusterContext *cc, struct cmd *command,
sub_command->clen += 15 + num_str_len;

sub_command->cmd =
hi_zalloc(sub_command->clen * sizeof(*sub_command->cmd));
hi_malloc(sub_command->clen * sizeof(*sub_command->cmd));
if (sub_command->cmd == NULL) {
__redisClusterSetError(cc, REDIS_ERR_OOM, "Out of memory");
slot_num = -1;
goto done;
}
memset(sub_command->cmd, 0,
sub_command->clen * sizeof(*sub_command->cmd));

sub_command->cmd[idx++] = '*';
memcpy(sub_command->cmd + idx, num_str, num_str_len);
Expand Down Expand Up @@ -2790,12 +2797,14 @@ static int command_pre_fragment(redisClusterContext *cc, struct cmd *command,
sub_command->clen += 13 + num_str_len;

sub_command->cmd =
hi_zalloc(sub_command->clen * sizeof(*sub_command->cmd));
hi_malloc(sub_command->clen * sizeof(*sub_command->cmd));
if (sub_command->cmd == NULL) {
__redisClusterSetError(cc, REDIS_ERR_OOM, "Out of memory");
slot_num = -1;
goto done;
}
memset(sub_command->cmd, 0,
sub_command->clen * sizeof(*sub_command->cmd));

sub_command->cmd[idx++] = '*';
memcpy(sub_command->cmd + idx, num_str, num_str_len);
Expand Down
11 changes: 0 additions & 11 deletions hiutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,6 @@ int _uint_len(uint32_t num) {
return n;
}

void *_hi_zalloc(size_t size) {
void *p;

p = hi_malloc(size);
if (p != NULL) {
memset(p, 0, size);
}

return p;
}

void hi_stacktrace(int skip_count) {
if (skip_count > 0) {
}
Expand Down
7 changes: 0 additions & 7 deletions hiutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,6 @@ int hi_valid_port(int n);

int _uint_len(uint32_t num);

/*
* Memory allocation wrapper.
*/
#define hi_zalloc(_s) _hi_zalloc((size_t)(_s))

void *_hi_zalloc(size_t size);

#ifndef WIN32
/*
* Wrappers to send or receive n byte message on a blocking
Expand Down

0 comments on commit 36dec32

Please sign in to comment.