Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Template for new versions:
## Fixes

## Misc Improvements
- Core: attempts to delete a pool-allocated DF object will now throw an exception instead of corrupting the heap

## Documentation

Expand Down
28 changes: 22 additions & 6 deletions library/include/DataDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ distribution.

#pragma once

#include <functional>
#include <list>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <unordered_map>
#include <vector>
#include <functional>

#include "BitArray.h"
#include "Export.h"
Expand Down Expand Up @@ -572,29 +573,44 @@ namespace df
*
*/

using df_pool_id_t = size_t;
template<typename T> concept pooled_object = requires () { { T::pool_id } -> std::convertible_to<df_pool_id_t>; };

template<typename T> concept copy_assignable = std::assignable_from<T&, T&> && std::assignable_from<T&, const T&>;

template<typename T>
void *allocator_fn(void *out, const void *in) {
if (out)
constexpr df_pool_id_t invalid_pool_id = static_cast<df_pool_id_t>(-1);
// unerase type
T* _out = out ? reinterpret_cast<T*>(out) : nullptr;
const T* _in = in ? reinterpret_cast<const T*>(in) : nullptr;

if (_out)
{
if constexpr (copy_assignable<T>)
{
*(T*)out = *(const T*)in;
*_out = *_in;
return out;
}
else
{
return nullptr;
}
}
else if (in)
else if (_in)
{
if constexpr (pooled_object<T>)
{
if (_in->pool_id != invalid_pool_id)
{
throw std::runtime_error("Pool-allocated type cannot be deallocated with allocator_fn");
}
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
delete (T*)in;
delete _in;
#pragma GCC diagnostic pop
return (T*)in;
return const_cast<void*>(in);
}
else
return new T();
Expand Down
Loading