[GRASS-SVN] r73500 - in grass/branches/releasebranch_7_6: include lib/segment
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Oct 8 10:16:26 PDT 2018
Author: mmetz
Date: 2018-10-08 10:16:26 -0700 (Mon, 08 Oct 2018)
New Revision: 73500
Modified:
grass/branches/releasebranch_7_6/include/segment.h
grass/branches/releasebranch_7_6/lib/segment/close.c
grass/branches/releasebranch_7_6/lib/segment/format.c
grass/branches/releasebranch_7_6/lib/segment/get.c
grass/branches/releasebranch_7_6/lib/segment/get_row.c
grass/branches/releasebranch_7_6/lib/segment/open.c
grass/branches/releasebranch_7_6/lib/segment/put.c
grass/branches/releasebranch_7_6/lib/segment/put_row.c
Log:
libsegment: + all in memory cache (backport trunk r73498)
Modified: grass/branches/releasebranch_7_6/include/segment.h
===================================================================
--- grass/branches/releasebranch_7_6/include/segment.h 2018-10-08 16:22:10 UTC (rev 73499)
+++ grass/branches/releasebranch_7_6/include/segment.h 2018-10-08 17:16:26 UTC (rev 73500)
@@ -58,6 +58,8 @@
int nseg; /* number of segments in memory */
int cur; /* last accessed segment */
int offset; /* offset of data past header */
+
+ char *cache; /* all in memory cache */
} SEGMENT;
#include <grass/defs/segment.h>
Modified: grass/branches/releasebranch_7_6/lib/segment/close.c
===================================================================
--- grass/branches/releasebranch_7_6/lib/segment/close.c 2018-10-08 16:22:10 UTC (rev 73499)
+++ grass/branches/releasebranch_7_6/lib/segment/close.c 2018-10-08 17:16:26 UTC (rev 73500)
@@ -8,7 +8,7 @@
*
* \author GRASS GIS Development Team
*
- * \date 2012
+ * \date 2018
*/
#include <unistd.h>
@@ -35,12 +35,19 @@
if (SEG->open != 1)
return -1;
- Segment_release(SEG);
- close(SEG->fd);
- unlink(SEG->fname);
+ if (SEG->scb) {
+ Segment_release(SEG);
+ close(SEG->fd);
+ unlink(SEG->fname);
- SEG->fd = -1;
- SEG->fname = NULL;
+ SEG->fd = -1;
+ SEG->fname = NULL;
+ }
+ else {
+ G_free(SEG->cache);
+ }
+ SEG->open = 0;
+
return 1;
}
Modified: grass/branches/releasebranch_7_6/lib/segment/format.c
===================================================================
--- grass/branches/releasebranch_7_6/lib/segment/format.c 2018-10-08 16:22:10 UTC (rev 73499)
+++ grass/branches/releasebranch_7_6/lib/segment/format.c 2018-10-08 17:16:26 UTC (rev 73500)
@@ -9,7 +9,7 @@
*
* \author GRASS GIS Development Team
*
- * \date 2005-2009
+ * \date 2005-2018
*/
#include <stdio.h>
@@ -105,9 +105,8 @@
}
-static int seg_format(int fd,
- off_t nrows, off_t ncols,
- int srows, int scols, int len, int fill)
+static int seg_format(int fd, off_t nrows, off_t ncols,
+ int srows, int scols, int len, int fill)
{
off_t nbytes;
int spr, size;
Modified: grass/branches/releasebranch_7_6/lib/segment/get.c
===================================================================
--- grass/branches/releasebranch_7_6/lib/segment/get.c 2018-10-08 16:22:10 UTC (rev 73499)
+++ grass/branches/releasebranch_7_6/lib/segment/get.c 2018-10-08 17:16:26 UTC (rev 73500)
@@ -9,7 +9,7 @@
*
* \author GRASS GIS Development Team
*
- * \date 2005-2009
+ * \date 2005-2018
*/
#include <string.h>
@@ -40,6 +40,12 @@
{
int index, n, i;
+ if (!SEG->scb) {
+ memcpy(buf, SEG->cache + ((size_t)row * SEG->ncols + col) * SEG->len, SEG->len);
+
+ return 1;
+ }
+
SEG->address(SEG, row, col, &n, &index);
if ((i = seg_pagein(SEG, n)) < 0)
return -1;
Modified: grass/branches/releasebranch_7_6/lib/segment/get_row.c
===================================================================
--- grass/branches/releasebranch_7_6/lib/segment/get_row.c 2018-10-08 16:22:10 UTC (rev 73499)
+++ grass/branches/releasebranch_7_6/lib/segment/get_row.c 2018-10-08 17:16:26 UTC (rev 73500)
@@ -9,7 +9,7 @@
*
* \author GRASS GIS Development Team
*
- * \date 2005-2009
+ * \date 2005-2018
*/
#include <stdio.h>
@@ -47,6 +47,12 @@
int scols;
int n, index;
+ if (!SEG->scb) {
+ memcpy(buf, SEG->cache + ((size_t)row * SEG->ncols) * SEG->len, SEG->len * SEG->ncols);
+
+ return 1;
+ }
+
ncols = SEG->ncols - SEG->spill;
scols = SEG->scols;
size = scols * SEG->len;
Modified: grass/branches/releasebranch_7_6/lib/segment/open.c
===================================================================
--- grass/branches/releasebranch_7_6/lib/segment/open.c 2018-10-08 16:22:10 UTC (rev 73499)
+++ grass/branches/releasebranch_7_6/lib/segment/open.c 2018-10-08 17:16:26 UTC (rev 73500)
@@ -9,7 +9,7 @@
*
* \author GRASS GIS Development Team
*
- * \date 2012
+ * \date 2018
*/
#include <unistd.h>
@@ -48,7 +48,27 @@
int srows, int scols, int len, int nseg)
{
int ret;
+ int nseg_total;
+ nseg_total = ((nrows + srows - 1) / srows) *
+ ((ncols + scols - 1) / scols);
+
+ if (nseg >= nseg_total) {
+ G_verbose_message(_("Using memory cache"));
+
+ SEG->nrows = nrows;
+ SEG->ncols = ncols;
+ SEG->len = len;
+ SEG->nseg = nseg;
+ SEG->cache = G_malloc(sizeof(char) * SEG->nrows * SEG->ncols * SEG->len);
+ SEG->scb = NULL;
+ SEG->open = 1;
+
+ return 1;
+ }
+
+ G_verbose_message(_("Using disk cache"));
+
if (!fname) {
G_warning(_("Segment file name is NULL"));
return -1;
Modified: grass/branches/releasebranch_7_6/lib/segment/put.c
===================================================================
--- grass/branches/releasebranch_7_6/lib/segment/put.c 2018-10-08 16:22:10 UTC (rev 73499)
+++ grass/branches/releasebranch_7_6/lib/segment/put.c 2018-10-08 17:16:26 UTC (rev 73500)
@@ -9,7 +9,7 @@
*
* \author GRASS GIS Development Team
*
- * \date 2005-2009
+ * \date 2005-2018
*/
#include <string.h>
@@ -46,6 +46,12 @@
{
int index, n, i;
+ if (!SEG->scb) {
+ memcpy(SEG->cache + ((size_t)row * SEG->ncols + col) * SEG->len, buf, SEG->len);
+
+ return 1;
+ }
+
SEG->address(SEG, row, col, &n, &index);
if ((i = seg_pagein(SEG, n)) < 0) {
G_warning("segment lib: put: pagein failed");
Modified: grass/branches/releasebranch_7_6/lib/segment/put_row.c
===================================================================
--- grass/branches/releasebranch_7_6/lib/segment/put_row.c 2018-10-08 16:22:10 UTC (rev 73499)
+++ grass/branches/releasebranch_7_6/lib/segment/put_row.c 2018-10-08 17:16:26 UTC (rev 73500)
@@ -9,7 +9,7 @@
*
* \author GRASS GIS Development Team
*
- * \date 2005-2009
+ * \date 2005-2018
*/
#include <stdio.h>
@@ -50,6 +50,12 @@
int result;
off_t col;
+ if (!SEG->scb) {
+ memcpy(SEG->cache + ((size_t)row * SEG->ncols) * SEG->len, buf, SEG->len * SEG->ncols);
+
+ return 1;
+ }
+
ncols = SEG->ncols - SEG->spill;
scols = SEG->scols;
size = scols * SEG->len;
More information about the grass-commit
mailing list