Routino SVN Repository Browser

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

ViewVC logotype

Annotation of /trunk/src/cache.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1415 - (hide annotations) (download) (as text)
Sat Jun 22 18:52:38 2013 UTC (11 years, 8 months ago) by amb
File MIME type: text/x-chdr
File size: 9179 byte(s)
Use SlimMapFile() and SlimUnmapFile() [see previous log message] and also use
SlimFetch() instead of SeekReadFileUnbuffered() and SlimReplace() instead of
SeekWriteFileUnbuffered() to hide the internals of the slim mode.

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 1297 #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 amb 1296 #define CACHE_INVALIDATECACHE_PROTO(type) static inline void Invalidate##type##Cache(type##Cache *cache);
70 amb 1292
71    
72 amb 1296 /* Macros for function definitions */
73 amb 1292
74 amb 1296 /*+ 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 amb 1292
88 amb 1296
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 amb 1297 /*+ A macro to create a function that fetches an item from a cache data structure or reads from file. +*/
99 amb 1296 #define CACHE_FETCHCACHE(type) \
100     \
101 amb 1415 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     SlimFetch(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 amb 1296 }
120    
121    
122 amb 1297 /*+ 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 amb 1415 SlimReplace(fd,&cache->data[row][col],sizeof(type),offset+(off_t)index*sizeof(type)); \
146 amb 1297 }
147    
148    
149 amb 1296 /*+ 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 amb 1297 /*+ 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 amb 1296 CACHE_STRUCTURE_FWD(Node)
173     CACHE_STRUCTURE_FWD(Segment)
174     CACHE_STRUCTURE_FWD(Way)
175     CACHE_STRUCTURE_FWD(TurnRelation)
176    
177    
178 amb 1293 #endif /* CACHE_H */
179 amb 1292
180 amb 1293 #endif /* SLIM */