Skip to content

Commit 587b0a0

Browse files
committed
qhull: Use std::vector for memory blocks.
1 parent 62925ed commit 587b0a0

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

src/qhull_wrap.cpp

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern const char qh_version[];
1818
#endif
1919
#include "libqhull_r/qhull_ra.h"
2020
#include <cstdio>
21+
#include <vector>
2122

2223

2324
#ifndef MPL_DEVNULL
@@ -51,7 +52,7 @@ get_facet_vertices(qhT* qh, const facetT* facet, int indices[3])
5152
/* Return the indices of the 3 triangles that are neighbors of the specified
5253
* facet (triangle). */
5354
static void
54-
get_facet_neighbours(const facetT* facet, const int* tri_indices,
55+
get_facet_neighbours(const facetT* facet, std::vector<int>& tri_indices,
5556
int indices[3])
5657
{
5758
facetT *neighbor, **neighborp;
@@ -103,12 +104,12 @@ delaunay_impl(npy_intp npoints, const double* x, const double* y,
103104
{
104105
qhT qh_qh; /* qh variable type and name must be like */
105106
qhT* qh = &qh_qh; /* this for Qhull macros to work correctly. */
106-
coordT* points = NULL;
107+
std::vector<coordT> points;
107108
facetT* facet;
108109
int i, ntri, max_facet_id;
109110
FILE* error_file = NULL; /* qhull expects a FILE* to write errors to. */
110111
int exitcode; /* Value returned from qh_new_qhull(). */
111-
int* tri_indices = NULL; /* Maps qhull facet id to triangle index. */
112+
std::vector<int> tri_indices; /* Maps qhull facet id to triangle index. */
112113
int indices[3];
113114
int curlong, totlong; /* Memory remaining after qh_memfreeshort. */
114115
PyObject* tuple; /* Return tuple (triangles, neighbors). */
@@ -124,12 +125,7 @@ delaunay_impl(npy_intp npoints, const double* x, const double* y,
124125
QHULL_LIB_CHECK
125126

126127
/* Allocate points. */
127-
points = (coordT*)malloc(npoints*ndim*sizeof(coordT));
128-
if (points == NULL) {
129-
PyErr_SetString(PyExc_MemoryError,
130-
"Could not allocate points array in qhull.delaunay");
131-
goto error_before_qhull;
132-
}
128+
points.resize(npoints * ndim);
133129

134130
/* Determine mean x, y coordinates. */
135131
for (i = 0; i < npoints; ++i) {
@@ -152,9 +148,7 @@ delaunay_impl(npy_intp npoints, const double* x, const double* y,
152148
* setupext.py and passed in via the macro MPL_DEVNULL. */
153149
error_file = fopen(STRINGIFY(MPL_DEVNULL), "w");
154150
if (error_file == NULL) {
155-
PyErr_SetString(PyExc_RuntimeError,
156-
"Could not open devnull in qhull.delaunay");
157-
goto error_before_qhull;
151+
throw std::runtime_error("Could not open devnull");
158152
}
159153
}
160154
else {
@@ -164,7 +158,7 @@ delaunay_impl(npy_intp npoints, const double* x, const double* y,
164158

165159
/* Perform Delaunay triangulation. */
166160
qh_zero(qh, error_file);
167-
exitcode = qh_new_qhull(qh, ndim, (int)npoints, points, False,
161+
exitcode = qh_new_qhull(qh, ndim, (int)npoints, points.data(), False,
168162
(char*)"qhull d Qt Qbb Qc Qz", NULL, error_file);
169163
if (exitcode != qh_ERRnone) {
170164
PyErr_Format(PyExc_RuntimeError,
@@ -189,12 +183,7 @@ delaunay_impl(npy_intp npoints, const double* x, const double* y,
189183
max_facet_id = qh->facet_id - 1;
190184

191185
/* Create array to map facet id to triangle index. */
192-
tri_indices = (int*)malloc((max_facet_id+1)*sizeof(int));
193-
if (tri_indices == NULL) {
194-
PyErr_SetString(PyExc_MemoryError,
195-
"Could not allocate triangle map in qhull.delaunay");
196-
goto error;
197-
}
186+
tri_indices.resize(max_facet_id+1);
198187

199188
/* Allocate Python arrays to return. */
200189
dims[0] = ntri;
@@ -251,8 +240,6 @@ delaunay_impl(npy_intp npoints, const double* x, const double* y,
251240
if (hide_qhull_errors) {
252241
fclose(error_file);
253242
}
254-
free(tri_indices);
255-
free(points);
256243

257244
tuple = PyTuple_New(2);
258245
PyTuple_SetItem(tuple, 0, (PyObject*)triangles);
@@ -269,10 +256,6 @@ delaunay_impl(npy_intp npoints, const double* x, const double* y,
269256
if (hide_qhull_errors) {
270257
fclose(error_file);
271258
}
272-
free(tri_indices);
273-
274-
error_before_qhull:
275-
free(points);
276259

277260
return NULL;
278261
}

0 commit comments

Comments
 (0)