Commit 8cd5f676 authored by Robert Edmonds's avatar Robert Edmonds

protobuf-c: remove tmp_alloc and max_alloca fields of ProtobufCAllocator

i'm confused as to why these fields exist, since the typical
implementation of a "temporary alloc" would be something like alloca(),
and alloca() is usually just inlined code that adjusts the stack
pointer, which is not a function whose address could be taken.

this breaks the API/ABI and will require a note in the ChangeLog.

possibly we could revisit the idea of "temporary allocations" by using
C99 variable length arrays. this would have the advantage of being
standardized, unlike alloca().
parent a533b594
...@@ -72,11 +72,6 @@ ...@@ -72,11 +72,6 @@
*/ */
#define MAX_MEMBERS_FOR_HASH_SIZE 128 #define MAX_MEMBERS_FOR_HASH_SIZE 128
/* Convenience macros. */
#define TMPALLOC(allocator, size) \
((allocator)->tmp_alloc((allocator)->allocator_data, (size)))
/* Try to allocate memory, running some special code if it fails. */ /* Try to allocate memory, running some special code if it fails. */
#define DO_ALLOC(dst, allocator, size, fail_code) \ #define DO_ALLOC(dst, allocator, size, fail_code) \
do { \ do { \
...@@ -173,8 +168,6 @@ system_free(void *allocator_data, void *data) ...@@ -173,8 +168,6 @@ system_free(void *allocator_data, void *data)
ProtobufCAllocator protobuf_c_default_allocator = { ProtobufCAllocator protobuf_c_default_allocator = {
.alloc = system_alloc, .alloc = system_alloc,
.free = system_free, .free = system_free,
.tmp_alloc = NULL,
.max_alloca = 8192,
.allocator_data = NULL, .allocator_data = NULL,
}; };
...@@ -2337,6 +2330,7 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc, ...@@ -2337,6 +2330,7 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc,
unsigned in_slab_index = 0; /* number of members in the slab */ unsigned in_slab_index = 0; /* number of members in the slab */
size_t n_unknown = 0; size_t n_unknown = 0;
unsigned f; unsigned f;
unsigned j;
unsigned i_slab; unsigned i_slab;
unsigned last_field_index = 0; unsigned last_field_index = 0;
unsigned char required_fields_bitmap[MAX_MEMBERS_FOR_HASH_SIZE / 8] = unsigned char required_fields_bitmap[MAX_MEMBERS_FOR_HASH_SIZE / 8] =
...@@ -2471,14 +2465,9 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc, ...@@ -2471,14 +2465,9 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc,
which_slab++; which_slab++;
size = sizeof(ScannedMember) size = sizeof(ScannedMember)
<< (which_slab + FIRST_SCANNED_MEMBER_SLAB_SIZE_LOG2); << (which_slab + FIRST_SCANNED_MEMBER_SLAB_SIZE_LOG2);
if (allocator->tmp_alloc != NULL) { DO_ALLOC(scanned_member_slabs[which_slab],
scanned_member_slabs[which_slab] = allocator, size,
TMPALLOC(allocator, size); goto error_cleanup_during_scan);
} else {
DO_ALLOC(scanned_member_slabs[which_slab],
allocator, size,
goto error_cleanup_during_scan);
}
} }
scanned_member_slabs[which_slab][in_slab_index++] = tmp; scanned_member_slabs[which_slab][in_slab_index++] = tmp;
...@@ -2574,30 +2563,21 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc, ...@@ -2574,30 +2563,21 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc,
} }
/* cleanup */ /* cleanup */
if (allocator->tmp_alloc == NULL) { for (j = 1; j <= which_slab; j++)
unsigned j; FREE(allocator, scanned_member_slabs[j]);
for (j = 1; j <= which_slab; j++)
FREE(allocator, scanned_member_slabs[j]);
}
return rv; return rv;
error_cleanup: error_cleanup:
protobuf_c_message_free_unpacked(rv, allocator); protobuf_c_message_free_unpacked(rv, allocator);
if (allocator->tmp_alloc == NULL) { for (j = 1; j <= which_slab; j++)
unsigned j; FREE(allocator, scanned_member_slabs[j]);
for (j = 1; j <= which_slab; j++)
FREE(allocator, scanned_member_slabs[j]);
}
return NULL; return NULL;
error_cleanup_during_scan: error_cleanup_during_scan:
FREE(allocator, rv); FREE(allocator, rv);
if (allocator->tmp_alloc == NULL) { for (j = 1; j <= which_slab; j++)
unsigned j; FREE(allocator, scanned_member_slabs[j]);
for (j = 1; j <= which_slab; j++)
FREE(allocator, scanned_member_slabs[j]);
}
return NULL; return NULL;
} }
......
...@@ -145,8 +145,6 @@ struct _ProtobufCAllocator ...@@ -145,8 +145,6 @@ struct _ProtobufCAllocator
{ {
void *(*alloc)(void *allocator_data, size_t size); void *(*alloc)(void *allocator_data, size_t size);
void (*free)(void *allocator_data, void *pointer); void (*free)(void *allocator_data, void *pointer);
void *(*tmp_alloc)(void *allocator_data, size_t size);
unsigned max_alloca;
void *allocator_data; void *allocator_data;
}; };
......
...@@ -1520,7 +1520,9 @@ static void test_free (void *allocator_data, void *data) ...@@ -1520,7 +1520,9 @@ static void test_free (void *allocator_data, void *data)
} }
static ProtobufCAllocator test_allocator = { static ProtobufCAllocator test_allocator = {
test_alloc, test_free, 0, 0, &test_allocator_data .alloc = test_alloc,
.free = test_free,
.allocator_data = &test_allocator_data,
}; };
#define SETUP_TEST_ALLOC_BUFFER(pbuf, len) \ #define SETUP_TEST_ALLOC_BUFFER(pbuf, len) \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment