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