diff --git a/src/grib_api_internal.h b/src/grib_api_internal.h index 4b3fd8ccb..20be6ec50 100644 --- a/src/grib_api_internal.h +++ b/src/grib_api_internal.h @@ -518,7 +518,6 @@ struct grib_nearest { grib_arguments* args; /** args of iterator */ grib_handle* h; - grib_context* context; double* values; size_t values_count; grib_nearest_class* cclass; diff --git a/src/grib_nearest.cc b/src/grib_nearest.cc index d440771f7..d020fb269 100644 --- a/src/grib_nearest.cc +++ b/src/grib_nearest.cc @@ -322,7 +322,7 @@ int grib_nearest_find_generic( if ((ret = grib_nearest_get_radius(h, &radiusInKm)) != GRIB_SUCCESS) return ret; - neighbours = (PointStore*)grib_context_malloc(nearest->context, nvalues * sizeof(PointStore)); + neighbours = (PointStore*)grib_context_malloc(h->context, nvalues * sizeof(PointStore)); for (i = 0; i < nvalues; ++i) { neighbours[i].m_dist = 1e10; /* set all distances to large number to begin with */ neighbours[i].m_lat = 0; @@ -345,14 +345,14 @@ int grib_nearest_find_generic( *out_lats_count = (int)nvalues; if (*out_lats) - grib_context_free(nearest->context, *out_lats); - *out_lats = (double*)grib_context_malloc(nearest->context, nvalues * sizeof(double)); + grib_context_free(h->context, *out_lats); + *out_lats = (double*)grib_context_malloc(h->context, nvalues * sizeof(double)); if (!*out_lats) return GRIB_OUT_OF_MEMORY; if (*out_lons) - grib_context_free(nearest->context, *out_lons); - *out_lons = (double*)grib_context_malloc(nearest->context, nvalues * sizeof(double)); + grib_context_free(h->context, *out_lons); + *out_lons = (double*)grib_context_malloc(h->context, nvalues * sizeof(double)); if (!*out_lons) return GRIB_OUT_OF_MEMORY; @@ -418,7 +418,7 @@ int grib_nearest_find_generic( /* GRIB_NEAREST_SAME_XXX not yet implemented */ if (!*out_distances) { - *out_distances = (double*)grib_context_malloc(nearest->context, 4 * sizeof(double)); + *out_distances = (double*)grib_context_malloc(h->context, 4 * sizeof(double)); } (*out_distances)[0] = neighbours[0].m_dist; (*out_distances)[1] = neighbours[1].m_dist; diff --git a/src/grib_nearest_class_gen.cc b/src/grib_nearest_class_gen.cc index 7564dc790..c97e6b8b9 100644 --- a/src/grib_nearest_class_gen.cc +++ b/src/grib_nearest_class_gen.cc @@ -81,16 +81,15 @@ static int init(grib_nearest* nearest, grib_handle* h, grib_arguments* args) self->radius = grib_arguments_get_name(h, args, self->cargs++); nearest->values = NULL; - nearest->context = h->context; - return ret; } static int destroy(grib_nearest* nearest) { + grib_context* c = grib_context_get_default(); if (nearest->values) - grib_context_free(nearest->context, nearest->values); - grib_context_free(nearest->context, nearest); + grib_context_free(c, nearest->values); + grib_context_free(c, nearest); return GRIB_SUCCESS; } diff --git a/src/grib_nearest_class_healpix.cc b/src/grib_nearest_class_healpix.cc index 915556a76..d8618ba34 100644 --- a/src/grib_nearest_class_healpix.cc +++ b/src/grib_nearest_class_healpix.cc @@ -124,11 +124,13 @@ static int find(grib_nearest* nearest, grib_handle* h, static int destroy(grib_nearest* nearest) { grib_nearest_healpix* self = (grib_nearest_healpix*)nearest; - if (self->lats) grib_context_free(nearest->context, self->lats); - if (self->lons) grib_context_free(nearest->context, self->lons); - if (self->i) grib_context_free(nearest->context, self->i); - if (self->j) grib_context_free(nearest->context, self->j); - if (self->k) grib_context_free(nearest->context, self->k); - if (self->distances) grib_context_free(nearest->context, self->distances); + grib_context* c = grib_context_get_default(); + + if (self->lats) grib_context_free(c, self->lats); + if (self->lons) grib_context_free(c, self->lons); + if (self->i) grib_context_free(c, self->i); + if (self->j) grib_context_free(c, self->j); + if (self->k) grib_context_free(c, self->k); + if (self->distances) grib_context_free(c, self->distances); return GRIB_SUCCESS; } diff --git a/src/grib_nearest_class_lambert_azimuthal_equal_area.cc b/src/grib_nearest_class_lambert_azimuthal_equal_area.cc index 911827ffd..951f3e571 100644 --- a/src/grib_nearest_class_lambert_azimuthal_equal_area.cc +++ b/src/grib_nearest_class_lambert_azimuthal_equal_area.cc @@ -122,11 +122,13 @@ static int find(grib_nearest* nearest, grib_handle* h, static int destroy(grib_nearest* nearest) { grib_nearest_lambert_azimuthal_equal_area* self = (grib_nearest_lambert_azimuthal_equal_area*)nearest; - if (self->lats) grib_context_free(nearest->context, self->lats); - if (self->lons) grib_context_free(nearest->context, self->lons); - if (self->i) grib_context_free(nearest->context, self->i); - if (self->j) grib_context_free(nearest->context, self->j); - if (self->k) grib_context_free(nearest->context, self->k); - if (self->distances) grib_context_free(nearest->context, self->distances); + grib_context* c = grib_context_get_default(); + + if (self->lats) grib_context_free(c, self->lats); + if (self->lons) grib_context_free(c, self->lons); + if (self->i) grib_context_free(c, self->i); + if (self->j) grib_context_free(c, self->j); + if (self->k) grib_context_free(c, self->k); + if (self->distances) grib_context_free(c, self->distances); return GRIB_SUCCESS; } diff --git a/src/grib_nearest_class_lambert_conformal.cc b/src/grib_nearest_class_lambert_conformal.cc index 883f3a868..9f96c6489 100644 --- a/src/grib_nearest_class_lambert_conformal.cc +++ b/src/grib_nearest_class_lambert_conformal.cc @@ -122,11 +122,13 @@ static int find(grib_nearest* nearest, grib_handle* h, static int destroy(grib_nearest* nearest) { grib_nearest_lambert_conformal* self = (grib_nearest_lambert_conformal*)nearest; - if (self->lats) grib_context_free(nearest->context, self->lats); - if (self->lons) grib_context_free(nearest->context, self->lons); - if (self->i) grib_context_free(nearest->context, self->i); - if (self->j) grib_context_free(nearest->context, self->j); - if (self->k) grib_context_free(nearest->context, self->k); - if (self->distances) grib_context_free(nearest->context, self->distances); + grib_context* c = grib_context_get_default(); + + if (self->lats) grib_context_free(c, self->lats); + if (self->lons) grib_context_free(c, self->lons); + if (self->i) grib_context_free(c, self->i); + if (self->j) grib_context_free(c, self->j); + if (self->k) grib_context_free(c, self->k); + if (self->distances) grib_context_free(c, self->distances); return GRIB_SUCCESS; } diff --git a/src/grib_nearest_class_latlon_reduced.cc b/src/grib_nearest_class_latlon_reduced.cc index e5c503e99..ad07f3816 100644 --- a/src/grib_nearest_class_latlon_reduced.cc +++ b/src/grib_nearest_class_latlon_reduced.cc @@ -98,7 +98,7 @@ static int init(grib_nearest* nearest, grib_handle* h, grib_arguments* args) self->j = (size_t*)grib_context_malloc(h->context, 2 * sizeof(size_t)); if (!self->j) return GRIB_OUT_OF_MEMORY; - self->k = (size_t*)grib_context_malloc(nearest->context, 4 * sizeof(size_t)); + self->k = (size_t*)grib_context_malloc(h->context, 4 * sizeof(size_t)); if (!self->k) return GRIB_OUT_OF_MEMORY; @@ -195,15 +195,15 @@ static int find_global(grib_nearest* nearest, grib_handle* h, self->lats_count = n; if (self->lats) - grib_context_free(nearest->context, self->lats); - self->lats = (double*)grib_context_malloc(nearest->context, + grib_context_free(h->context, self->lats); + self->lats = (double*)grib_context_malloc(h->context, self->lats_count * sizeof(double)); if (!self->lats) return GRIB_OUT_OF_MEMORY; if (self->lons) - grib_context_free(nearest->context, self->lons); - self->lons = (double*)grib_context_malloc(nearest->context, + grib_context_free(h->context, self->lons); + self->lons = (double*)grib_context_malloc(h->context, nearest->values_count * sizeof(double)); if (!self->lons) return GRIB_OUT_OF_MEMORY; @@ -300,7 +300,7 @@ static int find_global(grib_nearest* nearest, grib_handle* h, } if (!self->distances) - self->distances = (double*)grib_context_malloc(nearest->context, 4 * sizeof(double)); + self->distances = (double*)grib_context_malloc(h->context, 4 * sizeof(double)); if (!self->distances) return GRIB_OUT_OF_MEMORY; @@ -420,16 +420,17 @@ static int find_global(grib_nearest* nearest, grib_handle* h, static int destroy(grib_nearest* nearest) { grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*)nearest; + grib_context* c = grib_context_get_default(); if (self->lats) - grib_context_free(nearest->context, self->lats); + grib_context_free(c, self->lats); if (self->lons) - grib_context_free(nearest->context, self->lons); + grib_context_free(c, self->lons); if (self->j) - grib_context_free(nearest->context, self->j); + grib_context_free(c, self->j); if (self->k) - grib_context_free(nearest->context, self->k); + grib_context_free(c, self->k); if (self->distances) - grib_context_free(nearest->context, self->distances); + grib_context_free(c, self->distances); return GRIB_SUCCESS; } diff --git a/src/grib_nearest_class_mercator.cc b/src/grib_nearest_class_mercator.cc index c966012d4..a160d9dfb 100644 --- a/src/grib_nearest_class_mercator.cc +++ b/src/grib_nearest_class_mercator.cc @@ -124,11 +124,13 @@ static int find(grib_nearest* nearest, grib_handle* h, static int destroy(grib_nearest* nearest) { grib_nearest_mercator* self = (grib_nearest_mercator*)nearest; - if (self->lats) grib_context_free(nearest->context, self->lats); - if (self->lons) grib_context_free(nearest->context, self->lons); - if (self->i) grib_context_free(nearest->context, self->i); - if (self->j) grib_context_free(nearest->context, self->j); - if (self->k) grib_context_free(nearest->context, self->k); - if (self->distances) grib_context_free(nearest->context, self->distances); + grib_context* c = grib_context_get_default(); + + if (self->lats) grib_context_free(c, self->lats); + if (self->lons) grib_context_free(c, self->lons); + if (self->i) grib_context_free(c, self->i); + if (self->j) grib_context_free(c, self->j); + if (self->k) grib_context_free(c, self->k); + if (self->distances) grib_context_free(c, self->distances); return GRIB_SUCCESS; } diff --git a/src/grib_nearest_class_polar_stereographic.cc b/src/grib_nearest_class_polar_stereographic.cc index a160db445..7f04e4ef8 100644 --- a/src/grib_nearest_class_polar_stereographic.cc +++ b/src/grib_nearest_class_polar_stereographic.cc @@ -122,11 +122,13 @@ static int find(grib_nearest* nearest, grib_handle* h, static int destroy(grib_nearest* nearest) { grib_nearest_polar_stereographic* self = (grib_nearest_polar_stereographic*)nearest; - if (self->lats) grib_context_free(nearest->context, self->lats); - if (self->lons) grib_context_free(nearest->context, self->lons); - if (self->i) grib_context_free(nearest->context, self->i); - if (self->j) grib_context_free(nearest->context, self->j); - if (self->k) grib_context_free(nearest->context, self->k); - if (self->distances) grib_context_free(nearest->context, self->distances); + grib_context* c = grib_context_get_default(); + + if (self->lats) grib_context_free(c, self->lats); + if (self->lons) grib_context_free(c, self->lons); + if (self->i) grib_context_free(c, self->i); + if (self->j) grib_context_free(c, self->j); + if (self->k) grib_context_free(c, self->k); + if (self->distances) grib_context_free(c, self->distances); return GRIB_SUCCESS; } diff --git a/src/grib_nearest_class_reduced.cc b/src/grib_nearest_class_reduced.cc index 713570264..ffc8eaad8 100644 --- a/src/grib_nearest_class_reduced.cc +++ b/src/grib_nearest_class_reduced.cc @@ -106,7 +106,7 @@ static int init(grib_nearest* nearest, grib_handle* h, grib_arguments* args) self->rotated = -1; if (!self->j) return GRIB_OUT_OF_MEMORY; - self->k = (size_t*)grib_context_malloc(nearest->context, NUM_NEIGHBOURS * sizeof(size_t)); + self->k = (size_t*)grib_context_malloc(h->context, NUM_NEIGHBOURS * sizeof(size_t)); if (!self->k) return GRIB_OUT_OF_MEMORY; grib_get_long(h, "global", &self->global); @@ -251,16 +251,14 @@ static int find_global(grib_nearest* nearest, grib_handle* h, self->lats_count = n; if (self->lats) - grib_context_free(nearest->context, self->lats); - self->lats = (double*)grib_context_malloc(nearest->context, - self->lats_count * sizeof(double)); + grib_context_free(h->context, self->lats); + self->lats = (double*)grib_context_malloc(h->context, self->lats_count * sizeof(double)); if (!self->lats) return GRIB_OUT_OF_MEMORY; if (self->lons) - grib_context_free(nearest->context, self->lons); - self->lons = (double*)grib_context_malloc(nearest->context, - nearest->values_count * sizeof(double)); + grib_context_free(h->context, self->lons); + self->lons = (double*)grib_context_malloc(h->context, nearest->values_count * sizeof(double)); if (!self->lons) return GRIB_OUT_OF_MEMORY; @@ -321,12 +319,11 @@ static int find_global(grib_nearest* nearest, grib_handle* h, } if (!self->distances) - self->distances = (double*)grib_context_malloc(nearest->context, NUM_NEIGHBOURS * sizeof(double)); + self->distances = (double*)grib_context_malloc(h->context, NUM_NEIGHBOURS * sizeof(double)); if (!self->distances) return GRIB_OUT_OF_MEMORY; - grib_binary_search(self->lats, ilat - 1, inlat, - &(self->j[0]), &(self->j[1])); + grib_binary_search(self->lats, ilat - 1, inlat, &(self->j[0]), &(self->j[1])); plsize = self->lats_count; if ((err = grib_get_size(h, self->pl, &plsize)) != GRIB_SUCCESS) @@ -522,16 +519,13 @@ static int find_global(grib_nearest* nearest, grib_handle* h, static int destroy(grib_nearest* nearest) { grib_nearest_reduced* self = (grib_nearest_reduced*)nearest; - if (self->lats) - grib_context_free(nearest->context, self->lats); - if (self->lons) - grib_context_free(nearest->context, self->lons); - if (self->j) - grib_context_free(nearest->context, self->j); - if (self->k) - grib_context_free(nearest->context, self->k); - if (self->distances) - grib_context_free(nearest->context, self->distances); + grib_context* c = grib_context_get_default(); + + if (self->lats) grib_context_free(c, self->lats); + if (self->lons) grib_context_free(c, self->lons); + if (self->j) grib_context_free(c, self->j); + if (self->k) grib_context_free(c, self->k); + if (self->distances) grib_context_free(c, self->distances); return GRIB_SUCCESS; } diff --git a/src/grib_nearest_class_regular.cc b/src/grib_nearest_class_regular.cc index adb5320a1..5d591a6e3 100644 --- a/src/grib_nearest_class_regular.cc +++ b/src/grib_nearest_class_regular.cc @@ -128,6 +128,7 @@ static int find(grib_nearest* nearest, grib_handle* h, double lat = 0, lon = 0; const bool is_rotated = is_rotated_grid(h); double angleOfRotation = 0, southPoleLat = 0, southPoleLon = 0; + grib_context* c = h->context; while (inlon < 0) inlon += 360; @@ -194,16 +195,14 @@ static int find(grib_nearest* nearest, grib_handle* h, self->lats_count = n; if (self->lats) - grib_context_free(nearest->context, self->lats); - self->lats = (double*)grib_context_malloc(nearest->context, - self->lats_count * sizeof(double)); + grib_context_free(c, self->lats); + self->lats = (double*)grib_context_malloc(c, self->lats_count * sizeof(double)); if (!self->lats) return GRIB_OUT_OF_MEMORY; if (self->lons) - grib_context_free(nearest->context, self->lons); - self->lons = (double*)grib_context_malloc(nearest->context, - self->lons_count * sizeof(double)); + grib_context_free(c, self->lons); + self->lons = (double*)grib_context_malloc(c, self->lons_count * sizeof(double)); if (!self->lons) return GRIB_OUT_OF_MEMORY; @@ -289,9 +288,9 @@ static int find(grib_nearest* nearest, grib_handle* h, &(self->i[0]), &(self->i[1])); if (!self->distances) - self->distances = (double*)grib_context_malloc(nearest->context, NUM_NEIGHBOURS * sizeof(double)); + self->distances = (double*)grib_context_malloc(c, NUM_NEIGHBOURS * sizeof(double)); if (!self->k) - self->k = (size_t*)grib_context_malloc(nearest->context, NUM_NEIGHBOURS * sizeof(size_t)); + self->k = (size_t*)grib_context_malloc(c, NUM_NEIGHBOURS * sizeof(size_t)); kk = 0; for (jj = 0; jj < 2; jj++) { for (ii = 0; ii < 2; ii++) { @@ -310,7 +309,7 @@ static int find(grib_nearest* nearest, grib_handle* h, * First unpack all the values into an array. Then when we need the 4 points * we just index into this array so no need to call grib_get_double_element_internal * - * if (nearest->values) grib_context_free(nearest->context,nearest->values); + * if (nearest->values) grib_context_free(c,nearest->values); * nearest->values = grib_context_malloc(h->context,nvalues*sizeof(double)); * if (!nearest->values) return GRIB_OUT_OF_MEMORY; * ret = grib_get_double_array(h, self->values_key, nearest->values ,&nvalues); @@ -362,17 +361,13 @@ static int find(grib_nearest* nearest, grib_handle* h, static int destroy(grib_nearest* nearest) { grib_nearest_regular* self = (grib_nearest_regular*)nearest; - if (self->lats) - grib_context_free(nearest->context, self->lats); - if (self->lons) - grib_context_free(nearest->context, self->lons); - if (self->i) - grib_context_free(nearest->context, self->i); - if (self->j) - grib_context_free(nearest->context, self->j); - if (self->k) - grib_context_free(nearest->context, self->k); - if (self->distances) - grib_context_free(nearest->context, self->distances); + grib_context* c = grib_context_get_default(); + + if (self->lats) grib_context_free(c, self->lats); + if (self->lons) grib_context_free(c, self->lons); + if (self->i) grib_context_free(c, self->i); + if (self->j) grib_context_free(c, self->j); + if (self->k) grib_context_free(c, self->k); + if (self->distances) grib_context_free(c, self->distances); return GRIB_SUCCESS; } diff --git a/src/grib_nearest_class_space_view.cc b/src/grib_nearest_class_space_view.cc index 8d471b8fa..ccba897b9 100644 --- a/src/grib_nearest_class_space_view.cc +++ b/src/grib_nearest_class_space_view.cc @@ -122,11 +122,13 @@ static int find(grib_nearest* nearest, grib_handle* h, static int destroy(grib_nearest* nearest) { grib_nearest_space_view* self = (grib_nearest_space_view*)nearest; - if (self->lats) grib_context_free(nearest->context, self->lats); - if (self->lons) grib_context_free(nearest->context, self->lons); - if (self->i) grib_context_free(nearest->context, self->i); - if (self->j) grib_context_free(nearest->context, self->j); - if (self->k) grib_context_free(nearest->context, self->k); - if (self->distances) grib_context_free(nearest->context, self->distances); + grib_context* c = grib_context_get_default(); + + if (self->lats) grib_context_free(c, self->lats); + if (self->lons) grib_context_free(c, self->lons); + if (self->i) grib_context_free(c, self->i); + if (self->j) grib_context_free(c, self->j); + if (self->k) grib_context_free(c, self->k); + if (self->distances) grib_context_free(c, self->distances); return GRIB_SUCCESS; }