12 #define IS_ACTIVE_ELT(elt) (c->locks[elt] != 2)
13 #define IS_NOT_ACTIVE_ELT(elt) (c->locks[elt] == 2)
14 #define IS_LOCKED_ELT(elt) (c->locks[elt] == 1)
15 #define IS_UNLOCKED_ELT(elt) (c->locks[elt] == 0)
16 #define IS_NOT_IN_QUEUE_ELT(elt) (IS_LOCKED_ELT(elt))
17 #define IS_IN_QUEUE_ELT(elt) (!IS_NOT_IN_QUEUE_ELT(elt))
19 #define DEACTIVATE_ELT(elt) \
20 ((IS_LOCKED_ELT(elt) ? (c->nofUnlocked)++ : (0)), c->locks[elt] = 2)
21 #define LOCK_ELT(elt) \
22 ((IS_LOCKED_ELT(elt) ? (0) : (c->nofUnlocked)--), (c->locks[elt] = 1))
23 #define UNLOCK_ELT(elt) \
24 ((IS_LOCKED_ELT(elt) ? (c->nofUnlocked)++ : (0)), (c->locks[elt] = 0))
26 #define ONE_UNLOCKED_ELT_ONLY (c->first == c->last)
27 #define ARE_MIN_UNLOCKED (c->nofUnlocked <= c->minUnlocked)
35 for (i = 0; i < c->
nofElts; i++) {
55 static int cache_dummy_fun(
int tileIndex
UNUSED,
const void *tileBuf
UNUSED,
87 int (*eltRemoveFun)(
int,
const void *,
void *),
88 void *eltRemoveFunData,
89 int (*eltLoadFun)(
int,
void *,
void *),
97 Rast3d_error(
"Rast3d_cache_new: error in Rast3d_malloc");
115 Rast3d_error(
"Rast3d_cache_new: error in Rast3d_malloc");
119 for (i = 0; i < tmp->
nofElts; i++)
130 Rast3d_error(
"Rast3d_cache_new: error in Rast3d_cache_hash_new");
142 int (*eltRemoveFun)(
int,
const void *,
void *),
143 void *eltRemoveFunData)
152 int (*eltLoadFun)(
int,
void *,
void *),
153 void *eltLoadFunData)
162 read_fn *eltLoadFun,
void *eltLoadFunData)
165 NULL, eltLoadFun, eltLoadFunData);
175 if (index == c->
first)
177 if (index == c->
last)
180 if (c->
next[index] != -1)
182 if (c->
prev[index] != -1)
185 c->
next[index] = c->
prev[index] = -1;
190 static void cache_queue_enqueue(
RASTER3D_cache *c,
int left,
int index)
195 if (c->
first == -1) {
214 c->
prev[index] = left;
216 if (c->
next[left] == -1) {
217 c->
next[left] = index;
225 c->
next[left] = index;
237 cache_queue_dequeue(c, c->
first);
247 cache_queue_enqueue(c, c->
last, index);
254 cache_queue_enqueue(c, -1, index);
284 cache_queue_dequeue(c, index);
297 cache_queue_dequeue(c, index);
316 cache_queue_append(c, index);
328 for (index = 0; index < c->
nofElts; index++)
332 "Rast3d_cache_unlock_all: error in Rast3d_cache_unlock");
345 for (index = 0; index < c->
nofElts; index++)
381 Rast3d_error(
"Rast3d_cache_deactivate_elt : name not in cache");
389 cache_queue_dequeue(c, index);
396 Rast3d_error(
"cache_remove_elt: error in c->eltRemoveFun");
400 cache_queue_preppend(c, index);
412 if (!cache_remove_elt(c,
name, 0)) {
413 Rast3d_error(
"Rast3d_cache_remove_elt: error in cache_remove_elt");
424 if (!cache_remove_elt(c,
name, 1)) {
425 Rast3d_error(
"Rast3d_cache_flush: error in cache_remove_elt");
438 for (index = 0; index < c->
nofElts; index++)
442 "Rast3d_cache_remove_elt");
455 for (index = 0; index < c->
nofElts; index++)
459 "Rast3d_cache_flush_all: error in Rast3d_cache_flush");
470 int index, oldName, doUnlock;
485 oldName = c->
names[index];
489 Rast3d_error(
"Rast3d_cache_elt_ptr: error in c->eltRemoveFun");
504 Rast3d_error(
"Rast3d_cache_elt_ptr: error in Rast3d_cache_unlock");
509 Rast3d_error(
"Rast3d_cache_elt_ptr: error in c->eltLoadFun");
521 Rast3d_error(
"Rast3d_cache_load: error in Rast3d_cache_elt_ptr");
536 Rast3d_error(
"Rast3d_cache_get_elt: error in Rast3d_cache_elt_ptr");
553 Rast3d_error(
"Rast3d_cache_put_elt: error in Rast3d_cache_elt_ptr");
580 printf(
"\n--------------------------------\n");
581 for (i = 0; i < c->
nofElts; i++) {
582 printf(
"elt %d: ", i);
590 printf(
"name %d val %d %s\n", c->
names[i], a[17],
595 printf(
"\n--------------------------------\n");
603 static int cache_test_flush_fun(
int name,
const void *eltPtr,
void *data
UNUSED)
605 printf(
"flushing name %d value %d\n",
name, ((
const int *)eltPtr)[17]);
616 } cache_test_data_type;
618 static int cache_test_load_fun(
int name,
void *eltPtr,
void *data)
622 printf(
"loading name %d value %d\n",
name,
623 ((cache_test_data_type *)data)->value[17]);
625 src = ((cache_test_data_type *)data)->value;
626 memcpy(eltPtr, src, ((cache_test_data_type *)data)->size);
633 static cache_test_data_type ctd;
635 static void cache_test_add(
void *c,
int name,
int val)
637 static int firstTime = 1;
657 NULL, cache_test_load_fun, &ctd);
661 cache_test_add(c, 1111, -11);
663 cache_test_add(c, 2222, -22);
665 cache_test_add(c, 3333, -33);
667 cache_test_add(c, 4444, -44);
673 cache_test_add(c, 5555, -55);
675 cache_test_add(c, 6666, -66);
677 cache_test_add(c, 7777, -77);
679 cache_test_add(c, 8888, -88);
681 cache_test_add(c, 9999, -99);
687 cache_test_add(c, 1111, -11);
689 cache_test_add(c, 2222, -22);
691 cache_test_add(c, 3333, -33);
695 cache_test_add(c, 1111, -11);
697 cache_test_add(c, 2222, -22);
699 cache_test_add(c, 3333, -33);
int Rast3d_cache_lock(RASTER3D_cache *c, int name)
void * Rast3d_cache_new_read(int nofElts, int sizeOfElts, int nofNames, read_fn *eltLoadFun, void *eltLoadFunData)
#define IS_ACTIVE_ELT(elt)
int Rast3d_cache_get_elt(RASTER3D_cache *c, int name, void *dst)
void * Rast3d_cache_elt_ptr(RASTER3D_cache *c, int name)
void Rast3d_cache_reset(RASTER3D_cache *c)
#define ONE_UNLOCKED_ELT_ONLY
int Rast3d_cache_flush_all(RASTER3D_cache *c)
#define IS_IN_QUEUE_ELT(elt)
int Rast3d_cache_unlock_all(RASTER3D_cache *c)
void Rast3d_cache_set_min_unlock(RASTER3D_cache *c, int nofMinUnLocked)
int Rast3d_cache_remove_all(RASTER3D_cache *c)
void Rast3d_cache_set_load_fun(RASTER3D_cache *c, int(*eltLoadFun)(int, void *, void *), void *eltLoadFunData)
void Rast3d_cache_dispose(RASTER3D_cache *c)
int Rast3d_cache_load(RASTER3D_cache *c, int name)
void Rast3d_cache_autolock_on(RASTER3D_cache *c)
void Rast3d_cache_set_remove_fun(RASTER3D_cache *c, int(*eltRemoveFun)(int, const void *, void *), void *eltRemoveFunData)
int Rast3d_cache_put_elt(RASTER3D_cache *c, int name, const void *src)
#define IS_NOT_IN_QUEUE_ELT(elt)
void * Rast3d_cache_new(int nofElts, int sizeOfElts, int nofNames, int(*eltRemoveFun)(int, const void *, void *), void *eltRemoveFunData, int(*eltLoadFun)(int, void *, void *), void *eltLoadFunData)
int Rast3d_cache_unlock(RASTER3D_cache *c, int name)
int Rast3d_cache_flush(RASTER3D_cache *c, int name)
int Rast3d_cache_lock_all(RASTER3D_cache *c)
#define DEACTIVATE_ELT(elt)
#define IS_LOCKED_ELT(elt)
void Rast3d_cache_autolock_off(RASTER3D_cache *c)
#define IS_NOT_ACTIVE_ELT(elt)
void Rast3d_cache_lock_intern(RASTER3D_cache *c, int index)
int Rast3d_cache_remove_elt(RASTER3D_cache *c, int name)
#define IS_UNLOCKED_ELT(elt)
void Rast3d_free(void *)
Same as free (ptr).
void Rast3d_cache_hash_dispose(Rast3d_cache_hash *)
void Rast3d_cache_hash_remove_name(Rast3d_cache_hash *, int)
void Rast3d_cache_hash_reset(Rast3d_cache_hash *)
void * Rast3d_cache_hash_new(int)
void Rast3d_cache_hash_load_name(Rast3d_cache_hash *, int, int)
void Rast3d_error(const char *,...) __attribute__((format(printf
int Rast3d_cache_hash_name2index(Rast3d_cache_hash *, int)
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
void * Rast3d_malloc(int)
Same as malloc (nBytes), except that in case of error Rast3d_error() is invoked.
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
int read_fn(int, void *, void *)
int(* eltRemoveFun)(int, const void *, void *)
int(* eltLoadFun)(int, void *, void *)