From 862339e5cd5ed1b0e779667723c7af533df957ba Mon Sep 17 00:00:00 2001 From: Bborie Park Date: Tue, 25 Sep 2012 14:07:22 +0000 Subject: [PATCH] Added correct handling of rt_band's ownsdata flag indicating if the memory used for the band's data (only for inline, not offline) is managed internally. git-svn-id: http://svn.osgeo.org/postgis/trunk@10327 b70326c6-7e19-0410-871a-916f4a2858ee --- raster/loader/raster2pgsql.c | 2 +- raster/rt_core/rt_api.c | 43 +++++++++++++++++++++++++----------- raster/rt_core/rt_api.h | 8 ++++++- raster/test/core/testapi.c | 5 +---- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/raster/loader/raster2pgsql.c b/raster/loader/raster2pgsql.c index b88bd5b94..7c329381e 100644 --- a/raster/loader/raster2pgsql.c +++ b/raster/loader/raster2pgsql.c @@ -83,7 +83,7 @@ raster_destroy(rt_raster raster) { uint16_t nbands = rt_raster_get_num_bands(raster); for (i = 0; i < nbands; i++) { rt_band band = rt_raster_get_band(raster, i); - if (!rt_band_is_offline(band)) { + if (!rt_band_is_offline(band) && !rt_band_get_ownsdata_flag(band)) { void* mem = rt_band_get_data(band); if (mem) rtdealloc(mem); } diff --git a/raster/rt_core/rt_api.c b/raster/rt_core/rt_api.c index b3e694eaf..26c3b398e 100644 --- a/raster/rt_core/rt_api.c +++ b/raster/rt_core/rt_api.c @@ -1317,7 +1317,7 @@ rt_band_new_inline( band->hasnodata = hasnodata; band->nodataval = 0; band->data.mem = data; - band->ownsData = 0; + band->ownsdata = 0; /* we do NOT own this data!!! */ band->isnodata = FALSE; band->raster = NULL; @@ -1380,6 +1380,7 @@ rt_band_new_offline( band->hasnodata = hasnodata; band->nodataval = 0; band->isnodata = FALSE; + band->ownsdata = 0; band->raster = NULL; /* properly set nodataval as it may need to be constrained to the data type */ @@ -1389,11 +1390,7 @@ rt_band_new_offline( return NULL; } - /* XXX QUESTION (jorgearevalo): What does exactly ownsData mean?? I think that - * ownsData = 0 ==> the memory for band->data is externally owned - * ownsData = 1 ==> the memory for band->data is internally owned - */ - band->ownsData = 0; + band->ownsdata = 0; /* offline, flag is useless as all offline data cache is owned internally */ band->data.offline.bandNum = bandNum; @@ -1453,6 +1450,7 @@ rt_band_duplicate(rt_band band) { band->hasnodata, band->nodataval, data ); + rt_band_set_ownsdata_flag(rtn, 1); /* we DO own this data!!! */ } if (rtn == NULL) @@ -1484,11 +1482,10 @@ rt_band_destroy(rt_band band) { /* offline band and has data, free as data is internally owned */ if (band->offline && band->data.offline.mem != NULL) rtdealloc(band->data.offline.mem); + /* inline band and band owns the data */ + else if (!band->offline && band->data.mem != NULL && band->ownsdata) + rtdealloc(band->data.mem); - /* band->data content is externally owned */ - /* XXX jorgearevalo: not really... rt_band_from_wkb allocates memory for - * data.mem - */ rtdealloc(band); } @@ -1707,6 +1704,22 @@ rt_band_get_height(rt_band band) { return band->height; } +/* Get ownsdata flag */ +int +rt_band_get_ownsdata_flag(rt_band band) { + assert(NULL != band); + + return (int) band->ownsdata; +} + +/* set ownsdata flag */ +void +rt_band_set_ownsdata_flag(rt_band band, int flag) { + assert(NULL != band); + + band->ownsdata = (int8_t) flag; +} + #ifdef OPTIMIZE_SPACE /* @@ -4836,6 +4849,7 @@ rt_band_reclass(rt_band srcband, rt_pixtype pixtype, rtdealloc(mem); return 0; } + rt_band_set_ownsdata_flag(band, 1); /* we DO own this data!!! */ RASTER_DEBUGF(3, "rt_band_reclass: new band @ %p", band); for (x = 0; x < width; x++) { @@ -5364,13 +5378,14 @@ rt_raster_add_band(rt_raster raster, rt_band band, int index) { raster->numBands++; - RASTER_DEBUGF(4, "now raster has %d bands", raster->numBands); + RASTER_DEBUGF(4, "Raster now has %d bands", raster->numBands); return index; } /** * Generate a new inline band and add it to a raster. + * Memory is allocated in this function for band data. * * @param raster : the raster to add a band to * @param pixtype: the pixel type for the new band @@ -5547,6 +5562,7 @@ rt_raster_generate_new_band(rt_raster raster, rt_pixtype pixtype, rtdealloc(mem); return -1; } + rt_band_set_ownsdata_flag(band, 1); /* we DO own this data!!! */ index = rt_raster_add_band(raster, band, index); numbands = rt_raster_get_num_bands(raster); if (numbands == oldnumbands || index == -1) { @@ -7086,7 +7102,7 @@ rt_band_from_wkb(uint16_t width, uint16_t height, return 0; } - band->ownsData = 1; + band->ownsdata = 0; band->data.offline.path = rtalloc(sz + 1); memcpy(band->data.offline.path, *ptr, sz); @@ -7119,6 +7135,7 @@ rt_band_from_wkb(uint16_t width, uint16_t height, return 0; } + band->ownsdata = 1; /* we DO own this data!!! */ memcpy(band->data.mem, *ptr, sz); *ptr += sz; @@ -7930,7 +7947,7 @@ rt_raster_deserialize(void* serialized, int header_only) { band->isnodata = BANDTYPE_IS_NODATA(type) ? 1 : 0; band->width = rast->width; band->height = rast->height; - band->ownsData = 0; + band->ownsdata = 0; /* we do NOT own this data!!! */ band->raster = rast; /* Advance by data padding */ diff --git a/raster/rt_core/rt_api.h b/raster/rt_core/rt_api.h index 1f0cfd5e5..656b49f01 100644 --- a/raster/rt_core/rt_api.h +++ b/raster/rt_core/rt_api.h @@ -466,6 +466,12 @@ uint16_t rt_band_get_width(rt_band band); /* Get height of this band */ uint16_t rt_band_get_height(rt_band band); +/* Get own_data flag */ +int rt_band_get_ownsdata_flag(rt_band band); + +/* set ownsdata flag */ +void rt_band_set_ownsdata_flag(rt_band band, int flag); + /** * Get pointer to raster band data * @@ -1957,7 +1963,7 @@ struct rt_band_t { int32_t isnodata; /* a flag indicating if this band is filled only with nodata values */ double nodataval; /* int will be converted ... */ - int32_t ownsData; /* XXX mloskot: its behaviour needs to be documented */ + int8_t ownsdata; /* 0, externally owned. 1, internally owned. only applies to data.mem */ rt_raster raster; /* reference to parent raster */ diff --git a/raster/test/core/testapi.c b/raster/test/core/testapi.c index 2865d9f88..af8fadcd3 100644 --- a/raster/test/core/testapi.c +++ b/raster/test/core/testapi.c @@ -42,7 +42,7 @@ deepRelease(rt_raster raster) for (i=0; i