File: d4mem.c

package info (click to toggle)
netcdf 1:4.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 104,952 kB
  • sloc: ansic: 228,683; sh: 10,980; yacc: 2,561; makefile: 1,319; lex: 1,173; xml: 173; awk: 2
file content (406 lines) | stat: -rw-r--r-- 12,137 bytes parent folder | download | duplicates (3)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*********************************************************************
 *   Copyright 2018, UCAR/Unidata
 *   See netcdf/COPYRIGHT file for copying and redistribution conditions.
 *********************************************************************/

#include <stdlib.h>
#include "netcdf.h"
#include "ncoffsets.h"

/* It is helpful to have a structure that contains memory and an offset */
typedef struct Position {char* memory; ptrdiff_t offset;} Position;

#ifndef emalloc
#define emalloc(n) malloc(n)
#define ecalloc(n) calloc(1,n)
#define erealloc(p,n) realloc(p,n)
#define efree(p) free(p)
#endif

/*Forward*/
static int d4reclaim_datar(NC_TYPE_INFO_T* tsym, Position* reclaimer);
static int d4reclaim_usertype(NC_TYPE_INFO_T* tsym, Position* reclaimer);
static int d4reclaim_vlen(NC_TYPE_INFO_T* tsym, Position* reclaimer);
static int d4reclaim_enum(NC_TYPE_INFO_T* tsym, Position* reclaimer);
static int d4reclaim_opaque(NC_TYPE_INFO_T* tsym, Position* reclaimer);
static int d4reclaim_compound(NC_TYPE_INFO_T* tsym, Position* reclaimer);

static int d4clone_datar(NC_TYPE_INFO_T* tsym, Position* reclaimer);
static int d4clone_usertype(NC_TYPE_INFO_T* tsym, Position* reclaimer);
static int d4clone_vlen(NC_TYPE_INFO_T* tsym, Position* reclaimer);
static int d4clone_enum(NC_TYPE_INFO_T* tsym, Position* reclaimer);
static int d4clone_opaque(NC_TYPE_INFO_T* tsym, Position* reclaimer);
static int d4clone_compound(NC_TYPE_INFO_T* tsym, Position* reclaimer);

static ptrdiff_t read_align(ptrdiff_t offset, unsigned long alignment);
static size_t hdf5typealignment(NC_TYPE_INFO_T* type);

/**
Reclaim a chunk of memory. Do not reclaim top level
because we do not know how it was allocated.

@param tsym - the root type node of the data to be reclaimed
@param memory - top-level memory whose content is to be reclaimed
@param count - number of instances of tsym in memory

*/

int
d4reclaim_data(NCD4node* tsym, void* memory, size_t count)
{
    int stat = NC_NOERR;
    size_t i;
    Position position;
    
    if(tsym == NULL
       || (memory == NULL && count > 0))
        {stat = NC_EINVAL; goto done;}
    if(memory == NULL || count == 0)
        goto done; /* ok, do nothing */
    if(tsym->meta.isfixedsize)
    	goto done; /* Should be nothing hanging off of the memory chunk */
    position.offset = 0;
    position.memory = memory;
    for(i=0;i<count;i++) {
	if((stat=d4reclaim_datar(tsym,&position))) /* reclaim one instance */
	    break;
    }
done:
    return stat;
}

/* Recursive type walker: reclaim a single instance */
static int
d4reclaim_datar(NCD4node* tsym, Position* position)
{
    int stat = NC_NOERR;
    
    switch  (tsym->subsort) {
    case NC_CHAR: case NC_BYTE: case NC_UBYTE:
    case NC_SHORT: case NC_USHORT:
    case NC_INT: case NC_UINT: case NC_FLOAT:
    case NC_INT64: case NC_UINT64: case NC_DOUBLE:
        position->offset += tsym->meta.memsize;
	break;
    case NC_STRING: {
	char** sp = (char**)(position->memory+position->offset);
        /* Need to reclaim string */
	if(*sp != NULL) efree(*sp);
	position->offset += tsym->meta.memsize;
	} break;
    default:
    	/* reclaim a user type */
	stat = d4reclaim_usertype(tsym,position);
	break;
    }
    return stat;
}
	
static int
d4reclaim_usertype(NCD4node* tsym, Position* position)
{
    int stat = NC_NOERR;

    /* Get info about the xtype */
    switch (tsym->nc_type_class) {
    case NC_OPAQUE: stat = d4reclaim_opaque(tsym,position); break;
    case NC_ENUM: stat = d4reclaim_enum(tsym,position); break;
    case NC_VLEN: stat = d4reclaim_vlen(tsym,position); break;
    case NC_COMPOUND: stat = d4reclaim_compound(tsym,position); break;
    default:
        stat = NC_EINVAL;
	break;
    }
    return stat;
}

static int
d4reclaim_vlen(NCD4node* tsym, Position* position)
{
    int stat = NC_NOERR;
    size_t i;
    NCD4node* basetype;
    nc_vlen_t* vl = (nc_vlen_t*)(position->memory+position->offset);

    if((stat=nc4_find_type(tsym->container->nc4_info,tsym->u.v.base_nc_typeid,&basetype)))
	goto done;    

    /* Free up each entry in the vlen list */
    if(vl->p != NULL) {
	Reclaim vposition;
	vposition.memory = vl->p;
	vposition.offset = 0;
        for(i=0;i<vl->len;i++) {
	    size_t alignment = hdf5typealignment(basetype);
	    vposition.offset = read_align(vposition.offset,alignment);
	    if((stat = d4reclaim_datar(basetype,&vposition))) goto done;
	    vposition.offset += basetype->size;
	}
	position->offset += tsym->size;
	efree(vl->p);
    }
done:
    return stat;
}

static int
d4reclaim_enum(NCD4node* tsym, Position* position)
{
    int stat = NC_NOERR;
    NCD4node* basetype;
    if((stat=nc4_find_type(tsym->container->nc4_info,tsym->u.v.base_nc_typeid,&basetype)))
	return stat;
    return d4reclaim_datar(basetype,position);
}

static int
d4reclaim_opaque(NCD4node* tsym, Position* position)
{
    /* basically a fixed size sequence of bytes */
    position->offset += tsym->size;
    return NC_NOERR;
}

static int
d4reclaim_compound(NCD4node* tsym, Position* position)
{
    int stat = NC_NOERR;
    int nfields;
    size_t fid, i, arraycount;
    ptrdiff_t saveoffset;
    size_t cmpdalign = hdf5typealignment(tsym);

    position->offset = read_align(position->offset,cmpdalign);
    saveoffset = position->offset;

    /* Get info about each field in turn and reclaim it */
    nfields = nclistlength(tsym->u.c.field);
    for(fid=0;fid<nfields;fid++) {
	NC_FIELD_INFO_T* field = nclistget(tsym->u.c.field,fid);
	NCD4node* fieldtype;
        int ndims = field->ndims;
	size_t fieldalign;
	if((stat=nc4_find_type(tsym->container->nc4_info,field->nc_typeid,&fieldtype)))
	    goto done;    
	fieldalign = hdf5typealignment(fieldtype);
	/* compute the total number of elements in the field array */
	for(i=0;i<ndims;i++) arraycount *= field->dim_size[i];
	position->offset = read_align(position->offset,fieldalign);
	for(i=0;i<arraycount;i++) {
	    if((stat = d4reclaim_datar(fieldtype, position))) goto done;
	}		
    }
    position->offset = saveoffset;
    position->offset += tsym->size;
done:
    return stat;
}

/**************************************************/
int
d4clone_data(NCD4node* tsym, void* src, void** dstp, size_t count)
{
    int stat = NC_NOERR;
    size_t i;
    Position srcpos;
    Position dstpos = {NULL,0};
    
    if(tsym == NULL)
        {stat = NC_EINVAL; goto done;}
    if(src == NULL && count > 0)
        {stat = NC_EINVAL; goto done;}
    if(src == NULL || count == 0)
        goto done; /* ok, do nothing */
    srcpos.offset = 0;
    srcpos.memory = src;
    for(i=0;i<count;i++) {
	if((stat=d4clone_datar(tsym,&srcpos,&dstpos))) /* clone one instance */
	    break;
    }
done:
    return stat;
}

/* Recursive type walker: reclaim a single instance */
static int
d4clone_datar(NCD4node* tsym, Position* src, Position* dst)
{
    int stat = NC_NOERR;
    
    switch  (tsym->nc_type_class) {
    case NC_CHAR: case NC_BYTE: case NC_UBYTE:
    case NC_SHORT: case NC_USHORT:
    case NC_INT: case NC_UINT: case NC_FLOAT:
    case NC_INT64: case NC_UINT64: case NC_DOUBLE:
        position->offset += tsym->size;
	break;
    case NC_STRING: {
	char** sp = (char**)(position->memory+position->offset);
        /* Need to reclaim string */
	if(*sp != NULL) efree(*sp);
	position->offset += tsym->size;
	} break;
    default:
    	/* reclaim a user type */
	stat = d4clone_usertype(tsym,position);
	break;
    }
    return stat;
}
	
static int
d4clone_usertype(NCD4node* tsym, Position* position)
{
    int stat = NC_NOERR;

    /* Get info about the xtype */
    switch (tsym->nc_type_class) {
    case NC_OPAQUE: stat = d4clone_opaque(tsym,position); break;
    case NC_ENUM: stat = d4clone_enum(tsym,position); break;
    case NC_VLEN: stat = d4clone_vlen(tsym,position); break;
    case NC_COMPOUND: stat = d4clone_compound(tsym,position); break;
    default:
        stat = NC_EINVAL;
	break;
    }
    return stat;
}

static int
d4clone_vlen(NCD4node* tsym, Position* position)
{
    int stat = NC_NOERR;
    size_t i;
    NCD4node* basetype;
    nc_vlen_t* vl = (nc_vlen_t*)(position->memory+position->offset);

    if((stat=nc4_find_type(tsym->container->nc4_info,tsym->u.v.base_nc_typeid,&basetype)))
	goto done;    

    /* Free up each entry in the vlen list */
    if(vl->p != NULL) {
	Reclaim vposition;
	vposition.memory = vl->p;
	vposition.offset = 0;
        for(i=0;i<vl->len;i++) {
	    size_t alignment = hdf5typealignment(basetype);
	    vposition.offset = read_align(vposition.offset,alignment);
	    if((stat = d4clone_datar(basetype,&vposition))) goto done;
	    vposition.offset += basetype->size;
	}
	position->offset += tsym->size;
	efree(vl->p);
    }
done:
    return stat;
}

static int
d4clone_enum(NCD4node* tsym, Position* position)
{
    int stat = NC_NOERR;
    NCD4node* basetype;
    if((stat=nc4_find_type(tsym->container->nc4_info,tsym->u.v.base_nc_typeid,&basetype)))
	return stat;
    return d4clone_datar(basetype,position);
}

static int
d4clone_opaque(NCD4node* tsym, Position* position)
{
    /* basically a fixed size sequence of bytes */
    position->offset += tsym->size;
    return NC_NOERR;
}

static int
d4clone_compound(NCD4node* tsym, Position* position)
{
    int stat = NC_NOERR;
    int nfields;
    size_t fid, i, arraycount;
    ptrdiff_t saveoffset;
    size_t cmpdalign = hdf5typealignment(tsym);

    position->offset = read_align(position->offset,cmpdalign);
    saveoffset = position->offset;

    /* Get info about each field in turn and reclaim it */
    nfields = nclistlength(tsym->u.c.field);
    for(fid=0;fid<nfields;fid++) {
	NC_FIELD_INFO_T* field = nclistget(tsym->u.c.field,fid);
	NCD4node* fieldtype;
        int ndims = field->ndims;
	size_t fieldalign;
	if((stat=nc4_find_type(tsym->container->nc4_info,field->nc_typeid,&fieldtype)))
	    goto done;    
	fieldalign = hdf5typealignment(fieldtype);
	/* compute the total number of elements in the field array */
	for(i=0;i<ndims;i++) arraycount *= field->dim_size[i];
	position->offset = read_align(position->offset,fieldalign);
	for(i=0;i<arraycount;i++) {
	    if((stat = d4clone_datar(fieldtype, position))) goto done;
	}		
    }
    position->offset = saveoffset;
    position->offset += tsym->size;
done:
    return stat;
}


/**************************************************/
/* Alignment code */

static ptrdiff_t
read_align(ptrdiff_t offset, size_t alignment)
{
    size_t delta = (offset % alignment);
    if(delta == 0) return offset;
    return offset + (alignment - delta);
}

/*
The heart of this is the following macro,
which computes the offset of a field x
when preceded by a char field.
The assumptions appear to be as follows:
1. the offset produced in this situation indicates
   the alignment for x relative in such a way that it
   depends only on the types that precede it in the struct.
2. the compiler does not reorder fields.
3. arrays are tightly packed.
4. nested structs are alignd according to their first member
   (this actually follows from C language requirement that
    a struct can legally be cast to an instance of its first member).
Given the alignments for the various common primitive types,
it is assumed that one can use them anywhere to construct
the layout of a struct of such types.
It seems to work for HDF5 for a wide variety of machines.
*/

static size_t
hdf5typealignment(NCD4node* typ)
{
    if(!nc_alignments_computed) {
	nc_compute_alignments();
	nc_alignments_computed = 1;
    }
    if(typ->hdr.id <= NC_MAX_ATOMIC_TYPE)
        return nctypealignment(typ->hdr.id);
    else {/* Presumably a user type */
	switch(typ->nc_type_class) {
        case NC_VLEN: return nctypealignment(typ->hdr.id);
        case NC_OPAQUE: return nctypealignment(typ->hdr.id);
        case NC_COMPOUND: {/* get alignment of the first field of the compound */
	   NC_FIELD_INFO_T* field0 = nclistget(typ->u.c.field,0);
   	   NCD4node* fieldtype = NULL;
	   if(nc4_find_type(typ->container->nc4_info,field0->nc_typeid,&fieldtype))
	   	return 0;
	   return hdf5typealignment(fieldtype); /* may recurse repeatedly */
	} break;
        default: break;
	}
    }
    return 0; /* fail */
}