ncaux.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2. * Copyright by The HDF Group. *
  3. * Copyright by the Board of Trustees of the University of Illinois. *
  4. * All rights reserved. *
  5. * *
  6. * This file is part of HDF5. The full HDF5 copyright notice, including *
  7. * terms governing use, modification, and redistribution, is contained in *
  8. * the files COPYING and Copyright.html. COPYING can be found at the root *
  9. * of the source code distribution tree; Copyright.html can be found at the *
  10. * root level of an installed copy of the electronic HDF5 document set and *
  11. * is linked from the top-level documents page. It can also be found at *
  12. * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
  13. * access to either file, you may request a copy from help@hdfgroup.org. *
  14. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  15. /*
  16. This code is a variant of the H5detect.c code from HDF5.
  17. Author: D. Heimbigner 10/7/2008
  18. */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <assert.h>
  22. #include <netcdf.h>
  23. #include "ncaux.h"
  24. struct NCAUX_FIELD {
  25. char* name;
  26. nc_type fieldtype;
  27. int ndims;
  28. int dimsizes[NC_MAX_VAR_DIMS];
  29. size_t size;
  30. size_t offset;
  31. size_t alignment;
  32. };
  33. struct NCAUX_CMPD {
  34. int ncid;
  35. int mode;
  36. char* name;
  37. int nfields;
  38. struct NCAUX_FIELD* fields;
  39. size_t size;
  40. size_t offset; /* cumulative as fields are added */
  41. size_t alignment;
  42. };
  43. static int ncaux_initialized = 0;
  44. static void compute_alignments(void);
  45. static int computefieldinfo(struct NCAUX_CMPD* cmpd);
  46. int
  47. ncaux_begin_compound(int ncid, const char *name, int alignmode, void** tagp)
  48. {
  49. int status = NC_NOERR;
  50. struct NCAUX_CMPD* cmpd = NULL;
  51. if(!ncaux_initialized) {
  52. compute_alignments();
  53. ncaux_initialized = 1;
  54. }
  55. if(tagp) *tagp = NULL;
  56. cmpd = (struct NCAUX_CMPD*)calloc(1,sizeof(struct NCAUX_CMPD));
  57. if(cmpd == NULL) {status = NC_ENOMEM; goto fail;}
  58. cmpd->ncid = ncid;
  59. cmpd->mode = alignmode;
  60. cmpd->nfields = 0;
  61. cmpd->name = strdup(name);
  62. if(cmpd->name == NULL) {status = NC_ENOMEM; goto fail;}
  63. if(tagp) *tagp = (void*)cmpd;
  64. return status;
  65. fail:
  66. ncaux_abort_compound((void*)cmpd);
  67. return status;
  68. }
  69. int
  70. ncaux_abort_compound(void* tag)
  71. {
  72. int i;
  73. struct NCAUX_CMPD* cmpd = (struct NCAUX_CMPD*)tag;
  74. if(cmpd == NULL) goto done;
  75. if(cmpd->name) free(cmpd->name);
  76. for(i=0;i<cmpd->nfields;i++) {
  77. struct NCAUX_FIELD* field = &cmpd->fields[i];
  78. if(field->name) free(field->name);
  79. }
  80. if(cmpd->fields) free(cmpd->fields);
  81. free(cmpd);
  82. done:
  83. return NC_NOERR;
  84. }
  85. int
  86. ncaux_add_field(void* tag, const char *name, nc_type field_type,
  87. int ndims, const int* dimsizes)
  88. {
  89. int i;
  90. int status = NC_NOERR;
  91. struct NCAUX_CMPD* cmpd = (struct NCAUX_CMPD*)tag;
  92. struct NCAUX_FIELD* newfields = NULL;
  93. struct NCAUX_FIELD* field = NULL;
  94. if(cmpd == NULL) goto done;
  95. if(ndims < 0) {status = NC_EINVAL; goto done;}
  96. for(i=0;i<ndims;i++) {
  97. if(dimsizes[i] <= 0) {status = NC_EINVAL; goto done;}
  98. }
  99. if(cmpd->fields == NULL) {
  100. newfields = (struct NCAUX_FIELD*)calloc(1,sizeof(struct NCAUX_FIELD));
  101. } else {
  102. newfields = (struct NCAUX_FIELD*)realloc(cmpd->fields,cmpd->nfields+1*sizeof(struct NCAUX_FIELD));
  103. }
  104. if(cmpd->fields == NULL) {status = NC_ENOMEM; goto done;}
  105. cmpd->fields = newfields;
  106. field = &cmpd->fields[cmpd->nfields+1];
  107. field->name = strdup(name);
  108. field->fieldtype = field_type;
  109. if(field->name == NULL) {status = NC_ENOMEM; goto done;}
  110. field->ndims = ndims;
  111. memcpy(field->dimsizes,dimsizes,sizeof(int)*ndims);
  112. cmpd->nfields++;
  113. done:
  114. return status;
  115. }
  116. static size_t
  117. dimproduct(int ndims, int* dimsizes)
  118. {
  119. int i;
  120. size_t product = 1;
  121. for(i=0;i<ndims;i++) product *= dimsizes[i];
  122. return product;
  123. }
  124. int
  125. ncaux_end_compound(void* tag, nc_type* idp)
  126. {
  127. int i;
  128. int status = NC_NOERR;
  129. struct NCAUX_CMPD* cmpd = (struct NCAUX_CMPD*)tag;
  130. if(cmpd == NULL) {status = NC_EINVAL; goto done;}
  131. /* Compute field and compound info */
  132. status = computefieldinfo(cmpd);
  133. if(status != NC_NOERR) goto done;
  134. status = nc_def_compound(cmpd->ncid, cmpd->size, cmpd->name, idp);
  135. if(status != NC_NOERR) goto done;
  136. for(i=0;i<cmpd->nfields;i++) {
  137. struct NCAUX_FIELD* field = &cmpd->fields[i];
  138. if(field->ndims > 0) {
  139. status = nc_insert_compound(cmpd->ncid, *idp, field->name,
  140. field->offset, field->fieldtype);
  141. } else {
  142. status = nc_insert_array_compound(cmpd->ncid, *idp, field->name,
  143. field->offset, field->fieldtype,
  144. field->ndims,field->dimsizes);
  145. }
  146. if(status != NC_NOERR) goto done;
  147. }
  148. done:
  149. return status;
  150. }
  151. /**************************************************/
  152. /*
  153. The heart of this is the following macro,
  154. which computes the offset of a field x
  155. when preceded by a char field.
  156. The assumptions appear to be as follows:
  157. 1. the offset produced in this situation indicates
  158. the alignment for x relative in such a way that it
  159. depends only on the types that precede it in the struct.
  160. 2. the compiler does not reorder fields.
  161. 3. arrays are tightly packed.
  162. 4. nested structs are alignd according to their first member
  163. (this actually follows from C language requirement that
  164. a struct can legally be cast to an instance of its first member).
  165. Given the alignments for the various common primitive types,
  166. it is assumed that one can use them anywhere to construct
  167. the layout of a struct of such types.
  168. It seems to work for HDF5 for a wide variety of machines.
  169. */
  170. #define COMP_ALIGNMENT(DST,TYPE) {\
  171. struct {char f1; TYPE x;} tmp; \
  172. DST.typename = #TYPE ; \
  173. DST.alignment = (size_t)((char*)(&(tmp.x)) - (char*)(&tmp));}
  174. /* Define indices for every primitive C type */
  175. /* NAT => NOT-A-TYPE*/
  176. #define NATINDEX 0
  177. #define CHARINDEX 1
  178. #define UCHARINDEX 2
  179. #define SHORTINDEX 3
  180. #define USHORTINDEX 4
  181. #define INTINDEX 5
  182. #define UINTINDEX 6
  183. #define LONGINDEX 7
  184. #define ULONGINDEX 8
  185. #define LONGLONGINDEX 9
  186. #define ULONGLONGINDEX 10
  187. #define FLOATINDEX 11
  188. #define DOUBLEINDEX 12
  189. #define PTRINDEX 13
  190. #define NCVLENINDEX 14
  191. #define NCTYPES 15
  192. typedef struct Alignment {
  193. char* typename;
  194. int alignment;
  195. } Alignment;
  196. typedef Alignment Typealignvec;
  197. /* Capture in struct and in a vector*/
  198. typedef struct Typealignset {
  199. Alignment charalign; /* char*/
  200. Alignment ucharalign; /* unsigned char*/
  201. Alignment shortalign; /* short*/
  202. Alignment ushortalign; /* unsigned short*/
  203. Alignment intalign; /* int*/
  204. Alignment uintalign; /* unsigned int*/
  205. Alignment longalign; /* long*/
  206. Alignment ulongalign; /* unsigned long*/
  207. Alignment longlongalign; /* long long*/
  208. Alignment ulonglongalign; /* unsigned long long*/
  209. Alignment floatalign; /* float*/
  210. Alignment doublealign; /* double*/
  211. Alignment ptralign; /* void**/
  212. Alignment ncvlenalign; /* nc_vlen_t*/
  213. } Typealignset;
  214. static Typealignvec vec[NCTYPES];
  215. static Typealignset set;
  216. static void
  217. compute_alignments(void)
  218. {
  219. /* Compute the alignments for all the common C data types*/
  220. /* First for the struct*/
  221. /* initialize*/
  222. memset((void*)&set,0,sizeof(set));
  223. memset((void*)vec,0,sizeof(vec));
  224. COMP_ALIGNMENT(set.charalign,char);
  225. COMP_ALIGNMENT(set.ucharalign,unsigned char);
  226. COMP_ALIGNMENT(set.shortalign,short);
  227. COMP_ALIGNMENT(set.ushortalign,unsigned short);
  228. COMP_ALIGNMENT(set.intalign,int);
  229. COMP_ALIGNMENT(set.uintalign,unsigned int);
  230. COMP_ALIGNMENT(set.longalign,long);
  231. COMP_ALIGNMENT(set.ulongalign,unsigned long);
  232. COMP_ALIGNMENT(set.longlongalign,long long);
  233. COMP_ALIGNMENT(set.ulonglongalign,unsigned long long);
  234. COMP_ALIGNMENT(set.floatalign,float);
  235. COMP_ALIGNMENT(set.doublealign,double);
  236. COMP_ALIGNMENT(set.ptralign,void*);
  237. COMP_ALIGNMENT(set.ncvlenalign,nc_vlen_t);
  238. /* Then the vector*/
  239. COMP_ALIGNMENT(vec[CHARINDEX],char);
  240. COMP_ALIGNMENT(vec[UCHARINDEX],unsigned char);
  241. COMP_ALIGNMENT(vec[SHORTINDEX],short);
  242. COMP_ALIGNMENT(vec[USHORTINDEX],unsigned short);
  243. COMP_ALIGNMENT(vec[INTINDEX],int);
  244. COMP_ALIGNMENT(vec[UINTINDEX],unsigned int);
  245. COMP_ALIGNMENT(vec[LONGINDEX],long);
  246. COMP_ALIGNMENT(vec[ULONGINDEX],unsigned long);
  247. COMP_ALIGNMENT(vec[LONGLONGINDEX],long long);
  248. COMP_ALIGNMENT(vec[ULONGLONGINDEX],unsigned long long);
  249. COMP_ALIGNMENT(vec[FLOATINDEX],float);
  250. COMP_ALIGNMENT(vec[DOUBLEINDEX],double);
  251. COMP_ALIGNMENT(vec[PTRINDEX],void*);
  252. COMP_ALIGNMENT(vec[NCVLENINDEX],nc_vlen_t);
  253. }
  254. static size_t
  255. nctypealignment(nc_type nctype)
  256. {
  257. Alignment* align = NULL;
  258. int index = 0;
  259. switch (nctype) {
  260. case NC_BYTE: index = UCHARINDEX; break;
  261. case NC_CHAR: index = CHARINDEX; break;
  262. case NC_SHORT: index = SHORTINDEX; break;
  263. case NC_INT: index = INTINDEX; break;
  264. case NC_FLOAT: index = FLOATINDEX; break;
  265. case NC_DOUBLE: index = DOUBLEINDEX; break;
  266. case NC_UBYTE: index = UCHARINDEX; break;
  267. case NC_USHORT: index = USHORTINDEX; break;
  268. case NC_UINT: index = UINTINDEX; break;
  269. case NC_INT64: index = LONGLONGINDEX; break;
  270. case NC_UINT64: index = ULONGLONGINDEX; break;
  271. case NC_STRING: index = PTRINDEX; break;
  272. case NC_VLEN: index = NCVLENINDEX; break;
  273. case NC_OPAQUE: index = UCHARINDEX; break;
  274. default: assert(0);
  275. }
  276. align = &vec[index];
  277. return align->alignment;
  278. }
  279. static int
  280. getpadding(int offset, int alignment)
  281. {
  282. int rem = (alignment==0?0:(offset % alignment));
  283. int pad = (rem==0?0:(alignment - rem));
  284. return pad;
  285. }
  286. /* Find first primitive field of a possibly nested sequence of compounds */
  287. static nc_type
  288. findfirstfield(int ncid, nc_type xtype)
  289. {
  290. int status = NC_NOERR;
  291. nc_type fieldtype = xtype;
  292. if(xtype <= NC_MAX_ATOMIC_TYPE) goto done;
  293. status = nc_inq_compound_fieldtype(ncid, xtype, 0, &fieldtype);
  294. if(status != NC_NOERR) goto done;
  295. fieldtype = findfirstfield(ncid,fieldtype);
  296. done:
  297. return (status == NC_NOERR?fieldtype:NC_NAT);
  298. }
  299. static int
  300. computefieldinfo(struct NCAUX_CMPD* cmpd)
  301. {
  302. int i;
  303. int status = NC_NOERR;
  304. size_t offset = 0;
  305. size_t totaldimsize;
  306. /* Assign the sizes for the fields */
  307. for(i=0;i<cmpd->nfields;i++) {
  308. struct NCAUX_FIELD* field = &cmpd->fields[i];
  309. status = nc_inq_type(cmpd->ncid,field->fieldtype,NULL,&field->size);
  310. if(status != NC_NOERR) goto done;
  311. totaldimsize = dimproduct(field->ndims,field->dimsizes);
  312. field->size *= totaldimsize;
  313. }
  314. for(offset=0,i=0;i<cmpd->nfields;i++) {
  315. struct NCAUX_FIELD* field = &cmpd->fields[i];
  316. int alignment = 0;
  317. nc_type firsttype = findfirstfield(cmpd->ncid,field->fieldtype);
  318. /* only support 'c' alignment for now*/
  319. switch (field->fieldtype) {
  320. case NC_OPAQUE:
  321. field->alignment = 1;
  322. break;
  323. case NC_ENUM:
  324. field->alignment = nctypealignment(firsttype);
  325. break;
  326. case NC_VLEN: /*fall thru*/
  327. case NC_COMPOUND:
  328. field->alignment = nctypealignment(firsttype);
  329. break;
  330. default:
  331. field->alignment = nctypealignment(field->fieldtype);
  332. break;
  333. }
  334. offset += getpadding(offset,alignment);
  335. field->offset = offset;
  336. offset += field->size;
  337. }
  338. cmpd->size = offset;
  339. cmpd->alignment = cmpd->fields[0].alignment;
  340. done:
  341. return status;
  342. }