-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #191 from jumonatr/aligned
Aligned Allocations
- Loading branch information
Showing
6 changed files
with
101 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#pragma once | ||
|
||
#include <cstddef> | ||
|
||
#include "Eigen/Core" | ||
|
||
namespace g2o | ||
{ | ||
// 16 byte aligned allocation functions | ||
template<typename Type> | ||
Type* allocate_aligned(size_t n) | ||
{ | ||
return (Type*)Eigen::internal::aligned_malloc(n * sizeof(Type)); | ||
} | ||
|
||
template<typename Type> | ||
Type* reallocate_aligned(Type* ptr, size_t newSize, size_t oldSize) | ||
{ | ||
return (Type*)Eigen::internal::aligned_realloc(ptr, newSize * sizeof(Type), oldSize * sizeof(Type)); | ||
} | ||
|
||
template<typename Type> | ||
void free_aligned(Type* block) | ||
{ | ||
|
||
Eigen::internal::aligned_free(block); | ||
} | ||
|
||
template<typename Type> | ||
struct dynamic_aligned_buffer | ||
{ | ||
dynamic_aligned_buffer(size_t size) | ||
: m_size{ 0 }, m_ptr{ nullptr } | ||
{ | ||
allocate(size); | ||
} | ||
|
||
~dynamic_aligned_buffer() | ||
{ | ||
free(); | ||
} | ||
|
||
Type* request(size_t n) | ||
{ | ||
if (n <= m_size) | ||
return m_ptr; | ||
|
||
m_ptr = reallocate_aligned<Type>(m_ptr, n, m_size); | ||
m_size = m_ptr ? n : 0; | ||
|
||
return m_ptr; | ||
} | ||
|
||
private: | ||
void allocate(size_t size) | ||
{ | ||
m_ptr = allocate_aligned<Type>(size); | ||
if (m_ptr != nullptr) | ||
m_size = size; | ||
} | ||
|
||
void free() | ||
{ | ||
if (m_ptr != nullptr) | ||
{ | ||
free_aligned<Type>(m_ptr); | ||
m_size = 0; | ||
m_ptr = nullptr; | ||
} | ||
} | ||
|
||
std::size_t m_size; | ||
Type* m_ptr; | ||
}; | ||
} |
This file contains 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