C++ interop, abstract struct problem
RSY
rsy_881 at gmail.com
Tue Dec 29 12:02:15 UTC 2020
Hmm, something seems to be very wrong, here what i got so far
``` D
struct IAllocator
{ }
struct DefaultAllocator
{
// BASE --------------------------
IAllocator base = IAllocator();
alias base this;
//--------------------------------
ubyte* m_small_allocations = null;
void*[4] m_free_lists;
uint m_page_count = 0;
Mutex m_mutex;
}
void preinit(ref IAllocator allocator, bool load_renderdoc);
```
The problem is the allocator data seems to be corrupted, it
crashes on the C++ side when calling preinit
IAllocator is empty, but it doesn't get optimized as the wiki
say, since the size of DefaultAllocator is 64 bytes on the C++
side, and 56 bytes on D side, i get 64 bytes with the definition
above
Does anyone have an idea, did i translate the struct wrong?
```C++
struct LUMIX_ENGINE_API IAllocator {
virtual ~IAllocator() {}
virtual bool isDebug() const { return false; }
virtual void* allocate(size_t size) = 0;
virtual void deallocate(void* ptr) = 0;
virtual void* reallocate(void* ptr, size_t size) = 0;
virtual void* allocate_aligned(size_t size, size_t align) = 0;
virtual void deallocate_aligned(void* ptr) = 0;
virtual void* reallocate_aligned(void* ptr, size_t size, size_t
align) = 0;
template <typename T> void deleteObject(T* ptr) {
if (ptr)
{
ptr->~T();
deallocate_aligned(ptr);
}
}
};
```
and for DefaultAllocator
```c++
struct LUMIX_ENGINE_API DefaultAllocator final : IAllocator {
struct Page;
DefaultAllocator();
~DefaultAllocator();
void* allocate(size_t n) override;
void deallocate(void* p) override;
void* reallocate(void* ptr, size_t size) override;
void* allocate_aligned(size_t size, size_t align) override;
void deallocate_aligned(void* ptr) override;
void* reallocate_aligned(void* ptr, size_t size, size_t align)
override;
u8* m_small_allocations = nullptr;
Page* m_free_lists[4];
u32 m_page_count = 0;
Mutex m_mutex;
};
```
For Mutex:
```c++
struct alignas(8) LUMIX_ENGINE_API Mutex {
friend struct ConditionVariable;
Mutex();
Mutex(const Mutex&) = delete;
~Mutex();
void enter();
void exit();
private:
#ifdef _WIN32
u8 data[8];
#else
pthread_mutex_t mutex;
#endif
};
```
More information about the Digitalmars-d-learn
mailing list