-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathncproplist.c
350 lines (322 loc) · 10.1 KB
/
ncproplist.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*********************************************************************
* Copyright 2018, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* $Header$
*********************************************************************/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include "ncdispatch.h"
#include "nccrc.h"
#include "ncproplist.h"
#undef DEBUG
#define ASSERTIONS
#ifdef ASSERTIONS
#define ASSERT(x) assert(x)
#else
#define ASSERT(x)
#endif
/**************************************************/
#define MINPROPS 2
#define EXPANDFACTOR 1
#define hasspace(plist,nelems) ((plist)->alloc >= ((plist)->count + (nelems)))
#define emptyprop {" ",0,0,0,NULL}
/**************************************************/
/* Forward */
static int ncproplistinit(NCproplist* plist);
static int extendplist(NCproplist* plist, size_t nprops);
/* Static'ize everything for plugins */
#ifdef NETCDF_PROPLIST_H
#define OPTSTATIC static
static NCproplist* ncproplistnew(void);
static int ncproplistfree(NCproplist* plist);
static int ncproplistadd(NCproplist* plist, const char* key, uintptr_t value);
static int ncproplistaddbytes(NCproplist* plist, const char* key, void* value, uintptr_t size);
static int ncproplistaddstring(NCproplist* plist, const char* key, const char* str);
static int ncproplistaddx(NCproplist* plist, const char* key, void* value, uintptr_t size, uintptr_t userdata, NCPreclaimfcn fcn);
static int ncproplistclone(const NCproplist* src, NCproplist* clone);
static int ncproplistget(const NCproplist* plist, const char* key, uintptr_t* valuep, uintptr_t* sizep);
static int ncproplistith(const NCproplist* plist, size_t i, char* const * keyp, uintptr_t const * valuep, uintptr_t* sizep);
#else /*!NETCDF_PROPLIST_H*/
#define OPTSTATIC
#endif /*NETCDF_PROPLIST_H*/
/**
* Create new property list
* @return pointer to the created property list.
*/
OPTSTATIC NCproplist*
ncproplistnew(void)
{
NCproplist* plist = NULL;
plist = calloc(1,sizeof(NCproplist));
if(ncproplistinit(plist) != NC_NOERR)
{ncproplistfree(plist); plist = NULL;}
return plist;
}
/**
* Reclaim memory used by a property list
* @param plist to reclaim
* @return NC_NOERR if succeed, NC_EXXX otherwise.
*/
OPTSTATIC int
ncproplistfree(NCproplist* plist)
{
int stat = NC_NOERR;
size_t i;
if(plist == NULL) goto done;
if(plist->properties != NULL) {
for(i=0;i<plist->count;i++) {
NCProperty* prop = &plist->properties[i];
void* ptr = (void*)prop->value; /* convert to ptr */
assert(prop->flags & (NCPF_SIMPLE|NCPF_BYTES|NCPF_COMPLEX));
if(prop->flags & NCPF_SIMPLE) continue; /* no reclaim needed */
if(prop->flags & NCPF_BYTES) {
if(ptr != NULL) free(ptr);
} else { /* (prop->flags & NCPF_COMPLEX) */
int ok;
assert(prop->reclaim != NULL);
ok = prop->reclaim(prop->userdata, prop->key, ptr, prop->size);
if(!ok && stat == NC_NOERR) stat = NC_EINVAL;
}
}
free(plist->properties);
}
free(plist);
done:
return stat;
}
/**
* Add a non-reclaimable entry to the property list
* @param plist into which the value is be inserted.
* @param key
* @param value
* @return NC_NOERR if succeed, NC_EXXX otherwise.
*/
OPTSTATIC int
ncproplistadd(NCproplist* plist, const char* key, uintptr_t value)
{
int stat = NC_NOERR;
NCProperty* prop = NULL;
size_t keylen;
if(plist == NULL) goto done;
if(!hasspace(plist,1)) {if((stat = extendplist(plist,(plist->count+1)*EXPANDFACTOR))) goto done;} /* extra space */
prop = &plist->properties[plist->count];
keylen = strlen(key);
if(keylen > NCPROPSMAXKEY) keylen = NCPROPSMAXKEY; /* truncate */
memcpy(prop->key,key,keylen);
prop->key[keylen] = '\0';
prop->value = value;
prop->flags = NCPF_SIMPLE;
plist->count++;
done:
return stat;
}
/**
* Add a reclaimable entry to the property list, where the value
* can be reclaimed using a simple free();
* @param plist into which the value is be inserted.
* @param key
* @param value ptr to memory chunk
* @param size |*value|
* @return NC_NOERR if succeed, NC_EXXX otherwise.
*/
OPTSTATIC int
ncproplistaddbytes(NCproplist* plist, const char* key, void* value, uintptr_t size)
{
int stat = NC_NOERR;
NCProperty* prop = NULL;
size_t keylen;
if(plist == NULL) goto done;
if(!hasspace(plist,1)) {if((stat = extendplist(plist,(plist->count+1)*EXPANDFACTOR))) goto done;} /* extra space */
prop = &plist->properties[plist->count];
keylen = strlen(key);
if(keylen > NCPROPSMAXKEY) keylen = NCPROPSMAXKEY; /* truncate */
memcpy(prop->key,key,keylen);
prop->key[keylen] = '\0';
prop->value = (uintptr_t)value;
prop->flags = NCPF_BYTES;
plist->count++;
done:
return stat;
}
/**
* Add a reclaimable entry to the property list, where the value
* can be reclaimed using a simple free();
* @param plist into which the value is be inserted.
* @param key
* @param value ptr to memory chunk
* @param size |*value|
* @return NC_NOERR if succeed, NC_EXXX otherwise.
*/
OPTSTATIC int
ncproplistaddstring(NCproplist* plist, const char* key, const char* str)
{
uintptr_t size = 0;
if(str) size = (uintptr_t)strlen(str);
return ncproplistaddbytes(plist,key,(void*)str,size);
}
/**
* Most general case for adding a property.
* @param plist into which the value is be inserted.
* @param key
* @param value
* @param size
* @param userdata extra environment data for the reclaim function.
* @param fcn the reclaim function
* @return NC_NOERR if succeed, NC_EXXX otherwise.
*/
OPTSTATIC int
ncproplistaddx(NCproplist* plist, const char* key, void* value, uintptr_t size, uintptr_t userdata, NCPreclaimfcn fcn)
{
int stat = NC_NOERR;
NCProperty* prop = NULL;
size_t keylen;
if(plist == NULL) goto done;
if(!hasspace(plist,1)) {if((stat = extendplist(plist,(plist->count+1)*EXPANDFACTOR))) goto done;} /* extra space */
prop = &plist->properties[plist->count];
keylen = strlen(key);
if(keylen > NCPROPSMAXKEY) keylen = NCPROPSMAXKEY; /* truncate */
memcpy(prop->key,key,keylen);
prop->key[keylen] = '\0';
prop->value = (uintptr_t)value;
prop->size = size;
prop->reclaim = fcn;
prop->userdata = userdata;
prop->flags = NCPF_COMPLEX;
plist->count++;
done:
return stat;
}
OPTSTATIC int
ncproplistclone(const NCproplist* src, NCproplist* clone)
{
int stat = NC_NOERR;
size_t i;
NCProperty* srcprops;
NCProperty* cloneprops;
if(src == NULL || clone == NULL) {stat = NC_EINVAL; goto done;}
if((stat=ncproplistinit(clone))) goto done;
if((stat=extendplist(clone,src->count))) goto done;
srcprops = src->properties;
cloneprops = clone->properties;
for(i=0;i<src->count;i++) {
cloneprops[i] = srcprops[i];
strncpy(cloneprops[i].key,srcprops[i].key,sizeof(cloneprops[i].key));
#if 0
cloneprops[i]->flags = srcprops[i]->flags;
cloneprops[i]->value = srcprops[i]->value;
cloneprops[i]->size = srcprops[i]->size;
cloneprops[i]->userdata = srcprops[i]->userdata;
cloneprops[i]->reclaim = srcprops->reclaim;
#endif
}
done:
return stat;
}
/* Increase size of a plist to be at lease nprops properties */
static int
extendplist(NCproplist* plist, size_t nprops)
{
int stat = NC_NOERR;
size_t newsize = plist->count + nprops;
NCProperty* newlist = NULL;
if((plist->alloc >= newsize) || (nprops == 0))
goto done; /* Already enough space */
newlist = realloc(plist->properties,newsize*sizeof(NCProperty));
if(newlist == NULL) {stat = NC_ENOMEM; goto done;}
plist->properties = newlist; newlist = NULL;
plist->alloc = newsize;
done:
return stat;
}
/**
* Lookup key and return value and size
* @param plist to search
* @param key for which to search
* @param valuep returned value
* @param sizep returned size
* @return NC_NOERR if key found, NC_ENOOBJECT if key not found; NC_EXXX otherwise
*/
OPTSTATIC int
ncproplistget(const NCproplist* plist, const char* key, uintptr_t* valuep, uintptr_t* sizep)
{
int stat = NC_ENOOBJECT; /* assume not found til proven otherwise */
size_t i;
NCProperty* props;
uintptr_t value = 0;
uintptr_t size = 0;
if(plist == NULL || key == NULL) goto done;
for(i=0,props=plist->properties;i<plist->count;i++,props++) {
if(strcmp(props->key,key)==0) {
value = props->value;
size = props->size;
stat = NC_NOERR; /* found */
break;
}
}
if(valuep) *valuep = value;
if(sizep) *sizep = size;
done:
return stat;
}
/* Iteration support */
/**
* Get the ith key+value.a
* @param plist to search
* @param i which property to get.
* @param keyp return i'th key
* @param valuep return i'th value
* @param valuep return i'th size
* @return NC_NOERR if success, NC_EINVAL otherwise
*/
OPTSTATIC int
ncproplistith(const NCproplist* plist, size_t i, char* const * keyp, uintptr_t const * valuep, uintptr_t* sizep)
{
int stat = NC_NOERR;
NCProperty* prop = NULL;
if(plist == NULL) goto done;
if(i >= plist->count) {stat = NC_EINVAL; goto done;}
prop = &plist->properties[i];
if(keyp) *((char**)keyp) = (char*)prop->key;
if(valuep) *((uintptr_t*)valuep) = (uintptr_t)prop->value;
if(sizep) *sizep = prop->size;
done:
return stat;
}
/**************************************************/
/* Support Functions */
/**
* Initialize a new property list
*/
static int
ncproplistinit(NCproplist* plist)
{
/* Assume property list will hold at lease MINPROPS properties */
plist->alloc = MINPROPS;
plist->count = 0;
plist->properties = (NCProperty*)calloc(MINPROPS,sizeof(NCProperty));
return (plist->properties?NC_NOERR:NC_ENOMEM);
}
/* Suppress unused statics warning */
static void
ncproplist_unused(void)
{
void* unused = ncproplist_unused;
unused = ncproplistnew;
unused = ncproplistfree;
unused = ncproplistadd;
unused = ncproplistaddbytes;
unused = ncproplistaddstring;
unused = ncproplistaddx;
unused = ncproplistclone;
unused = ncproplistget;
unused = ncproplistith;
unused = ncproplistinit;
unused = (void*)ncproplistith;
unused = unused;
}