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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
|
/*
Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata
See COPYRIGHT for license information.
*/
/** \internal
This file contains various instance operations that operate
on a deep level rather than the shallow level of e.g. nc_free_vlen_t.
Currently two operations are defined:
1. reclaim a vector of instances
2. copy a vector of instances
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "netcdf.h"
#include "nc4internal.h"
#include "nc4dispatch.h"
#include "ncoffsets.h"
#include "ncbytes.h"
#undef REPORT
#undef DEBUG
/* It is helpful to have a structure that identifies a pointer into the overall memory */
typedef struct Position{char* memory;} Position;
static int type_alignment_initialized = 0;
/* Forward */
#ifdef USE_NETCDF4
static int reclaim_datar(NC_FILE_INFO_T* file, NC_TYPE_INFO_T* utype, Position instance);
static int copy_datar(NC_FILE_INFO_T* file, NC_TYPE_INFO_T* utype, Position src, Position dst);
#endif
int NC_print_data(NC_FILE_INFO_T* file, nc_type xtype, const void* memory, size_t count);
/**
Reclaim a vector of instances of a type. This recursively walks the
top-level instances to reclaim any nested data such as vlen or strings
or such.
Assumes it is passed a pointer to count instances of xtype.
Reclaims any nested data.
These are the internal equivalents of nc_reclaim_data[_all] and
nc_copy_data[_all] and as such operate using internal data structures.
WARNING: DOES NOT RECLAIM THE TOP-LEVEL MEMORY.
The reason is that we do not know how it was allocated (e.g. static vs
dynamic); only the caller can know that. But note that it assumes all
memory blocks other than the top were dynamically allocated, so they
will be free'd.
Should work for any netcdf type.
Note that this has been optimized significantly, largely
by unwinding various reclaim cliche procedures.
@param nc NC* structure
@param xtype type id
@param memory ptr to top-level memory to reclaim
@param count number of instances of the type in memory block
@return error code
*/
int
NC_reclaim_data(NC* nc, nc_type xtype, void* memory, size_t count)
{
int stat = NC_NOERR;
size_t i;
Position instance;
NC_FILE_INFO_T* file = NULL;
NC_TYPE_INFO_T* utype = NULL;
assert(nc != NULL);
assert((memory == NULL && count == 0) || (memory != NULL || count > 0));
/* Process atomic types */
/* Optimize: Vector of fixed size atomic types (always the case for netcdf-3)*/
if(xtype < NC_STRING) goto done;
#ifdef USE_NETCDF4
/* Optimize: Vector of strings */
if(xtype == NC_STRING) {
char** ss = (char**)memory;
for(i=0;i<count;i++) {
nullfree(ss[i]);
}
goto done;
}
/* Process User types */
assert(USEFILEINFO(nc) != 0);
file = (NC_FILE_INFO_T*)(nc)->dispatchdata;
if((stat = nc4_find_type(file,xtype,&utype))) goto done;
/* Optimize: vector of fixed sized compound type instances */
if(!utype->varsized) goto done; /* no need to reclaim anything */
/* Remaining cases: vector of VLEN and vector of (transitive) variable sized compound types.
These all require potential recursion.
*/
/* Build a memory walker object */
instance.memory = (char*)memory; /* use char* so we can do pointer arithmetic */
/* Walk each vector instance */
/* Note that we avoid reclaiming the top level memory */
for(i=0;i<count;i++) {
if((stat=reclaim_datar(file,utype,instance))) goto done;
instance.memory += utype->size; /* move to next entry */
}
#else
stat = NC_EBADTYPE;
#endif
done:
return stat;
}
#ifdef USE_NETCDF4
/* Recursive type walker: reclaim a single instance of a variable-sized user-defined type;
specifically a vlen or a variable-sized compound type instance
*/
static int
reclaim_datar(NC_FILE_INFO_T* file, NC_TYPE_INFO_T* utype, Position instance)
{
int i,stat = NC_NOERR;
nc_type basetypeid;
NC_TYPE_INFO_T* basetype = NULL;
size_t nfields;
nc_vlen_t* vlen;
size_t fid;
int ndims;
int dimsizes[NC_MAX_VAR_DIMS];
size_t alignment = 0;
Position vinstance; /* walk the vlen instance memory */
assert(utype->varsized); /* All fixed size cases are optimized out */
/* Leaving VLEN or Compound */
if(utype->nc_type_class == NC_VLEN) {
basetypeid = utype->u.v.base_nc_typeid; /* Get basetype */
vlen = (nc_vlen_t*)instance.memory;/* memory as vector of nc_vlen_t instances */
/* Optimize: basetype is atomic fixed size */
if(basetypeid < NC_STRING) {
goto out;
}
/* Optimize: basetype is string */
if(basetypeid == NC_STRING) {
if(vlen->len > 0 && vlen->p != NULL) {
char** slist = (char**)vlen->p; /* vlen instance is a vector of string pointers */
for(i=0;i<vlen->len;i++) {if(slist[i] != NULL) free(slist[i]);}
}
goto out;
}
/* Optimize: vlen basetype is a fixed-size user-type */
if((stat = nc4_find_type(file,basetypeid,&basetype))) goto done;
if(!basetype->varsized) {
goto out;
}
/* Remaining case: basetype is itself variable size => recurse */
if((stat = NC_type_alignment_internal(file,basetypeid,basetype,&alignment))) goto done;;
vinstance.memory = (char*)vlen->p; /* use char* so we can do pointer arithmetic */
vinstance.memory = (void*)NC_read_align((uintptr_t)vinstance.memory,alignment);
for(i=0;i<vlen->len;i++) {
if((stat=reclaim_datar(file,basetype,vinstance))) goto done; /* reclaim one basetype instance */
vinstance.memory += basetype->size; /* move to next base instance */
}
out:
if(vlen->len > 0 && vlen->p != NULL) {free(vlen->p);}
goto done;
} else if(utype->nc_type_class == NC_COMPOUND) {
Position finstance; /* mark the fields's instance */
nfields = nclistlength(utype->u.c.field);
/* Get info about each field in turn and reclaim it */
for(fid=0;fid<nfields;fid++) {
NC_FIELD_INFO_T* field = NULL;
/* Get field's dimension sizes */
field = (NC_FIELD_INFO_T*)nclistget(utype->u.c.field,fid);
ndims = field->ndims;
int arraycount = 1;
for(i=0;i<ndims;i++) {dimsizes[i] = field->dim_size[i]; arraycount *= dimsizes[i];}
if(field->ndims == 0) {ndims=1; dimsizes[0]=1;} /* fake the scalar case */
/* "Move" to start of this field's instance */
finstance.memory = instance.memory + field->offset; /* includes proper alignment */
/* optimize: fixed length atomic type */
if(field->nc_typeid < NC_STRING) continue;
/* optimize: string field type */
if(field->nc_typeid == NC_STRING) {
char** strvec = (char**)finstance.memory;
for(i=0;i<arraycount;i++) {
if(strvec[i] != NULL) free(strvec[i]);
}
continue; /* do next field */
}
/* optimize: fixed length compound base type */
if((stat = nc4_find_type(file,field->nc_typeid,&basetype))) goto done;
if(!basetype->varsized) continue;
/* Field is itself variable length (possibly transitively) */
for(i=0;i<arraycount;i++) {
if((stat = reclaim_datar(file, basetype, finstance))) goto done;
finstance.memory += basetype->size;
}
}
goto done;
} else {stat = NC_EBADTYPE; goto done;}
done:
return stat;
}
#endif
/**************************************************/
/**
Copy a vector of instances of a type. This recursively walks
the top-level instances to copy any nested data such as vlen or
strings or such.
Assumes it is passed a pointer to count instances of xtype and a
space into which to copy the instance. Copys any nested data
by calling malloc().
WARNING: DOES NOT ALLOCATE THE TOP-LEVEL MEMORY (see the
nc_copy_data_all function). Note that all memory blocks other
than the top are dynamically allocated.
Should work for any netcdf type.
@param file NC_FILE_INFO_T* structure
@param xtype type id
@param memory ptr to top-level memory to reclaim
@param count number of instances of the type in memory block
@param copy top-level space into which to copy the instance
@return error code
*/
int
NC_copy_data(NC* nc, nc_type xtype, const void* memory, size_t count, void* copy)
{
int stat = NC_NOERR;
size_t i;
Position src;
Position dst;
NC_FILE_INFO_T* file = NULL;
NC_TYPE_INFO_T* utype = NULL;
size_t typesize = 0;
if(memory == NULL || count == 0)
goto done; /* ok, do nothing */
assert(nc != NULL);
assert(memory != NULL || count > 0);
assert(copy != NULL || count == 0);
/* Process atomic types */
/* Optimize: Vector of fixed size atomic types */
if(xtype < NC_STRING) {
typesize = NC_atomictypelen(xtype);
memcpy(copy,memory,count*typesize);
goto done;
}
#ifdef USE_NETCDF4
/* Optimize: Vector of strings */
if(xtype == NC_STRING) {
char** svec = (char**)memory;
char** dvec = (char**)copy;
size_t len;
for(i=0;i<count;i++) {
const char* ssrc = svec[i];
char* sdst;
if(ssrc == NULL)
sdst = NULL;
else {
len = nulllen(ssrc);
if((sdst = (char*)malloc(len+1))==NULL) {stat = NC_ENOMEM; goto done;}
memcpy(sdst,ssrc,len+1); /* +1 for trailing nul */
}
dvec[i] = sdst;
}
goto done;
}
assert(USEFILEINFO(nc) != 0);
file = (NC_FILE_INFO_T*)(nc)->dispatchdata;
/* Process User types */
if((stat = nc4_find_type(file,xtype,&utype))) goto done;
/* Optimize: vector of fixed sized compound type instances */
if(!utype->varsized) {
memcpy(copy,memory,count*utype->size);
goto done;
}
/* Remaining cases: vector of VLEN and vector of variable sized compound types.
These all require potential recursion.
*/
src.memory = (char*)memory; /* use char* so we can do pointer arithmetic */
dst.memory = (char*)copy; /* use char* so we can do pointer arithmetic */
/* Walk each vector instance */
for(i=0;i<count;i++) {
if((stat=copy_datar(file,utype,src,dst))) goto done;
src.memory += utype->size;
dst.memory += utype->size;
}
#else
stat = NC_EBADTYPE;
#endif
done:
return stat;
}
#ifdef USE_NETCDF4
/* Recursive type walker: reclaim an instance of variable-sized user-defined types;
specifically a vlen or a variable-sized compound type instance
*/
static int
copy_datar(NC_FILE_INFO_T* file, NC_TYPE_INFO_T* utype, Position src, Position dst)
{
int i, stat = NC_NOERR;
nc_type basetypeid;
NC_TYPE_INFO_T* basetype = NULL;
size_t nfields;
nc_vlen_t* srcvlens = NULL;
nc_vlen_t* dstvlens = NULL;
size_t fid, arraycount;
int ndims;
int dimsizes[NC_MAX_VAR_DIMS];
Position vsrc, vdst;
size_t alignment = 0;
assert(utype->varsized); /* All fixed size cases are optimized out */
/* Leaving VLEN or Compound */
if(utype->nc_type_class == NC_VLEN) {
size_t basetypesize = 0;
size_t copycount = 0;
basetypeid = utype->u.v.base_nc_typeid; /* Get basetype */
srcvlens = (nc_vlen_t*)src.memory;
dstvlens = (nc_vlen_t*)dst.memory;
dstvlens->len = srcvlens->len; /* always */
if(srcvlens->len == 0) {
dstvlens->p = NULL;
goto done;
}
if(basetypeid <= NC_MAX_ATOMIC_TYPE)
basetypesize = NC_atomictypelen(basetypeid);
copycount = srcvlens->len*basetypesize;
/* Optimize: basetype is atomic fixed size */
if(basetypeid < NC_STRING) {
if((dstvlens->p = (void*)malloc(copycount))==NULL) {stat = NC_ENOMEM; goto done;}
memcpy(dstvlens->p,srcvlens->p,copycount);
goto done;
}
/* Optimize: basetype is string */
if(basetypeid == NC_STRING) {
char** srcstrvec = (char**)srcvlens->p;
char** dststrvec = NULL;
if((dststrvec = (void*)malloc(copycount))==NULL) {stat = NC_ENOMEM; goto done;}
dstvlens->p = (void*)dststrvec;
for(i=0;i<srcvlens->len;i++) {
if((dststrvec[i] = strdup(srcstrvec[i]))==NULL) {stat = NC_ENOMEM; goto done;}
}
goto done;
}
/* User-defined type */
/* Recompute base type size */
if((stat = nc4_find_type(file,basetypeid,&basetype))) goto done;
basetypesize = basetype->size;
copycount = srcvlens->len*basetypesize;
/* Optimize: basetype is user-type fixed size */
if(!basetype->varsized) {
if((dstvlens->p = (void*)malloc(copycount))==NULL) {stat = NC_ENOMEM; goto done;}
memcpy(dstvlens->p,srcvlens->p,copycount);
goto done;
}
/* Remaining case: basetype is itself variable size => recurse */
if((stat = NC_type_alignment_internal(file,basetypeid,basetype,&alignment))) goto done;;
vsrc.memory = (char*)srcvlens->p; /* use char* so we can do pointer arithmetic */
if((vdst.memory = (char*)malloc(copycount))==NULL) {stat = NC_ENOMEM; goto done;}
dstvlens->p = vdst.memory; /* don't lose it */
vsrc.memory = (void*)NC_read_align((uintptr_t)vsrc.memory,alignment);
vdst.memory = (void*)NC_read_align((uintptr_t)vdst.memory,alignment);
for(i=0;i<srcvlens->len;i++) {
if((stat=copy_datar(file,basetype,vsrc,vdst))) goto done;
vsrc.memory += basetype->size;
vdst.memory += basetype->size;
}
goto done;
} else if(utype->nc_type_class == NC_COMPOUND) {
Position fsrc; /* mark the src fields's instance */
Position fdst; /* mark the dst fields's instance */
nfields = nclistlength(utype->u.c.field);
/* Get info about each field in turn and copy it */
for(fid=0;fid<nfields;fid++) {
NC_FIELD_INFO_T* field = NULL;
field = (NC_FIELD_INFO_T*)nclistget(utype->u.c.field,fid);
ndims = field->ndims;
arraycount = 1;
for(i=0;i<ndims;i++) {dimsizes[i] = field->dim_size[i]; arraycount *= (size_t)dimsizes[i];}
if(field->ndims == 0) {ndims=1; dimsizes[0]=1;} /* fake the scalar case */
/* "move" to this field */
fsrc.memory = src.memory + field->offset;
fdst.memory = dst.memory + field->offset;
/* optimize: fixed length atomic type */
if(field->nc_typeid < NC_STRING) {
size_t typesize = NC_atomictypelen(field->nc_typeid);
memcpy(fdst.memory,fsrc.memory,arraycount*typesize);
continue; /* move to next field */
}
/* optimize: string field type */
if(field->nc_typeid == NC_STRING) {
char** srcstrvec = (char**)src.memory;
char** dststrvec = (char**)dst.memory;
for(i=0;i<arraycount;i++)
{if(srcstrvec[i] != NULL) {dststrvec[i] = strdup(srcstrvec[i]);} else {dststrvec[i] = NULL;}}
continue; /* move to next field */
}
/* optimize: fixed length compound base type */
if((stat = nc4_find_type(file,field->nc_typeid,&basetype))) goto done;
if(!basetype->varsized) {
memcpy(fdst.memory,fsrc.memory,arraycount*basetype->size);
continue; /* move to next field */
}
/* Remaining case; field type is variable type */
for(i=0;i<arraycount;i++) {
if((stat = copy_datar(file, basetype, fsrc, fdst))) goto done;
fsrc.memory += basetype->size;
fdst.memory += basetype->size;
}
}
goto done;
} else {stat = NC_EBADTYPE; goto done;}
done:
return stat;
}
#endif
/**************************************************/
/* Alignment functions */
#ifdef USE_NETCDF4
uintptr_t
NC_read_align(uintptr_t addr, size_t alignment)
{
size_t loc_align = (alignment == 0 ? 1 : alignment);
size_t delta = (addr % loc_align);
if(delta == 0) return addr;
return (addr + (alignment - delta));
}
/**
* Compute proper data alignment for a type
* @param file - only needed for a compound type
* @param xtype - type for which alignment is requested
* @param utype - if known
* @return 0 if not found
*/
int
NC_type_alignment_internal(NC_FILE_INFO_T* file, nc_type xtype, NC_TYPE_INFO_T* utype, size_t* alignp)
{
int stat = NC_NOERR;
size_t align = 0;
int klass;
if(!type_alignment_initialized) {
NC_compute_alignments();
type_alignment_initialized = 1;
}
if(xtype <= NC_MAX_ATOMIC_TYPE)
{stat = NC_class_alignment(xtype,&align); goto done;}
else {/* Presumably a user type */
if(utype == NULL) {
if((stat = nc4_find_type(file,xtype,&utype))) goto done;
}
klass = utype->nc_type_class;
switch(klass) {
case NC_VLEN: stat = NC_class_alignment(klass,&align); break;
case NC_OPAQUE: stat = NC_class_alignment(klass,&align); break;
case NC_COMPOUND: {/* get alignment of the first field of the compound */
NC_FIELD_INFO_T* field = NULL;
NC_TYPE_INFO_T* basetype = NULL;
if(nclistlength(utype->u.c.field) == 0) {stat = NC_EINVAL; goto done;}
field = (NC_FIELD_INFO_T*)nclistget(utype->u.c.field,0);
if(field->nc_typeid <= NC_MAX_ATOMIC_TYPE) {
if((stat = nc4_find_type(file,field->nc_typeid,&basetype))) goto done;
}
stat = NC_type_alignment_internal(file,field->nc_typeid,basetype,&align); /* may recurse repeatedly */
} break;
default: break;
}
}
if(alignp) *alignp = align;
done:
#if 0
Why was this here?
if(stat == NC_NOERR && align == 0) stat = NC_EINVAL;
#endif
return stat;
}
#endif
/**************************************************/
/* Internal versions of the XX_all functions */
/* Alternate entry point: includes recovering the top-level memory */
int
NC_reclaim_data_all(NC* nc, nc_type xtypeid, void* memory, size_t count)
{
int stat = NC_NOERR;
assert(nc != NULL);
stat = NC_reclaim_data(nc,xtypeid,memory,count);
if(stat == NC_NOERR && memory != NULL)
free(memory);
return stat;
}
/* Alternate entry point: includes recovering the top-level memory */
int
NC_copy_data_all(NC* nc, nc_type xtype, const void* memory, size_t count, void** copyp)
{
int stat = NC_NOERR;
size_t xsize = 0;
void* copy = NULL;
NC_FILE_INFO_T* file = NULL;
NC_TYPE_INFO_T* utype = NULL;
assert(nc != NULL);
if(xtype <= NC_STRING && count > 0) {
xsize = NC_atomictypelen(xtype);
if((copy = calloc(count,xsize))==NULL) {stat = NC_ENOMEM; goto done;}
if(xtype < NC_STRING) /* fixed-size atomic type */
memcpy(copy,memory,xsize*count);
else { /* string type */
size_t i;
char** strvec = (char**)memory;
char** scopyvec = (char**)copy;
for(i=0;i<count;i++) {
char* s = strvec[i];
char* sdup = NULL;
if(s != NULL) sdup = strdup(s);
scopyvec[i] = sdup;
}
}
}
#ifdef USE_NETCDF4
else {
file = (NC_FILE_INFO_T*)(nc)->dispatchdata;
if((stat = nc4_find_type(file,xtype,&utype))) goto done;
xsize = utype->size;
/* allocate the top-level */
if(count > 0) {
if((copy = calloc(count,xsize))==NULL) {stat = NC_ENOMEM; goto done;}
}
if((stat = NC_copy_data(nc,xtype,memory,count,copy)))
(void)NC_reclaim_data_all(nc,xtype,copy,count);
}
#endif
if(copyp) {*copyp = copy; copy = NULL;}
done:
return stat;
}
/* Alternate entry point: includes recovering the top-level memory */
int
NC_print_data(NC_FILE_INFO_T* file, nc_type xtype, const void* memory, size_t count)
{
EXTERNL int nc_print_data(int ncid, nc_type xtype, const void* memory, size_t count);
return nc_print_data(file->controller->ext_ncid,xtype,memory,count);
}
|