-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathncxcache.c
311 lines (267 loc) · 6.18 KB
/
ncxcache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata
See LICENSE.txt for license information.
*/
/** \file \internal
Internal netcdf-4 functions.
This file contains functions for manipulating NCxcache objects.
Warning: This code depends critically on the assumption that
|void*| == |uintptr_t|
*/
/* 0 => no debug */
#define DEBUG 0
#define CATCH
/* Define this for debug so that table sizes are small */
#define SMALLTABLE
#ifdef CATCH
/* Warning: do not evalue x more than once */
#define THROW(x) throw(x)
static void breakpoint(void) {}
static int ignore[] = {0};
static int throw(int x)
{
int* p;
if(x != 0) {
for(p=ignore;*p;p++) {if(x == *p) break;}
if(*p == 0) breakpoint();
}
return x;
}
#else
#define THROW(x) (x)
#endif
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <assert.h>
#include "nc4internal.h"
#include "ncexhash.h"
#include "ncxcache.h"
#ifdef SMALLTABLE
/* Keep the table sizes small initially */
#define DFALTTABLESIZE 4
#define DFALTLEAFLEN 4
#else
#define DFALTTABLESIZE 32
#define DFALTLEAFLEN 12
#endif
static void insertafter(NCxnode* current, NCxnode* node);
static void unlinknode(NCxnode* node);
#if DEBUG > 0
void verifylru(NCxcache* cache);
static void
xverify(NCxcache* cache)
{
verifylru(cache);
}
void
verifylru(NCxcache* cache)
{
NCxnode* p;
for(p=cache->lru.next;p != &cache->lru;p=p->next) {
if(p->next == NULL || p->prev == NULL) {
xverify(cache);
}
}
}
#endif
/* Locate object by hashkey in an NCxcache */
int
ncxcachelookup(NCxcache* NCxcache, ncexhashkey_t hkey, void** op)
{
int stat = NC_NOERR;
uintptr_t inode = 0;
NCxnode* node = NULL;
if(NCxcache == NULL) return THROW(NC_EINVAL);
assert(NCxcache->map != NULL);
if((stat=ncexhashget(NCxcache->map,hkey,&inode)))
{stat = THROW(NC_ENOOBJECT); goto done;} /* not present */
node = (void*)inode;
if(op) *op = node->content;
done:
return stat;
}
/* Move object to the front of the LRU list */
int
ncxcachetouch(NCxcache* cache, ncexhashkey_t hkey)
{
int stat = NC_NOERR;
uintptr_t inode = 0;
NCxnode* node = NULL;
if(cache == NULL) return THROW(NC_EINVAL);
if((stat=ncexhashget(cache->map,hkey,&inode)))
{stat = THROW(NC_ENOOBJECT); goto done;} /* not present */
node = (void*)inode;
/* unlink */
unlinknode(node);
/* Relink into front of chain */
insertafter(&cache->lru,node);
#if DEBUG > 0
verifylru(cache);
#endif
done:
return stat;
}
/* Add object to the cache */
int
ncxcacheinsert(NCxcache* cache, const ncexhashkey_t hkey, void* o)
{
int stat = NC_NOERR;
uintptr_t inode = 0;
NCxnode* node = NULL;
if(cache == NULL) return THROW(NC_EINVAL);
#ifndef NCXUSER
node = calloc(1,sizeof(NCxnode));
#else
node = (NCxnode*)o;
#endif
node->content = o; /* Cheat and make content point to the node part*/
inode = (uintptr_t)node;
stat = ncexhashput(cache->map,hkey,inode);
if(stat)
goto done;
/* link into the LRU chain at front */
insertafter(&cache->lru,node);
#if DEBUG > 0
verifylru(cache);
#endif
node = NULL;
done:
#ifndef NCXUSER
if(node) nullfree(node);
#endif
return THROW(stat);
}
/* Remove object from the index;*/
int
ncxcacheremove(NCxcache* cache, ncexhashkey_t hkey, void** op)
{
int stat = NC_NOERR;
uintptr_t inode = 0;
NCxnode* node = NULL;
if(cache == NULL) return THROW(NC_EINVAL);
/* Remove from the hash map */
if((stat=ncexhashremove(cache->map,hkey,&inode)))
{stat = NC_ENOOBJECT; goto done;} /* not present */
node = (NCxnode*)inode;
/* unlink */
unlinknode(node);
#if DEBUG > 0
verifylru(cache);
#endif
if(op) {
*op = node->content;
}
#ifndef NCXUSER
nullfree(node);
#endif
done:
return THROW(stat);
}
/* Free a cache */
void
ncxcachefree(NCxcache* cache)
{
NCxnode* lru = NULL;
if(cache == NULL) return;
lru = &cache->lru;
#ifndef NCXUSER
{
NCxnode* p = NULL;
NCxnode* next = NULL;
/* walk the lru chain */
next = NULL;
for(p=lru->next;p != lru;) {
next = p->next;
nullfree(p);
p = next;
}
}
#endif
lru->next = (lru->prev = lru); /*reset*/
ncexhashmapfree(cache->map);
free(cache);
}
/* Create a new cache holding at least size objects */
int
ncxcachenew(size_t leaflen, NCxcache** cachep)
{
int stat = NC_NOERR;
NCxcache* cache = NULL;
if(leaflen == 0) leaflen = DFALTLEAFLEN;
cache = calloc(1,sizeof(NCxcache));
if(cache == NULL)
{stat = NC_ENOMEM; goto done;}
cache->map = ncexhashnew((int)leaflen);
if(cache->map == NULL)
{stat = NC_ENOMEM; goto done;}
cache->lru.next = &cache->lru;
cache->lru.prev = &cache->lru;
if(cachep) {*cachep = cache; cache = NULL;}
done:
ncxcachefree(cache);
return THROW(stat);
}
void
ncxcacheprint(NCxcache* cache)
{
int i;
NCxnode* p = NULL;
fprintf(stderr,"NCxcache: lru=");
fprintf(stderr,"{");
for(i=0,p=cache->lru.next;p != &cache->lru;p=p->next,i++) {
if(i>0) fprintf(stderr,",");
fprintf(stderr,"%p:%p",p,p->content);
}
fprintf(stderr,"}\n");
ncexhashprint(cache->map);
}
void*
ncxcachefirst(NCxcache* cache)
{
if(cache == NULL) return NULL;
if(ncexhashcount(cache->map) == 0)
return NULL;
return cache->lru.next->content;
}
void*
ncxcachelast(NCxcache* cache)
{
if(cache == NULL) return NULL;
if(ncexhashcount(cache->map) == 0)
return NULL;
return cache->lru.prev->content;
}
/* Insert node after current */
static void
insertafter(NCxnode* current, NCxnode* node)
{
NCxnode* curnext = current->next;
current->next = node;
node->prev = current;
node->next = curnext;
curnext->prev = node;
}
/* Remove node from chain */
static void
unlinknode(NCxnode* node)
{
NCxnode* next;
NCxnode* prev;
assert(node != NULL);
next = node->next;
prev = node->prev;
/* repair the chain */
next->prev = prev;
prev->next = next;
node->next = node->prev = NULL;
}
ncexhashkey_t
ncxcachekey(const void* key, size_t size)
{
return ncexhashkey((unsigned char*)key,size);
}