4646**
4747*************************************************************************************
4848*/
49- #include "libltfs/ltfs.h"
5049#include "cache_manager.h"
50+ #include "libltfs/ltfs.h"
5151
5252/**
5353 * cache_pool structure.
6262 * have been allocated through this cache pool; it doesn't necessarily say how
6363 * many available objects are in the pool.
6464 */
65- struct cache_pool {
66- size_t object_size ; /**< The size of each object in this pool */
67- size_t initial_capacity ; /**< Low water mark. Defines the initial capacity of the pool */
68- size_t max_capacity ; /**< High water mark. Defines the maximum capacity of the pool */
69- size_t current_capacity ; /**< How many objects are currently allocated */
65+ struct cache_pool
66+ {
67+ size_t object_size ; /**< The size of each object in this pool */
68+ size_t initial_capacity ; /**< Low water mark. Defines the initial capacity of the pool */
69+ size_t max_capacity ; /**< High water mark. Defines the maximum capacity of the pool */
70+ size_t current_capacity ; /**< How many objects are currently allocated */
7071 TAILQ_HEAD (objects_struct , cache_object ) pool ; /**< Pool of cached objects */
7172};
7273
7374/**
7475 * cache_object structure.
7576 * Holds objects of size @object_size, as stored in the cache_pool structure.
7677 */
77- struct cache_object {
78- uint32_t refcount ; /**< Reference count */
79- ltfs_mutex_t lock ; /**< Lock to protect access to the refcount */
80- void * data ; /**< Cached data. Must be the first element in the structure */
81- struct cache_pool * pool ; /**< Backpointer to the cache pool this object is part of */
78+ struct cache_object
79+ {
80+ uint32_t refcount ; /**< Reference count */
81+ ltfs_mutex_t lock ; /**< Lock to protect access to the refcount */
82+ void * data ; /**< Cached data. Must be the first element in the structure */
83+ struct cache_pool * pool ; /**< Backpointer to the cache pool this object is part of */
8284 TAILQ_ENTRY (cache_object ) list ; /**< Pointers to next and previous cache objects */
8385};
8486
@@ -92,12 +94,13 @@ struct cache_object *_cache_manager_create_object(struct cache_pool *pool)
9294{
9395 int ret ;
9496 struct cache_object * object = calloc (1 , sizeof (struct cache_object ));
95- if (! object ) {
97+ if (!object ) {
9698 ltfsmsg (LTFS_ERR , 10001E , "cache manager: object" );
9799 return NULL ;
98100 }
99- object -> data = calloc (1 , pool -> object_size + LTFS_CRC_SIZE ); /* Allocate extra 4-bytes for SCSI logical block protection */
100- if (! object -> data ) {
101+ object -> data =
102+ calloc (1 , pool -> object_size + LTFS_CRC_SIZE ); /* Allocate extra 4-bytes for SCSI logical block protection */
103+ if (!object -> data ) {
101104 ltfsmsg (LTFS_ERR , 10001E , "cache manager: object data" );
102105 free (object );
103106 return NULL ;
@@ -127,8 +130,8 @@ void *cache_manager_init(size_t object_size, size_t initial_capacity, size_t max
127130 struct cache_pool * pool ;
128131 size_t i ;
129132
130- pool = (struct cache_pool * ) calloc (1 , sizeof (struct cache_pool ));
131- if (! pool ) {
133+ pool = (struct cache_pool * )calloc (1 , sizeof (struct cache_pool ));
134+ if (!pool ) {
132135 ltfsmsg (LTFS_ERR , 10001E , "cache manager: pool" );
133136 return NULL ;
134137 }
@@ -138,16 +141,16 @@ void *cache_manager_init(size_t object_size, size_t initial_capacity, size_t max
138141 pool -> current_capacity = initial_capacity ;
139142 TAILQ_INIT (& pool -> pool );
140143
141- for (i = 0 ; i < initial_capacity ; ++ i ) {
144+ for (i = 0 ; i < initial_capacity ; ++ i ) {
142145 struct cache_object * object = _cache_manager_create_object (pool );
143- if (! object ) {
146+ if (!object ) {
144147 ltfsmsg (LTFS_ERR , 11114E );
145148 cache_manager_destroy (pool );
146149 return NULL ;
147150 }
148151 }
149152
150- return (void * ) pool ;
153+ return (void * )pool ;
151154}
152155
153156/**
@@ -157,17 +160,17 @@ void *cache_manager_init(size_t object_size, size_t initial_capacity, size_t max
157160void cache_manager_destroy (void * cache )
158161{
159162 struct cache_object * object , * aux ;
160- struct cache_pool * pool = (struct cache_pool * ) cache ;
161- if (! pool ) {
163+ struct cache_pool * pool = (struct cache_pool * )cache ;
164+ if (!pool ) {
162165 ltfsmsg (LTFS_WARN , 10006W , "pool" , __FUNCTION__ );
163166 return ;
164167 }
165168
166- TAILQ_FOREACH_SAFE (object , & pool -> pool , list , aux ) {
169+ TAILQ_FOREACH_SAFE (object , & pool -> pool , list , aux )
170+ {
167171 TAILQ_REMOVE (& pool -> pool , object , list );
168172 ltfs_mutex_destroy (& object -> lock );
169- if (object -> data )
170- free (object -> data );
173+ if (object -> data ) free (object -> data );
171174 free (object );
172175 }
173176
@@ -181,10 +184,10 @@ void cache_manager_destroy(void *cache)
181184 */
182185bool cache_manager_has_room (void * cache )
183186{
184- struct cache_pool * pool = (struct cache_pool * ) cache ;
187+ struct cache_pool * pool = (struct cache_pool * )cache ;
185188 CHECK_ARG_NULL (pool , false);
186189
187- return ! (TAILQ_EMPTY (& pool -> pool ) && pool -> current_capacity == pool -> max_capacity );
190+ return !(TAILQ_EMPTY (& pool -> pool ) && pool -> current_capacity == pool -> max_capacity );
188191}
189192
190193/**
@@ -202,14 +205,15 @@ void *cache_manager_allocate_object(void *cache)
202205{
203206 size_t i , new_size = 0 ;
204207 struct cache_object * object , * last_object = NULL ;
205- struct cache_pool * pool = (struct cache_pool * ) cache ;
208+ struct cache_pool * pool = (struct cache_pool * )cache ;
206209 CHECK_ARG_NULL (pool , NULL );
207210
208- TAILQ_FOREACH (object , & pool -> pool , list ) {
211+ TAILQ_FOREACH (object , & pool -> pool , list )
212+ {
209213 /* Return the first available object */
210214 TAILQ_REMOVE (& pool -> pool , object , list );
211215 object -> refcount = 1 ;
212- return (void * ) object ;
216+ return (void * )object ;
213217 }
214218
215219 /*
@@ -229,9 +233,9 @@ void *cache_manager_allocate_object(void *cache)
229233 /* Expand until the maximum capacity */
230234 new_size = pool -> max_capacity ;
231235
232- for (i = pool -> current_capacity ; i < new_size ; ++ i ) {
236+ for (i = pool -> current_capacity ; i < new_size ; ++ i ) {
233237 struct cache_object * object = _cache_manager_create_object (pool );
234- if (! object ) {
238+ if (!object ) {
235239 /* Luckily we might have increased the size of the cache by a few entries.. */
236240 ltfsmsg (LTFS_WARN , 11115W );
237241 break ;
@@ -240,7 +244,7 @@ void *cache_manager_allocate_object(void *cache)
240244 pool -> current_capacity ++ ;
241245 }
242246
243- if (! last_object ) {
247+ if (!last_object ) {
244248 /* If we couldn't grow the cache any further return failure */
245249 ltfsmsg (LTFS_ERR , 11116E );
246250 return NULL ;
@@ -258,7 +262,7 @@ void *cache_manager_allocate_object(void *cache)
258262 */
259263void * cache_manager_get_object (void * cache_object )
260264{
261- struct cache_object * object = (struct cache_object * ) cache_object ;
265+ struct cache_object * object = (struct cache_object * )cache_object ;
262266
263267 CHECK_ARG_NULL (cache_object , NULL );
264268
@@ -276,8 +280,8 @@ void *cache_manager_get_object(void *cache_object)
276280void cache_manager_free_object (void * cache_object , size_t count )
277281{
278282 struct cache_pool * pool ;
279- struct cache_object * object = (struct cache_object * ) cache_object ;
280- if (! object ) {
283+ struct cache_object * object = (struct cache_object * )cache_object ;
284+ if (!object ) {
281285 ltfsmsg (LTFS_WARN , 10006W , "object" , __FUNCTION__ );
282286 return ;
283287 }
@@ -316,7 +320,7 @@ void cache_manager_free_object(void *cache_object, size_t count)
316320 */
317321void * cache_manager_get_object_data (void * cache_object )
318322{
319- struct cache_object * object = (struct cache_object * ) cache_object ;
323+ struct cache_object * object = (struct cache_object * )cache_object ;
320324 CHECK_ARG_NULL (object , NULL );
321325 return object -> data ;
322326}
@@ -328,7 +332,7 @@ void *cache_manager_get_object_data(void *cache_object)
328332 */
329333size_t cache_manager_get_object_size (void * cache_object )
330334{
331- struct cache_object * object = (struct cache_object * ) cache_object ;
335+ struct cache_object * object = (struct cache_object * )cache_object ;
332336 CHECK_ARG_NULL (object , 0 );
333337 return object -> pool -> object_size ;
334338}
0 commit comments