[169] | 1 | /* |
---|
| 2 | * Copyright (C) 2008-2010 Timothy D. Morgan |
---|
[168] | 3 | * |
---|
[108] | 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 | * |
---|
[122] | 17 | * $Id: lru_cache.h 253 2011-06-13 02:27:42Z tim $ |
---|
[108] | 18 | */ |
---|
| 19 | |
---|
[169] | 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 | |
---|
[108] | 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> |
---|
[201] | 37 | #include <talloc.h> |
---|
[108] | 38 | |
---|
[253] | 39 | #include "compat.h" |
---|
[201] | 40 | |
---|
[253] | 41 | |
---|
[108] | 42 | struct lru_cache_element; |
---|
| 43 | typedef struct lru_cache_element lru_cache_element; |
---|
| 44 | |
---|
| 45 | struct lru_cache_element |
---|
| 46 | { |
---|
| 47 | void* index; |
---|
| 48 | uint32_t index_len; |
---|
| 49 | void* data; |
---|
| 50 | lru_cache_element* next; |
---|
| 51 | lru_cache_element* older; |
---|
| 52 | lru_cache_element* newer; |
---|
| 53 | }; |
---|
| 54 | |
---|
[169] | 55 | |
---|
| 56 | /** XXX: document this. */ |
---|
[108] | 57 | typedef struct _lru_cache |
---|
| 58 | { |
---|
| 59 | uint32_t secret; |
---|
| 60 | uint32_t num_keys; |
---|
| 61 | uint32_t num_buckets; |
---|
| 62 | uint32_t max_keys; |
---|
| 63 | lru_cache_element* oldest; |
---|
| 64 | lru_cache_element* newest; |
---|
| 65 | lru_cache_element** table; |
---|
[147] | 66 | bool talloc_data; |
---|
[108] | 67 | } lru_cache; |
---|
| 68 | |
---|
| 69 | |
---|
[169] | 70 | /** |
---|
| 71 | * XXX: finish documenting. |
---|
| 72 | */ |
---|
[253] | 73 | _EXPORT() |
---|
[147] | 74 | lru_cache* lru_cache_create(uint32_t max_keys, uint32_t secret); |
---|
[169] | 75 | |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * XXX: finish documenting. |
---|
| 79 | */ |
---|
[253] | 80 | _EXPORT() |
---|
[147] | 81 | lru_cache* lru_cache_create_ctx(void* talloc_ctx, uint32_t max_keys, |
---|
| 82 | uint32_t secret, bool talloc_data); |
---|
[169] | 83 | |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * XXX: finish documenting. |
---|
| 87 | */ |
---|
[253] | 88 | _EXPORT() |
---|
[108] | 89 | void lru_cache_destroy(lru_cache* ht); |
---|
| 90 | |
---|
[169] | 91 | |
---|
| 92 | /** |
---|
| 93 | * XXX: finish documenting. |
---|
[108] | 94 | */ |
---|
[253] | 95 | _EXPORT() |
---|
[108] | 96 | bool lru_cache_update(lru_cache* ht, const void* index, |
---|
| 97 | uint32_t index_len, void* data); |
---|
| 98 | |
---|
[169] | 99 | /** |
---|
| 100 | * XXX: finish documenting. |
---|
| 101 | * |
---|
| 102 | * @return A pointer to data previously stored at index. |
---|
| 103 | * If no data was found at index, NULL is returned. |
---|
[108] | 104 | */ |
---|
[253] | 105 | _EXPORT() |
---|
[108] | 106 | void* lru_cache_find(lru_cache* ht, const void* index, |
---|
| 107 | uint32_t index_len); |
---|
| 108 | |
---|
[169] | 109 | /** |
---|
| 110 | * XXX: finish documenting. |
---|
| 111 | * |
---|
| 112 | * Removes entry from table at index. |
---|
| 113 | * |
---|
| 114 | * @return A pointer to data that was there previously or NULL if no entry is |
---|
| 115 | * at index. |
---|
[108] | 116 | */ |
---|
[253] | 117 | _EXPORT() |
---|
[108] | 118 | bool lru_cache_remove(lru_cache* ht, const void* index, |
---|
| 119 | uint32_t index_len); |
---|
| 120 | |
---|
| 121 | #endif |
---|