Missed line in r30818
[maemo-rb.git] / firmware / lru.c
blob5d70511434a53042af51a662a71a26b95f7be2c2
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2003 Tat Tang
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 #include "lru.h"
23 struct lru_node
25 short _next;
26 short _prev;
27 unsigned char data[1]; /* place holder */
30 #define lru_node_p(pl, x) \
31 ((struct lru_node*)(pl->_base + pl->_slot_size * x))
33 /*******************************************************************************
34 * lru_create
35 ******************************************************************************/
36 void lru_create(struct lru* pl, void *buf, short size, short data_size)
38 pl->_base = (unsigned char*) buf;
39 pl->_slot_size = data_size + LRU_SLOT_OVERHEAD;
41 pl->_size = size;
43 pl->_head = 0;
44 pl->_tail = pl->_size - 1;
46 /* Initialise slots */
47 int i;
48 for (i=0; i<pl->_size; i++)
50 lru_node_p(pl, i)->_next = i + 1;
53 /* Fix up head and tail to form circular buffer */
54 lru_node_p(pl, pl->_tail)->_next = pl->_head;
57 /*******************************************************************************
58 * lru_traverse
59 ******************************************************************************/
60 void lru_traverse(struct lru* pl, void (*callback)(void* data))
62 short i;
63 struct lru_node* slot;
64 short loc = pl->_head;
66 for (i = 0; i < pl->_size; i++)
68 slot = lru_node_p(pl, loc);
69 callback(slot->data);
70 loc = slot->_next;
74 /*******************************************************************************
75 * lru_touch
76 ******************************************************************************/
77 void lru_touch(struct lru* pl, short handle)
79 if (handle == pl->_head)
81 pl->_head = lru_node_p(pl, pl->_head)->_next;
82 pl->_tail = lru_node_p(pl, pl->_tail)->_next;
83 return;
86 if (handle == pl->_tail)
88 return;
91 /* Remove current node from linked list */
92 struct lru_node* curr_node = lru_node_p(pl, handle);
94 /* insert current node at tail */
95 struct lru_node* tail_node = lru_node_p(pl, pl->_tail);
97 curr_node->_next = tail_node->_next;
98 tail_node->_next = handle;
100 pl->_tail = handle;
103 /*******************************************************************************
104 * lru_data
105 ******************************************************************************/
106 void *lru_data(struct lru* pl, short handle)
108 return lru_node_p(pl, handle)->data;