-
Notifications
You must be signed in to change notification settings - Fork 472
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
Use calloc for potentially large user-defined inputs #100
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for doing this, @isaachier 😃
Np thanks for pointing this out @dfellis. 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, are we sure it covers all cases? I'd imagine we'd be most concerned with arrays of hexagons.
@@ -791,7 +792,7 @@ void H3_EXPORT(polyfill)(const GeoPolygon* geoPolygon, int res, H3Index* out) { | |||
// This first part is identical to the maxPolyfillSize above. | |||
|
|||
// Get the bounding boxes for the polygon and any holes | |||
STACK_ARRAY_CALLOC(BBox, bboxes, geoPolygon->numHoles + 1); | |||
BBox* bboxes = calloc(geoPolygon->numHoles + 1, sizeof(BBox)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this one's a risk - it scales with the number of polygons, not the number of hexagons, and should be fine with stack allocation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I say better safe than sorry here. What if someone provides a geofence of the US, but just does it lazily by shoving in all of the county geofences in a huge array? (Or attempting to guarantee exact set matching if they need national and county level hexagon sets as a more rational explanation.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Point taken. I didn't realize we were removing all instances of STACK_ARRAY_CALLOC
, in which case sure. No real preference here.
So I'm pretty sure @isaachier has removed all uses of
Two uses in So the only stack allocations are now constant amounts of memory, which is much safer. (It would be nice if we could eliminate all arbitrary memory allocations from the library and let the user decide on stack vs heap allocation themselves, but that would require some API changes and some awkward API usage patterns, where some blocks of memory are requested to be freed immediately by the user of the API.) |
@dfellis many C libraries let you override the default allocator. Here's an example from one of my favorite C libraries, Jansson:
It just stores a custom |
Something to consider, but that proposed implementation wouldn't be threadsafe if you want to choose the allocator based on the function call or data volume. Go too far and we'll start reimplementing C++. 😉 |
You can always use pthreads... but I get the point. Globals aren't straightforward. |
e978d55
to
70a099f
Compare
Use calloc for potentially large user-defined inputs
No description provided.