]> granicus.if.org Git - postgis/commitdiff
Make the data of externally-loaded band internally owned so a call to rt_band_destroy...
authorBborie Park <bkpark at ucdavis.edu>
Mon, 9 Jan 2012 20:08:15 +0000 (20:08 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Mon, 9 Jan 2012 20:08:15 +0000 (20:08 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@8731 b70326c6-7e19-0410-871a-916f4a2858ee

raster/rt_core/rt_api.c
raster/rt_core/rt_api.h
raster/test/core/testapi.c

index 76ff011ae49025093445e6ff4ea24c2da85a55e7..c7961d67fa31f5198ba52c466eeb5f964f9f7dbc 100644 (file)
@@ -1131,6 +1131,10 @@ rt_band_destroy(rt_band band) {
 
     RASTER_DEBUGF(3, "Destroying rt_band @ %p", 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);
+
     /* band->data content is externally owned */
     /* XXX jorgearevalo: not really... rt_band_from_wkb allocates memory for
      * data.mem
@@ -1192,7 +1196,9 @@ rt_band_get_data(rt_band band) {
 }
 
 /**
-       * Load offline band's data
+       * Load offline band's data.  Loaded data is internally owned
+       * and should not be released by the caller.  Data will be
+       * released when band is destroyed with rt_band_destroy().
        *
        * @param band : the band who's data to get
        *
@@ -1304,6 +1310,12 @@ rt_band_load_offline_band(rt_band band) {
                return 1;
        }
 
+       /* band->data.offline.mem not NULL, free first */
+       if (band->data.offline.mem != NULL) {
+               rtdealloc(band->data.offline.mem);
+               band->data.offline.mem = NULL;
+       }
+
        band->data.offline.mem = _band->data.mem;
 
        rt_band_destroy(_band);
index 4e6ed8d0952cd63bf98701dc5f46b975176d6f87..c19884484f03bb30cd01df89743006bbeebe0bf3 100644 (file)
@@ -404,7 +404,9 @@ uint16_t rt_band_get_height(rt_band band);
 void* rt_band_get_data(rt_band band);
 
 /**
-       * Load offline band's data
+       * Load offline band's data.  Loaded data is internally owned
+       * and should not be released by the caller.  Data will be
+       * released when band is destroyed with rt_band_destroy().
        *
        * @param band : the band who's data to get
        *
@@ -1426,7 +1428,7 @@ struct rt_raster_t {
 struct rt_extband_t {
     uint8_t bandNum; /* 0-based */
     char* path; /* externally owned ? */
-               void *mem; /* loaded external band data */
+               void *mem; /* loaded external band data, internally owned */
 };
 
 struct rt_band_t {
index 12eabd04e9a1a95fe4c4861c63536fa206adabb6..0a2c45d66705926f013a143ba7210e63c84507d7 100644 (file)
@@ -22,7 +22,7 @@ addBand(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval)
     uint16_t height = rt_raster_get_height(raster);
 
     datasize = rt_pixtype_size(pixtype)*width*height;
-    mem = malloc(datasize);
+    mem = rtalloc(datasize);
 
     rt_band band = rt_band_new_inline(width, height,
                                       pixtype, hasnodata, nodataval, mem);
@@ -39,8 +39,10 @@ deepRelease(rt_raster raster)
     for (i=0; i<nbands; ++i)
     {
         rt_band band = rt_raster_get_band(raster, i);
-        void* mem = rt_band_get_data(band);
-        if ( mem ) free(mem);
+                               if (!rt_band_is_offline(band)) {
+               void* mem = rt_band_get_data(band);
+               if (mem) rtdealloc(mem);
+                               }
         rt_band_destroy(band);
     }
     rt_raster_destroy(raster);
@@ -1212,7 +1214,7 @@ static void testRasterReplaceBand() {
        height = rt_raster_get_height(raster);
 
        datasize = rt_pixtype_size(PT_8BUI) * width * height;
-       mem = malloc(datasize);
+       mem = rtalloc(datasize);
        band = rt_band_new_inline(width, height, PT_8BUI, 1, 1, mem);
        assert(band);
 
@@ -1259,11 +1261,11 @@ static void testBandReclass() {
        nodata = rt_band_get_nodata(band);
        CHECK_EQUALS(nodata, 0);
 
-       exprset = malloc(cnt * sizeof(rt_reclassexpr));
+       exprset = rtalloc(cnt * sizeof(rt_reclassexpr));
        assert(exprset);
 
        for (i = 0; i < cnt; i++) {
-               exprset[i] = malloc(sizeof(struct rt_reclassexpr_t));
+               exprset[i] = rtalloc(sizeof(struct rt_reclassexpr_t));
                assert(exprset[i]);
 
                if (i == 0) {
@@ -1309,12 +1311,12 @@ static void testBandReclass() {
        CHECK((rtn != -1));
        CHECK_EQUALS(val, 255);
 
-       for (i = cnt - 1; i >= 0; i--) free(exprset[i]);
-       free(exprset);
+       for (i = cnt - 1; i >= 0; i--) rtdealloc(exprset[i]);
+       rtdealloc(exprset);
        deepRelease(raster);
 
        mem = rt_band_get_data(newband);
-       if (mem) free(mem);
+       if (mem) rtdealloc(mem);
        rt_band_destroy(newband);
 }
 
@@ -1575,7 +1577,7 @@ static void testGDALRasterize() {
 
        /* hex to byte */
        wkb_len = (int) ceil(((double) strlen(wkb_hex)) / 2);
-       wkb = (unsigned char *) malloc(sizeof(unsigned char) * wkb_len);
+       wkb = (unsigned char *) rtalloc(sizeof(unsigned char) * wkb_len);
        for (i = 0; i < wkb_len; i++) {
                sscanf(pos, "%2hhx", &wkb[i]);
                pos += 2;
@@ -1595,7 +1597,7 @@ static void testGDALRasterize() {
                NULL
        );
 
-       free(wkb);
+       rtdealloc(wkb);
 
        CHECK(raster);
        CHECK((rt_raster_get_width(raster) == 100));