dapalign.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*********************************************************************
  2. * Copyright 1993, UCAR/Unidata
  3. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  4. * $Header: /upc/share/CVS/netcdf-3/libncdap3/dapalign.c,v 1.5 2009/09/23 22:26:00 dmh Exp $
  5. *********************************************************************/
  6. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  7. * Copyright by The HDF Group. *
  8. * Copyright by the Board of Trustees of the University of Illinois. *
  9. * All rights reserved. *
  10. * *
  11. * This file is part of HDF5. The full HDF5 copyright notice, including *
  12. * terms governing use, modification, and redistribution, is contained in *
  13. * the files COPYING and Copyright.html. COPYING can be found at the root *
  14. * of the source code distribution tree; Copyright.html can be found at the *
  15. * root level of an installed copy of the electronic HDF5 document set and *
  16. * is linked from the top-level documents page. It can also be found at *
  17. * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
  18. * access to either file, you may request a copy from help@hdfgroup.org. *
  19. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  20. /*
  21. This code is a variantion of the H5detect.c code from HDF5.
  22. Author: D. Heimbigner 10/7/2008
  23. */
  24. #include "config.h"
  25. #ifndef OFFSETTEST
  26. #include "ncdap3.h"
  27. #else
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <assert.h>
  31. #endif
  32. #include "dapnc.h"
  33. #include "dapdebug.h"
  34. #include "dapalign.h"
  35. typedef struct nccalignvlen_t {
  36. size_t len;
  37. void* p;
  38. } nccalignvlen_t;
  39. #ifdef OFFSETTEST
  40. typedef int nc_type;
  41. #define NC_NAT 0 /* NAT = 'Not A Type' (c.f. NaN) */
  42. #define NC_BYTE 1 /* signed 1 byte integer */
  43. #define NC_CHAR 2 /* ISO/ASCII character */
  44. #define NC_SHORT 3 /* signed 2 byte integer */
  45. #define NC_INT 4 /* signed 4 byte integer */
  46. #define NC_FLOAT 5 /* single precision floating point number */
  47. #define NC_DOUBLE 6 /* double precision floating point number */
  48. #define NC_UBYTE 7 /* unsigned 1 byte int */
  49. #define NC_USHORT 8 /* unsigned 2-byte int */
  50. #define NC_UINT 9 /* unsigned 4-byte int */
  51. #define NC_INT64 10 /* signed 8-byte int */
  52. #define NC_UINT64 11 /* unsigned 8-byte int */
  53. #define NC_STRING 12 /* string */
  54. #define NC_VLEN 13
  55. #define NC_OPAQUE 14
  56. #endif
  57. /*
  58. The heart of this is the following macro,
  59. which computes the offset of a field x
  60. when preceded by a char field.
  61. The assumptions appear to be as follows:
  62. 1. the offset produced in this situation indicates
  63. the alignment for x relative in such a way that it
  64. depends only on the types that precede it in the struct.
  65. 2. the compiler does not reorder fields.
  66. 3. arrays are tightly packed.
  67. 4. nested structs are alignd according to their first member
  68. (this actually follows from C language requirement that
  69. a struct can legally be cast to an instance of its first member).
  70. Given the alignments for the various common primitive types,
  71. it is assumed that one can use them anywhere to construct
  72. the layout of a struct of such types.
  73. It seems to work for HDF5 for a wide variety of machines.
  74. */
  75. #define COMP_ALIGNMENT(DST,TYPE) {\
  76. struct {char f1; TYPE x;} tmp; \
  77. DST.typename = #TYPE ; \
  78. DST.alignment = (size_t)((char*)(&(tmp.x)) - (char*)(&tmp));}
  79. #define NCCTYPECOUNT (NCCTYPENCVLEN+1)
  80. static NCtypealignvec vec[NCCTYPECOUNT];
  81. static NCtypealignset set;
  82. static int dapaligninit = 0;
  83. unsigned int
  84. ncctypealignment(int nctype)
  85. {
  86. NCtypealignment* align = NULL;
  87. int index = 0;
  88. if(!dapaligninit) compute_nccalignments();
  89. switch (nctype) {
  90. case NC_BYTE: index = NCCTYPEUCHAR; break;
  91. case NC_CHAR: index = NCCTYPECHAR; break;
  92. case NC_SHORT: index = NCCTYPESHORT; break;
  93. case NC_INT: index = NCCTYPEINT; break;
  94. case NC_FLOAT: index = NCCTYPEFLOAT; break;
  95. case NC_DOUBLE: index = NCCTYPEDOUBLE; break;
  96. case NC_UBYTE: index = NCCTYPEUCHAR; break;
  97. case NC_USHORT: index = NCCTYPEUSHORT; break;
  98. case NC_UINT: index = NCCTYPEUINT; break;
  99. case NC_INT64: index = NCCTYPELONGLONG; break;
  100. case NC_UINT64: index = NCCTYPEULONGLONG; break;
  101. case NC_STRING: index = NCCTYPEPTR; break;
  102. case NC_VLEN: index = NCCTYPENCVLEN; break;
  103. case NC_OPAQUE: index = NCCTYPEUCHAR; break;
  104. default:
  105. #ifndef OFFSETTEST
  106. PANIC1("nctypealignment: bad type code: %d",nctype);
  107. #else
  108. return 0;
  109. #endif
  110. }
  111. align = &vec[index];
  112. return align->alignment;
  113. }
  114. void
  115. compute_nccalignments(void)
  116. {
  117. /* Compute the alignments for all the common C data types*/
  118. /* First for the struct*/
  119. /* initialize*/
  120. memset((void*)&set,0,sizeof(set));
  121. memset((void*)vec,0,sizeof(vec));
  122. COMP_ALIGNMENT(set.charalign,char);
  123. COMP_ALIGNMENT(set.ucharalign,unsigned char);
  124. COMP_ALIGNMENT(set.shortalign,short);
  125. COMP_ALIGNMENT(set.ushortalign,unsigned short);
  126. COMP_ALIGNMENT(set.intalign,int);
  127. COMP_ALIGNMENT(set.uintalign,unsigned int);
  128. COMP_ALIGNMENT(set.longalign,long);
  129. COMP_ALIGNMENT(set.ulongalign,unsigned long);
  130. COMP_ALIGNMENT(set.longlongalign,long long);
  131. COMP_ALIGNMENT(set.ulonglongalign,unsigned long long);
  132. COMP_ALIGNMENT(set.floatalign,float);
  133. COMP_ALIGNMENT(set.doublealign,double);
  134. COMP_ALIGNMENT(set.ptralign,void*);
  135. COMP_ALIGNMENT(set.ncvlenalign,nccalignvlen_t);
  136. /* Then the vector*/
  137. COMP_ALIGNMENT(vec[NCCTYPECHAR],char);
  138. COMP_ALIGNMENT(vec[NCCTYPEUCHAR],unsigned char);
  139. COMP_ALIGNMENT(vec[NCCTYPESHORT],short);
  140. COMP_ALIGNMENT(vec[NCCTYPEUSHORT],unsigned short);
  141. COMP_ALIGNMENT(vec[NCCTYPEINT],int);
  142. COMP_ALIGNMENT(vec[NCCTYPEUINT],unsigned int);
  143. COMP_ALIGNMENT(vec[NCCTYPELONG],long);
  144. COMP_ALIGNMENT(vec[NCCTYPEULONG],unsigned long);
  145. COMP_ALIGNMENT(vec[NCCTYPELONGLONG],long long);
  146. COMP_ALIGNMENT(vec[NCCTYPEULONGLONG],unsigned long long);
  147. COMP_ALIGNMENT(vec[NCCTYPEFLOAT],float);
  148. COMP_ALIGNMENT(vec[NCCTYPEDOUBLE],double);
  149. COMP_ALIGNMENT(vec[NCCTYPEPTR],void*);
  150. COMP_ALIGNMENT(vec[NCCTYPENCVLEN],nccalignvlen_t);
  151. dapaligninit = 1;
  152. }
  153. /* Compute padding */
  154. int
  155. nccpadding(unsigned long offset, int alignment)
  156. {
  157. int pad,rem;
  158. rem = (alignment==0?0:(offset % alignment));
  159. pad = (rem==0?0:(alignment - rem));
  160. return pad;
  161. }
  162. #ifdef OFFSETTEST
  163. #define COMP_ALIGNMENT1(DST,TYPE1,TYPE) {\
  164. struct {TYPE1 f1; TYPE x;} tmp; \
  165. DST.typename = #TYPE ; \
  166. DST.alignment = (size_t)((char*)(&(tmp.x)) - (char*)(&tmp));}
  167. #define COMP_ALIGNMENT2(DST,TYPE1,TYPE2,TYPE) {\
  168. struct {TYPE1 f1, TYPE2 f2; TYPE x;} tmp; \
  169. DST.typename = #TYPE ; \
  170. DST.alignment = (size_t)((char*)(&(tmp.x)) - (char*)(&tmp));}
  171. #define COMP_SIZE0(DST,TYPE1,TYPE2) {\
  172. struct {TYPE1 c; TYPE2 x;} tmp; \
  173. DST = sizeof(tmp); }
  174. static char*
  175. padname(char* name)
  176. {
  177. #define MAX 20
  178. if(name == NULL) name = "null";
  179. int len = strlen(name);
  180. if(len > MAX) len = MAX;
  181. char* s = (char*)malloc(MAX+1);
  182. memset(s,' ',MAX);
  183. s[MAX+1] = '\0';
  184. strncpy(s,name,len);
  185. return s;
  186. }
  187. static void
  188. verify(NCtypealignvec* vec)
  189. {
  190. int i,j;
  191. NCtypealignvec* vec16;
  192. NCtypealignvec* vec32;
  193. int* sizes8;
  194. int* sizes16;
  195. int* sizes32;
  196. vec16 = (NCtypealignvec*)malloc(sizeof(NCtypealignvec)*NCCTYPECOUNT);
  197. vec32 = (NCtypealignvec*)malloc(sizeof(NCtypealignvec)*NCCTYPECOUNT);
  198. sizes8 = (int*)malloc(sizeof(int)*NCCTYPECOUNT);
  199. sizes16 = (int*)malloc(sizeof(int)*NCCTYPECOUNT);
  200. sizes32 = (int*)malloc(sizeof(int)*NCCTYPECOUNT);
  201. COMP_SIZE0(sizes8[1],char,char);
  202. COMP_SIZE0(sizes8[2],unsigned char,char);
  203. COMP_SIZE0(sizes8[3],short,char);
  204. COMP_SIZE0(sizes8[4],unsigned short,char);
  205. COMP_SIZE0(sizes8[5],int,char);
  206. COMP_SIZE0(sizes8[6],unsigned int,char);
  207. COMP_SIZE0(sizes8[7],long,char);
  208. COMP_SIZE0(sizes8[8],unsigned long,char);
  209. COMP_SIZE0(sizes8[9],long long,char);
  210. COMP_SIZE0(sizes8[10],unsigned long long,char);
  211. COMP_SIZE0(sizes8[11],float,char);
  212. COMP_SIZE0(sizes8[12],double,char) ;
  213. COMP_SIZE0(sizes8[13],void*,char);
  214. COMP_SIZE0(sizes8[14],alignvlen_t,char);
  215. COMP_SIZE0(sizes16[1],char,short);
  216. COMP_SIZE0(sizes16[2],unsigned char,short);
  217. COMP_SIZE0(sizes16[3],short,short);
  218. COMP_SIZE0(sizes16[4],unsigned short,short);
  219. COMP_SIZE0(sizes16[5],int,short);
  220. COMP_SIZE0(sizes16[6],unsigned int,short);
  221. COMP_SIZE0(sizes16[7],long,short);
  222. COMP_SIZE0(sizes16[8],unsigned long,short);
  223. COMP_SIZE0(sizes16[9],long long,short);
  224. COMP_SIZE0(sizes16[10],unsigned long long,short);
  225. COMP_SIZE0(sizes16[11],float,short);
  226. COMP_SIZE0(sizes16[12],double,short) ;
  227. COMP_SIZE0(sizes16[13],void*,short);
  228. COMP_SIZE0(sizes16[14],alignvlen_t*,short);
  229. COMP_SIZE0(sizes32[1],char,int);
  230. COMP_SIZE0(sizes32[2],unsigned char,int);
  231. COMP_SIZE0(sizes32[3],short,int);
  232. COMP_SIZE0(sizes32[4],unsigned short,int);
  233. COMP_SIZE0(sizes32[5],int,int);
  234. COMP_SIZE0(sizes32[6],unsigned int,int);
  235. COMP_SIZE0(sizes32[7],long,int);
  236. COMP_SIZE0(sizes32[8],unsigned long,int);
  237. COMP_SIZE0(sizes32[9],long long,int);
  238. COMP_SIZE0(sizes32[10],unsigned long long,int);
  239. COMP_SIZE0(sizes32[11],float,int);
  240. COMP_SIZE0(sizes32[12],double,int) ;
  241. COMP_SIZE0(sizes32[13],void*,int);
  242. COMP_SIZE0(sizes32[14],alignvlen_t*,int);
  243. COMP_ALIGNMENT1(vec16[1],char,short);
  244. COMP_ALIGNMENT1(vec16[2],unsigned char,short);
  245. COMP_ALIGNMENT1(vec16[3],short,short);
  246. COMP_ALIGNMENT1(vec16[4],unsigned short,short);
  247. COMP_ALIGNMENT1(vec16[5],int,short);
  248. COMP_ALIGNMENT1(vec16[6],unsigned int,short);
  249. COMP_ALIGNMENT1(vec32[7],long,short);
  250. COMP_ALIGNMENT1(vec32[8],unsigned long,short);
  251. COMP_ALIGNMENT1(vec32[9],long long,short);
  252. COMP_ALIGNMENT1(vec32[10],unsigned long long,short);
  253. COMP_ALIGNMENT1(vec16[11],float,short);
  254. COMP_ALIGNMENT1(vec16[12],double,short);
  255. COMP_ALIGNMENT1(vec16[13],void*,short);
  256. COMP_ALIGNMENT1(vec16[14],alignvlen_t*,short);
  257. COMP_ALIGNMENT1(vec32[1],char,short);
  258. COMP_ALIGNMENT1(vec32[2],unsigned char,short);
  259. COMP_ALIGNMENT1(vec32[3],char,short);
  260. COMP_ALIGNMENT1(vec32[4],unsigned short,short);
  261. COMP_ALIGNMENT1(vec32[5],int,int);
  262. COMP_ALIGNMENT1(vec32[6],unsigned int,int);
  263. COMP_ALIGNMENT1(vec32[7],long,int);
  264. COMP_ALIGNMENT1(vec32[8],unsigned long,int);
  265. COMP_ALIGNMENT1(vec32[9],long long,int);
  266. COMP_ALIGNMENT1(vec32[10],unsigned long long,int);
  267. COMP_ALIGNMENT1(vec32[11],float,int);
  268. COMP_ALIGNMENT1(vec32[12],double,int);
  269. COMP_ALIGNMENT1(vec32[13],void*,int);
  270. COMP_ALIGNMENT1(vec32[14],alignvlen_t*,int);
  271. for(i=0;i<NCCTYPECOUNT;i++) {
  272. printf("%s: size=%2d alignment=%2d\n",
  273. padname(vec[i].typename),sizes8[i],vec[i].alignment);
  274. }
  275. for(i=0;i<NCCTYPECOUNT;i++) {
  276. printf("short vs %s: size=%2d alignment=%2d\n",
  277. padname(vec[i].typename),sizes16[i],vec16[i].alignment);
  278. }
  279. for(i=0;i<NCCTYPECOUNT;i++) {
  280. printf("int vs %s: size=%2d alignment=%2d\n",
  281. padname(vec[i].typename),sizes32[i],vec32[i].alignment);
  282. }
  283. }
  284. int
  285. main(int argc, char** argv)
  286. {
  287. int i;
  288. compute_nccalignments();
  289. verify(vec);
  290. /*
  291. for(i=0;i<NCCTYPECOUNT;i++) {
  292. printf("%s:\talignment=%d\n",vec[i].typename,vec[i].alignment);
  293. }
  294. */
  295. exit(0);
  296. }
  297. #endif /*OFFSETTEST*/