File: lru.c

package info (click to toggle)
links2 2.29-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 27,852 kB
  • sloc: ansic: 181,859; sh: 2,585; cpp: 1,450; makefile: 84; awk: 49; perl: 34
file content (114 lines) | stat: -rw-r--r-- 2,468 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* lru.c
 * LRU cache
 * (c) 2002 Karel 'Clock' Kulhavy
 * This file is a part of the Links program, released under GPL.
 */

#include "cfg.h"

#ifdef G

#include "links.h"

static inline void row_insert(struct lru_entry **row, struct lru_entry *ptr)
{
	ptr->next = *row;
	if (ptr->next) ptr->next->previous = &ptr->next;
	ptr->previous = row;
	*row = ptr;
}

static inline void row_delete(struct lru_entry *ptr)
{
	if (ptr->next) ptr->next->previous = ptr->previous;
	*ptr->previous = ptr->next;
}

void lru_insert(struct lru *cache, void *entry, struct lru_entry **row, unsigned bytes_consumed)
{
	struct lru_entry *new_entry = mem_alloc(sizeof(*new_entry));

	new_entry->above = NULL;
	new_entry->below = cache->top;
	new_entry->data = entry;
	new_entry->bytes_consumed = bytes_consumed;
	if (new_entry->below)
		new_entry->below->above = new_entry;
	else
		cache->bottom = new_entry;
	row_insert(row, new_entry);
	cache->top = new_entry;
	cache->bytes += bytes_consumed;
	cache->items++;
}

/* Returns bottom (or NULL if the cache is empty) but doesn't
 * unlink it.
 */
void *lru_get_bottom(struct lru *cache)
{
	if (!cache->bottom) return NULL;
	return cache->bottom->data;
}

/* Destroys bottom on nonempty cache. If the cache is empty, segmentation
 * fault results.
 */
void lru_destroy_bottom(struct lru *cache)
{
	struct lru_entry *it = cache->bottom;

	cache->bytes -= cache->bottom->bytes_consumed;
	cache->items--;
	cache->bottom = it->above;
	if (cache->bottom)
		cache->bottom->below = NULL;
	else
		cache->top = NULL;

	row_delete(it);
	mem_free(it);
}

/* Returns a value of "data"
 * template is what we search for.
 */
void *lru_lookup(struct lru *cache, void *templat, struct lru_entry **row)
{
	struct lru_entry *ptr = *row;
	while (ptr) {
		if (!cache->compare_function(ptr->data,templat)) {
			/* Found */
			if (ptr->above) {
				if (ptr->below) {
					ptr->below->above = ptr->above;
				} else {
					cache->bottom = ptr->above;
				}
				ptr->above->below = ptr->below;
				ptr->above = NULL;
				ptr->below = cache->top;
				cache->top->above = ptr;
				cache->top = ptr;
			}
			if (ptr != *row) {
				row_delete(ptr);
				row_insert(row, ptr);
			}
			return ptr->data;
		}
		ptr = ptr->next;
	}
	return NULL;
}

void lru_init(struct lru *cache, int (*compare_function)(void *entry, void *templat))
{
	cache->compare_function = compare_function;
	cache->top = NULL;
	cache->bottom = NULL;
	cache->bytes = 0;
	cache->items = 0;
}

#endif