/*
* Copyright (c) 2020 YuQing <384681@qq.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the Lesser GNU General Public License, version 3
* or later ("LGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see .
*/
#include "sf_shared_mbuffer.h"
static int sf_shared_mbuffer_alloc_init(void *element, void *args)
{
SFSharedMBuffer *buffer;
buffer = (SFSharedMBuffer *)((char *)element +
sizeof(struct fast_allocator_wrapper));
buffer->ctx = (SFSharedMBufferContext *)args;
return 0;
}
int sf_shared_mbuffer_init_ex(SFSharedMBufferContext *context,
const char *name_prefix, const int buff_extra_size,
const int min_buff_size, const int max_buff_size,
const int min_alloc_once, const int64_t memory_limit,
const bool need_lock)
{
const double expect_usage_ratio = 0.75;
const int reclaim_interval = 1;
struct fast_region_info regions[32];
struct fast_mblock_object_callbacks object_callbacks;
int count;
int start;
int end;
int alloc_once;
int buff_size;
int i;
alloc_once = (4 * 1024 * 1024) / max_buff_size;
if (alloc_once == 0) {
alloc_once = min_alloc_once;
} else {
i = min_alloc_once;
while (i < alloc_once) {
i *= 2;
}
alloc_once = i;
}
count = 1;
buff_size = min_buff_size;
while (buff_size < max_buff_size) {
buff_size *= 2;
++count;
alloc_once *= 2;
}
buff_size = min_buff_size;
start = 0;
end = buff_extra_size + buff_size;
FAST_ALLOCATOR_INIT_REGION(regions[0], start, end,
end - start, alloc_once);
//logInfo("[1] start: %d, end: %d, alloc_once: %d", start, end, alloc_once);
start = end;
for (i=1; iallocator, name_prefix,
sizeof(SFSharedMBuffer), &object_callbacks, regions, count,
memory_limit, expect_usage_ratio, reclaim_interval, need_lock);
}
void sf_shared_mbuffer_destroy(SFSharedMBufferContext *context)
{
fast_allocator_destroy(&context->allocator);
}