42
42
43
43
#include " ../networks/aig.hpp"
44
44
#include < fstream>
45
+ #include < optional>
45
46
#include < parallel_hashmap/phmap_dump.h>
46
47
47
48
namespace mockturtle
@@ -93,7 +94,10 @@ struct serializer
93
94
bool operator ()( phmap::BinaryOutputArchive& os, regular_node<Fanin, Size , PointerFieldSize> const & n ) const
94
95
{
95
96
uint64_t size = n.children .size ();
96
- os.dump ( (char *)&size, sizeof ( uint64_t ) );
97
+ if ( !os.dump ( (char *)&size, sizeof ( uint64_t ) ) )
98
+ {
99
+ return false ;
100
+ }
97
101
98
102
for ( const auto & c : n.children )
99
103
{
@@ -105,7 +109,10 @@ struct serializer
105
109
}
106
110
107
111
size = n.data .size ();
108
- os.dump ( (char *)&size, sizeof ( uint64_t ) );
112
+ if ( !os.dump ( (char *)&size, sizeof ( uint64_t ) ) )
113
+ {
114
+ return false ;
115
+ }
109
116
for ( const auto & d : n.data )
110
117
{
111
118
bool result = this ->operator ()( os, d );
@@ -122,7 +129,11 @@ struct serializer
122
129
bool operator ()( phmap::BinaryInputArchive& ar_input, const regular_node<Fanin, Size , PointerFieldSize>* n ) const
123
130
{
124
131
uint64_t size;
125
- ar_input.load ( (char *)&size, sizeof ( uint64_t ) );
132
+ if ( !ar_input.load ( (char *)&size, sizeof ( uint64_t ) ) )
133
+ {
134
+ return false ;
135
+ }
136
+
126
137
for ( uint64_t i = 0 ; i < size; ++i )
127
138
{
128
139
pointer_type ptr;
@@ -163,7 +174,10 @@ struct serializer
163
174
{
164
175
/* nodes */
165
176
uint64_t size = storage.nodes .size ();
166
- os.dump ( (char *)&size, sizeof ( uint64_t ) );
177
+ if ( !os.dump ( (char *)&size, sizeof ( uint64_t ) ) )
178
+ {
179
+ return false ;
180
+ }
167
181
for ( const auto & n : storage.nodes )
168
182
{
169
183
if ( !this ->operator ()( os, n ) )
@@ -174,7 +188,10 @@ struct serializer
174
188
175
189
/* inputs */
176
190
size = storage.inputs .size ();
177
- os.dump ( (char *)&size, sizeof ( uint64_t ) );
191
+ if ( !os.dump ( (char *)&size, sizeof ( uint64_t ) ) )
192
+ {
193
+ return false ;
194
+ }
178
195
for ( const auto & i : storage.inputs )
179
196
{
180
197
if ( !this ->operator ()( os, i ) )
@@ -185,7 +202,10 @@ struct serializer
185
202
186
203
/* outputs */
187
204
size = storage.outputs .size ();
188
- os.dump ( (char *)&size, sizeof ( uint64_t ) );
205
+ if ( !os.dump ( (char *)&size, sizeof ( uint64_t ) ) )
206
+ {
207
+ return false ;
208
+ }
189
209
for ( const auto & o : storage.outputs )
190
210
{
191
211
if ( !this ->operator ()( os, o ) )
@@ -200,7 +220,10 @@ struct serializer
200
220
return false ;
201
221
}
202
222
203
- os.dump ( (char *)&storage.trav_id , sizeof ( uint32_t ) );
223
+ if ( !os.dump ( (char *)&storage.trav_id , sizeof ( uint32_t ) ) )
224
+ {
225
+ return false ;
226
+ }
204
227
205
228
return true ;
206
229
}
@@ -209,7 +232,10 @@ struct serializer
209
232
{
210
233
/* nodes */
211
234
uint64_t size;
212
- ar_input.load ( (char *)&size, sizeof ( uint64_t ) );
235
+ if ( !ar_input.load ( (char *)&size, sizeof ( uint64_t ) ) )
236
+ {
237
+ return false ;
238
+ }
213
239
for ( uint64_t i = 0 ; i < size; ++i )
214
240
{
215
241
node_type n;
@@ -221,16 +247,25 @@ struct serializer
221
247
}
222
248
223
249
/* inputs */
224
- ar_input.load ( (char *)&size, sizeof ( uint64_t ) );
250
+ if ( !ar_input.load ( (char *)&size, sizeof ( uint64_t ) ) )
251
+ {
252
+ return false ;
253
+ }
225
254
for ( uint64_t i = 0 ; i < size; ++i )
226
255
{
227
256
uint64_t value;
228
- ar_input.load ( (char *)&value, sizeof ( uint64_t ) );
257
+ if ( !ar_input.load ( (char *)&value, sizeof ( uint64_t ) ) )
258
+ {
259
+ return false ;
260
+ }
229
261
storage->inputs .push_back ( value );
230
262
}
231
263
232
264
/* outputs */
233
- ar_input.load ( (char *)&size, sizeof ( uint64_t ) );
265
+ if ( !ar_input.load ( (char *)&size, sizeof ( uint64_t ) ) )
266
+ {
267
+ return false ;
268
+ }
234
269
for ( uint64_t i = 0 ; i < size; ++i )
235
270
{
236
271
pointer_type ptr;
@@ -247,23 +282,36 @@ struct serializer
247
282
return false ;
248
283
}
249
284
250
- ar_input.load ( (char *)&storage->trav_id , sizeof ( uint32_t ) );
285
+ if ( !ar_input.load ( (char *)&storage->trav_id , sizeof ( uint32_t ) ) )
286
+ {
287
+ return false ;
288
+ }
251
289
252
290
return true ;
253
291
}
254
292
}; /* struct serializer */
255
293
256
294
} /* namespace detail */
257
295
296
+ /* ! \brief Serializes a combinational AIG network to a archive, returning false on failure
297
+ *
298
+ * \param aig Combinational AIG network
299
+ * \param os Output archive
300
+ */
301
+ inline bool serialize_network_fallible ( aig_network const & aig, phmap::BinaryOutputArchive& os )
302
+ {
303
+ detail::serializer _serializer;
304
+ return _serializer ( os, *aig._storage );
305
+ }
306
+
258
307
/* ! \brief Serializes a combinational AIG network to a archive
259
308
*
260
309
* \param aig Combinational AIG network
261
310
* \param os Output archive
262
311
*/
263
312
inline void serialize_network ( aig_network const & aig, phmap::BinaryOutputArchive& os )
264
313
{
265
- detail::serializer _serializer;
266
- bool const okay = _serializer ( os, *aig._storage );
314
+ bool const okay = serialize_network_fallible ( aig, os );
267
315
(void )okay;
268
316
assert ( okay && " failed to serialize the network onto stream" );
269
317
}
@@ -279,12 +327,12 @@ inline void serialize_network( aig_network const& aig, std::string const& filena
279
327
serialize_network ( aig, ar_out );
280
328
}
281
329
282
- /* ! \brief Deserializes a combinational AIG network from a input archive
330
+ /* ! \brief Deserializes a combinational AIG network from a input archive, returning nullopt on failure
283
331
*
284
332
* \param ar_input Input archive
285
333
* \return Deserialized AIG network
286
334
*/
287
- inline aig_network deserialize_network ( phmap::BinaryInputArchive& ar_input )
335
+ inline std::optional< aig_network> deserialize_network_fallible ( phmap::BinaryInputArchive& ar_input )
288
336
{
289
337
detail::serializer _serializer;
290
338
auto storage = std::make_shared<aig_storage>();
@@ -293,10 +341,25 @@ inline aig_network deserialize_network( phmap::BinaryInputArchive& ar_input )
293
341
storage->outputs .clear ();
294
342
storage->hash .clear ();
295
343
296
- bool const okay = _serializer ( ar_input, storage.get () );
297
- (void )okay;
298
- assert ( okay && " failed to deserialize the network onto stream" );
299
- return aig_network{ storage };
344
+ if ( _serializer ( ar_input, storage.get () ) )
345
+ {
346
+ return aig_network{ storage };
347
+ }
348
+
349
+ return std::nullopt;
350
+ }
351
+
352
+ /* ! \brief Deserializes a combinational AIG network from a input archive
353
+ *
354
+ * \param ar_input Input archive
355
+ * \return Deserialized AIG network
356
+ */
357
+ inline aig_network deserialize_network ( phmap::BinaryInputArchive& ar_input )
358
+ {
359
+ auto result = deserialize_network_fallible ( ar_input );
360
+ (void )result.has_value ();
361
+ assert ( result.has_value () && " failed to deserialize the network onto stream" );
362
+ return *result;
300
363
}
301
364
302
365
/* ! \brief Deserializes a combinational AIG network from a file
0 commit comments