[Tarantool-patches] [PATCH small 1/1] region: new region_alloc_array, updated alloc_object

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Wed May 27 03:00:26 MSK 2020


Hi! Thanks for the review!

> On 5/21/20 11:32 PM, Vladislav Shpilevoy wrote:
>> Also the patch adds an out parameter 'size' for both macros. It
>> simplifies total size calculation, which is needed almost always,
>> because total size is included into an error message, if the
>> allocation fails.
> I don't like the size returning. Even for array allocation it looks annoying.
> It's too easy to calculate the size, and compilers will omit the second
> multiplication with the same args.
> For single allocation it looks ugly. It's not even a calculation.

This is not a matter of how easy it is to calculate a size. This is
a matter of code duplication. Type and count are needed in 2 places:
in region_alloc_array() and then in diag_set(). This is much more ugly,
and occupies significantly more space, when you need to write them
both twice. Compare:

	struct key_def **keys =
		region_alloc_array(&fiber()->gc, typeof(keys[0]),
				   key_count);
	if (keys == NULL) {
		diag_set(OutOfMemory, sizeof(keys[0]) * key_count,
			 "region_alloc_array", "keys");
		return NULL;
	}

And

	size_t bsize;
	struct key_def **keys =
		region_alloc_array(&fiber()->gc, typeof(keys[0]),
				   key_count, &bsize);
	if (keys == NULL) {
		diag_set(OutOfMemory, bsize, "region_alloc_array", "keys");
		return NULL;
	}

In the first option 'keys[0]' and 'count' need to be written 2
times. This is getting worse, when variable name is like 'region_links',
or count is 'def->field_count'.

Worse again it becomes, when size is needed not only in diag, but
also in case of success. For example, to make a memset(0). In this
case it is either triple code duplication, or double code duplication
but with size_t size/bsize variable anyway.

region_alloc_object() got the out parameter to be consistent with
region_alloc_array().

I tried removing the out parameter, and it started looking worse.


More information about the Tarantool-patches mailing list