Skip to content

Commit c480724

Browse files
macdiceCommitfest Bot
authored and
Commitfest Bot
committed
Improve API for storing data in read streams.
Read stream callbacks receive a void pointer into the per-buffer data queue so that can store data there for later retrieval by the buffer consumer. We can improve readability and safety a bit by changing cast-and-assign or raw memcpy() to: read_stream_put_value(stream, per_buffer_data, my_int); This form infers the size and asserts that the storage space matches, generally mirroring the read_stream_get_buffer_and_value() call used for retrieving the streamed data later.
1 parent f819ff0 commit c480724

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

‎src/backend/access/heap/vacuumlazy.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ heap_vac_scan_next_block(ReadStream *stream,
16301630
*/
16311631
vacrel->current_block = next_block;
16321632
blk_info |= VAC_BLK_ALL_VISIBLE_ACCORDING_TO_VM;
1633-
*((uint8 *) per_buffer_data) = blk_info;
1633+
read_stream_put_value(stream, per_buffer_data, blk_info);
16341634
return vacrel->current_block;
16351635
}
16361636
else
@@ -1646,7 +1646,7 @@ heap_vac_scan_next_block(ReadStream *stream,
16461646
blk_info |= VAC_BLK_ALL_VISIBLE_ACCORDING_TO_VM;
16471647
if (vacrel->next_unskippable_eager_scanned)
16481648
blk_info |= VAC_BLK_WAS_EAGER_SCANNED;
1649-
*((uint8 *) per_buffer_data) = blk_info;
1649+
read_stream_put_value(stream, per_buffer_data, blk_info);
16501650
return vacrel->current_block;
16511651
}
16521652
}
@@ -2691,7 +2691,7 @@ vacuum_reap_lp_read_stream_next(ReadStream *stream,
26912691
* Save the TidStoreIterResult for later, so we can extract the offsets.
26922692
* It is safe to copy the result, according to TidStoreIterateNext().
26932693
*/
2694-
memcpy(per_buffer_data, iter_result, sizeof(*iter_result));
2694+
read_stream_put_value(stream, per_buffer_data, *iter_result);
26952695

26962696
return iter_result->blkno;
26972697
}

‎src/include/storage/read_stream.h

+9
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,13 @@ read_stream_get_buffer_and_value_with_size(ReadStream *stream,
157157
(AssertMacro(sizeof(**(pointer)) <= read_stream_per_buffer_data_size(stream)), \
158158
read_stream_next_buffer((stream), ((void **) (pointer))))
159159

160+
/*
161+
* Set the per-buffer data by value. This can be called from inside a
162+
* callback that is returning block numbers. It asserts that the value's size
163+
* matches the available space.
164+
*/
165+
#define read_stream_put_value(stream, per_buffer_data, value) \
166+
(AssertMacro(sizeof(value) == read_stream_per_buffer_data_size(stream)), \
167+
memcpy((per_buffer_data), &(value), sizeof(value)))
168+
160169
#endif /* READ_STREAM_H */

0 commit comments

Comments
 (0)