12
12
13
13
namespace
14
14
{
15
+ constexpr int64_t arrow_alignment = 8 ;
16
+
15
17
// Aligns a value to the next multiple of 8, as required by the Arrow IPC format for message bodies.
16
18
int64_t align_to_8 (int64_t n)
17
19
{
18
- return (n + 7 ) & -8 ;
20
+ return (n + arrow_alignment - 1 ) & -arrow_alignment ;
19
21
}
20
22
21
23
// TODO Complete this with all possible formats?
@@ -137,7 +139,7 @@ std::vector<uint8_t> serialize_primitive_array(const sparrow::primitive_array<T>
137
139
// Copy the metadata into the buffer, after the 4-byte length prefix
138
140
memcpy (final_buffer.data () + sizeof (uint32_t ), schema_builder.GetBufferPointer (), schema_len);
139
141
// Write the 4-byte metadata length at the beginning of the message
140
- *( reinterpret_cast < uint32_t *>( final_buffer.data ())) = schema_len;
142
+ memcpy ( final_buffer.data (), &schema_len, sizeof ( schema_len)) ;
141
143
}
142
144
143
145
// II - Serialize the RecordBatch message
@@ -148,11 +150,11 @@ std::vector<uint8_t> serialize_primitive_array(const sparrow::primitive_array<T>
148
150
149
151
// arrow_arr.buffers[0] is the validity bitmap
150
152
// arrow_arr.buffers[1] is the data buffer
151
- const uint8_t * validity_bitmap = reinterpret_cast <const uint8_t *>(arrow_arr.buffers [0 ]);
152
- const uint8_t * data_buffer = reinterpret_cast <const uint8_t *>(arrow_arr.buffers [1 ]);
153
+ const uint8_t * validity_bitmap = static_cast <const uint8_t *>(arrow_arr.buffers [0 ]);
154
+ const uint8_t * data_buffer = static_cast <const uint8_t *>(arrow_arr.buffers [1 ]);
153
155
154
156
// Calculate the size of the validity and data buffers
155
- int64_t validity_size = (arrow_arr.length + 7 ) / 8 ;
157
+ int64_t validity_size = (arrow_arr.length + arrow_alignment - 1 ) / arrow_alignment ;
156
158
int64_t data_size = arrow_arr.length * sizeof (T);
157
159
int64_t body_len = validity_size + data_size; // The total size of the message body
158
160
@@ -190,7 +192,7 @@ std::vector<uint8_t> serialize_primitive_array(const sparrow::primitive_array<T>
190
192
uint8_t * dst = final_buffer.data () + current_size; // Get a pointer to where the new message will start
191
193
192
194
// Write the 4-byte metadata length for the RecordBatch message
193
- *( reinterpret_cast < uint32_t *>( dst)) = batch_meta_len;
195
+ memcpy ( dst, &batch_meta_len, sizeof ( batch_meta_len)) ;
194
196
dst += sizeof (uint32_t );
195
197
// Copy the RecordBatch metadata into the buffer
196
198
memcpy (dst, batch_builder.GetBufferPointer (), batch_meta_len);
@@ -228,7 +230,8 @@ sparrow::primitive_array<T> deserialize_primitive_array(const std::vector<uint8_
228
230
size_t current_offset = 0 ;
229
231
230
232
// I - Deserialize the Schema message
231
- uint32_t schema_meta_len = *(reinterpret_cast <const uint32_t *>(buf_ptr + current_offset));
233
+ uint32_t schema_meta_len;
234
+ memcpy (&schema_meta_len, buf_ptr + current_offset, sizeof (schema_meta_len));
232
235
current_offset += sizeof (uint32_t );
233
236
auto schema_message = org::apache::arrow::flatbuf::GetMessage (buf_ptr + current_offset);
234
237
if (schema_message->header_type () != org::apache::arrow::flatbuf::MessageHeader::Schema)
@@ -245,7 +248,8 @@ sparrow::primitive_array<T> deserialize_primitive_array(const std::vector<uint8_
245
248
current_offset += schema_meta_len;
246
249
247
250
// II - Deserialize the RecordBatch message
248
- uint32_t batch_meta_len = *(reinterpret_cast <const uint32_t *>(buf_ptr + current_offset));
251
+ uint32_t batch_meta_len;
252
+ memcpy (&batch_meta_len, buf_ptr + current_offset, sizeof (batch_meta_len));
249
253
current_offset += sizeof (uint32_t );
250
254
auto batch_message = org::apache::arrow::flatbuf::GetMessage (buf_ptr + current_offset);
251
255
if (batch_message->header_type () != org::apache::arrow::flatbuf::MessageHeader::RecordBatch)
0 commit comments