-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement __aligned_malloc() and __aligned_free() and fixes on LE3.1(#59
) * implement __aligned_malloc() and __aligned_free() Call posix_memalign() if building with `-mzos-target=zosv3r1` on z/OS 3.1, in which case `__aligned_free()` calls `free()`. Otherwise use a local implementation to align memory from `malloc()`. * fix include_next typo On LE 3.1 zos-io.cc fails to pick up LOCK_* definitions from the system's sys/file.h. * fix conflicts with system's time.h __clockid_t is defined in sys/types.h if >= v2r5, CLOCK_REALTIME and CLOCK_MONOTONIC are already defined in the system's time.h if >= v2r5.
- Loading branch information
Showing
5 changed files
with
131 additions
and
8 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,65 @@ | ||
#include "zos.h" | ||
#include "gtest/gtest.h" | ||
|
||
#include <math.h> | ||
#include <unistd.h> | ||
|
||
namespace { | ||
|
||
constexpr int KB = 1024; | ||
constexpr int MB = KB * 1024; | ||
|
||
#if (__TARGET_LIB__ >= 0x43010000) | ||
#define AlignedAlloc posix_memalign | ||
#else | ||
#define AlignedAlloc AlignedAlloc | ||
#endif | ||
|
||
TEST(AlignedAlloc, TestOne) { | ||
size_t alignment = sysconf(_SC_PAGESIZE); | ||
size_t size = 123; | ||
void *ptr = __aligned_malloc(size, alignment); | ||
ASSERT_NE(ptr, nullptr); | ||
ASSERT_EQ(reinterpret_cast<size_t>(ptr) % alignment, 0); | ||
__aligned_free(ptr); | ||
} | ||
|
||
TEST(AlignedAlloc, TestTwo) { | ||
size_t alignment; | ||
size_t size = 4096; | ||
void *ptr; | ||
for (int i=3; i<=30; i++) { | ||
alignment = powl(2, i); | ||
ASSERT_EQ(alignment % sizeof(void*), 0); | ||
ptr = __aligned_malloc(size, alignment); | ||
ASSERT_NE(ptr, nullptr); | ||
ASSERT_EQ(reinterpret_cast<size_t>(ptr) % alignment, 0); | ||
__aligned_free(ptr); | ||
} | ||
} | ||
|
||
TEST(AlignedAlloc, TestThree) { | ||
size_t alignment; | ||
void *ptr; | ||
for (int i=3; i<=20; i++) { | ||
alignment = powl(2, i); | ||
ASSERT_EQ(alignment % sizeof(void*), 0); | ||
for (size_t size=1; size <= MB; size+=10) { | ||
ptr = __aligned_malloc(size, alignment); | ||
ASSERT_NE(ptr, nullptr); | ||
ASSERT_EQ(reinterpret_cast<size_t>(ptr) % alignment, 0); | ||
__aligned_free(ptr); | ||
} | ||
} | ||
} | ||
|
||
TEST(AlignedAlloc, TestFour) { | ||
size_t alignment = 0; | ||
size_t size = 4096; | ||
void *ptr; | ||
ptr = __aligned_malloc(size, alignment); | ||
ASSERT_NE(ptr, nullptr); | ||
__aligned_free(ptr); | ||
} | ||
|
||
} // namespace |