src/backend/replication/logical/reorderbuffer.c
ReorderBufferTupleBuf *
ReorderBufferGetTupleBuf(ReorderBuffer *rb, Size tuple_len)
{
	ReorderBufferTupleBuf *tuple;
	Size		alloc_len;

	alloc_len = tuple_len + SizeofHeapTupleHeader;

	/*
	 * Most tuples are below MaxHeapTupleSize, so we use a slab allocator for
	 * those. Thus always allocate at least MaxHeapTupleSize. Note that tuples
	 * generated for oldtuples can be bigger, as they don't have out-of-line
	 * toast columns.
	 */
	if (alloc_len < MaxHeapTupleSize)
		alloc_len = MaxHeapTupleSize;


	/* if small enough, check the slab cache */
	if (alloc_len <= MaxHeapTupleSize && rb->nr_cached_tuplebufs)
	{
		rb->nr_cached_tuplebufs--;
		tuple = slist_container(ReorderBufferTupleBuf, node,
								slist_pop_head_node(&rb->cached_tuplebufs));
		Assert(tuple->alloc_tuple_size == MaxHeapTupleSize);
#ifdef USE_ASSERT_CHECKING
		memset(&tuple->tuple, 0xa9, sizeof(HeapTupleData));
		VALGRIND_MAKE_MEM_UNDEFINED(&tuple->tuple, sizeof(HeapTupleData));
#endif
		tuple->tuple.t_data = ReorderBufferTupleBufData(tuple);
#ifdef USE_ASSERT_CHECKING
		memset(tuple->tuple.t_data, 0xa8, tuple->alloc_tuple_size);
		VALGRIND_MAKE_MEM_UNDEFINED(tuple->tuple.t_data, tuple->alloc_tuple_size);
#endif
	}
	else
	{
		tuple = (ReorderBufferTupleBuf *)
			MemoryContextAlloc(rb->context,
							   sizeof(ReorderBufferTupleBuf) +
							   MAXIMUM_ALIGNOF + alloc_len);
		tuple->alloc_tuple_size = alloc_len;
		tuple->tuple.t_data = ReorderBufferTupleBufData(tuple);
	}

	return tuple;
}
/*
 * Get an unused, possibly preallocated, ReorderBufferTupleBuf fitting at
 * least a tuple of size tuple_len (excluding header overhead).
 */
Get an unused, possibly preallocated, ReorderBufferTupleBuf fitting at
least a tuple of size tuple_len (excluding header overhead).
Get an unused possibly preallocated ReorderBufferTupleBuf fitting at least a tuple of size tuple_len ( excluding header overhead ) .
ReorderBufferTupleBuf *
ReorderBufferGetTupleBuf(ReorderBuffer *rb, Size tuple_len)
{
	ReorderBufferTupleBuf *tuple;
	Size		alloc_len;
	alloc_len = tuple_len + SizeofHeapTupleHeader;
	if (alloc_len < MaxHeapTupleSize)
		alloc_len = MaxHeapTupleSize;
	if (alloc_len <= MaxHeapTupleSize && rb->nr_cached_tuplebufs)
	{
		rb->nr_cached_tuplebufs--;
		tuple = slist_container(ReorderBufferTupleBuf, node,
								slist_pop_head_node(&rb->cached_tuplebufs));
		Assert(tuple->alloc_tuple_size == MaxHeapTupleSize);
#ifdef USE_ASSERT_CHECKING
		memset(&tuple->tuple, 0xa9, sizeof(HeapTupleData));
		VALGRIND_MAKE_MEM_UNDEFINED(&tuple->tuple, sizeof(HeapTupleData));
#endif
		tuple->tuple.t_data = ReorderBufferTupleBufData(tuple);
#ifdef USE_ASSERT_CHECKING
		memset(tuple->tuple.t_data, 0xa8, tuple->alloc_tuple_size);
		VALGRIND_MAKE_MEM_UNDEFINED(tuple->tuple.t_data, tuple->alloc_tuple_size);
#endif
	}
	else
	{
		tuple = (ReorderBufferTupleBuf *)
			MemoryContextAlloc(rb->context,
							   sizeof(ReorderBufferTupleBuf) +
							   MAXIMUM_ALIGNOF + alloc_len);
		tuple->alloc_tuple_size = alloc_len;
		tuple->tuple.t_data = ReorderBufferTupleBufData(tuple);
	}
	return tuple;
}
ReorderBufferTupleBuf * ReorderBufferGetTupleBuf ( ReorderBuffer * rb , Size tuple_len ) { ReorderBufferTupleBuf * tuple ; Size alloc_len ; alloc_len = tuple_len + SizeofHeapTupleHeader ; if ( alloc_len < MaxHeapTupleSize ) alloc_len = MaxHeapTupleSize ; if ( alloc_len <= MaxHeapTupleSize && rb -> nr_cached_tuplebufs ) { rb -> nr_cached_tuplebufs -- ; tuple = slist_container ( ReorderBufferTupleBuf , node , slist_pop_head_node ( & rb -> cached_tuplebufs ) ) ; Assert ( tuple -> alloc_tuple_size == MaxHeapTupleSize ) ; #ifdef USE_ASSERT_CHECKING memset ( & tuple -> tuple , 0xa9 , sizeof ( HeapTupleData ) ) ; VALGRIND_MAKE_MEM_UNDEFINED ( & tuple -> tuple , sizeof ( HeapTupleData ) ) ; #endif tuple -> tuple . t_data = ReorderBufferTupleBufData ( tuple ) ; #ifdef USE_ASSERT_CHECKING memset ( tuple -> tuple . t_data , 0xa8 , tuple -> alloc_tuple_size ) ; VALGRIND_MAKE_MEM_UNDEFINED ( tuple -> tuple . t_data , tuple -> alloc_tuple_size ) ; #endif } else { tuple = ( ReorderBufferTupleBuf * ) MemoryContextAlloc ( rb -> context , sizeof ( ReorderBufferTupleBuf ) + MAXIMUM_ALIGNOF + alloc_len ) ; tuple -> alloc_tuple_size = alloc_len ; tuple -> tuple . t_data = ReorderBufferTupleBufData ( tuple ) ; } return tuple ; }
Get an unused, possibly preallocated, ReorderBufferTupleBuf fitting at
 least a tuple of size tuple_len (excluding header overhead).
Get an unused possibly preallocated ReorderBufferTupleBuf fitting at least a tuple of size tuple_len ( excluding header overhead ) .
/*
	 * Most tuples are below MaxHeapTupleSize, so we use a slab allocator for
	 * those. Thus always allocate at least MaxHeapTupleSize. Note that tuples
	 * generated for oldtuples can be bigger, as they don't have out-of-line
	 * toast columns.
	 */ /* if small enough, check the slab cache */