Skip to content

closes https://github.com/kimkulling/cppcore/issues/16 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/cppcore/CPPCoreCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace CPPCore {
// All disabled warnings for windows
# pragma warning( disable : 4251 ) // <class> needs to have dll-interface to be used by clients of class <class>
#else
# define DLL_CPPCORE_EXPORT
# define DLL_CPPCORE_EXPORT __attribute__((visibility("default")))
#endif

//-------------------------------------------------------------------------------------------------
Expand Down
23 changes: 12 additions & 11 deletions include/cppcore/Memory/MemUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ namespace CPPCore {

#define ALIGN_MASK(value, mask) (((value) + (mask)) & ((~0) & (~(mask))))

template<class T>
inline size_t align(size_t n) {
return (n + sizeof(T) - 1) & ~(sizeof(T) - 1);
}

union {
const void *mPtr;
uintptr_t mAddr;
} unaligned;

//-------------------------------------------------------------------------------------------------
/// @class THashMap
/// @ingroup CPPCore
Expand All @@ -43,28 +53,19 @@ class MemUtils {

static bool isAligned(const void *ptr, size_t align);

static void *alignPtr(void *ptr, size_t extra, size_t align);
static const void *alignPtr(void *ptr, size_t extra, size_t align);

MemUtils() = delete;
~MemUtils() = delete;
};

inline bool MemUtils::isAligned(const void *ptr, size_t align) {
union {
const void *mPtr;
uintptr_t mAddr;
} unaligned;

unaligned.mPtr = ptr;
return 0 == (unaligned.mAddr & (align - 1));
}

inline void *MemUtils::alignPtr(void *ptr, size_t extra, size_t align) {
union {
void *mPtr;
uintptr_t mAddr;
} unaligned;

inline const void *MemUtils::alignPtr(void *ptr, size_t extra, size_t align) {
unaligned.mPtr = ptr;
uintptr_t un = unaligned.mAddr + extra; // space for header
const uintptr_t mask = align - 1;
Expand Down
16 changes: 11 additions & 5 deletions test/container/TStaticArrayTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ TEST_F(TStaticArrayTest, access_items_Test) {

TEST_F(TStaticArrayTest, clear_Test) {
TStaticArray<int, 4> arr;
int ii=0;
for (size_t i = 0; i < 4; ++i) {
arr[i] = (int) i;
arr[i] = ii;
ii++;
}
arr.memset( 0 );
for (size_t i = 0; i < 4; ++i) {
Expand All @@ -70,12 +72,16 @@ TEST_F(TStaticArrayTest, clear_Test) {

TEST_F(TStaticArrayTest, access_string_Test) {
TStaticArray <std::string, 4> arr;
arr[0] = std::string("bla");
arr[1] = std::string("bla");
arr[2] = std::string("bla");
arr[3] = std::string("huhu");
arr[0] = std::string("str_0");
arr[1] = std::string("str_1");
arr[2] = std::string("str_2");
arr[3] = std::string("str_3");

EXPECT_EQ( 4u, arr.size());
EXPECT_EQ("str_0", arr[0]);
EXPECT_EQ("str_1", arr[1]);
EXPECT_EQ("str_2", arr[2]);
EXPECT_EQ("str_3", arr[3]);
}

TEST_F(TStaticArrayTest, string_Test) {
Expand Down