"Fossies" - the Fresh Open Source Software Archive

Member "aspell-0.60.8/common/cache.cpp" (8 Oct 2019, 2334 Bytes) of package /linux/misc/aspell-0.60.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "cache.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.60.7-rc1_vs_0.60.7-rc2.

    1 #include <assert.h>
    2 
    3 #include "stack_ptr.hpp"
    4 #include "cache-t.hpp"
    5 
    6 namespace acommon {
    7 
    8 static GlobalCacheBase * first_cache = 0;
    9 Mutex GlobalCacheBase::global_cache_lock;
   10 
   11 void Cacheable::copy() const
   12 {
   13   //CERR << "COPY\n";
   14   LOCK(&cache->lock);
   15   copy_no_lock();
   16 }
   17 
   18 void GlobalCacheBase::del(Cacheable * n)
   19 {
   20   *n->prev = n->next;
   21   if (n->next) n->next->prev = n->prev;
   22   n->next = 0;
   23   n->prev = 0;
   24 }
   25 
   26 void GlobalCacheBase::add(Cacheable * n) 
   27 {
   28   assert(n->refcount > 0);
   29   n->next = first;
   30   n->prev = &first;
   31   if (first) first->prev = &n->next;
   32   first = n;
   33   n->cache = this;
   34 }
   35 
   36 void GlobalCacheBase::release(Cacheable * d) 
   37 {
   38   //CERR << "RELEASE\n";
   39   LOCK(&lock);
   40   d->refcount--;
   41   assert(d->refcount >= 0);
   42   if (d->refcount != 0) return;
   43   //CERR << "DEL\n";
   44   if (d->attached()) del(d);
   45   delete d;
   46 }
   47 
   48 void GlobalCacheBase::detach(Cacheable * d)
   49 {
   50   LOCK(&lock);
   51   if (d->attached()) del(d);
   52 }
   53 
   54 void GlobalCacheBase::detach_all()
   55 {
   56   LOCK(&lock);
   57   Cacheable * p = first;
   58   while (p) {
   59     *p->prev = 0;
   60     p->prev = 0;
   61     p = p->next;
   62   }
   63 }
   64 
   65 void release_cache_data(GlobalCacheBase * cache, const Cacheable * d)
   66 {
   67   cache->release(const_cast<Cacheable *>(d));
   68 }
   69 
   70 GlobalCacheBase::GlobalCacheBase(const char * n)
   71   : name (n)
   72 {
   73   LOCK(&global_cache_lock);
   74   next = first_cache;
   75   prev = &first_cache;
   76   if (first_cache) first_cache->prev = &next;
   77   first_cache = this;
   78 }
   79 
   80 GlobalCacheBase::~GlobalCacheBase()
   81 {
   82   detach_all();
   83   LOCK(&global_cache_lock);
   84   *prev = next;
   85   if (next) next->prev = prev;
   86 }
   87 
   88 bool reset_cache(const char * which)
   89 {
   90   LOCK(&GlobalCacheBase::global_cache_lock);
   91   bool any = false;
   92   for (GlobalCacheBase * i = first_cache; i; i = i->next)
   93   {
   94     if (which && strcmp(i->name, which) == 0) {i->detach_all(); any = true;}
   95   }
   96   return any;
   97 }
   98 
   99 extern "C"
  100 int aspell_reset_cache(const char * which)
  101 {
  102   return reset_cache(which);
  103 }
  104 
  105 #if 0
  106 
  107 struct CacheableImpl : public Cacheable
  108 {
  109   class CacheConfig;
  110   typedef String CacheKey;
  111   bool cache_key_eq(const CacheKey &);
  112   static PosibErr<CacheableImpl *> get_new(const CacheKey &, CacheConfig *);
  113 };
  114 
  115 template
  116 PosibErr<CacheableImpl *> get_cache_data(GlobalCache<CacheableImpl> *, 
  117                                          CacheableImpl::CacheConfig *, 
  118                                          const CacheableImpl::CacheKey &);
  119 
  120 #endif
  121 
  122 }