diff --git a/lib/raster/close.c b/lib/raster/close.c index 8de233d42fc..1a7bb636a56 100644 --- a/lib/raster/close.c +++ b/lib/raster/close.c @@ -11,7 +11,6 @@ * * \author USACERL and many others */ - #ifdef _WIN32 #include #endif @@ -82,7 +81,7 @@ static void write_fp_format(int fd); * overwrite the support files. See \ref * Raster_Map_Layer_Support_Routines for routines which write raster * support files. - * + * If the map is a new floating point, move the .tmp file * into the fcell element, create an empty file in the * cell directory; write the floating-point range file; write @@ -91,7 +90,7 @@ static void write_fp_format(int fd); * file, with max cat = max value (for backwards compatibility). Move * the .tmp NULL-value bitmap file to the cell_misc * directory. - * + * \param fd file descriptor * * \return void @@ -124,9 +123,9 @@ void Rast_close(int fd) * eventually remove the temporary file, but the file can be quite * large and will take up disk space until GRASS does remove it. Use * this routine as a courtesy to the user. - * + * \param fd file descriptor - * + * \return void */ void Rast_unopen(int fd) @@ -144,13 +143,13 @@ void Rast_unopen(int fd) /*! * \brief Unopen all raster maps - * + * Unopen all raster maps opened for write. Memory allocated for * raster processing is freed, and the temporary file created when the * raster map was opened is removed (see \ref * Creating_and_Opening_New_Raster_Files). This routine is useful when * errors are detected and it is desired to remove temporary files. - * + * \return void */ void Rast__unopen_all(void) @@ -198,7 +197,6 @@ static int close_old(int fd) G_free(fcb->name); if (fcb->reclass_flag) Rast_free_reclass(&fcb->reclass); - fcb->open_mode = -1; if (fcb->map_type != CELL_TYPE) { Rast_quant_free(&fcb->quant); @@ -206,6 +204,8 @@ static int close_old(int fd) if (fcb->data_fd >= 0) close(fcb->data_fd); +#pragma omp critical(R_RASTER_OPEN) + fcb->open_mode = -1; return 1; } @@ -347,7 +347,7 @@ static int close_new_gdal(int fd, int ok) Rast_close_gdal_link(fcb->gdal); } - fcb->open_mode = -1; + /* fcb->open_mode = -1; */ if (fcb->data != NULL) G_free(fcb->data); @@ -361,6 +361,9 @@ static int close_new_gdal(int fd, int ok) if (fcb->map_type != CELL_TYPE) Rast_quant_free(&fcb->quant); +#pragma omp critical(R_RASTER_OPEN) + fcb->open_mode = -1; + return stat; } @@ -477,7 +480,6 @@ static int close_new(int fd, int ok) sync_and_close(fcb->data_fd, (fcb->map_type == CELL_TYPE ? "cell" : "fcell"), fcb->name); - fcb->open_mode = -1; if (fcb->null_fd >= 0) { sync_and_close(fcb->null_fd, @@ -531,6 +533,8 @@ static int close_new(int fd, int ok) if (fcb->map_type != CELL_TYPE) Rast_quant_free(&fcb->quant); +#pragma omp critical(R_RASTER_OPEN) + fcb->open_mode = -1; return stat; } @@ -577,6 +581,7 @@ void Rast__close_null(int fd) G_free(fcb->null_bits); +#pragma omp critical(R_RASTER_OPEN) fcb->open_mode = -1; } diff --git a/lib/raster/open.c b/lib/raster/open.c index bf9b0fb1bb8..8d7c4d691db 100644 --- a/lib/raster/open.c +++ b/lib/raster/open.c @@ -11,7 +11,6 @@ * * \author USACERL and many others */ - #include #include #include @@ -27,43 +26,60 @@ #include "R.h" #define FORMAT_FILE "f_format" #define NULL_FILE "null" -/* cmpressed null file */ +/* compressed null file */ #define NULLC_FILE "nullcmpr" static int new_fileinfo(void) { - int oldsize = R__.fileinfo_count; - int newsize = oldsize; + int oldsize; + int newsize; int i; + int fd = -1; - for (i = 0; i < oldsize; i++) - if (R__.fileinfo[i].open_mode <= 0) { - memset(&R__.fileinfo[i], 0, sizeof(struct fileinfo)); - R__.fileinfo[i].open_mode = -1; - return i; +#pragma omp critical(R_RASTER_OPEN) + { + oldsize = R__.fileinfo_count; + newsize = oldsize; + + for (i = 0; i < oldsize; i++) { + if (R__.fileinfo[i].open_mode <= 0) { + memset(&R__.fileinfo[i], 0, sizeof(struct fileinfo)); + /* mark as reserved */ + R__.fileinfo[i].open_mode = 200; + fd = i; + break; + } } - if (newsize < 20) - newsize += 20; - else - newsize *= 2; + if (fd < 0) { + if (newsize < 20) + newsize += 20; + else + newsize *= 2; - R__.fileinfo = G_realloc(R__.fileinfo, newsize * sizeof(struct fileinfo)); + R__.fileinfo = + G_realloc(R__.fileinfo, newsize * sizeof(struct fileinfo)); - /* Mark all cell files as closed */ - for (i = oldsize; i < newsize; i++) { - memset(&R__.fileinfo[i], 0, sizeof(struct fileinfo)); - R__.fileinfo[i].open_mode = -1; - } + /* Mark all cell files as closed */ + for (i = oldsize; i < newsize; i++) { + memset(&R__.fileinfo[i], 0, sizeof(struct fileinfo)); + R__.fileinfo[i].open_mode = -1; + } + + R__.fileinfo_count = newsize; - R__.fileinfo_count = newsize; + /* mark as reserved */ + R__.fileinfo[oldsize].open_mode = 200; + fd = oldsize; + } + } - return oldsize; + return fd; } /*! * \brief Open raster file - * + * Arrange for the NULL-value bitmap to be read as well as the raster * map. If no NULL-value bitmap exists, arrange for the production of * NULL-values based on zeros in the raster map. If the map is @@ -73,11 +89,11 @@ static int new_fileinfo(void) * the floating point map using uing quant rules other than the ones * stored in map's quant file, he/she should call Rast_set_quant_rules() * after the call to Rast_open_old(). - * + * \param name map name * \param open_mode mode * \param map_type map type (CELL, FCELL, DCELL) - * + * \return open file descriptor ( >= 0) if successful */ static int open_raster_new(const char *name, int open_mode, @@ -302,7 +318,6 @@ int Rast__open_old(const char *name, const char *mapset) fcb->null_bits = Rast__allocate_null_bits(cellhd.cols); /* mark closed */ - fcb->open_mode = -1; /* save name and mapset */ fcb->name = G_store(name); @@ -528,7 +543,6 @@ static int open_raster_new_gdal(char *map, char *mapset, /* mark closed */ fcb->map_type = map_type; - fcb->open_mode = -1; fcb->gdal = Rast_create_gdal_link(map, map_type); if (!fcb->gdal) @@ -644,7 +658,6 @@ static int open_raster_new(const char *name, int open_mode, /* mark closed */ fcb->map_type = map_type; - fcb->open_mode = -1; fcb->gdal = NULL; fcb->vrt = NULL;