Skip to content

Commit

Permalink
Remove own assert macro in cluster code
Browse files Browse the repository at this point in the history
There are a mixed usage of own and standard assert,
lets use standard from <assert.h> only.

Signed-off-by: Björn Svensson <[email protected]>
  • Loading branch information
bjosv committed Jun 29, 2024
1 parent c2b145e commit 1a02d10
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 85 deletions.
5 changes: 3 additions & 2 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#ifndef _WIN32
Expand Down Expand Up @@ -192,7 +193,7 @@ static inline int push_keypos(struct cmd *r, char *arg, uint32_t arglen) {
*
*/
void valkey_parse_cmd(struct cmd *r) {
ASSERT(r->cmd != NULL && r->clen > 0);
assert(r->cmd != NULL && r->clen > 0);
char *p = r->cmd;
char *end = r->cmd + r->clen;
uint32_t rnarg = 0; /* Number of args including cmd name */
Expand Down Expand Up @@ -322,7 +323,7 @@ void valkey_parse_cmd(struct cmd *r) {
goto oom;

done:
ASSERT(r->type > CMD_UNKNOWN && r->type < CMD_SENTINEL);
assert(r->type > CMD_UNKNOWN && r->type < CMD_SENTINEL);
r->result = CMD_PARSE_OK;
return;

Expand Down
2 changes: 1 addition & 1 deletion src/valkeycluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ static cluster_slot *cluster_slot_create(valkeyClusterNode *node) {
slot->node = node;

if (node != NULL) {
ASSERT(node->role == VALKEY_ROLE_MASTER);
assert(node->role == VALKEY_ROLE_MASTER);
if (node->slots == NULL) {
node->slots = listCreate();
if (node->slots == NULL) {
Expand Down
23 changes: 12 additions & 11 deletions src/vkarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include <stdlib.h>

#include "alloc.h"
Expand All @@ -38,7 +39,7 @@
struct vkarray *vkarray_create(uint32_t n, size_t size) {
struct vkarray *a;

ASSERT(n != 0 && size != 0);
assert(n != 0 && size != 0);

a = vk_malloc(sizeof(*a));
if (a == NULL) {
Expand All @@ -64,7 +65,7 @@ void vkarray_destroy(struct vkarray *a) {
}

void vkarray_deinit(struct vkarray *a) {
ASSERT(a->nelem == 0);
assert(a->nelem == 0);

vk_free(a->elem);
a->elem = NULL;
Expand All @@ -74,13 +75,13 @@ uint32_t vkarray_idx(struct vkarray *a, void *elem) {
uint8_t *p, *q;
uint32_t off, idx;

ASSERT(elem >= a->elem);
assert(elem >= a->elem);

p = a->elem;
q = elem;
off = (uint32_t)(q - p);

ASSERT(off % (uint32_t)a->size == 0);
assert(off % (uint32_t)a->size == 0);

idx = off / (uint32_t)a->size;

Expand Down Expand Up @@ -113,7 +114,7 @@ void *vkarray_push(struct vkarray *a) {
void *vkarray_pop(struct vkarray *a) {
void *elem;

ASSERT(a->nelem != 0);
assert(a->nelem != 0);

a->nelem--;
elem = (uint8_t *)a->elem + a->size * a->nelem;
Expand All @@ -124,16 +125,16 @@ void *vkarray_pop(struct vkarray *a) {
void *vkarray_get(struct vkarray *a, uint32_t idx) {
void *elem;

ASSERT(a->nelem != 0);
ASSERT(idx < a->nelem);
assert(a->nelem != 0);
assert(idx < a->nelem);

elem = (uint8_t *)a->elem + (a->size * idx);

return elem;
}

void *vkarray_top(struct vkarray *a) {
ASSERT(a->nelem != 0);
assert(a->nelem != 0);

return vkarray_get(a, a->nelem - 1);
}
Expand All @@ -151,7 +152,7 @@ void vkarray_swap(struct vkarray *a, struct vkarray *b) {
* compare comparator.
*/
void vkarray_sort(struct vkarray *a, vkarray_compare_t compare) {
ASSERT(a->nelem != 0);
assert(a->nelem != 0);

qsort(a->elem, a->nelem, a->size, compare);
}
Expand All @@ -163,8 +164,8 @@ void vkarray_sort(struct vkarray *a, vkarray_compare_t compare) {
int vkarray_each(struct vkarray *a, vkarray_each_t func, void *data) {
uint32_t i, nelem;

ASSERT(vkarray_n(a) != 0);
ASSERT(func != NULL);
assert(vkarray_n(a) != 0);
assert(func != NULL);

for (i = 0, nelem = vkarray_n(a); i < nelem; i++) {
void *elem = vkarray_get(a, i);
Expand Down
39 changes: 0 additions & 39 deletions src/vkutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
#include <sys/time.h>
#endif

#ifdef VK_HAVE_BACKTRACE
#include <execinfo.h>
#endif

#include "alloc.h"
#include "vkutil.h"

Expand All @@ -64,41 +60,6 @@ int _vk_atoi(uint8_t *line, size_t n) {
return value;
}

void vk_stacktrace(int skip_count) {
#ifdef VK_HAVE_BACKTRACE
void *stack[64];
char **symbols;
int size, i, j;

size = backtrace(stack, 64);
symbols = backtrace_symbols(stack, size);
if (symbols == NULL) {
return;
}

skip_count++; /* skip the current frame also */

for (i = skip_count, j = 0; i < size; i++, j++) {
printf("[%d] %s\n", j, symbols[i]);
}

vk_free(symbols);
#else
(void)skip_count;
#endif
}

void vk_assert(const char *cond, const char *file, int line, int panic) {

printf("File: %s Line: %d: %s\n", file, line, cond);

if (panic) {
vk_stacktrace(1);
abort();
}
abort();
}

/*
* Return the current time in microseconds since Epoch
*/
Expand Down
32 changes: 0 additions & 32 deletions src/vkutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,6 @@ typedef int rstatus_t; /* return type */

int _vk_atoi(uint8_t *line, size_t n);

/*
* Wrappers for defining custom assert based on whether macro
* VK_ASSERT_PANIC or VK_ASSERT_LOG was defined at the moment
* ASSERT was called.
*/
#ifdef VK_ASSERT_PANIC

#define ASSERT(_x) \
do { \
if (!(_x)) { \
vk_assert(#_x, __FILE__, __LINE__, 1); \
} \
} while (0)

#elif VK_ASSERT_LOG

#define ASSERT(_x) \
do { \
if (!(_x)) { \
vk_assert(#_x, __FILE__, __LINE__, 0); \
} \
} while (0)

#else

#define ASSERT(_x)

#endif

void vk_assert(const char *cond, const char *file, int line, int panic);
void vk_stacktrace(int skip_count);

int64_t vk_usec_now(void);

uint16_t crc16(const char *buf, int len);
Expand Down

0 comments on commit 1a02d10

Please sign in to comment.