Example #1
0
int fast_mblock_init_ex(struct fast_mblock_man *mblock,
        const int element_size, const int alloc_elements_once,
        fast_mblock_alloc_init_func init_func, const bool need_lock)
{
    return fast_mblock_init_ex2(mblock, NULL, element_size,
            alloc_elements_once, init_func, need_lock, NULL, NULL, NULL);
}
static int region_init(struct fast_allocator_context *acontext,
	struct fast_region_info *region)
{
	int result;
	int bytes;
	int element_size;
	int allocator_count;
	struct fast_allocator_info *allocator;

	region->pad_mask = region->step - 1;
	allocator_count = (region->end - region->start) / region->step;
	bytes = sizeof(struct fast_allocator_info) * allocator_count;
	region->allocators = (struct fast_allocator_info *)malloc(bytes);
	if (region->allocators == NULL)
	{
		result = errno != 0 ? errno : ENOMEM;
		logError("file: "__FILE__", line: %d, "
				"malloc %d bytes fail, errno: %d, error info: %s",
				__LINE__, bytes, result, STRERROR(result));
		return result;
	}
	memset(region->allocators, 0, bytes);


	if ((result=allocator_array_check_capacity(acontext, allocator_count)) != 0)
	{
		return result;
	}

	result = 0;
 	allocator = region->allocators;
	for (element_size=region->start+region->step; element_size<=region->end;
		element_size+=region->step,allocator++)
	{
		result = fast_mblock_init_ex2(&allocator->mblock, NULL, element_size,
			region->alloc_elements_once, NULL, acontext->need_lock,
			fast_allocator_malloc_trunk_check,
			fast_allocator_malloc_trunk_notify_func, acontext);
		if (result != 0)
		{
			break;
		}

		ADD_ALLOCATOR_TO_ARRAY(acontext, allocator, true);
	}

	return result;
}