Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Contents of /trunk/src/cache.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1680 - (show annotations) (download) (as text)
Tue May 26 17:28:56 2015 UTC (9 years, 9 months ago) by amb
File MIME type: text/x-chdr
File size: 9164 byte(s)
Merge branch 'MSVC' back into the trunk.

1 /***************************************
2 Functions to maintain an in-RAM cache of on-disk data for slim mode.
3
4 Part of the Routino routing software.
5 ******************/ /******************
6 This file Copyright 2013-2015 Andrew M. Bishop
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ***************************************/
21
22
23 #if SLIM
24
25 #ifndef CACHE_H
26 #define CACHE_H /*+ To stop multiple inclusions. +*/
27
28 #include <stdlib.h>
29
30 #include "types.h"
31
32
33 /* Macros for constants */
34
35 #define CACHEWIDTH 8192 /*+ The width of the cache. +*/
36 #define CACHEDEPTH 16 /*+ The depth of the cache. +*/
37
38
39 /* Macro for structure forward declaration */
40
41 #define CACHE_STRUCTURE_FWD(type) typedef struct _##type##Cache type##Cache;
42
43
44 /* Macro for structure declaration */
45
46 /*+ A macro to create a cache structure. +*/
47 #define CACHE_STRUCTURE(type) \
48 \
49 struct _##type##Cache \
50 { \
51 int first [CACHEWIDTH]; /*+ The first entry to fill +*/ \
52 \
53 type data [CACHEWIDTH][CACHEDEPTH]; /*+ The array of type##s. +*/ \
54 index_t indices[CACHEWIDTH][CACHEDEPTH]; /*+ The array of indexes. +*/ \
55 };
56
57
58 /* Macros for function prototypes */
59
60 #define CACHE_NEWCACHE_PROTO(type) static inline type##Cache *New##type##Cache(void);
61
62 #define CACHE_DELETECACHE_PROTO(type) static inline void Delete##type##Cache(type##Cache *cache);
63
64 #define CACHE_FETCHCACHE_PROTO(type) static inline type *FetchCached##type(type##Cache *cache,index_t index,int fd,off_t offset);
65
66 #define CACHE_REPLACECACHE_PROTO(type) static inline void ReplaceCached##type(type##Cache *cache,type *value,index_t index,int fd,off_t offset);
67
68 #define CACHE_INVALIDATECACHE_PROTO(type) static inline void Invalidate##type##Cache(type##Cache *cache);
69
70
71 /* Macros for function definitions */
72
73 /*+ A macro to create a function that creates a new cache data structure. +*/
74 #define CACHE_NEWCACHE(type) \
75 \
76 static inline type##Cache *New##type##Cache(void) \
77 { \
78 type##Cache *cache; \
79 \
80 cache=(type##Cache*)malloc(sizeof(type##Cache)); \
81 \
82 Invalidate##type##Cache(cache); \
83 \
84 return(cache); \
85 }
86
87
88 /*+ A macro to create a function that deletes a cache data structure. +*/
89 #define CACHE_DELETECACHE(type) \
90 \
91 static inline void Delete##type##Cache(type##Cache *cache) \
92 { \
93 free(cache); \
94 }
95
96
97 /*+ A macro to create a function that fetches an item from a cache data structure or reads from file. +*/
98 #define CACHE_FETCHCACHE(type) \
99 \
100 static inline type *FetchCached##type(type##Cache *cache,index_t index,int fd,off_t offset) \
101 { \
102 int row=index%CACHEWIDTH; \
103 int col; \
104 \
105 for(col=0;col<CACHEDEPTH;col++) \
106 if(cache->indices[row][col]==index) \
107 return(&cache->data[row][col]); \
108 \
109 col=cache->first[row]; \
110 \
111 cache->first[row]=(cache->first[row]+1)%CACHEDEPTH; \
112 \
113 SlimFetch(fd,&cache->data[row][col],sizeof(type),offset+(off_t)index*sizeof(type)); \
114 \
115 cache->indices[row][col]=index; \
116 \
117 return(&cache->data[row][col]); \
118 }
119
120
121 /*+ A macro to create a function that replaces an item in a cache data structure and writes to file. +*/
122 #define CACHE_REPLACECACHE(type) \
123 \
124 static inline void ReplaceCached##type(type##Cache *cache,type *value,index_t index,int fd,off_t offset) \
125 { \
126 int row=index%CACHEWIDTH; \
127 int col; \
128 \
129 for(col=0;col<CACHEDEPTH;col++) \
130 if(cache->indices[row][col]==index) \
131 break; \
132 \
133 if(col==CACHEDEPTH) \
134 { \
135 col=cache->first[row]; \
136 \
137 cache->first[row]=(cache->first[row]+1)%CACHEDEPTH; \
138 } \
139 \
140 cache->indices[row][col]=index; \
141 \
142 cache->data[row][col]=*value; \
143 \
144 SlimReplace(fd,&cache->data[row][col],sizeof(type),offset+(off_t)index*sizeof(type)); \
145 }
146
147
148 /*+ A macro to create a function that invalidates the contents of a cache data structure. +*/
149 #define CACHE_INVALIDATECACHE(type) \
150 \
151 static inline void Invalidate##type##Cache(type##Cache *cache) \
152 { \
153 int row,col; \
154 \
155 for(row=0;row<CACHEWIDTH;row++) \
156 { \
157 cache->first[row]=0; \
158 \
159 for(col=0;col<CACHEDEPTH;col++) \
160 cache->indices[row][col]=NO_NODE; \
161 } \
162 }
163
164
165 /*+ Cache data structure forward declarations (for planetsplitter). +*/
166 CACHE_STRUCTURE_FWD(NodeX)
167 CACHE_STRUCTURE_FWD(SegmentX)
168 CACHE_STRUCTURE_FWD(WayX)
169
170 /*+ Cache data structure forward declarations (for router). +*/
171 CACHE_STRUCTURE_FWD(Node)
172 CACHE_STRUCTURE_FWD(Segment)
173 CACHE_STRUCTURE_FWD(Way)
174 CACHE_STRUCTURE_FWD(TurnRelation)
175
176
177 #endif /* CACHE_H */
178
179 #endif /* SLIM */