From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Date: Mon, 4 Feb 2019 12:41:31 +0300 From: Vladimir Davydov Subject: Re: [PATCH v2 1/1] Implement mp_stack class Message-ID: <20190204094131.vw5kdo3rhuj4xxz3@esperanza> References: <473372ec0b111ff0731857bb4c45409866cb3a5d.1549009319.git.kshcherbatov@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <473372ec0b111ff0731857bb4c45409866cb3a5d.1549009319.git.kshcherbatov@gmail.com> To: Kirill Shcherbatov Cc: tarantool-patches@freelists.org, Kirill Shcherbatov List-ID: On Sun, Feb 03, 2019 at 01:21:43PM +0300, Kirill Shcherbatov wrote: > diff --git a/msgpuck.c b/msgpuck.c > index 72b800c..cb93d68 100644 > --- a/msgpuck.c > +++ b/msgpuck.c > @@ -323,25 +334,14 @@ mp_fprint_internal(FILE *file, const char **data) > total_bytes += bytes; \ > } while (0) > #define PRINT(...) HANDLE(fprintf, __VA_ARGS__) > -#define SELF(...) HANDLE(mp_fprint_internal, __VA_ARGS__) > -MP_PRINT(SELF, PRINT) > +MP_PRINT(PRINT) > #undef HANDLE HANDLE is redundant, I removed it. > -#undef SELF > #undef PRINT > return total_bytes; > } > +/** > + * \brief Initialize mp_stack \a stack of specified size \a size > + * and user-allocated array \a frames. > + * The \a frames allocation must have at least \a size mp_frame > + * items. > + * \param stack - the pointer to a mp_stack to initialize. > + * \param size - stack size, count of stack::frames to use. > + * \param frames - mp_frame preallocated array of size \a size > + * of struct mp_frame items > + */ > +MP_PROTO void > +mp_stack_create(struct mp_stack *stack, uint32_t size, struct mp_frame *frames); > + > +/** > + * \brief Construct a new mp_frame and push it on to mp_stack > + * \a stack. > + * \param stack - the pointer to a stack to operate with. > + * \param type - the type of mp_frame to create. > + * \param count - the count of itemes of mp_frame to create. > + * \pre mp_stack_is_full(stack) == false > + */ > +MP_PROTO void > +mp_stack_push(struct mp_stack *stack, enum mp_type type, uint32_t count); s/uint32_t/int - Fixed. Pushed to the master. My incremental diff is below. diff --git a/msgpuck.c b/msgpuck.c index cb93d68902bd..3338a3c38698 100644 --- a/msgpuck.c +++ b/msgpuck.c @@ -327,15 +327,13 @@ mp_fprint(FILE *file, const char *data) if (!file) file = stdout; int total_bytes = 0; -#define HANDLE(FUN, ...) do { \ - int bytes = FUN(file, __VA_ARGS__); \ +#define PRINT(...) do { \ + int bytes = fprintf(file, __VA_ARGS__); \ if (mp_unlikely(bytes < 0)) \ return -1; \ total_bytes += bytes; \ } while (0) -#define PRINT(...) HANDLE(fprintf, __VA_ARGS__) -MP_PRINT(PRINT) -#undef HANDLE + MP_PRINT(PRINT) #undef PRINT return total_bytes; } @@ -344,8 +342,8 @@ int mp_snprint(char *buf, int size, const char *data) { int total_bytes = 0; -#define HANDLE(FUN, ...) do { \ - int bytes = FUN(buf, size, __VA_ARGS__); \ +#define PRINT(...) do { \ + int bytes = snprintf(buf, size, __VA_ARGS__); \ if (mp_unlikely(bytes < 0)) \ return -1; \ total_bytes += bytes; \ @@ -358,9 +356,7 @@ mp_snprint(char *buf, int size, const char *data) size = 0; \ } \ } while (0) -#define PRINT(...) HANDLE(snprintf, __VA_ARGS__) -MP_PRINT(PRINT) -#undef HANDLE + MP_PRINT(PRINT) #undef PRINT return total_bytes; } diff --git a/msgpuck.h b/msgpuck.h index 15a6c9b36855..ab84f0b4936f 100644 --- a/msgpuck.h +++ b/msgpuck.h @@ -1237,7 +1237,7 @@ struct mp_stack { * of struct mp_frame items */ MP_PROTO void -mp_stack_create(struct mp_stack *stack, uint32_t size, struct mp_frame *frames); +mp_stack_create(struct mp_stack *stack, int size, struct mp_frame *frames); /** * \brief Test if mp_stack \a stack is empty. @@ -1272,7 +1272,7 @@ mp_stack_pop(struct mp_stack *stack); * \pre mp_stack_is_full(stack) == false */ MP_PROTO void -mp_stack_push(struct mp_stack *stack, enum mp_type type, uint32_t count); +mp_stack_push(struct mp_stack *stack, enum mp_type type, int count); /** * \brief Get type attribute of the \a stack top frame. @@ -2304,7 +2304,7 @@ mp_check(const char **data, const char *end) } MP_IMPL void -mp_stack_create(struct mp_stack *stack, uint32_t size, struct mp_frame *frames) +mp_stack_create(struct mp_stack *stack, int size, struct mp_frame *frames) { stack->frames = frames; stack->size = size; @@ -2331,10 +2331,10 @@ mp_stack_pop(struct mp_stack *stack) } MP_IMPL void -mp_stack_push(struct mp_stack *stack, enum mp_type type, uint32_t count) +mp_stack_push(struct mp_stack *stack, enum mp_type type, int count) { assert(!mp_stack_is_full(stack)); - uint32_t idx = stack->used++; + int idx = stack->used++; stack->frames[idx].type = type; stack->frames[idx].count = count; stack->frames[idx].curr = -1;