Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/src/cache.h
Parent Directory
|
Revision Log
Revision 1297 -
(show annotations)
(download)
(as text)
Tue May 7 14:41:11 2013 UTC (11 years, 10 months ago) by amb
File MIME type: text/x-chdr
File size: 9179 byte(s)
Tue May 7 14:41:11 2013 UTC (11 years, 10 months ago) by amb
File MIME type: text/x-chdr
File size: 9179 byte(s)
Add cache functions for NodesX, SegmentsX and WaysX to speed up the planetsplitter in slim mode.
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 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 <unistd.h> |
29 | #include <stdlib.h> |
30 | |
31 | #include "types.h" |
32 | |
33 | |
34 | /* Macros for constants */ |
35 | |
36 | #define CACHEWIDTH 2048 /*+ The width of the cache. +*/ |
37 | #define CACHEDEPTH 16 /*+ The depth of the cache. +*/ |
38 | |
39 | |
40 | /* Macro for structure forward declaration */ |
41 | |
42 | #define CACHE_STRUCTURE_FWD(type) typedef struct _##type##Cache type##Cache; |
43 | |
44 | |
45 | /* Macro for structure declaration */ |
46 | |
47 | /*+ A macro to create a cache structure. +*/ |
48 | #define CACHE_STRUCTURE(type) \ |
49 | \ |
50 | struct _##type##Cache \ |
51 | { \ |
52 | int first [CACHEWIDTH]; /*+ The first entry to fill +*/ \ |
53 | \ |
54 | type data [CACHEWIDTH][CACHEDEPTH]; /*+ The array of type##s. +*/ \ |
55 | index_t indices[CACHEWIDTH][CACHEDEPTH]; /*+ The array of indexes. +*/ \ |
56 | }; |
57 | |
58 | |
59 | /* Macros for function prototypes */ |
60 | |
61 | #define CACHE_NEWCACHE_PROTO(type) static inline type##Cache *New##type##Cache(void); |
62 | |
63 | #define CACHE_DELETECACHE_PROTO(type) static inline void Delete##type##Cache(type##Cache *cache); |
64 | |
65 | #define CACHE_FETCHCACHE_PROTO(type) static inline type *FetchCached##type(type##Cache *cache,index_t index,int fd,off_t offset); |
66 | |
67 | #define CACHE_REPLACECACHE_PROTO(type) static inline void ReplaceCached##type(type##Cache *cache,type *value,index_t index,int fd,off_t offset); |
68 | |
69 | #define CACHE_INVALIDATECACHE_PROTO(type) static inline void Invalidate##type##Cache(type##Cache *cache); |
70 | |
71 | |
72 | /* Macros for function definitions */ |
73 | |
74 | /*+ A macro to create a function that creates a new cache data structure. +*/ |
75 | #define CACHE_NEWCACHE(type) \ |
76 | \ |
77 | static inline type##Cache *New##type##Cache(void) \ |
78 | { \ |
79 | type##Cache *cache; \ |
80 | \ |
81 | cache=(type##Cache*)malloc(sizeof(type##Cache)); \ |
82 | \ |
83 | Invalidate##type##Cache(cache); \ |
84 | \ |
85 | return(cache); \ |
86 | } |
87 | |
88 | |
89 | /*+ A macro to create a function that deletes a cache data structure. +*/ |
90 | #define CACHE_DELETECACHE(type) \ |
91 | \ |
92 | static inline void Delete##type##Cache(type##Cache *cache) \ |
93 | { \ |
94 | free(cache); \ |
95 | } |
96 | |
97 | |
98 | /*+ A macro to create a function that fetches an item from a cache data structure or reads from file. +*/ |
99 | #define CACHE_FETCHCACHE(type) \ |
100 | \ |
101 | static inline type *FetchCached##type(type##Cache *cache,index_t index,int fd,off_t offset) \ |
102 | { \ |
103 | int row=index%CACHEWIDTH; \ |
104 | int col; \ |
105 | \ |
106 | for(col=0;col<CACHEDEPTH;col++) \ |
107 | if(cache->indices[row][col]==index) \ |
108 | return(&cache->data[row][col]); \ |
109 | \ |
110 | col=cache->first[row]; \ |
111 | \ |
112 | cache->first[row]=(cache->first[row]+1)%CACHEDEPTH; \ |
113 | \ |
114 | SeekReadFile(fd,&cache->data[row][col],sizeof(type),offset+(off_t)index*sizeof(type)); \ |
115 | \ |
116 | cache->indices[row][col]=index; \ |
117 | \ |
118 | return(&cache->data[row][col]); \ |
119 | } |
120 | |
121 | |
122 | /*+ A macro to create a function that replaces an item in a cache data structure and writes to file. +*/ |
123 | #define CACHE_REPLACECACHE(type) \ |
124 | \ |
125 | static inline void ReplaceCached##type(type##Cache *cache,type *value,index_t index,int fd,off_t offset) \ |
126 | { \ |
127 | int row=index%CACHEWIDTH; \ |
128 | int col; \ |
129 | \ |
130 | for(col=0;col<CACHEDEPTH;col++) \ |
131 | if(cache->indices[row][col]==index) \ |
132 | break; \ |
133 | \ |
134 | if(col==CACHEDEPTH) \ |
135 | { \ |
136 | col=cache->first[row]; \ |
137 | \ |
138 | cache->first[row]=(cache->first[row]+1)%CACHEDEPTH; \ |
139 | } \ |
140 | \ |
141 | cache->indices[row][col]=index; \ |
142 | \ |
143 | cache->data[row][col]=*value; \ |
144 | \ |
145 | SeekWriteFile(fd,&cache->data[row][col],sizeof(type),offset+(off_t)index*sizeof(type)); \ |
146 | } |
147 | |
148 | |
149 | /*+ A macro to create a function that invalidates the contents of a cache data structure. +*/ |
150 | #define CACHE_INVALIDATECACHE(type) \ |
151 | \ |
152 | static inline void Invalidate##type##Cache(type##Cache *cache) \ |
153 | { \ |
154 | int row,col; \ |
155 | \ |
156 | for(row=0;row<CACHEWIDTH;row++) \ |
157 | { \ |
158 | cache->first[row]=0; \ |
159 | \ |
160 | for(col=0;col<CACHEDEPTH;col++) \ |
161 | cache->indices[row][col]=NO_NODE; \ |
162 | } \ |
163 | } |
164 | |
165 | |
166 | /*+ Cache data structure forward declarations (for planetsplitter). +*/ |
167 | CACHE_STRUCTURE_FWD(NodeX) |
168 | CACHE_STRUCTURE_FWD(SegmentX) |
169 | CACHE_STRUCTURE_FWD(WayX) |
170 | |
171 | /*+ Cache data structure forward declarations (for router). +*/ |
172 | CACHE_STRUCTURE_FWD(Node) |
173 | CACHE_STRUCTURE_FWD(Segment) |
174 | CACHE_STRUCTURE_FWD(Way) |
175 | CACHE_STRUCTURE_FWD(TurnRelation) |
176 | |
177 | |
178 | #endif /* CACHE_H */ |
179 | |
180 | #endif /* SLIM */ |