mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-07 02:09:59 +00:00
malloc/realloc the stack when its length becomes > MSGPACK_EMBED_STACK_SIZE
This commit is contained in:
parent
87835a4e60
commit
a6ec726ed7
5 changed files with 103 additions and 17 deletions
|
|
@ -19,6 +19,7 @@
|
|||
#define MSGPACK_UNPACK_DEFINE_H__
|
||||
|
||||
#include "msgpack/sysdep.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
|
@ -28,8 +29,8 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
|
||||
#ifndef MSGPACK_MAX_STACK_SIZE
|
||||
#define MSGPACK_MAX_STACK_SIZE 16
|
||||
#ifndef MSGPACK_EMBED_STACK_SIZE
|
||||
#define MSGPACK_EMBED_STACK_SIZE 1
|
||||
#endif
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -58,7 +58,9 @@ msgpack_unpack_struct_decl(_context) {
|
|||
unsigned int cs;
|
||||
unsigned int trail;
|
||||
unsigned int top;
|
||||
msgpack_unpack_struct(_stack) stack[MSGPACK_MAX_STACK_SIZE];
|
||||
msgpack_unpack_struct(_stack)* stack;
|
||||
unsigned int stack_size;
|
||||
msgpack_unpack_struct(_stack) embed_stack[MSGPACK_EMBED_STACK_SIZE];
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -67,9 +69,18 @@ msgpack_unpack_func(void, _init)(msgpack_unpack_struct(_context)* ctx)
|
|||
ctx->cs = CS_HEADER;
|
||||
ctx->trail = 0;
|
||||
ctx->top = 0;
|
||||
ctx->stack = ctx->embed_stack;
|
||||
ctx->stack_size = MSGPACK_EMBED_STACK_SIZE;
|
||||
ctx->stack[0].obj = msgpack_unpack_callback(_root)(&ctx->user);
|
||||
}
|
||||
|
||||
msgpack_unpack_func(void, _destroy)(msgpack_unpack_struct(_context)* ctx)
|
||||
{
|
||||
if(ctx->stack_size != MSGPACK_EMBED_STACK_SIZE) {
|
||||
free(ctx->stack);
|
||||
}
|
||||
}
|
||||
|
||||
msgpack_unpack_func(msgpack_unpack_object, _data)(msgpack_unpack_struct(_context)* ctx)
|
||||
{
|
||||
return (ctx)->stack[0].obj;
|
||||
|
|
@ -119,12 +130,28 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
|
|||
#define start_container(func, count_, ct_) \
|
||||
if(msgpack_unpack_callback(func)(user, count_, &stack[top].obj) < 0) { goto _failed; } \
|
||||
if((count_) == 0) { obj = stack[top].obj; goto _push; } \
|
||||
if(top >= MSGPACK_MAX_STACK_SIZE) { goto _failed; } \
|
||||
stack[top].ct = ct_; \
|
||||
stack[top].count = count_; \
|
||||
++top; \
|
||||
/*printf("container %d count %d stack %d\n",stack[top].obj,count_,top);*/ \
|
||||
/*printf("stack push %d\n", top);*/ \
|
||||
++top; \
|
||||
if(top >= ctx->stack_size) { \
|
||||
if(ctx->stack_size == MSGPACK_EMBED_STACK_SIZE) { \
|
||||
size_t csize = sizeof(msgpack_unpack_struct(_stack)) * MSGPACK_EMBED_STACK_SIZE; \
|
||||
size_t nsize = csize * 2; \
|
||||
msgpack_unpack_struct(_stack)* tmp = (msgpack_unpack_struct(_stack)*)malloc(nsize); \
|
||||
if(tmp == NULL) { goto _failed; } \
|
||||
memcpy(tmp, ctx->stack, csize); \
|
||||
ctx->stack = tmp; \
|
||||
ctx->stack_size = MSGPACK_EMBED_STACK_SIZE * 2; \
|
||||
} else { \
|
||||
size_t nsize = sizeof(msgpack_unpack_struct(_stack)) * ctx->stack_size * 2; \
|
||||
msgpack_unpack_struct(_stack)* tmp = (msgpack_unpack_struct(_stack)*)realloc(ctx->stack, nsize); \
|
||||
if(tmp == NULL) { goto _failed; } \
|
||||
ctx->stack = tmp; \
|
||||
ctx->stack_size *= 2; \
|
||||
} \
|
||||
} \
|
||||
goto _header_again
|
||||
|
||||
#define NEXT_CS(p) \
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue