From ac938ca77c039987b81d7ca24fa699759fc37bdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ture=20P=C3=A5lsson?= <ture@lysator.liu.se> Date: Fri, 5 Dec 2014 09:15:04 +0100 Subject: [PATCH] Streamline uint32 handling --- headers/readosm_protobuf.h | 6 ++-- src/protobuf.c | 72 ++++++++------------------------------ 2 files changed, 18 insertions(+), 60 deletions(-) diff --git a/headers/readosm_protobuf.h b/headers/readosm_protobuf.h index 0c3b897..3c08e83 100644 --- a/headers/readosm_protobuf.h +++ b/headers/readosm_protobuf.h @@ -168,10 +168,10 @@ typedef struct readosm_uint32_struct typedef struct readosm_uint32_packed_struct { /* a PBF uint32 packed object */ - readosm_uint32 *first; - readosm_uint32 *last; - int count; unsigned int *values; + unsigned int *next; + int count; + int max; } readosm_uint32_packed; typedef struct readosm_int32_struct diff --git a/src/protobuf.c b/src/protobuf.c index 03f5b4b..a38533d 100644 --- a/src/protobuf.c +++ b/src/protobuf.c @@ -252,70 +252,38 @@ static void init_uint32_packed (readosm_uint32_packed * packed) { /* initialing an empty PBF uint32 packed object */ - packed->first = NULL; - packed->last = NULL; + packed->max = 128; + packed->values = malloc(packed->max * sizeof(unsigned int)); + packed->next = packed->values; packed->count = 0; - packed->values = NULL; } static void append_uint32_packed (readosm_uint32_packed * packed, unsigned int val) { /* appending an uint32 value to a PBF packed object */ - readosm_uint32 *value = malloc (sizeof (readosm_uint32)); - value->value = val; - value->next = NULL; - if (packed->first == NULL) - packed->first = value; - if (packed->last != NULL) - packed->last->next = value; - packed->last = value; + if (packed->count == packed->max) { + packed->max *= 2; + packed->values = realloc(packed->values, packed->max * sizeof(unsigned int)); + packed->next = packed->values + packed->count; + } + *(packed->next++) = val; + packed->count++; } static void array_from_uint32_packed (readosm_uint32_packed * packed) { -/* creating an array supporting an uint32 packed object */ - int i; - readosm_uint32 *value = packed->first; - while (value != NULL) - { - /* counting how many values are into the packed list */ - packed->count++; - value = value->next; - } - if (packed->count <= 0) - return; - -/* allocating the array */ - packed->values = malloc (sizeof (unsigned int) * packed->count); - i = 0; - value = packed->first; - while (value != NULL) - { - /* setting up array values */ - *(packed->values + i) = value->value; - i++; - value = value->next; - } } static void finalize_uint32_packed (readosm_uint32_packed * packed) { /* cleaning any memory allocation for an uint32 packed object */ - readosm_uint32 *value; - readosm_uint32 *value_n; - value = packed->first; - while (value) - { - value_n = value->next; - free (value); - value = value_n; - } if (packed->values) free (packed->values); - + packed->values = packed->next = NULL; + packed->max = packed->count = 0; } static void @@ -1108,13 +1076,7 @@ parse_pbf_node_infos (readosm_packed_infos * packed_infos, variant.pointer + variant.length - 1, variant.little_endian_cpu)) goto error; - count = 0; - pu32 = packed_u32.first; - while (pu32) - { - count++; - pu32 = pu32->next; - } + count = packed_u32.count; packed_infos->ver_count = count; if (packed_infos->versions != NULL) { @@ -1124,13 +1086,9 @@ parse_pbf_node_infos (readosm_packed_infos * packed_infos, if (count > 0) { packed_infos->versions = malloc (sizeof (int) * count); - count = 0; - pu32 = packed_u32.first; - while (pu32) + for (int kk = 0; kk < count; kk++) { - *(packed_infos->versions + count) = pu32->value; - count++; - pu32 = pu32->next; + *(packed_infos->versions + kk) = packed_u32.values[kk]; } } reset_uint32_packed (&packed_u32); -- GitLab