Last active
July 30, 2017 14:41
-
-
Save akinomyoga/a3c5d2e234a1542c29aeefd2fe4cb66f to your computer and use it in GitHub Desktop.
a simple example of custom allocator beyond DLL boundaries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // compile with /MTd /LD | |
| #include <vector> | |
| #include "my_allocator.hpp" | |
| __declspec(dllexport) void test_in_dll(void (*proc)(std::vector<int, my_allocator<int> >&)) { | |
| std::vector<int, my_allocator<int> > vec {1, 2, 3, 4, 5}; | |
| proc(vec); | |
| } | |
| __declspec(dllexport) void add_elements_in_dll(std::vector<int, my_allocator<int> >& vec) { | |
| for (int i = 0; i < 100; i++) vec.push_back(i); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // compile with /MTd dll.lib | |
| #include <vector> | |
| #include <iostream> | |
| #include "my_allocator.hpp" | |
| // from dll | |
| void test_in_dll(void (*proc)(std::vector<int, my_allocator<int> >&)); | |
| void add_elements_in_dll(std::vector<int, my_allocator<int> >& vec); | |
| void test_in_exe(void (*proc)(std::vector<int, my_allocator<int> >&)) { | |
| std::vector<int, my_allocator<int> > vec {1, 2, 3, 4, 5}; | |
| proc(vec); | |
| } | |
| void add_elements_in_exe(std::vector<int, my_allocator<int> >& vec) { | |
| for (int i = 0; i < 100; i++) | |
| vec.push_back(i); | |
| } | |
| int main() { | |
| test_in_dll(&add_elements_in_exe); | |
| test_in_exe(&add_elements_in_dll); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef MY_ALLOCATOR_HPP | |
| #define MY_ALLOCATOR_HPP | |
| #include <cstddef> | |
| #include <new> | |
| template<typename T> | |
| class my_allocator { | |
| void* (*m_new)(std::size_t); | |
| void (*m_delete)(void*); | |
| public: | |
| typedef T value_type; | |
| T* allocate(std::size_t n, const void* hint = nullptr) { | |
| return static_cast<T*>(m_new(n * sizeof(T))); | |
| } | |
| void deallocate(T* p, std::size_t n) { | |
| return m_delete(static_cast<void*>(p)); | |
| } | |
| my_allocator(): m_new(&::operator new), m_delete(&::operator delete) {} | |
| template<typename U> friend class my_allocator; | |
| template<typename U> my_allocator(my_allocator<U> const& u): | |
| m_new(u.m_new), m_delete(u.m_delete) {} | |
| bool operator==(my_allocator const& rhs) const { | |
| return m_new == rhs.m_new && m_delete == rhs.m_delete; | |
| } | |
| bool operator!=(my_allocator const& rhs) const {return !operator==(rhs);} | |
| }; | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment