Allowing <type>__free_unpacked(NULL, ...)

This makes __free_unpacked() consistent with free(), and simplifies
callers by allowing them to indiscriminately free message objects
without regard to whether they have been allocated or not.
This commit is contained in:
Richard Kettlewell 2017-02-25 19:58:34 +00:00
parent f06a172a91
commit c23065e4b1
3 changed files with 11 additions and 1 deletions

View File

@ -139,7 +139,7 @@ Foo__Bar__BazBah *
~~~
*
* - `free_unpacked()`. Frees a message object obtained with the `unpack()`
* method.
* method. Freeing `NULL` is allowed (the same as with `free()`).
*
~~~{.c}
void foo__bar__baz_bah__free_unpacked

2
protoc-c/c_message.cc Normal file → Executable file
View File

@ -376,6 +376,8 @@ GenerateHelperFunctionDefinitions(io::Printer* printer, bool is_submessage)
" ($classname$ *message,\n"
" ProtobufCAllocator *allocator)\n"
"{\n"
" if(!message)\n"
" return;\n"
" assert(message->base.descriptor == &$lcclassname$__descriptor);\n"
" protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);\n"
"}\n"

8
t/generated-code2/test-generated-code2.c Normal file → Executable file
View File

@ -2181,6 +2181,12 @@ test_message_check(void)
assert(1 == protobuf_c_message_check(&m.base));
}
static void
test_message_free_null (void)
{
foo__sub_mess__free_unpacked (NULL, NULL);
}
/* === simple testing framework === */
typedef void (*TestFunc) (void);
@ -2316,6 +2322,8 @@ static Test tests[] =
{ "test field flags", test_field_flags },
{ "test message_check()", test_message_check },
{ "test freeing NULL", test_message_free_null },
};
#define n_tests (sizeof(tests)/sizeof(Test))