1 //vim: expandtab:shiftwidth=4:fileencoding=utf-8 :
3 /* Copyright ® 2008-2009 Fulvio Satta
5 If you want contact me, send an email to Yota_VGA@users.sf.net
7 This file is part of Biscotto
9 Biscotto is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 Biscotto is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <boost/pool/pool_alloc.hpp>
30 #include <boost/thread.hpp>
36 * The cache system work correctly only if the ids of the protected objects
37 * are all in the unsigned long int intervall size and if the ++ overflow of
38 * the unsigned long int give 0.
39 * This is sure in all the pratical cases.
43 friend class CacheObject
;
46 /* Functor for the monitor of the cache */
55 /* Internal rappresentation of the cache */
58 /* Syncronization variables */
59 mutable boost::mutex mutex
;
60 boost::condition_variable command
;
61 boost::condition_variable_any waitdelfirst
;
63 /* State of the cache */
64 unsigned long int position
;
65 unsigned long int size
;
66 unsigned long int first
;
69 /* Maximum size of the cache, not strictly followed */
70 unsigned long int max
;
72 /* Functor for the map order */
75 inline bool operator()(unsigned long int a
, unsigned long int b
)
77 if (a
< cache().position
and b
>= cache().position
)
84 /* Order of the objects */
85 std::map
<unsigned long int, CacheObject
*,
87 boost::pool_allocator
<
88 std::pair
<unsigned long int, CacheObject
*> > >
91 /* Object collection */
92 std::set
<CacheObject
*, std::less
<CacheObject
*>,
93 boost::pool_allocator
<CacheObject
*> >
96 /* Internal costructor, is a singleton */
99 /* Add an object in the cache */
100 void add(CacheObject
&object
);
102 /* Delete an object in the cache */
103 void del(CacheObject
&object
);
105 /* Resize an object */
106 void resize(long int delta
);
108 /* Call this if an object is used */
109 void use(CacheObject
&object
);
112 /* Return the cache (singleton) */
113 static inline Cache
&cache()
118 /* Set the cache limit (not strictly) */
119 void setLimit(unsigned long int imit
);
121 /* Get the cache limit (not strictly) */
122 unsigned long int limit() const;
124 CacheObject
*readPointer(CacheObject
**ptr
) const;
127 #include "CacheObject.h"