mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
fix(container_allocator): Fix MSVC compilation by correcting allocator semantics
ROOT CAUSE: The allocate() function was incorrectly constructing objects during memory allocation, violating C++ allocator requirements. MSVC's std::_Tree_node has a deleted default constructor, causing compilation failure. CHANGES: - container_allocator::allocate() now only allocates raw memory without constructing objects (removed mem::$new and ipc::construct calls) - container_allocator::deallocate() now only frees memory without destroying objects (removed mem::$delete and ipc::destroy_n calls) WHY THIS FIXES THE ISSUE: C++ allocator semantics require strict separation: * allocate() -> raw memory allocation only * construct() -> object construction with proper arguments * destroy() -> object destruction * deallocate() -> memory deallocation only Standard containers (like std::map) call construct() with proper arguments (key, value) to initialize nodes, not allocate(). Since std::_Tree_node in MSVC has no default constructor (= delete), attempting to construct it without arguments always fails. Fixes MSVC 2017 compilation error: error C2280: 'std::_Tree_node<...>::_Tree_node(void)': attempting to reference a deleted function
This commit is contained in:
parent
bb3c6eb534
commit
eede00165e
@ -63,26 +63,18 @@ public:
|
|||||||
pointer allocate(size_type count) noexcept {
|
pointer allocate(size_type count) noexcept {
|
||||||
if (count == 0) return nullptr;
|
if (count == 0) return nullptr;
|
||||||
if (count > this->max_size()) return nullptr;
|
if (count > this->max_size()) return nullptr;
|
||||||
if (count == 1) {
|
// Allocate raw memory without constructing objects
|
||||||
return mem::$new<value_type>();
|
// Construction should be done by construct() member function
|
||||||
} else {
|
|
||||||
void *p = mem::alloc(sizeof(value_type) * count);
|
void *p = mem::alloc(sizeof(value_type) * count);
|
||||||
if (p == nullptr) return nullptr;
|
|
||||||
for (std::size_t i = 0; i < count; ++i) {
|
|
||||||
std::ignore = ipc::construct<value_type>(static_cast<byte *>(p) + sizeof(value_type) * i);
|
|
||||||
}
|
|
||||||
return static_cast<pointer>(p);
|
return static_cast<pointer>(p);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void deallocate(pointer p, size_type count) noexcept {
|
void deallocate(pointer p, size_type count) noexcept {
|
||||||
if (count == 0) return;
|
if (count == 0) return;
|
||||||
if (count > this->max_size()) return;
|
if (count > this->max_size()) return;
|
||||||
if (count == 1) {
|
// Deallocate raw memory without destroying objects
|
||||||
mem::$delete(p);
|
// Destruction should be done by destroy() member function before deallocate
|
||||||
} else {
|
mem::free(p, sizeof(value_type) * count);
|
||||||
mem::free(ipc::destroy_n(p, count), sizeof(value_type) * count);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... P>
|
template <typename... P>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user