1 | /* |
---|
2 | * Copyright (C) 2008-2010 Timothy D. Morgan |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; version 3 of the License. |
---|
7 | * |
---|
8 | * This program is distributed in the hope that it will be useful, |
---|
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
11 | * GNU General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program; if not, write to the Free Software |
---|
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
16 | * |
---|
17 | * $Id: lru_cache.h 201 2010-06-05 04:45:05Z tim $ |
---|
18 | */ |
---|
19 | |
---|
20 | /** |
---|
21 | * @file |
---|
22 | * |
---|
23 | * A data structure which approximates a least recently used (LRU) cache. |
---|
24 | * Implemented as a basic randomized hash table. |
---|
25 | */ |
---|
26 | |
---|
27 | |
---|
28 | #ifndef LRU_CACHE_H |
---|
29 | #define LRU_CACHE_H |
---|
30 | |
---|
31 | #include <stdbool.h> |
---|
32 | #include <stdint.h> |
---|
33 | #include <stdlib.h> |
---|
34 | #include <stdio.h> |
---|
35 | #include <string.h> |
---|
36 | #include <unistd.h> |
---|
37 | #include <talloc.h> |
---|
38 | |
---|
39 | /* GCC-specific macro for library exports */ |
---|
40 | #ifdef _EXPORT |
---|
41 | #undef _EXPORT |
---|
42 | #endif |
---|
43 | #define _EXPORT __attribute__((visibility("default"))) |
---|
44 | |
---|
45 | struct lru_cache_element; |
---|
46 | typedef struct lru_cache_element lru_cache_element; |
---|
47 | |
---|
48 | struct lru_cache_element |
---|
49 | { |
---|
50 | void* index; |
---|
51 | uint32_t index_len; |
---|
52 | void* data; |
---|
53 | lru_cache_element* next; |
---|
54 | lru_cache_element* older; |
---|
55 | lru_cache_element* newer; |
---|
56 | }; |
---|
57 | |
---|
58 | |
---|
59 | /** XXX: document this. */ |
---|
60 | typedef struct _lru_cache |
---|
61 | { |
---|
62 | uint32_t secret; |
---|
63 | uint32_t num_keys; |
---|
64 | uint32_t num_buckets; |
---|
65 | uint32_t max_keys; |
---|
66 | lru_cache_element* oldest; |
---|
67 | lru_cache_element* newest; |
---|
68 | lru_cache_element** table; |
---|
69 | bool talloc_data; |
---|
70 | } lru_cache; |
---|
71 | |
---|
72 | |
---|
73 | /** |
---|
74 | * XXX: finish documenting. |
---|
75 | */ |
---|
76 | _EXPORT |
---|
77 | lru_cache* lru_cache_create(uint32_t max_keys, uint32_t secret); |
---|
78 | |
---|
79 | |
---|
80 | /** |
---|
81 | * XXX: finish documenting. |
---|
82 | */ |
---|
83 | _EXPORT |
---|
84 | lru_cache* lru_cache_create_ctx(void* talloc_ctx, uint32_t max_keys, |
---|
85 | uint32_t secret, bool talloc_data); |
---|
86 | |
---|
87 | |
---|
88 | /** |
---|
89 | * XXX: finish documenting. |
---|
90 | */ |
---|
91 | _EXPORT |
---|
92 | void lru_cache_destroy(lru_cache* ht); |
---|
93 | |
---|
94 | |
---|
95 | /** |
---|
96 | * XXX: finish documenting. |
---|
97 | */ |
---|
98 | _EXPORT |
---|
99 | bool lru_cache_update(lru_cache* ht, const void* index, |
---|
100 | uint32_t index_len, void* data); |
---|
101 | |
---|
102 | /** |
---|
103 | * XXX: finish documenting. |
---|
104 | * |
---|
105 | * @return A pointer to data previously stored at index. |
---|
106 | * If no data was found at index, NULL is returned. |
---|
107 | */ |
---|
108 | _EXPORT |
---|
109 | void* lru_cache_find(lru_cache* ht, const void* index, |
---|
110 | uint32_t index_len); |
---|
111 | |
---|
112 | /** |
---|
113 | * XXX: finish documenting. |
---|
114 | * |
---|
115 | * Removes entry from table at index. |
---|
116 | * |
---|
117 | * @return A pointer to data that was there previously or NULL if no entry is |
---|
118 | * at index. |
---|
119 | */ |
---|
120 | _EXPORT |
---|
121 | bool lru_cache_remove(lru_cache* ht, const void* index, |
---|
122 | uint32_t index_len); |
---|
123 | |
---|
124 | #endif |
---|