From e195d5379deb5699ea7cb76e4b3077a2cffa40da Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 26 Feb 2009 17:41:43 -0800 Subject: Since the stack has a fixed size, don't allocate dynamically. --- pbstream.c | 11 ++--------- pbstream.h | 8 +++++--- tests.c | 8 +++----- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/pbstream.c b/pbstream.c index 4ce8cc3..d5ddc1c 100644 --- a/pbstream.c +++ b/pbstream.c @@ -4,7 +4,6 @@ * Copyright (c) 2008-2009 Joshua Haberman. See LICENSE for details. */ -#include #include #include "pbstream.h" @@ -292,14 +291,8 @@ void pbstream_init_parser( struct pbstream_fieldset *toplevel_fieldset) { state->offset = 0; - /* We match proto2's limit of 64 for maximum stack depth. */ - state->top = state->base = malloc(64*sizeof(*state->base)); - state->limit = state->base + 64; + state->top = state->stack; + state->limit = state->top + PBSTREAM_MAX_STACK; state->top->fieldset = toplevel_fieldset; state->top->end_offset = SIZE_MAX; } - -void pbstream_free_parser(struct pbstream_parse_state *state) -{ - free(state->base); -} diff --git a/pbstream.h b/pbstream.h index d564970..b8befc6 100644 --- a/pbstream.h +++ b/pbstream.h @@ -7,6 +7,9 @@ #include #include +/* The maximum that any submessages can be nested. Matches proto2's limit. */ +#define PBSTREAM_MAX_STACK 64 + /* A list of types as they can appear in a .proto file. */ typedef enum pbstream_type { PBSTREAM_TYPE_DOUBLE = 0, @@ -101,7 +104,8 @@ struct pbstream_parse_stack_frame { /* The stream parser's state. */ struct pbstream_parse_state { size_t offset; - struct pbstream_parse_stack_frame *base, *top, *limit; + struct pbstream_parse_stack_frame stack[PBSTREAM_MAX_STACK]; + struct pbstream_parse_stack_frame *top, *limit; }; /* Call this once before parsing to initialize the data structures. @@ -111,8 +115,6 @@ void pbstream_init_parser( struct pbstream_parse_state *state, struct pbstream_fieldset *toplevel_fieldset); -void pbstream_free_parser(struct pbstream_parse_state *state); - /* Status as returned by pbstream_parse(). Status codes <0 are fatal errors * that cannot be recovered. Status codes >0 are unusual but nonfatal events, * which nonetheless must be handled differently since they do not return data diff --git a/tests.c b/tests.c index 7a6e51b..2cc9128 100644 --- a/tests.c +++ b/tests.c @@ -1,5 +1,6 @@ #include #include +#include #include "pbstream.c" void test_get_v_uint64_t() @@ -66,7 +67,6 @@ void test_simple_proto() assert(val.field == &fieldset1->fields[0]); assert(val.v.int32 == 150); assert(s.offset == 3); - pbstream_free_parser(&s); char message2[] = {0x12, 0x07, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67}; pbstream_init_parser(&s, fieldset1); @@ -76,7 +76,6 @@ void test_simple_proto() assert(val.v.delimited.offset == 2); assert(val.v.delimited.len == 7); assert(s.offset == 9); - pbstream_free_parser(&s); struct pbstream_fieldset *fieldset2 = malloc(sizeof(*fieldset1) + sizeof(struct pbstream_field[3])); @@ -92,7 +91,7 @@ void test_simple_proto() assert(val.v.delimited.offset == 2); assert(val.v.delimited.len == 3); assert(s.offset == 2); - assert(s.top-1 == s.base); + assert(s.top-1 == s.stack); assert(s.top->fieldset == fieldset1); assert(s.top->end_offset == 5); @@ -105,8 +104,7 @@ void test_simple_proto() assert(pbstream_parse_field(&s, NULL /* shouldn't be read */, &fieldnum, &val, &wv) == PBSTREAM_STATUS_SUBMESSAGE_END); - assert(s.top == s.base); - pbstream_free_parser(&s); + assert(s.top == s.stack); free(fieldset1); free(fieldset2); -- cgit v1.2.3