1 /****************************************************************\
2 *                                                                *
3 *  Sparse Cache Object                                           *
4 *                                                                *
5 *  Guy St.C. Slater..   mailto:guy@ebi.ac.uk                     *
6 *  Copyright (C) 2000-2009.  All Rights Reserved.                *
7 *                                                                *
8 *  This source code is distributed under the terms of the        *
9 *  GNU General Public License, version 3. See the file COPYING   *
10 *  or http://www.gnu.org/licenses/gpl.txt for details            *
11 *                                                                *
12 *  If you use this code, please keep this notice intact.         *
13 *                                                                *
14 \****************************************************************/
15 
16 #ifndef INCLUDED_SPARSECACHE_H
17 #define INCLUDED_SPARSECACHE_H
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif /* __cplusplus */
22 
23 #include <glib.h>
24 
25 /* Currently using simple demand paging
26  *
27  * FIXME: optimisation: add option to flush of LRU pages ?
28  */
29 
30 typedef gpointer (*SparseCache_GetFunc)(gint pos, gpointer page_data,
31                                         gpointer user_data);
32 typedef gint (*SparseCache_CopyFunc)(gint start, gint length, gchar *dst,
33                                      gpointer page_data, gpointer user_data);
34 
35 typedef struct {
36                    gpointer data;
37         SparseCache_GetFunc get_func;
38        SparseCache_CopyFunc copy_func;
39                       gsize data_size;
40 } SparseCache_Page;
41 
42 typedef SparseCache_Page *(*SparseCache_FillFunc)(gint start,
43                                                   gpointer user_data);
44 
45 typedef void (*SparseCache_EmptyFunc)(SparseCache_Page *page, gpointer user_data);
46 typedef void (*SparseCache_FreeFunc)(gpointer user_data);
47 
48 #define SparseCache_PAGE_SIZE_BIT_WIDTH 12
49 #define SparseCache_PAGE_SIZE (1 << SparseCache_PAGE_SIZE_BIT_WIDTH)
50 #define SparseCache_pos2page(pos) ((pos) >> SparseCache_PAGE_SIZE_BIT_WIDTH)
51 
52 typedef struct {
53                     gint   ref_count;
54         SparseCache_Page **page_list;
55                     gint   page_total;
56                     gint   page_used;
57                     gint   length;
58                    gsize   page_memory_usage;
59     SparseCache_FillFunc   fill_func;
60    SparseCache_EmptyFunc   empty_func;
61     SparseCache_FreeFunc   free_func;
62                 gpointer   user_data;
63 } SparseCache;
64 
65 SparseCache *SparseCache_create(gint length,
66                                 SparseCache_FillFunc fill_func,
67                                 SparseCache_EmptyFunc empty_func,
68                                 SparseCache_FreeFunc free_func,
69                                 gpointer user_data);
70         void SparseCache_destroy(SparseCache *sc);
71 SparseCache *SparseCache_share(SparseCache *sc);
72     gpointer SparseCache_get(SparseCache *sc, gint pos);
73        gsize SparseCache_memory_usage(SparseCache *sc);
74         void SparseCache_copy(SparseCache *sc, gint start, gint length,
75                               gchar *dst);
76 
77 #ifdef __cplusplus
78 }
79 #endif /* __cplusplus */
80 
81 #endif /* INCLUDED_SPARSECACHE_H */
82 
83