Я пытаюсь написать свой собственный распределитель. Я хотел бы создать пул предварительно выделенных заказов, которые будут возвращаться при каждом выделении.
Глядя на журнал, я вижу, что распределителю предлагается освободить больше выделенных объектов, и я не могу понять, почему.
Я работаю в Linux, и мое приложение вылетает, говоря:
terminate called after throwing an instance of 'std::invalid_argument'
what(): exceeding removed object
Aborted (core dumped)
Код распределителя приведен ниже.
Пока распределитель вызывается только с узлами std::list .
#include
#include
#include
#include
template
struct VectorAllocator
{
static inline constexpr size_t K_Size=32;
using value_type = T;
static inline std::array _cache;
static inline std::vector _freeList{K_Size};
static inline bool _initiated = false;
static void InitiateOnce()
{
if( !_initiated ) {
for(size_t i=0; i < K_Size; i++) {
_cache = new T();
_freeList = _cache;
}
_initiated = true;
}
}
VectorAllocator()
{
InitiateOnce();
}
T* allocate(std::size_t num_object)
{
if(num_object!=1)
throw std::invalid_argument("expected allocation of 1 object");
if (_freeList.empty() )
throw std::runtime_error("no more capactity in allocator");
auto iter = _freeList.rbegin();
T* out = *iter;
_freeList.erase( (iter++).base() );
return out;
}
void deallocate( T* ptr, std::size_t num_object)
{
if(num_object!=1)
throw std::invalid_argument("expected allocation of 1 object");
_freeList.push_back(ptr);
if(_freeList.size() > K_Size)
throw std::invalid_argument("exceeding removed object");
}
};
struct Object
{
size_t _value = 0;
};
int main()
{
std::list myList;
for(size_t i=1; i
Подробнее здесь: https://stackoverflow.com/questions/798 ... of-objects