|
|
|
@ -39,6 +39,7 @@ public:
|
|
|
|
|
// printf("head: %ld\n", (long)head);
|
|
|
|
|
node* next = ::new (block) node(head);
|
|
|
|
|
head = next;
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t* try_pop() noexcept
|
|
|
|
@ -48,11 +49,14 @@ public:
|
|
|
|
|
}
|
|
|
|
|
node* last_head = head;
|
|
|
|
|
head = head->prev;
|
|
|
|
|
count--;
|
|
|
|
|
return (uint8_t*)last_head;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool is_empty() const { return head == nullptr; }
|
|
|
|
|
|
|
|
|
|
size_t size() const { return count; }
|
|
|
|
|
|
|
|
|
|
void clear() { head = nullptr; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
@ -63,6 +67,7 @@ private:
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
node* head = nullptr;
|
|
|
|
|
size_t count = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// memblock stack that mutexes pushing/popping
|
|
|
|
@ -130,9 +135,17 @@ public:
|
|
|
|
|
void operator()(void* block) { pool->stack.push(static_cast<uint8_t*>(block)); }
|
|
|
|
|
single_thread_obj_pool<T>* pool;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using obj_ptr = std::unique_ptr<T, obj_deleter>;
|
|
|
|
|
|
|
|
|
|
~single_thread_obj_pool()
|
|
|
|
|
{
|
|
|
|
|
uint8_t* block = stack.try_pop();
|
|
|
|
|
while (block != nullptr) {
|
|
|
|
|
delete[] block;
|
|
|
|
|
block = stack.try_pop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// allocate object
|
|
|
|
|
template <typename... Args>
|
|
|
|
|
obj_ptr make(Args&&... args)
|
|
|
|
@ -152,6 +165,8 @@ public:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t capacity() const { return stack.size(); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
memblock_stack stack;
|
|
|
|
|
};
|
|
|
|
|