Kaynağa Gözat

libsegment: + all in memory cache

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@73498 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz 6 yıl önce
ebeveyn
işleme
581ec989e8

+ 2 - 0
include/segment.h

@@ -58,6 +58,8 @@ typedef struct
     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>

+ 13 - 6
lib/segment/close.c

@@ -8,7 +8,7 @@
  *
  * \author GRASS GIS Development Team
  *
- * \date 2012
+ * \date 2018
  */
 
 #include <unistd.h>
@@ -35,12 +35,19 @@ int Segment_close(SEGMENT *SEG)
     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;
 }

+ 3 - 4
lib/segment/format.c

@@ -9,7 +9,7 @@
  *
  * \author GRASS GIS Development Team
  *
- * \date 2005-2009
+ * \date 2005-2018
  */
 
 #include <stdio.h>
@@ -105,9 +105,8 @@ int Segment_format_nofill(int fd, off_t nrows, off_t ncols, int srows, int scols
 }
 
 
-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;

+ 7 - 1
lib/segment/get.c

@@ -9,7 +9,7 @@
  *
  * \author GRASS GIS Development Team
  *
- * \date 2005-2009
+ * \date 2005-2018
  */
 
 #include <string.h>
@@ -40,6 +40,12 @@ int Segment_get(SEGMENT * SEG, void *buf, off_t row, off_t col)
 {
     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;

+ 7 - 1
lib/segment/get_row.c

@@ -9,7 +9,7 @@
  *
  * \author GRASS GIS Development Team
  *
- * \date 2005-2009
+ * \date 2005-2018
  */
 
 #include <stdio.h>
@@ -47,6 +47,12 @@ int Segment_get_row(const SEGMENT * SEG, void *buf, off_t row)
     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;

+ 21 - 1
lib/segment/open.c

@@ -9,7 +9,7 @@
  *
  * \author GRASS GIS Development Team
  *
- * \date 2012
+ * \date 2018
  */
 
 #include <unistd.h>
@@ -48,6 +48,26 @@ Segment_open(SEGMENT *SEG, char *fname, off_t nrows, off_t ncols,
              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"));

+ 7 - 1
lib/segment/put.c

@@ -9,7 +9,7 @@
  *
  * \author GRASS GIS Development Team
  *
- * \date 2005-2009
+ * \date 2005-2018
  */
 
 #include <string.h>
@@ -46,6 +46,12 @@ int Segment_put(SEGMENT * SEG, const void *buf, off_t row, off_t col)
 {
     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");

+ 7 - 1
lib/segment/put_row.c

@@ -9,7 +9,7 @@
  *
  * \author GRASS GIS Development Team
  *
- * \date 2005-2009
+ * \date 2005-2018
  */
 
 #include <stdio.h>
@@ -50,6 +50,12 @@ int Segment_put_row(const SEGMENT * SEG, const void *buf, off_t row)
     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;