Skip to content

Commit

Permalink
Fixed zone expansion algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
redboltz committed Jul 23, 2017
1 parent a0e4294 commit 288a6b2
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 52 deletions.
41 changes: 24 additions & 17 deletions erb/v1/cpp03_zone.hpp.erb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ private:
template <typename T>
static void object_delete(void* obj);

void* allocate_expand(size_t size);
static char* get_aligned(char* ptr, size_t align);

char* allocate_expand(size_t size);
private:
zone(const zone&);
zone& operator=(const zone&);
Expand All @@ -188,37 +190,42 @@ inline zone::zone(size_t chunk_size) /* throw() */ :m_chunk_size(chunk_size), m_
{
}

inline void* zone::allocate_align(size_t size, size_t align)
inline char* zone::get_aligned(char* ptr, size_t align)
{
char* aligned =
return
reinterpret_cast<char*>(
reinterpret_cast<size_t>(
(m_chunk_list.m_ptr + (align - 1))) / align * align);
(ptr + (align - 1))) / align * align);
}

inline void* zone::allocate_align(size_t size, size_t align)
{
char* aligned = get_aligned(m_chunk_list.m_ptr, align);
size_t adjusted_size = size + (aligned - m_chunk_list.m_ptr);
if(m_chunk_list.m_free >= adjusted_size) {
m_chunk_list.m_free -= adjusted_size;
m_chunk_list.m_ptr += adjusted_size;
return aligned;
if (m_chunk_list.m_free < adjusted_size) {
size_t enough_size = size + align - 1;
char* ptr = allocate_expand(enough_size);
aligned = get_aligned(ptr, align);
adjusted_size = size + (aligned - m_chunk_list.m_ptr);
}
return reinterpret_cast<char*>(
reinterpret_cast<size_t>(
allocate_expand(size + (align - 1))) / align * align);
m_chunk_list.m_free -= adjusted_size;
m_chunk_list.m_ptr += adjusted_size;
return aligned;
}

inline void* zone::allocate_no_align(size_t size)
{
char* ptr = m_chunk_list.m_ptr;
if(m_chunk_list.m_free < size) {
return allocate_expand(size);
ptr = allocate_expand(size);
}

char* ptr = m_chunk_list.m_ptr;
m_chunk_list.m_free -= size;
m_chunk_list.m_ptr += size;

return ptr;
}

inline void* zone::allocate_expand(size_t size)
inline char* zone::allocate_expand(size_t size)
{
chunk_list* const cl = &m_chunk_list;

Expand All @@ -240,8 +247,8 @@ inline void* zone::allocate_expand(size_t size)

c->m_next = cl->m_head;
cl->m_head = c;
cl->m_free = sz - size;
cl->m_ptr = ptr + size;
cl->m_free = sz;
cl->m_ptr = ptr;

return ptr;
}
Expand Down
8 changes: 8 additions & 0 deletions erb/v1/cpp03_zone_decl.hpp.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
#define MSGPACK_ZONE_ALIGN sizeof(void*)
#endif

#if defined(_MSC_VER)
#define MSGPACK_ZONE_ALIGNOF(type) __alignof(type)
#else
#define MSGPACK_ZONE_ALIGNOF(type) __alignof__(type)
#endif
// For a compiler that doesn't support __alignof__:
// #define MSGPACK_ZONE_ALIGNOF(type) MSGPACK_ZONE_ALIGN

<% GENERATION_LIMIT = 15 %>
namespace msgpack {

Expand Down
43 changes: 25 additions & 18 deletions include/msgpack/v1/detail/cpp03_zone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ class zone {
template <typename T>
static void object_delete(void* obj);

void* allocate_expand(size_t size);
static char* get_aligned(char* ptr, size_t align);

char* allocate_expand(size_t size);
private:
zone(const zone&);
zone& operator=(const zone&);
Expand All @@ -233,37 +235,42 @@ inline zone::zone(size_t chunk_size) /* throw() */ :m_chunk_size(chunk_size), m_
{
}

inline void* zone::allocate_align(size_t size, size_t align)
inline char* zone::get_aligned(char* ptr, size_t align)
{
char* aligned =
return
reinterpret_cast<char*>(
reinterpret_cast<size_t>(
(m_chunk_list.m_ptr + (align - 1))) / align * align);
(ptr + (align - 1))) / align * align);
}

inline void* zone::allocate_align(size_t size, size_t align)
{
char* aligned = get_aligned(m_chunk_list.m_ptr, align);
size_t adjusted_size = size + (aligned - m_chunk_list.m_ptr);
if(m_chunk_list.m_free >= adjusted_size) {
m_chunk_list.m_free -= adjusted_size;
m_chunk_list.m_ptr += adjusted_size;
return aligned;
}
return reinterpret_cast<char*>(
reinterpret_cast<size_t>(
allocate_expand(size + (align - 1))) / align * align);
if (m_chunk_list.m_free < adjusted_size) {
size_t enough_size = size + align - 1;
char* ptr = allocate_expand(enough_size);
aligned = get_aligned(ptr, align);
adjusted_size = size + (aligned - m_chunk_list.m_ptr);
}
m_chunk_list.m_free -= adjusted_size;
m_chunk_list.m_ptr += adjusted_size;
return aligned;
}

inline void* zone::allocate_no_align(size_t size)
{
char* ptr = m_chunk_list.m_ptr;
if(m_chunk_list.m_free < size) {
return allocate_expand(size);
ptr = allocate_expand(size);
}

char* ptr = m_chunk_list.m_ptr;
m_chunk_list.m_free -= size;
m_chunk_list.m_ptr += size;

return ptr;
}

inline void* zone::allocate_expand(size_t size)
inline char* zone::allocate_expand(size_t size)
{
chunk_list* const cl = &m_chunk_list;

Expand All @@ -285,8 +292,8 @@ inline void* zone::allocate_expand(size_t size)

c->m_next = cl->m_head;
cl->m_head = c;
cl->m_free = sz - size;
cl->m_ptr = ptr + size;
cl->m_free = sz;
cl->m_ptr = ptr;

return ptr;
}
Expand Down
3 changes: 3 additions & 0 deletions include/msgpack/v1/detail/cpp03_zone_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#else
#define MSGPACK_ZONE_ALIGNOF(type) __alignof__(type)
#endif
// For a compiler that doesn't support __alignof__:
// #define MSGPACK_ZONE_ALIGNOF(type) MSGPACK_ZONE_ALIGN


namespace msgpack {

Expand Down
41 changes: 24 additions & 17 deletions include/msgpack/v1/detail/cpp11_zone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,44 +219,51 @@ class zone {
template <typename T>
static void object_delete(void* obj);

void* allocate_expand(size_t size);
static char* get_aligned(char* ptr, size_t align);

char* allocate_expand(size_t size);
};

inline zone::zone(size_t chunk_size) noexcept:m_chunk_size(chunk_size), m_chunk_list(m_chunk_size)
{
}

inline void* zone::allocate_align(size_t size, size_t align)
inline char* zone::get_aligned(char* ptr, size_t align)
{
char* aligned =
return
reinterpret_cast<char*>(
reinterpret_cast<size_t>(
(m_chunk_list.m_ptr + (align - 1))) / align * align);
(ptr + (align - 1))) / align * align);
}

inline void* zone::allocate_align(size_t size, size_t align)
{
char* aligned = get_aligned(m_chunk_list.m_ptr, align);
size_t adjusted_size = size + (aligned - m_chunk_list.m_ptr);
if(m_chunk_list.m_free >= adjusted_size) {
m_chunk_list.m_free -= adjusted_size;
m_chunk_list.m_ptr += adjusted_size;
return aligned;
if (m_chunk_list.m_free < adjusted_size) {
size_t enough_size = size + align - 1;
char* ptr = allocate_expand(enough_size);
aligned = get_aligned(ptr, align);
adjusted_size = size + (aligned - m_chunk_list.m_ptr);
}
return reinterpret_cast<char*>(
reinterpret_cast<size_t>(
allocate_expand(size + (align - 1))) / align * align);
m_chunk_list.m_free -= adjusted_size;
m_chunk_list.m_ptr += adjusted_size;
return aligned;
}

inline void* zone::allocate_no_align(size_t size)
{
char* ptr = m_chunk_list.m_ptr;
if(m_chunk_list.m_free < size) {
return allocate_expand(size);
ptr = allocate_expand(size);
}

char* ptr = m_chunk_list.m_ptr;
m_chunk_list.m_free -= size;
m_chunk_list.m_ptr += size;

return ptr;
}

inline void* zone::allocate_expand(size_t size)
inline char* zone::allocate_expand(size_t size)
{
chunk_list* const cl = &m_chunk_list;

Expand All @@ -278,8 +285,8 @@ inline void* zone::allocate_expand(size_t size)

c->m_next = cl->m_head;
cl->m_head = c;
cl->m_free = sz - size;
cl->m_ptr = ptr + size;
cl->m_free = sz;
cl->m_ptr = ptr;

return ptr;
}
Expand Down
2 changes: 2 additions & 0 deletions include/msgpack/v1/detail/cpp11_zone_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#else
#define MSGPACK_ZONE_ALIGNOF(type) __alignof__(type)
#endif
// For a compiler that doesn't support __alignof__:
// #define MSGPACK_ZONE_ALIGNOF(type) MSGPACK_ZONE_ALIGN

namespace msgpack {

Expand Down

0 comments on commit 288a6b2

Please sign in to comment.