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) ((IS_LOCKED_ELT(elt) ? \ 
   20                               (c->nofUnlocked)++ : (0)), \ 
   22 #define LOCK_ELT(elt) ((IS_LOCKED_ELT(elt) ? \ 
   23                         (0) : (c->nofUnlocked)--), \ 
   25 #define UNLOCK_ELT(elt) ((IS_LOCKED_ELT(elt) ? \ 
   26                           (c->nofUnlocked)++ : (0)), \ 
   29 #define ONE_UNLOCKED_ELT_ONLY (c->first == c->last) 
   30 #define ARE_MIN_UNLOCKED (c->nofUnlocked <= c->minUnlocked) 
   38     for (i = 0; i < c->nofElts; i++) {
 
   45     c->prev[0] = c->next[c->nofElts - 1] = -1;
 
   47     c->last = c->nofElts - 1;
 
   50     c->nofUnlocked = c->nofElts;
 
   58 static int cache_dummy_fun(
int tileIndex, 
const void *tileBuf, 
void *map)
 
   89                     int (*eltRemoveFun) (), 
void *eltRemoveFunData,
 
   90                     int (*eltLoadFun) (), 
void *eltLoadFunData)
 
   97         G3d_error(
"G3d_cache_new: error in G3d_malloc");
 
  103     tmp->nofElts = nofElts;
 
  104     tmp->eltSize = sizeOfElts;
 
  105     tmp->elts = 
G3d_malloc(tmp->eltSize * tmp->nofElts);
 
  106     tmp->names = 
G3d_malloc(
sizeof(
int) * tmp->nofElts);
 
  108     tmp->next = 
G3d_malloc(
sizeof(
int) * tmp->nofElts);
 
  109     tmp->prev = 
G3d_malloc(
sizeof(
int) * tmp->nofElts);
 
  111     if ((tmp->elts == 
NULL) || (tmp->names == 
NULL) || (tmp->locks == 
NULL) ||
 
  112         (tmp->next == 
NULL) || (tmp->prev == 
NULL)) {
 
  115         G3d_error(
"G3d_cache_new: error in G3d_malloc");
 
  120     for(i = 0; i < tmp->nofElts; i++)
 
  123     tmp->eltRemoveFun = eltRemoveFun;
 
  124     tmp->eltRemoveFunData = eltRemoveFunData;
 
  125     tmp->eltLoadFun = eltLoadFun;
 
  126     tmp->eltLoadFunData = eltLoadFunData;
 
  129     if (tmp->hash == 
NULL) {
 
  131         G3d_error(
"G3d_cache_new: error in G3d_cache_hash_new");
 
  144                         void *eltRemoveFunData)
 
  146     c->eltRemoveFun = eltRemoveFun;
 
  147     c->eltRemoveFunData = eltRemoveFunData;
 
  154                       void *eltLoadFunData)
 
  156     c->eltLoadFun = eltLoadFun;
 
  157     c->eltLoadFunData = eltLoadFunData;
 
  163                          read_fn * eltLoadFun, 
void *eltLoadFunData)
 
  166                          cache_dummy_fun, 
NULL, eltLoadFun, eltLoadFunData);
 
  171 static void cache_queue_dequeue(G3D_cache * c, 
int index)
 
  176     if (index == c->first)
 
  177         c->first = c->next[index];
 
  178     if (index == c->last)
 
  179         c->last = c->prev[index];
 
  181     if (c->next[index] != -1)
 
  182         c->prev[c->next[index]] = c->prev[index];
 
  183     if (c->prev[index] != -1)
 
  184         c->next[c->prev[index]] = c->next[index];
 
  186     c->next[index] = c->prev[index] = -1;
 
  191 static void cache_queue_enqueue(G3D_cache * c, 
int left, 
int index)
 
  196     if (c->first == -1) {
 
  200         c->first = c->last = index;
 
  209         c->next[index] = c->first;
 
  210         c->prev[c->first] = index;
 
  216     c->prev[index] = left;
 
  218     if (c->next[left] == -1) {
 
  219         c->next[left] = index;
 
  225     c->prev[c->next[left]] = index;
 
  226     c->next[index] = c->next[left];
 
  227     c->next[left] = index;
 
  232 static int cache_queue_get_top(G3D_cache * c)
 
  238     cache_queue_dequeue(c, c->first);
 
  245 static void cache_queue_append(G3D_cache * c, 
int index)
 
  247     cache_queue_enqueue(c, c->last, index);
 
  252 static void cache_queue_preppend(G3D_cache * c, 
int index)
 
  254     cache_queue_enqueue(c, -1, index);
 
  273         G3d_error(
"G3d_cache_lock: name not in cache");
 
  284     cache_queue_dequeue(c, index);
 
  297     cache_queue_dequeue(c, index);
 
  309         G3d_error(
"G3d_cache_unlock: name not in cache");
 
  316     cache_queue_append(c, index);
 
  328     for (index = 0; index < c->nofElts; index++)
 
  331                 G3d_error(
"G3d_cache_unlock_all: error in G3d_cache_unlock");
 
  344     for (index = 0; index < c->nofElts; index++)
 
  369     c->minUnlocked = nofMinUnLocked;
 
  374 static int cache_remove_elt(G3D_cache * c, 
int name, 
int doFlush)
 
  380         G3d_error(
"G3d_cache_deactivate_elt : name not in cache");
 
  388         cache_queue_dequeue(c, index);
 
  393         if (!c->eltRemoveFun(name, c->elts + c->eltSize * index,
 
  394                              c->eltRemoveFunData)) {
 
  395             G3d_error(
"cache_remove_elt: error in c->eltRemoveFun");
 
  399     cache_queue_preppend(c, index);
 
  411     if (!cache_remove_elt(c, name, 0)) {
 
  412         G3d_error(
"G3d_cache_remove_elt: error in cache_remove_elt");
 
  423     if (!cache_remove_elt(c, name, 1)) {
 
  424         G3d_error(
"G3d_cache_flush: error in cache_remove_elt");
 
  437     for (index = 0; index < c->nofElts; index++)
 
  441                     (
"G3d_cache_remove_all: error in G3d_cache_remove_elt");
 
  454     for (index = 0; index < c->nofElts; index++)
 
  457                 G3d_error(
"G3d_cache_flush_all: error in G3d_cache_flush");
 
  468     int index, oldName, doUnlock;
 
  478         return c->elts + c->eltSize * index;
 
  483         oldName = c->names[index];
 
  485         if (!c->eltRemoveFun(oldName, c->elts + c->eltSize * index,
 
  486                              c->eltRemoveFunData)) {
 
  487             G3d_error(
"G3d_cache_elt_ptr: error in c->eltRemoveFun");
 
  497     c->names[index] = 
name;
 
  502             G3d_error(
"G3d_cache_elt_ptr: error in G3d_cache_unlock");
 
  506     if (!c->eltLoadFun(name, c->elts + c->eltSize * index, c->eltLoadFunData)) {
 
  507         G3d_error(
"G3d_cache_elt_ptr: error in c->eltLoadFun");
 
  511     return c->elts + c->eltSize * index;
 
  519         G3d_error(
"G3d_cache_load: error in G3d_cache_elt_ptr");
 
  534         G3d_error(
"G3d_cache_get_elt: error in G3d_cache_elt_ptr");
 
  538     memcpy(dst, elt, c->eltSize);
 
  551         G3d_error(
"G3d_cache_put_elt: error in G3d_cache_elt_ptr");
 
  555     memcpy(elt, src, c->eltSize);
 
  570 static void cache_test_print(G3D_cache * c)
 
  578     printf(
"\n--------------------------------\n");
 
  579     for (i = 0; i < c->nofElts; i++) {
 
  580         printf(
"elt %d: ", i);
 
  588         printf(
"name %d val %d %s\n", c->names[i], a[17],
 
  592     printf(
"\n--------------------------------\n");
 
  600 static int cache_test_flush_fun(
int name, 
const void *eltPtr, 
void *
data)
 
  602     printf(
"flushing name %d value %d\n", name, ((
const int *)eltPtr)[17]);
 
  616 static int cache_test_load_fun(
int name, 
void *eltPtr, 
void *data)
 
  620     printf(
"loading name %d value %d\n", name,
 
  633 static void cache_test_add(
void *c, 
int name, 
int val)
 
  635     static int firstTime = 1;
 
  643     ctd.
size = ((G3D_cache *) c)->eltSize;
 
  655                       cache_test_flush_fun, 
NULL, cache_test_load_fun, &ctd);
 
  659     cache_test_add(c, 1111, -11);
 
  661     cache_test_add(c, 2222, -22);
 
  663     cache_test_add(c, 3333, -33);
 
  665     cache_test_add(c, 4444, -44);
 
  671     cache_test_add(c, 5555, -55);
 
  673     cache_test_add(c, 6666, -66);
 
  675     cache_test_add(c, 7777, -77);
 
  677     cache_test_add(c, 8888, -88);
 
  679     cache_test_add(c, 9999, -99);
 
  685     cache_test_add(c, 1111, -11);
 
  687     cache_test_add(c, 2222, -22);
 
  689     cache_test_add(c, 3333, -33);
 
  693     cache_test_add(c, 1111, -11);
 
  695     cache_test_add(c, 2222, -22);
 
  697     cache_test_add(c, 3333, -33);
 
void * G3d_cache_new(int nofElts, int sizeOfElts, int nofNames, int(*eltRemoveFun)(), void *eltRemoveFunData, int(*eltLoadFun)(), void *eltLoadFunData)
 
#define IS_NOT_IN_QUEUE_ELT(elt)
 
int G3d_cache_remove_all(G3D_cache *c)
 
void G3d_cache_lock_intern(G3D_cache *c, int index)
 
void G3d_free(void *buf)
Same as free (ptr). 
 
#define IS_NOT_ACTIVE_ELT(elt)
 
void G3d_error(const char *msg,...)
 
#define IS_ACTIVE_ELT(elt)
 
void G3d_cache_set_loadFun(G3D_cache *c, int(*eltLoadFun)(), void *eltLoadFunData)
 
int G3d_cache_unlock(G3D_cache *c, int name)
 
int G3d_cache_load(G3D_cache *c, int name)
 
#define IS_IN_QUEUE_ELT(elt)
 
int G3d_cache_get_elt(G3D_cache *c, int name, void *dst)
 
#define DEACTIVATE_ELT(elt)
 
void G3d_cache_autolock_off(G3D_cache *c)
 
void G3d_cache_set_minUnlock(G3D_cache *c, int nofMinUnLocked)
 
void G3d_cache_hash_reset(G3d_cache_hash *h)
 
int G3d_cache_put_elt(G3D_cache *c, int name, const void *src)
 
void G3d_cache_autolock_on(G3D_cache *c)
 
void G3d_cache_hash_dispose(G3d_cache_hash *h)
 
#define ONE_UNLOCKED_ELT_ONLY
 
#define IS_LOCKED_ELT(elt)
 
void * G3d_cache_hash_new(int nofNames)
 
void G3d_cache_reset(G3D_cache *c)
 
int G3d_cache_lock(G3D_cache *c, int name)
 
int G3d_cache_flush(G3D_cache *c, int name)
 
int G3d_cache_unlock_all(G3D_cache *c)
 
void G3d_cache_dispose(G3D_cache *c)
 
#define IS_UNLOCKED_ELT(elt)
 
int G3d_cache_flush_all(G3D_cache *c)
 
void * G3d_malloc(int nBytes)
Same as malloc (nBytes), except that in case of error G3d_error() is invoked. 
 
void * G3d_cache_new_read(int nofElts, int sizeOfElts, int nofNames, read_fn *eltLoadFun, void *eltLoadFunData)
 
void G3d_cache_hash_load_name(G3d_cache_hash *h, int name, int index)
 
int G3d_cache_lock_all(G3D_cache *c)
 
void G3d_cache_hash_remove_name(G3d_cache_hash *h, int name)
 
void * G3d_cache_elt_ptr(G3D_cache *c, int name)
 
void G3d_cache_set_removeFun(G3D_cache *c, int(*eltRemoveFun)(), void *eltRemoveFunData)
 
int G3d_cache_remove_elt(G3D_cache *c, int name)
 
void G3d_fatalError(const char *,...)
This function prints the error message msg, and terminates the program with an error status...
 
int G3d_cache_hash_name2index(G3d_cache_hash *h, int name)