| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
#include <sys/types.h> |
| 10 |
#include <sys/stat.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include "global.h" |
| 14 |
|
| 15 |
struct cache *readcell(int fdi, int size, int target_env) |
| 16 |
{ |
| 17 |
DCELL *tmpbuf; |
| 18 |
struct cache *c; |
| 19 |
int nrows; |
| 20 |
int ncols; |
| 21 |
int row; |
| 22 |
char *filename; |
| 23 |
int nx, ny; |
| 24 |
int nblocks; |
| 25 |
int i; |
| 26 |
|
| 27 |
if (target_env) { |
| 28 |
select_target_env(); |
| 29 |
nrows = Rast_output_window_rows(); |
| 30 |
ncols = Rast_output_window_cols(); |
| 31 |
} |
| 32 |
else { |
| 33 |
select_current_env(); |
| 34 |
nrows = Rast_input_window_rows(); |
| 35 |
ncols = Rast_input_window_cols(); |
| 36 |
} |
| 37 |
G_srand48(0); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
if (target_env) |
| 42 |
select_current_env(); |
| 43 |
|
| 44 |
ny = (nrows + BDIM - 1) / BDIM; |
| 45 |
nx = (ncols + BDIM - 1) / BDIM; |
| 46 |
|
| 47 |
if (size > 0) |
| 48 |
nblocks = size * ((1 << 20) / sizeof(block)); |
| 49 |
else |
| 50 |
nblocks = (nx + ny) * 2; |
| 51 |
|
| 52 |
if (nblocks > nx * ny) |
| 53 |
nblocks = nx * ny; |
| 54 |
|
| 55 |
c = G_malloc(sizeof(struct cache)); |
| 56 |
c->stride = nx; |
| 57 |
c->nblocks = nblocks; |
| 58 |
c->grid = (block **)G_calloc(nx * ny, sizeof(block *)); |
| 59 |
c->blocks = (block *)G_malloc(nblocks * sizeof(block)); |
| 60 |
c->refs = (int *)G_malloc(nblocks * sizeof(int)); |
| 61 |
|
| 62 |
if (nblocks < nx * ny) { |
| 63 |
filename = G_tempfile(); |
| 64 |
c->fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600); |
| 65 |
if (c->fd < 0) |
| 66 |
G_fatal_error(_("Unable to open temporary file")); |
| 67 |
remove(filename); |
| 68 |
} |
| 69 |
else |
| 70 |
c->fd = -1; |
| 71 |
|
| 72 |
G_debug(1, "%d of %d blocks in memory", nblocks, nx * ny); |
| 73 |
|
| 74 |
G_important_message(_("Allocating memory and reading input map...")); |
| 75 |
G_percent(0, nrows, 5); |
| 76 |
|
| 77 |
for (i = 0; i < c->nblocks; i++) |
| 78 |
c->refs[i] = -1; |
| 79 |
|
| 80 |
tmpbuf = (DCELL *)G_malloc(nx * sizeof(block)); |
| 81 |
|
| 82 |
if (target_env) |
| 83 |
select_target_env(); |
| 84 |
for (row = 0; row < nrows; row += BDIM) { |
| 85 |
int x, y; |
| 86 |
|
| 87 |
for (y = 0; y < BDIM; y++) { |
| 88 |
G_percent(row + y, nrows, 5); |
| 89 |
|
| 90 |
if (row + y >= nrows) |
| 91 |
break; |
| 92 |
|
| 93 |
Rast_get_d_row(fdi, &tmpbuf[y * nx * BDIM], row + y); |
| 94 |
} |
| 95 |
|
| 96 |
for (x = 0; x < nx; x++) |
| 97 |
for (y = 0; y < BDIM; y++) |
| 98 |
if (c->fd >= 0) { |
| 99 |
if (write(c->fd, &tmpbuf[(y * nx + x) * BDIM], |
| 100 |
BDIM * sizeof(DCELL)) < 0) |
| 101 |
G_fatal_error(_("Error writing segment file")); |
| 102 |
} |
| 103 |
else |
| 104 |
memcpy(&c->blocks[BKIDX(c, HI(row), x)][LO(y)][0], |
| 105 |
&tmpbuf[(y * nx + x) * BDIM], BDIM * sizeof(DCELL)); |
| 106 |
} |
| 107 |
|
| 108 |
G_free(tmpbuf); |
| 109 |
|
| 110 |
if (c->fd < 0) |
| 111 |
for (i = 0; i < c->nblocks; i++) { |
| 112 |
c->grid[i] = &c->blocks[i]; |
| 113 |
c->refs[i] = i; |
| 114 |
} |
| 115 |
|
| 116 |
if (target_env) |
| 117 |
select_current_env(); |
| 118 |
|
| 119 |
return c; |
| 120 |
} |
| 121 |
|
| 122 |
block *get_block(struct cache *c, int idx) |
| 123 |
{ |
| 124 |
int replace = G_lrand48() % c->nblocks; |
| 125 |
block *p = &c->blocks[replace]; |
| 126 |
int cref = c->refs[replace]; |
| 127 |
off_t offset = (off_t)idx * sizeof(DCELL) << L2BSIZE; |
| 128 |
|
| 129 |
if (c->fd < 0) |
| 130 |
G_fatal_error(_("Internal error: cache miss on fully-cached map")); |
| 131 |
|
| 132 |
if (cref >= 0) |
| 133 |
c->grid[cref] = NULL; |
| 134 |
|
| 135 |
c->grid[idx] = p; |
| 136 |
c->refs[replace] = idx; |
| 137 |
|
| 138 |
if (lseek(c->fd, offset, SEEK_SET) == -1) |
| 139 |
G_fatal_error(_("Error seeking on segment file")); |
| 140 |
|
| 141 |
if (read(c->fd, p, sizeof(block)) < 0) |
| 142 |
G_fatal_error(_("Error reading segment file")); |
| 143 |
|
| 144 |
return p; |
| 145 |
} |
| 146 |
|
| 147 |
void release_cache(struct cache *c) |
| 148 |
{ |
| 149 |
G_free(c->refs); |
| 150 |
G_free(c->blocks); |
| 151 |
G_free(c->grid); |
| 152 |
|
| 153 |
G_free(c); |
| 154 |
c = NULL; |
| 155 |
} |