]> granicus.if.org Git - postgis/commitdiff
Added correct handling of rt_band's ownsdata flag indicating if the
authorBborie Park <bkpark at ucdavis.edu>
Tue, 25 Sep 2012 14:07:22 +0000 (14:07 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Tue, 25 Sep 2012 14:07:22 +0000 (14:07 +0000)
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
raster/rt_core/rt_api.c
raster/rt_core/rt_api.h
raster/test/core/testapi.c

index b88bd5b947a2238cc0093197b5e1650366577935..7c329381e91f7e4fa354686362cf7255090625e6 100644 (file)
@@ -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);
                }
index b3e694eaf5507131e04203c14317085b2d70343b..26c3b398ebf74f1e87effc8aedb51d9435c838ea 100644 (file)
@@ -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 */
index 1f0cfd5e5a4e5c112aee90b6ea91acbdc480cd03..656b49f0181d2ee89463c8a5ff019eb3edb7d564 100644 (file)
@@ -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 */
 
index 2865d9f883c26a4d9953d4d629d7b89fb79bca41..af8fadcd34259e297a852c6b3ae831bba930cf69 100644 (file)
@@ -42,7 +42,7 @@ deepRelease(rt_raster 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);
                                }
@@ -1446,7 +1446,6 @@ static void testBandReclass() {
        int rtn;
        rt_band newband;
        double val;
-       void *mem = NULL;
 
        raster = rt_raster_new(100, 10);
        assert(raster); /* or we're out of virtual memory */
@@ -1518,8 +1517,6 @@ static void testBandReclass() {
        rtdealloc(exprset);
        deepRelease(raster);
 
-       mem = rt_band_get_data(newband);
-       if (mem) rtdealloc(mem);
        rt_band_destroy(newband);
 }