ext/h3/src/src/h3lib/lib/linkedGeo.c in h3-3.6.2 vs ext/h3/src/src/h3lib/lib/linkedGeo.c in h3-3.7.1
- old
+ new
@@ -16,23 +16,26 @@
/** @file linkedGeo.c
* @brief Linked data structure for geo data
*/
#include "linkedGeo.h"
+
#include <assert.h>
#include <stdlib.h>
+
+#include "alloc.h"
#include "geoCoord.h"
#include "h3api.h"
/**
* Add a linked polygon to the current polygon
* @param polygon Polygon to add link to
* @return Pointer to new polygon
*/
LinkedGeoPolygon* addNewLinkedPolygon(LinkedGeoPolygon* polygon) {
assert(polygon->next == NULL);
- LinkedGeoPolygon* next = calloc(1, sizeof(*next));
+ LinkedGeoPolygon* next = H3_MEMORY(calloc)(1, sizeof(*next));
assert(next != NULL);
polygon->next = next;
return next;
}
@@ -40,11 +43,11 @@
* Add a new linked loop to the current polygon
* @param polygon Polygon to add loop to
* @return Pointer to loop
*/
LinkedGeoLoop* addNewLinkedLoop(LinkedGeoPolygon* polygon) {
- LinkedGeoLoop* loop = calloc(1, sizeof(*loop));
+ LinkedGeoLoop* loop = H3_MEMORY(calloc)(1, sizeof(*loop));
assert(loop != NULL);
return addLinkedLoop(polygon, loop);
}
/**
@@ -69,11 +72,11 @@
* @param loop Loop to add coordinate to
* @param vertex Coordinate to add
* @return Pointer to the coordinate
*/
LinkedGeoCoord* addLinkedCoord(LinkedGeoLoop* loop, const GeoCoord* vertex) {
- LinkedGeoCoord* coord = malloc(sizeof(*coord));
+ LinkedGeoCoord* coord = H3_MEMORY(malloc)(sizeof(*coord));
assert(coord != NULL);
*coord = (LinkedGeoCoord){.vertex = *vertex, .next = NULL};
LinkedGeoCoord* last = loop->last;
if (last == NULL) {
assert(loop->first == NULL);
@@ -93,11 +96,11 @@
void destroyLinkedGeoLoop(LinkedGeoLoop* loop) {
LinkedGeoCoord* nextCoord;
for (LinkedGeoCoord* currentCoord = loop->first; currentCoord != NULL;
currentCoord = nextCoord) {
nextCoord = currentCoord->next;
- free(currentCoord);
+ H3_MEMORY(free)(currentCoord);
}
}
/**
* Free all allocated memory for a linked geo structure. The caller is
@@ -113,18 +116,18 @@
currentPolygon = nextPolygon) {
for (LinkedGeoLoop* currentLoop = currentPolygon->first;
currentLoop != NULL; currentLoop = nextLoop) {
destroyLinkedGeoLoop(currentLoop);
nextLoop = currentLoop->next;
- free(currentLoop);
+ H3_MEMORY(free)(currentLoop);
}
nextPolygon = currentPolygon->next;
if (skip) {
// do not free the input polygon
skip = false;
} else {
- free(currentPolygon);
+ H3_MEMORY(free)(currentPolygon);
}
}
}
/**
@@ -240,13 +243,14 @@
if (polygonCount == 0) {
return NULL;
}
// Initialize arrays for candidate loops and their bounding boxes
const LinkedGeoPolygon** candidates =
- malloc(polygonCount * sizeof(LinkedGeoPolygon*));
+ H3_MEMORY(malloc)(polygonCount * sizeof(LinkedGeoPolygon*));
assert(candidates != NULL);
- const BBox** candidateBBoxes = malloc(polygonCount * sizeof(BBox*));
+ const BBox** candidateBBoxes =
+ H3_MEMORY(malloc)(polygonCount * sizeof(BBox*));
assert(candidateBBoxes != NULL);
// Find all polygons that contain the loop
int candidateCount = 0;
int index = 0;
@@ -265,12 +269,12 @@
// The most deeply nested container is the immediate parent
const LinkedGeoPolygon* parent =
findDeepestContainer(candidates, candidateBBoxes, candidateCount);
// Free allocated memory
- free(candidates);
- free(candidateBBoxes);
+ H3_MEMORY(free)(candidates);
+ H3_MEMORY(free)(candidateBBoxes);
return parent;
}
/**
@@ -306,15 +310,16 @@
int outerCount = 0;
// Create an array to hold all of the inner loops. Note that
// this array will never be full, as there will always be fewer
// inner loops than outer loops.
- LinkedGeoLoop** innerLoops = malloc(loopCount * sizeof(LinkedGeoLoop*));
+ LinkedGeoLoop** innerLoops =
+ H3_MEMORY(malloc)(loopCount * sizeof(LinkedGeoLoop*));
assert(innerLoops != NULL);
// Create an array to hold the bounding boxes for the outer loops
- BBox* bboxes = malloc(loopCount * sizeof(BBox));
+ BBox* bboxes = H3_MEMORY(malloc)(loopCount * sizeof(BBox));
assert(bboxes != NULL);
// Get the first loop and unlink it from root
LinkedGeoLoop* loop = root->first;
*root = (LinkedGeoPolygon){0};
@@ -347,17 +352,17 @@
// If we can't find a polygon (possible with invalid input), then
// we need to release the memory for the hole, because the loop has
// been unlinked from the root and the caller will no longer have
// a way to destroy it with destroyLinkedPolygon.
destroyLinkedGeoLoop(innerLoops[i]);
- free(innerLoops[i]);
+ H3_MEMORY(free)(innerLoops[i]);
resultCode = NORMALIZATION_ERR_UNASSIGNED_HOLES;
}
}
// Free allocated memory
- free(innerLoops);
- free(bboxes);
+ H3_MEMORY(free)(innerLoops);
+ H3_MEMORY(free)(bboxes);
return resultCode;
}
// Define macros used in polygon algos for LinkedGeoLoop