Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Replace posix_memalign with std::aligned_alloc #859

Merged
merged 2 commits into from
Sep 19, 2022
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
13 changes: 7 additions & 6 deletions coreneuron/utils/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,15 @@ class MemoryManaged {
// does nothing by default
};

#include <stdlib.h>
#include <cstdlib>

inline void alloc_memory(void*& pointer, size_t num_bytes, size_t alignment) {
#if defined(MINGW)
nrn_assert((pointer = _aligned_malloc(num_bytes, alignment)) != nullptr);
#else
nrn_assert(posix_memalign(&pointer, alignment, num_bytes) == 0);
#endif
size_t fill = 0;
if (num_bytes % alignment != 0) {
size_t multiple = num_bytes / alignment;
fill = alignment * (multiple + 1) - num_bytes;
}
nrn_assert((pointer = std::aligned_alloc(alignment, num_bytes + fill)) != nullptr);
}

inline void calloc_memory(void*& pointer, size_t num_bytes, size_t alignment) {
Expand Down
9 changes: 1 addition & 8 deletions coreneuron/utils/nrnoc_aux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,7 @@ void* erealloc(void* ptr, size_t size) {
}

void* nrn_cacheline_alloc(void** memptr, size_t size) {
#if HAVE_MEMALIGN
if (posix_memalign(memptr, 64, size) != 0) {
fprintf(stderr, "posix_memalign not working\n");
assert(0);
}
#else
*memptr = emalloc(size);
#endif
alloc_memory(*memptr, size, 64);
return *memptr;
}

Expand Down