00001 #pragma once
00002 #ifndef PROBE_CACHE_H
00003 #define PROBE_CACHE_H
00004
00005 #include <stdbool.h>
00006 #include <pthread.h>
00007 #include <sexp-types.h>
00008 #include <SEAP/generic/redblack.h>
00009 #include <../../common/util.h>
00010
00011 OSCAP_HIDDEN_START;
00012
00013 DEFRBTREE(pcache, SEXP_t * id;
00014 SEXP_t * item);
00015
00016 typedef struct {
00017 TREETYPE(pcache) tree;
00018 pthread_rwlock_t lock;
00019 } pcache_t;
00020
00021 static inline int pcache_rlock(pcache_t * c)
00022 {
00023 return (pthread_rwlock_rdlock(&c->lock) == 0 ? 0 : -1);
00024 }
00025
00026 static inline int pcache_wlock(pcache_t * c)
00027 {
00028 return (pthread_rwlock_wrlock(&c->lock) == 0 ? 0 : -1);
00029 }
00030
00031 static inline int pcache_wunlock(pcache_t * c)
00032 {
00033 return (pthread_rwlock_unlock(&c->lock) == 0 ? 0 : -1);
00034 }
00035
00036 #define pcache_runlock(c) pcache_wunlock(c)
00037 #define with_pcache_rlocked(c) for (bool __rlk__ = (pcache_rlock (c) == 0 ? true : false); __rlk__; __rlk__ = (pcache_runlock (c) == 0 ? false : abort(), false))
00038 #define with_pcache_wlocked(c) for (bool __wlk__ = (pcache_wlock (c) == 0 ? true : false); __wlk__; __wlk__ = (pcache_wunlock (c) == 0 ? false : abort(), false))
00039
00040 pcache_t *pcache_new(void);
00041 void pcache_free(pcache_t * cache);
00042
00043 int pcache_sexp_add(pcache_t * cache, const SEXP_t * id, SEXP_t * item);
00044 int pcache_cstr_add(pcache_t * cache, const char *id, SEXP_t * item);
00045
00046 int pcache_sexp_del(pcache_t * cache, const SEXP_t * id);
00047 int pcache_cstr_del(pcache_t * cache, const char *id);
00048
00049 SEXP_t *pcache_sexp_get(pcache_t * cache, const SEXP_t * id);
00050 SEXP_t *pcache_cstr_get(pcache_t * cache, const char *id);
00051
00052 typedef struct {
00053 char *iname;
00054 SEXP_t *names;
00055 uint16_t count;
00056 } ncache_item_t;
00057
00058 #define NCACHE_MAX_ITEMS 8
00059
00060 typedef struct {
00061 ncache_item_t *items[NCACHE_MAX_ITEMS];
00062 uint8_t count;
00063 pthread_rwlock_t lock;
00064 } ncache_t;
00065
00066 ncache_t *ncache_new(void);
00067 void ncache_free(ncache_t * cache);
00068
00069 int ncache_item_add(ncache_t * cache, const char *iname, SEXP_t * names, uint16_t count);
00070 int ncache_name_add(ncache_t * cache, const char *iname, SEXP_t * name);
00071
00072 #define with_ncache_rlocked(c) for (bool __rlk__ = (ncache_rlock (c) == 0 ? true : false); __rlk__; __rlk__ = ncache_runlock (c) == 0 ? false : abort())
00073 #define with_ncache_wlocked(c) for (bool __wlk__ = (ncache_wlock (c) == 0 ? true : false); __wlk__; __wlk__ = ncache_wunlock (c) == 0 ? false : abort())
00074
00075 OSCAP_HIDDEN_END;
00076
00077 #endif