59 #include <grass/gis.h>
60 #include <grass/glocale.h>
61 #include <grass/gstypes.h>
67 static int init_gsds(
void);
68 static int check_numsets(
void);
69 static dataset *get_dataset(
int);
70 static int get_type(dataset *);
72 static dataset *Data[
MAX_DS];
75 static int Numsets = 0;
77 static int Cur_id =
LUCKY;
79 static int Tot_mem = 0;
84 static int init_gsds(
void)
88 for (i = 0; i <
MAX_DS; i++) {
103 static int check_numsets(
void)
105 if (Numsets < Cur_max) {
123 static dataset *get_dataset(
int id)
127 for (i = 0; i < Numsets; i++) {
128 if (Data[i]->data_id ==
id) {
144 static int get_type(dataset * ds)
147 if (ds->databuff.bm) {
151 if (ds->databuff.cb) {
155 if (ds->databuff.sb) {
159 if (ds->databuff.ib) {
163 if (ds->databuff.fb) {
193 start = begin ? 0 : i + 1;
195 for (i = start; i < Numsets; i++) {
196 if (!strcmp(Data[i]->unique_name, name)) {
197 if ((Data[i]->changed & *changes) || !(Data[i]->changed)) {
198 if (get_type(Data[i]) & *types) {
199 *changes = Data[i]->changed;
200 *types = get_type(Data[i]);
202 return (Data[i]->data_id);
222 static int first = 1;
226 if (0 > init_gsds()) {
232 else if (0 > check_numsets()) {
244 new->data_id = Cur_id++;
246 for (i = 0; i < MAXDIMS; i++) {
250 new->unique_name =
G_store(name);
251 new->databuff.fb =
NULL;
252 new->databuff.ib =
NULL;
253 new->databuff.sb =
NULL;
254 new->databuff.cb =
NULL;
255 new->databuff.bm =
NULL;
256 new->databuff.nm =
NULL;
257 new->databuff.k = 0.0;
260 new->need_reload = 1;
262 return (new->data_id);
285 if ((ds = get_dataset(
id))) {
286 ds->changed = ds->changed | change_flag;
289 return (&(ds->databuff));
307 static char retstr[GPATH_MAX];
309 for (i = 0; i < Numsets; i++) {
310 if (Data[i]->data_id ==
id) {
312 strcpy(retstr, fds->unique_name);
336 for (i = 0; i < Numsets; i++) {
337 if (Data[i]->data_id ==
id) {
341 G_free((
void *)fds->unique_name);
342 fds->unique_name =
NULL;
345 for (j = i; j < (Numsets - 1); j++) {
346 Data[j] = Data[j + 1];
374 for (i = 0; i < Numsets; i++) {
375 if (Data[i]->data_id ==
id) {
395 int nsiz = 1, i, siz, freed = 0;
397 for (i = 0; i < ds->ndims; i++) {
401 if (typ & ATTY_NULL) {
402 if (ds->databuff.nm) {
405 ds->databuff.nm =
NULL;
410 if (typ & ATTY_MASK) {
411 if (ds->databuff.bm) {
414 ds->databuff.bm =
NULL;
419 if (typ & ATTY_CHAR) {
420 if (ds->databuff.cb) {
421 siz = nsiz *
sizeof(char);
422 free(ds->databuff.cb);
423 ds->databuff.cb =
NULL;
428 if (typ & ATTY_SHORT) {
429 if (ds->databuff.sb) {
430 siz = nsiz *
sizeof(short);
431 free(ds->databuff.sb);
432 ds->databuff.sb =
NULL;
437 if (typ & ATTY_INT) {
438 if (ds->databuff.ib) {
439 siz = nsiz *
sizeof(
int);
440 free(ds->databuff.ib);
441 ds->databuff.ib =
NULL;
446 if (typ & ATTY_FLOAT) {
447 if (ds->databuff.fb) {
448 siz = nsiz *
sizeof(float);
449 free(ds->databuff.fb);
450 ds->databuff.fb =
NULL;
456 ds->numbytes -= freed;
459 G_debug(5,
"free_data_buffs(): freed data from id no. %d",
462 "free_data_buffs(): %.3f Kbytes freed, current total = %.3f",
463 freed / 1000., Tot_mem / 1000.);
487 if ((ds = get_dataset(
id))) {
495 for (i = 0; i < ndims; i++) {
496 ds->dims[i] = dims[i];
507 if (
NULL == (ds->databuff.nm =
BM_create(dims[1], dims[0]))) {
521 if (
NULL == (ds->databuff.bm =
BM_create(dims[1], dims[0]))) {
534 (ds->databuff.cb = (
unsigned char *)G_malloc(siz))) {
545 siz *=
sizeof(short);
548 if (
NULL == (ds->databuff.sb = (
short *)G_malloc(siz))) {
562 if (
NULL == (ds->databuff.ib = (
int *)G_malloc(siz))) {
573 siz *=
sizeof(float);
576 if (
NULL == (ds->databuff.fb = (
float *)G_malloc(siz))) {
597 "gsds_alloc_typbuff(): %f Kbytes allocated, current total = %f",
598 siz / 1000., Tot_mem / 1000.);
618 if ((ds = get_dataset(
id))) {
619 return ((
int)ds->changed);
638 if ((ds = get_dataset(
id))) {
639 ds->changed = reason;
656 ds = get_dataset(
id);
658 return (get_type(ds));
void G_free(void *buf)
Free allocated memory.
char * G_store(const char *s)
Copy string to allocated memory.
int gsds_findh(const char *name, IFLAG *changes, IFLAG *types, int begin)
Get handle to gsds.
int free_data_buffs(dataset *ds, int typ)
Free data buffer.
typbuff * gsds_get_typbuff(int id, IFLAG change_flag)
Get data buffer.
int BM_destroy(struct BM *map)
Destroy bitmap and free all associated memory.
int gsds_alloc_typbuff(int id, int *dims, int ndims, int type)
Allocates correct buffer according to type, keeps track of total mem.
int gsds_get_type(int id)
ADD.
char * gsds_get_name(int id)
Get name.
int gsds_free_data_buff(int id, int typ)
Free allocated buffer.
int gsds_get_changed(int id)
ADD.
struct BM * BM_create(int x, int y)
Create bitmap of dimension x/y and return structure token.
int G_debug(int level, const char *msg,...)
Print debugging message.
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int gsds_newh(const char *name)
Get handle to gsds.
int gsds_set_changed(int id, IFLAG reason)
ADD.
int BM_get_map_size(struct BM *map)
Returns size in bytes that bitmap is taking up.
int gsds_free_datah(int id)
Free allocated dataset.