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, e, cache_entrydn_cmp, avl_dup_error )
115 Debug( LDAP_DEBUG_TRACE,
116 "entry %20s id %d already in dn cache\n", e->e_dn,
119 /* free cache mutex */
120 pthread_mutex_unlock( &cache->c_mutex );
125 if ( avl_insert( &cache->c_idtree, e, cache_entryid_cmp, avl_dup_error )
127 Debug( LDAP_DEBUG_ANY, "entry %20s id %d already in id cache\n",
128 e->e_dn, e->e_id, 0 );
130 /* delete from dn tree inserted above */
131 if ( avl_delete( &cache->c_dntree, e, cache_entrydn_cmp )
133 Debug( LDAP_DEBUG_ANY, "can't delete from dn cache\n",
137 /* free cache mutex */
138 pthread_mutex_unlock( &cache->c_mutex );
147 if ( ++cache->c_cursize > cache->c_maxsize ) {
149 * find the lru entry not currently in use and delete it.
150 * in case a lot of entries are in use, only look at the
151 * first 10 on the tail of the list.
154 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
156 /* move this in-use entry to the front of the q */
157 ee = cache->c_lrutail;
158 LRU_DELETE( cache, ee );
159 LRU_ADD( cache, ee );
164 * found at least one to delete - try to get back under
165 * the max cache size.
167 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
168 == 0 && cache->c_cursize > cache->c_maxsize ) {
169 e = cache->c_lrutail;
171 /* delete from cache and lru q */
172 rc = cache_delete_entry_internal( cache, e );
178 /* free cache mutex */
179 pthread_mutex_unlock( &cache->c_mutex );
184 * cache_find_entry_dn - find an entry in the cache, given dn
195 /* set cache mutex */
196 pthread_mutex_lock( &cache->c_mutex );
199 if ( (ep = (Entry *) avl_find( cache->c_dntree, &e, cache_entrydn_cmp ))
202 * entry is deleted or not fully created yet
204 if ( ep->e_state == ENTRY_STATE_DELETED ||
205 ep->e_state == ENTRY_STATE_CREATING )
207 /* free cache mutex */
208 pthread_mutex_unlock( &cache->c_mutex );
214 LRU_DELETE( cache, ep );
215 LRU_ADD( cache, ep );
218 /* free cache mutex */
219 pthread_mutex_unlock( &cache->c_mutex );
225 * cache_find_entry_id - find an entry in the cache, given id
237 /* set cache mutex */
238 pthread_mutex_lock( &cache->c_mutex );
241 if ( (ep = (Entry *) avl_find( cache->c_idtree, &e, cache_entryid_cmp ))
244 * entry is deleted or not fully created yet
246 if ( ep->e_state == ENTRY_STATE_DELETED ||
247 ep->e_state == ENTRY_STATE_CREATING )
249 /* free cache mutex */
250 pthread_mutex_unlock( &cache->c_mutex );
256 LRU_DELETE( cache, ep );
257 LRU_ADD( cache, ep );
260 /* free cache mutex */
261 pthread_mutex_unlock( &cache->c_mutex );
267 * cache_delete_entry - delete the entry e from the cache. the caller
268 * should have obtained e (increasing its ref count) via a call to one
269 * of the cache_find_* routines. the caller should *not* call the
270 * cache_return_entry() routine prior to calling cache_delete_entry().
271 * it performs this function.
273 * returns: 0 e was deleted ok
274 * 1 e was not in the cache
275 * -1 something bad happened
285 /* set cache mutex */
286 pthread_mutex_lock( &cache->c_mutex );
288 rc = cache_delete_entry_internal( cache, e );
290 /* free cache mutex */
291 pthread_mutex_unlock( &cache->c_mutex );
296 cache_delete_entry_internal(
302 if ( avl_delete( &cache->c_dntree, e, cache_entrydn_cmp ) == NULL ) {
307 if ( avl_delete( &cache->c_idtree, e, cache_entryid_cmp ) == NULL ) {
312 LRU_DELETE( cache, e );
316 * flag entry to be freed later by a call to cache_return_entry()
318 e->e_state = ENTRY_STATE_DELETED;
326 lru_print( struct cache *cache )
330 fprintf( stderr, "LRU queue (head to tail):\n" );
331 for ( e = cache->c_lruhead; e != NULL; e = e->e_lrunext ) {
332 fprintf( stderr, "\tdn %20s id %d refcnt %d\n", e->e_dn,
333 e->e_id, e->e_refcnt );
335 fprintf( stderr, "LRU queue (tail to head):\n" );
336 for ( e = cache->c_lrutail; e != NULL; e = e->e_lruprev ) {
337 fprintf( stderr, "\tdn %20s id %d refcnt %d\n", e->e_dn,
338 e->e_id, e->e_refcnt );