1 /* cache.c - routines to maintain an in-core cache of entries */
5 #include <sys/socket.h>
9 static int cache_delete_entry_internal();
11 static void lru_print();
15 * the cache has three entry points (ways to find things):
17 * by entry e.g., if you already have an entry from the cache
18 * and want to delete it. (really by entry ptr)
19 * by dn e.g., when looking for the base object of a search
20 * by id e.g., for search candidates
22 * these correspond to three different avl trees that are maintained.
26 cache_entry_cmp( Entry *e1, Entry *e2 )
28 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
32 cache_entrydn_cmp( Entry *e1, Entry *e2 )
34 return( strcasecmp( e1->e_dn, e2->e_dn ) );
38 cache_entryid_cmp( Entry *e1, Entry *e2 )
40 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
44 cache_set_state( struct cache *cache, Entry *e, int state )
47 pthread_mutex_lock( &cache->c_mutex );
51 /* free cache mutex */
52 pthread_mutex_unlock( &cache->c_mutex );
56 cache_return_entry( struct cache *cache, Entry *e )
59 pthread_mutex_lock( &cache->c_mutex );
61 if ( --e->e_refcnt == 0 && e->e_state == ENTRY_STATE_DELETED ) {
65 /* free cache mutex */
66 pthread_mutex_unlock( &cache->c_mutex );
69 #define LRU_DELETE( cache, e ) { \
70 if ( e->e_lruprev != NULL ) { \
71 e->e_lruprev->e_lrunext = e->e_lrunext; \
73 cache->c_lruhead = e->e_lrunext; \
75 if ( e->e_lrunext != NULL ) { \
76 e->e_lrunext->e_lruprev = e->e_lruprev; \
78 cache->c_lrutail = e->e_lruprev; \
82 #define LRU_ADD( cache, e ) { \
83 e->e_lrunext = cache->c_lruhead; \
84 if ( e->e_lrunext != NULL ) { \
85 e->e_lrunext->e_lruprev = e; \
87 cache->c_lruhead = e; \
88 e->e_lruprev = NULL; \
89 if ( cache->c_lrutail == NULL ) { \
90 cache->c_lrutail = e; \
95 * cache_create_entry_lock - create an entry in the cache, and lock it.
96 * returns: 0 entry has been created and locked
97 * 1 entry already existed
98 * -1 something bad happened
101 cache_add_entry_lock(
110 /* set cache mutex */
111 pthread_mutex_lock( &cache->c_mutex );
113 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
114 cache_entrydn_cmp, avl_dup_error ) != 0 )
116 Debug( LDAP_DEBUG_TRACE,
117 "entry %20s id %d already in dn cache\n", e->e_dn,
120 /* free cache mutex */
121 pthread_mutex_unlock( &cache->c_mutex );
126 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
127 cache_entryid_cmp, avl_dup_error ) != 0 )
129 Debug( LDAP_DEBUG_ANY, "entry %20s id %d already in id cache\n",
130 e->e_dn, e->e_id, 0 );
132 /* delete from dn tree inserted above */
133 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
134 cache_entrydn_cmp ) == NULL )
136 Debug( LDAP_DEBUG_ANY, "can't delete from dn cache\n",
140 /* free cache mutex */
141 pthread_mutex_unlock( &cache->c_mutex );
150 if ( ++cache->c_cursize > cache->c_maxsize ) {
152 * find the lru entry not currently in use and delete it.
153 * in case a lot of entries are in use, only look at the
154 * first 10 on the tail of the list.
157 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
159 /* move this in-use entry to the front of the q */
160 ee = cache->c_lrutail;
161 LRU_DELETE( cache, ee );
162 LRU_ADD( cache, ee );
167 * found at least one to delete - try to get back under
168 * the max cache size.
170 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
171 == 0 && cache->c_cursize > cache->c_maxsize ) {
172 e = cache->c_lrutail;
174 /* delete from cache and lru q */
175 rc = cache_delete_entry_internal( cache, e );
181 /* free cache mutex */
182 pthread_mutex_unlock( &cache->c_mutex );
187 * cache_find_entry_dn - find an entry in the cache, given dn
198 /* set cache mutex */
199 pthread_mutex_lock( &cache->c_mutex );
202 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
203 cache_entrydn_cmp )) != NULL )
206 * entry is deleted or not fully created yet
208 if ( ep->e_state == ENTRY_STATE_DELETED ||
209 ep->e_state == ENTRY_STATE_CREATING )
211 /* free cache mutex */
212 pthread_mutex_unlock( &cache->c_mutex );
218 LRU_DELETE( cache, ep );
219 LRU_ADD( cache, ep );
222 /* free cache mutex */
223 pthread_mutex_unlock( &cache->c_mutex );
229 * cache_find_entry_id - find an entry in the cache, given id
241 /* set cache mutex */
242 pthread_mutex_lock( &cache->c_mutex );
245 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
246 cache_entryid_cmp )) != NULL )
249 * entry is deleted or not fully created yet
251 if ( ep->e_state == ENTRY_STATE_DELETED ||
252 ep->e_state == ENTRY_STATE_CREATING )
254 /* free cache mutex */
255 pthread_mutex_unlock( &cache->c_mutex );
261 LRU_DELETE( cache, ep );
262 LRU_ADD( cache, ep );
265 /* free cache mutex */
266 pthread_mutex_unlock( &cache->c_mutex );
272 * cache_delete_entry - delete the entry e from the cache. the caller
273 * should have obtained e (increasing its ref count) via a call to one
274 * of the cache_find_* routines. the caller should *not* call the
275 * cache_return_entry() routine prior to calling cache_delete_entry().
276 * it performs this function.
278 * returns: 0 e was deleted ok
279 * 1 e was not in the cache
280 * -1 something bad happened
290 /* set cache mutex */
291 pthread_mutex_lock( &cache->c_mutex );
293 rc = cache_delete_entry_internal( cache, e );
295 /* free cache mutex */
296 pthread_mutex_unlock( &cache->c_mutex );
301 cache_delete_entry_internal(
307 if ( avl_delete( &cache->c_dntree, (caddr_t) e, cache_entrydn_cmp )
314 if ( avl_delete( &cache->c_idtree, (caddr_t) e, cache_entryid_cmp )
321 LRU_DELETE( cache, e );
325 * flag entry to be freed later by a call to cache_return_entry()
327 e->e_state = ENTRY_STATE_DELETED;
335 lru_print( struct cache *cache )
339 fprintf( stderr, "LRU queue (head to tail):\n" );
340 for ( e = cache->c_lruhead; e != NULL; e = e->e_lrunext ) {
341 fprintf( stderr, "\tdn %20s id %d refcnt %d\n", e->e_dn,
342 e->e_id, e->e_refcnt );
344 fprintf( stderr, "LRU queue (tail to head):\n" );
345 for ( e = cache->c_lrutail; e != NULL; e = e->e_lruprev ) {
346 fprintf( stderr, "\tdn %20s id %d refcnt %d\n", e->e_dn,
347 e->e_id, e->e_refcnt );