Skip to content

Commit 571d257

Browse files
committed
Update comments in lru_evict header
Added an explicit include of <assert.h> to support new checks in the LRU cache implementation. Updated the comments and logic of lru_evict() to mandate moving the target node to the front and abort if the key does not match, preventing misuse.
1 parent acae9be commit 571d257

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

auparse/lru.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "config.h"
2525
#include <stdlib.h>
2626
#include <string.h>
27+
#include <assert.h>
2728
#include "lru.h"
2829

2930
//#define DEBUG
@@ -275,15 +276,33 @@ static void dequeue(Queue *queue)
275276
queue->count--;
276277
}
277278

278-
// Remove front of the queue because its a mismatch
279+
/*
280+
* lru_evict - remove the cache entry that should be at the front of the
281+
* queue
282+
* @queue: pointer to the LRU queue
283+
* @key: hash index for the entry to evict
284+
*
285+
* The caller must first move the desired entry to the front of the queue by
286+
* calling check_lru_cache() with the same key. This ensures that @key refers
287+
* to the front node. If the node at the front does not match @key, the
288+
* program will abort as this is a usage error.
289+
*/
279290
void lru_evict(Queue *queue, unsigned int key)
280291
{
281292
if (queue_is_empty(queue))
282293
return;
283294

295+
assert(key < queue->total);
296+
284297
Hash *hash = queue->hash;
285298
QNode *temp = queue->front;
286299

300+
if (hash->array[key] != temp) {
301+
msg(LOG_ERR, "lru_evict called with mismatched key %s",
302+
queue->name);
303+
abort();
304+
}
305+
287306
hash->array[key] = NULL;
288307
remove_node(queue, queue->front);
289308

0 commit comments

Comments
 (0)