Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for custom 'realloc' with a context #775 #779

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ConradWood
Copy link
Contributor

implements code and tests as discussed here #775
also patches example 'simple' to compile unconditionally (.PHONY) (because it seems useful for an example to reuse latest and it compiles in less than a second anyways) and with malloc support.

@PetteriAimonen
Copy link
Member

The implementation looks mostly good.
I have a few notes:

  1. The comments indicate that realloc() would be used for releasing, but I don't see the corresponding modification to pb_release_single_field(). It also looks like the test case does not check for releasing through the custom allocator, it could count the number of new allocations (ptr == NULL) and number of releases (size == 0).
  2. I don't see the point of changing examples/simple Makefile. That example is supposed to be basic and simple, and doesn't use dynamic allocation in any case.
  3. I would like the PB_ENABLE_MALLOC to remain the main and only switch for dynamic allocation. I.e. if it is not specified, PB_ENABLE_MALLOC_CONTEXT should add the extra field in pb_istream_t, but not enable dynamic allocation. I think in the next major release when breaking changes are made, I might make PB_ENABLE_MALLOC_CONTEXT default on, but PB_ENABLE_MALLOC will remain default off.
  4. The alltypes style test cases are a bit of a maintenance burden. I'll probably merge custom_realloc to alltypes_pointer test case at some point. But I guess it is reasonable to have it separate for now when PB_ENABLE_MALLOC_CONTEXT remains a default-off feature.

@ConradWood
Copy link
Contributor Author

ConradWood commented May 16, 2022 via email

@PetteriAimonen
Copy link
Member

I agree, in your use case where allocator releases all allocations made in context, pb_release() is optional.

For general use, having stream passed to pb_release() makes most sense to me.
I think it could be made an optional parameter, and if the stream is NULL then pb_release() would internally do:

pb_istream_t default = {NULL, NULL, 0, &pb_free_wrapper};
if (!stream) stream = &default;

To provide a reasonable upgrade path to get clean API for 0.5 and also backward compatibility for 0.4 series, I suggest the following on header side:

#ifdef PB_ENABLE_MALLOC_CONTEXT
/* stream is optionally used for error messages and to set realloc function, if NULL then pb_free() is used */
void pb_release(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct);
#else
void pb_release(const pb_msgdesc_t *fields, void *dest_struct);
#endif

@@ -43,6 +43,10 @@ struct pb_istream_s
#ifndef PB_NO_ERRMSG
const char *errmsg;
#endif
#ifdef PB_ENABLE_MALLOC_CONTEXT
/* Note: we do not need a corresponding free() here, because we can (and do) call realloc with size==0 to free memory. */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meta data might be keeped so it won't completely free ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All definitions of realloc I've seen fully free the object. See e.g. https://pubs.opengroup.org/onlinepubs/009695299/functions/realloc.html "If size is 0 and ptr is not a null pointer, the object pointed to is freed."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like C17/C23 deprecate the zero size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants