2121
2222// Write an array to a dataset / file
2323// Returns 0 on success, nonzero on error.
24- static inline int bsp_write_array (hid_t f , const char * label , bsp_array_t array ,
25- int compression_level ) {
24+ static inline bsp_error_t bsp_write_array (hid_t f , const char * label ,
25+ bsp_array_t array ,
26+ int compression_level ) {
2627 if (array .type == BSP_COMPLEX_FLOAT32 || array .type == BSP_COMPLEX_FLOAT64 ) {
2728 bsp_error_t error = bsp_complex_array_to_fp (& array );
2829 if (error != BSP_SUCCESS ) {
29- return -3 ; // Type conversion error
30+ return BSP_ERROR_TYPE ;
3031 }
3132 }
3233
@@ -59,52 +60,63 @@ static inline int bsp_write_array(hid_t f, const char* label, bsp_array_t array,
5960 H5Dcreate2 (f , label , hdf5_standard_type , fspace , lcpl , dcpl , H5P_DEFAULT );
6061
6162 if (dset == H5I_INVALID_HID ) {
62- return -1 ;
63+ H5Sclose (fspace );
64+ H5Pclose (lcpl );
65+ H5Pclose (dcpl );
66+ return BSP_ERROR_IO ;
6367 }
6468
6569 hid_t hdf5_native_type = bsp_get_hdf5_native_type (array .type );
6670
6771 hid_t r = H5Dwrite (dset , hdf5_native_type , H5S_ALL , fspace , H5P_DEFAULT ,
6872 array .data );
6973
70- if (r == H5I_INVALID_HID ) {
71- return -2 ;
74+ if (r < 0 ) {
75+ H5Dclose (dset );
76+ H5Sclose (fspace );
77+ H5Pclose (lcpl );
78+ H5Pclose (dcpl );
79+ return BSP_ERROR_IO ;
7280 }
7381
7482 H5Sclose (fspace );
83+ H5Dclose (dset );
7584 H5Pclose (lcpl );
7685 H5Pclose (dcpl );
7786
78- return 0 ;
87+ return BSP_SUCCESS ;
7988}
8089
8190#if __STDC_VERSION__ >= 201112L
82- static inline bsp_array_t bsp_read_array_parallel (hid_t f , const char * label ,
91+ static inline bsp_error_t bsp_read_array_parallel (bsp_array_t * array , hid_t f ,
92+ const char * label ,
8393 int num_threads ) {
8494 hid_t dset = H5Dopen2 (f , label , H5P_DEFAULT );
8595
8696 if (dset == H5I_INVALID_HID ) {
87- bsp_array_t empty_array ;
88- bsp_construct_default_array_t (& empty_array );
89- return empty_array ;
97+ H5Dclose ( dset ) ;
98+ bsp_construct_default_array_t (array );
99+ return BSP_ERROR_IO ;
90100 }
91101
92102 hid_t fspace = H5Dget_space (dset );
93103
94104 if (fspace == H5I_INVALID_HID ) {
95- bsp_array_t empty_array ;
96- bsp_construct_default_array_t (& empty_array );
97- return empty_array ;
105+ H5Sclose (fspace );
106+ H5Dclose (dset );
107+ bsp_construct_default_array_t (array );
108+ return BSP_ERROR_IO ;
98109 }
99110
100111 hsize_t dims [3 ];
101112
102113 int r = H5Sget_simple_extent_dims (fspace , dims , NULL );
103114
104115 if (r < 0 ) {
105- bsp_array_t empty_array ;
106- bsp_construct_default_array_t (& empty_array );
107- return empty_array ;
116+ H5Sclose (fspace );
117+ H5Dclose (dset );
118+ bsp_construct_default_array_t (array );
119+ return BSP_ERROR_IO ;
108120 }
109121
110122 hid_t hdf5_type = H5Dget_type (dset );
@@ -113,10 +125,9 @@ static inline bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
113125
114126 // Array will be written into a POSIX shared memory.
115127 bsp_shm_t array_shm = bsp_shm_new (dims [0 ] * bsp_type_size (type ));
116- bsp_array_t array ;
117- array .type = type ;
118- array .size = dims [0 ];
119- array .allocator = bsp_shm_allocator ;
128+ array -> type = type ;
129+ array -> size = dims [0 ];
130+ array -> allocator = bsp_shm_allocator ;
120131
121132 bsp_shm_t active_children_shm = bsp_shm_new (sizeof (_Atomic int ));
122133
@@ -140,35 +151,35 @@ static inline bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
140151 }
141152 }
142153
143- array . data = bsp_shm_attach (array_shm );
154+ array -> data = bsp_shm_attach (array_shm );
144155 if (thread_num == 0 ) {
145156 bsp_shm_delete (array_shm );
146157 }
147158
148- hsize_t chunk_size = (array . size + num_threads - 1 ) / num_threads ;
149- hsize_t start = (chunk_size * thread_num < array . size )
159+ hsize_t chunk_size = (array -> size + num_threads - 1 ) / num_threads ;
160+ hsize_t start = (chunk_size * thread_num < array -> size )
150161 ? chunk_size * thread_num
151- : array . size ;
162+ : array -> size ;
152163 hsize_t count =
153- (start + chunk_size <= array . size ) ? chunk_size : array . size - start ;
164+ (start + chunk_size <= array -> size ) ? chunk_size : array -> size - start ;
154165
155166 if (count > 0 ) {
156167 H5Sselect_hyperslab (fspace , H5S_SELECT_SET , & start , NULL , & count , NULL );
157168
158169 hid_t memspace_id = H5Screate_simple (1 , & count , NULL );
159170
160171 H5Dread (dset , bsp_get_hdf5_native_type (type ), memspace_id , fspace ,
161- H5P_DEFAULT , ((char * ) array . data ) + start * bsp_type_size (type ));
172+ H5P_DEFAULT , ((char * ) array -> data ) + start * bsp_type_size (type ));
162173 H5Sclose (memspace_id );
163174 }
164175
165- H5Dclose (dset );
166176 H5Sclose (fspace );
177+ H5Dclose (dset );
167178
168179 if (thread_num > 0 ) {
169180 atomic_fetch_add_explicit (active_children , -1 , memory_order_relaxed );
170181 bsp_shm_detach (active_children );
171- bsp_shm_detach (array . data );
182+ bsp_shm_detach (array -> data );
172183 exit (0 );
173184 }
174185
@@ -178,73 +189,69 @@ static inline bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
178189 }
179190 bsp_shm_detach (active_children );
180191
181- return array ;
192+ return BSP_SUCCESS ;
182193}
183194#endif
184195
185- static inline bsp_array_t bsp_read_array (hid_t f , const char * label ) {
196+ static inline bsp_error_t bsp_read_array (bsp_array_t * array , hid_t f ,
197+ const char * label ) {
186198 hid_t dset = H5Dopen2 (f , label , H5P_DEFAULT );
187199
188200 if (dset == H5I_INVALID_HID ) {
189- bsp_array_t empty_array ;
190- bsp_construct_default_array_t (& empty_array );
191- return empty_array ;
201+ bsp_construct_default_array_t (array );
202+ return BSP_ERROR_IO ;
192203 }
193204
194205 hid_t fspace = H5Dget_space (dset );
195206
196207 if (fspace == H5I_INVALID_HID ) {
197- bsp_array_t empty_array ;
198- bsp_construct_default_array_t (& empty_array );
199- return empty_array ;
208+ H5Sclose (fspace );
209+ H5Dclose (dset );
210+ bsp_construct_default_array_t (array );
211+ return BSP_ERROR_IO ;
200212 }
201213
202214 hsize_t dims [3 ];
203215
204216 int r = H5Sget_simple_extent_dims (fspace , dims , NULL );
205217
206218 if (r < 0 ) {
207- bsp_array_t empty_array ;
208- bsp_construct_default_array_t (& empty_array );
209- return empty_array ;
219+ H5Dclose (dset );
220+ H5Sclose (fspace );
221+ bsp_construct_default_array_t (array );
222+ return BSP_ERROR_IO ;
210223 }
211224
212225 hid_t hdf5_type = H5Dget_type (dset );
213226
214227 bsp_type_t type = bsp_get_bsp_type (hdf5_type );
215228
216- bsp_array_t array ;
217- bsp_construct_default_array_t (& array );
218-
219- bsp_error_t error = bsp_construct_array_t (& array , dims [0 ], type );
229+ bsp_error_t error = bsp_construct_array_t (array , dims [0 ], type );
220230 if (error != BSP_SUCCESS ) {
221- bsp_destroy_array_t (& array );
222231 H5Dclose (dset );
223232 H5Sclose (fspace );
224- bsp_array_t empty_array ;
225- bsp_construct_default_array_t (& empty_array );
226- return empty_array ;
233+ bsp_construct_default_array_t (array );
234+ return BSP_ERROR_MEMORY ;
227235 }
228236
229237 herr_t status = H5Dread (dset , bsp_get_hdf5_native_type (type ), H5S_ALL ,
230- H5S_ALL , H5P_DEFAULT , array . data );
238+ H5S_ALL , H5P_DEFAULT , array -> data );
231239
232240 if (status < 0 ) {
233- bsp_destroy_array_t (& array );
234- H5Dclose (dset );
241+ bsp_destroy_array_t (array );
235242 H5Sclose (fspace );
236- bsp_array_t empty_array ;
237- bsp_construct_default_array_t (& empty_array );
238- return empty_array ;
243+ H5Dclose ( dset ) ;
244+ bsp_construct_default_array_t (array );
245+ return BSP_ERROR_IO ;
239246 }
240247
241- H5Dclose (dset );
242248 H5Sclose (fspace );
243- return array ;
249+ H5Dclose (dset );
250+ return BSP_SUCCESS ;
244251}
245252
246- static inline void bsp_write_attribute (hid_t f , const char * label ,
247- const char * string ) {
253+ static inline bsp_error_t bsp_write_attribute (hid_t f , const char * label ,
254+ const char * string ) {
248255 hid_t strtype = H5Tcopy (H5T_C_S1 );
249256 H5Tset_size (strtype , strlen (string ));
250257 H5Tset_cset (strtype , H5T_CSET_UTF8 );
@@ -258,23 +265,28 @@ static inline void bsp_write_attribute(hid_t f, const char* label,
258265 H5Tclose (strtype );
259266 H5Aclose (attribute );
260267 H5Sclose (dataspace );
268+
269+ return BSP_SUCCESS ;
261270}
262271
263- static inline char * bsp_read_attribute (hid_t f , const char * label ) {
272+ static inline bsp_error_t bsp_read_attribute (char * * string , hid_t f ,
273+ const char * label ) {
264274 hid_t attribute = H5Aopen (f , label , H5P_DEFAULT );
265- hid_t strtype = H5Aget_type (attribute );
266275
267- hid_t type_class = H5Tget_class (strtype );
268- assert (type_class == H5T_STRING );
276+ if (attribute == H5I_INVALID_HID ) {
277+ return BSP_ERROR_FORMAT ;
278+ }
279+
280+ hid_t strtype = H5Aget_type (attribute );
269281
270282 size_t size = H5Tget_size (strtype );
271283
272- char * string = (char * ) malloc (size + 1 );
284+ * string = (char * ) malloc (size + 1 );
273285
274- H5Aread (attribute , strtype , string );
286+ H5Aread (attribute , strtype , * string );
275287
276288 H5Aclose (attribute );
277289 H5Tclose (strtype );
278290
279- return string ;
291+ return BSP_SUCCESS ;
280292}
0 commit comments