Skip to content

Instantly share code, notes, and snippets.

@miguelibero
Created September 21, 2014 23:00
Show Gist options
  • Save miguelibero/e8f1357a3752e2c126d7 to your computer and use it in GitHub Desktop.
Save miguelibero/e8f1357a3752e2c126d7 to your computer and use it in GitHub Desktop.
Joint Memory
#ifndef _joint_mem_hpp_
#define _joint_mem_hpp_
#include <memory>
#include <cstdint>
typedef uint8_t joint_mem_unit;
template<typename Alloc = std::allocator<joint_mem_unit>>
class joint_mem_alloc
{
public:
typedef joint_mem_unit unit;
private:
unit* _mem;
size_t _size;
Alloc _alloc;
template<typename Element, typename... Elements>
static size_t size_of(Element*& elm, Elements*&... elms)
{
return size_of(elm)+size_of(elms...);
}
template<typename Element>
static size_t size_of(Element*& elm)
{
return sizeof(Element);
}
template<typename Element, typename... Elements>
static void assign(unit* mem, Element*& elm, Elements*&... elms)
{
assign(mem, elm);
assign(mem+size_of(elm), elms...);
}
template<typename Element>
static void assign(unit* mem, Element*& elm)
{
elm = reinterpret_cast<Element*>(mem);
}
template<typename... Elements>
void init(Elements*&... elms)
{
_size = size_of(elms...);
_mem = Alloc().allocate(_size);
assign(_mem, elms...);
}
public:
template<typename... Elements>
joint_mem_alloc(Elements*&... elms):
_alloc(Alloc())
{
init(elms...);
}
template<typename... Elements>
joint_mem_alloc(const Alloc& alloc, Elements*&... elms):
_alloc(alloc)
{
init(elms...);
}
size_t size() const
{
return _size;
}
const void* data() const
{
return _mem;
}
void* data()
{
return _mem;
}
template<typename Type>
const Type* data() const
{
return reinterpret_cast<const Type*>(_mem);
}
template<typename Type>
Type* data()
{
return reinterpret_cast<Type*>(_mem);
}
~joint_mem_alloc()
{
_alloc.deallocate(_mem, _size);
}
};
class joint_mem : public joint_mem_alloc<>
{
public:
template<typename... Elements>
joint_mem(Elements*&... elms):
joint_mem_alloc(elms...)
{
}
};
#endif
#include <joint_mem/joint_mem.h>
#include "gtest/gtest.h"
struct test_member
{
int a;
};
struct test_struct1 : public joint_mem
{
test_member* member1;
test_member* member2;
test_struct1():
joint_mem(member1, member2)
{
}
};
struct test_struct2
{
test_member* member1;
test_member* member2;
joint_mem mem;
test_struct2():
mem(member1, member2)
{
}
};
TEST(joint_mem, basic1) {
test_struct1 struc;
test_member* ptr = struc.data<test_member>();
ASSERT_EQ(2*sizeof(test_member), struc.size());
ASSERT_EQ(struc.member1, ptr);
ASSERT_EQ(struc.member2, ptr+1);
}
TEST(joint_mem, basic2) {
test_struct2 struc;
test_member* ptr = struc.mem.data<test_member>();
ASSERT_EQ(2*sizeof(test_member), struc.mem.size());
ASSERT_EQ(struc.member1, ptr);
ASSERT_EQ(struc.member2, ptr+1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment