Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Annotation of /trunk/src/cache.h
Parent Directory
|
Revision Log
Revision 1296 -
(hide annotations)
(download)
(as text)
Tue May 7 09:31:00 2013 UTC (11 years, 10 months ago) by amb
File MIME type: text/x-chdr
File size: 6419 byte(s)
Tue May 7 09:31:00 2013 UTC (11 years, 10 months ago) by amb
File MIME type: text/x-chdr
File size: 6419 byte(s)
Move the cache functions out of cache.c and into each data type's header file as inline functions.
1 | amb | 1292 | /*************************************** |
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 | amb | 1293 | #if SLIM |
24 | |||
25 | amb | 1292 | #ifndef CACHE_H |
26 | #define CACHE_H /*+ To stop multiple inclusions. +*/ | ||
27 | |||
28 | amb | 1293 | #include <unistd.h> |
29 | amb | 1296 | #include <stdlib.h> |
30 | amb | 1293 | |
31 | amb | 1292 | #include "types.h" |
32 | |||
33 | |||
34 | amb | 1296 | /* Macros for constants */ |
35 | amb | 1292 | |
36 | amb | 1296 | #define CACHEWIDTH 2048 /*+ The width of the cache. +*/ |
37 | #define CACHEDEPTH 16 /*+ The depth of the cache. +*/ | ||
38 | amb | 1292 | |
39 | |||
40 | amb | 1296 | /* Macro for structure forward declaration */ |
41 | amb | 1292 | |
42 | amb | 1296 | #define CACHE_STRUCTURE_FWD(type) typedef struct _##type##Cache type##Cache; |
43 | amb | 1292 | |
44 | |||
45 | amb | 1296 | /* Macro for structure declaration */ |
46 | amb | 1292 | |
47 | amb | 1296 | /*+ 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 | amb | 1292 | |
58 | |||
59 | amb | 1296 | /* Macros for function prototypes */ |
60 | amb | 1292 | |
61 | amb | 1296 | #define CACHE_NEWCACHE_PROTO(type) static inline type##Cache *New##type##Cache(void); |
62 | amb | 1292 | |
63 | amb | 1296 | #define CACHE_DELETECACHE_PROTO(type) static inline void Delete##type##Cache(type##Cache *cache); |
64 | amb | 1292 | |
65 | amb | 1296 | #define CACHE_FETCHCACHE_PROTO(type) static inline type *FetchCached##type(type##Cache *cache,index_t index,int fd,off_t offset); |
66 | amb | 1292 | |
67 | amb | 1296 | #define CACHE_INVALIDATECACHE_PROTO(type) static inline void Invalidate##type##Cache(type##Cache *cache); |
68 | amb | 1292 | |
69 | |||
70 | amb | 1296 | /* Macros for function definitions */ |
71 | amb | 1292 | |
72 | amb | 1296 | /*+ A macro to create a function that creates a new cache data structure. +*/ |
73 | #define CACHE_NEWCACHE(type) \ | ||
74 | \ | ||
75 | static inline type##Cache *New##type##Cache(void) \ | ||
76 | { \ | ||
77 | type##Cache *cache; \ | ||
78 | \ | ||
79 | cache=(type##Cache*)malloc(sizeof(type##Cache)); \ | ||
80 | \ | ||
81 | Invalidate##type##Cache(cache); \ | ||
82 | \ | ||
83 | return(cache); \ | ||
84 | } | ||
85 | amb | 1292 | |
86 | amb | 1296 | |
87 | /*+ A macro to create a function that deletes a cache data structure. +*/ | ||
88 | #define CACHE_DELETECACHE(type) \ | ||
89 | \ | ||
90 | static inline void Delete##type##Cache(type##Cache *cache) \ | ||
91 | { \ | ||
92 | free(cache); \ | ||
93 | } | ||
94 | |||
95 | |||
96 | /*+ A macro to create a function that fetches an item from a cache data structure. +*/ | ||
97 | #define CACHE_FETCHCACHE(type) \ | ||
98 | \ | ||
99 | static inline type *FetchCached##type(type##Cache *cache,index_t index,int fd,off_t offset) \ | ||
100 | { \ | ||
101 | int row=index%CACHEWIDTH; \ | ||
102 | int col; \ | ||
103 | \ | ||
104 | for(col=0;col<CACHEDEPTH;col++) \ | ||
105 | if(cache->indices[row][col]==index) \ | ||
106 | return(&cache->data[row][col]); \ | ||
107 | \ | ||
108 | col=cache->first[row]; \ | ||
109 | \ | ||
110 | cache->first[row]=(cache->first[row]+1)%CACHEDEPTH; \ | ||
111 | \ | ||
112 | SeekReadFile(fd,&cache->data[row][col],sizeof(type),offset+(off_t)index*sizeof(type)); \ | ||
113 | \ | ||
114 | cache->indices[row][col]=index; \ | ||
115 | \ | ||
116 | return(&cache->data[row][col]); \ | ||
117 | } | ||
118 | |||
119 | |||
120 | /*+ A macro to create a function that invalidates the contents of a cache data structure. +*/ | ||
121 | #define CACHE_INVALIDATECACHE(type) \ | ||
122 | \ | ||
123 | static inline void Invalidate##type##Cache(type##Cache *cache) \ | ||
124 | { \ | ||
125 | int row,col; \ | ||
126 | \ | ||
127 | for(row=0;row<CACHEWIDTH;row++) \ | ||
128 | { \ | ||
129 | cache->first[row]=0; \ | ||
130 | \ | ||
131 | for(col=0;col<CACHEDEPTH;col++) \ | ||
132 | cache->indices[row][col]=NO_NODE; \ | ||
133 | } \ | ||
134 | } | ||
135 | |||
136 | |||
137 | /*+ Cache data structure forward declarations. +*/ | ||
138 | CACHE_STRUCTURE_FWD(Node) | ||
139 | CACHE_STRUCTURE_FWD(Segment) | ||
140 | CACHE_STRUCTURE_FWD(Way) | ||
141 | CACHE_STRUCTURE_FWD(TurnRelation) | ||
142 | |||
143 | |||
144 | amb | 1293 | #endif /* CACHE_H */ |
145 | amb | 1292 | |
146 | amb | 1293 | #endif /* SLIM */ |