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

Commit a964c7a

Browse files
authored
Use matched allocation/deallocation routines. (#816)
Fixes an AddressSanitizer error in the test.
1 parent 7f7c2ba commit a964c7a

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

coreneuron/permute/cellorder.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void copy_array(T*& dest, T* src, size_t n) {
116116
// copy src array to dest with NRN_SOA_BYTE_ALIGN ecalloc_align allocation
117117
template <typename T>
118118
void copy_align_array(T*& dest, T* src, size_t n) {
119-
dest = (T*) ecalloc_align(n, sizeof(T));
119+
dest = static_cast<T*>(ecalloc_align(n, sizeof(T)));
120120
std::copy(src, src + n, dest);
121121
}
122122

coreneuron/permute/cellorder1.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,7 @@ static void admin1(int ncell,
532532

533533
// this vector is used to move from one compartment to the other (per cell)
534534
// its length is equal to the cell with the highest number of compartments
535-
stride = (int*) ecalloc_align(nstride + 1, sizeof(int));
536-
for (int i = 0; i <= nstride; ++i) {
537-
stride[i] = 0;
538-
}
535+
stride = static_cast<int*>(ecalloc_align(nstride + 1, sizeof(int)));
539536
for (size_t i = ncell; i < nodevec.size(); ++i) {
540537
TNode& nd = *nodevec[i];
541538
// compute how many compartments with the same order

coreneuron/utils/memory.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,13 @@ inline int soa_padded_size(int cnt, int layout) {
213213

214214
/** Check for the pointer alignment.
215215
*/
216-
inline bool is_aligned(void* pointer, size_t alignment) {
217-
return (((uintptr_t)(const void*) (pointer)) % (alignment) == 0);
216+
inline bool is_aligned(void* pointer, std::size_t alignment) {
217+
return (reinterpret_cast<std::uintptr_t>(pointer) % alignment) == 0;
218218
}
219219

220-
/** Allocate the aligned memory.
220+
/**
221+
* Allocate aligned memory. This will be unified memory if the corresponding
222+
* CMake option is set. This must be freed with the free_memory method.
221223
*/
222224
inline void* emalloc_align(size_t size, size_t alignment = NRN_SOA_BYTE_ALIGN) {
223225
void* memptr;
@@ -226,7 +228,10 @@ inline void* emalloc_align(size_t size, size_t alignment = NRN_SOA_BYTE_ALIGN) {
226228
return memptr;
227229
}
228230

229-
/** Allocate the aligned memory and set it to 0.
231+
/**
232+
* Allocate the aligned memory and set it to 0. This will be unified memory if
233+
* the corresponding CMake option is set. This must be freed with the
234+
* free_memory method.
230235
*/
231236
inline void* ecalloc_align(size_t n, size_t size, size_t alignment = NRN_SOA_BYTE_ALIGN) {
232237
void* p;

tests/unit/interleave_info/check_constructors.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ BOOST_AUTO_TEST_CASE(interleave_info_test) {
2525
info1.nstride = nstride;
2626

2727
// to avoid same values, different sub-array is used to initialize different members
28-
copy_array(info1.stridedispl, data1, nwarp + 1);
29-
copy_array(info1.stride, data1 + 1, nstride);
30-
copy_array(info1.firstnode, data1 + 1, nwarp + 1);
31-
copy_array(info1.lastnode, data1 + 1, nwarp + 1);
28+
copy_align_array(info1.stridedispl, data1, nwarp + 1);
29+
copy_align_array(info1.stride, data1 + 1, nstride);
30+
copy_align_array(info1.firstnode, data1 + 1, nwarp + 1);
31+
copy_align_array(info1.lastnode, data1 + 1, nwarp + 1);
3232

3333
// check if copy_array works
3434
BOOST_CHECK_NE(info1.firstnode, info1.lastnode);
@@ -37,7 +37,7 @@ BOOST_AUTO_TEST_CASE(interleave_info_test) {
3737
info1.lastnode,
3838
info1.lastnode + nwarp + 1);
3939

40-
copy_array(info1.cellsize, data1 + 4, nwarp);
40+
copy_align_array(info1.cellsize, data1 + 4, nwarp);
4141
copy_array(info1.nnode, data2, nwarp);
4242
copy_array(info1.ncycle, data2 + 1, nwarp);
4343
copy_array(info1.idle, data2 + 2, nwarp);

0 commit comments

Comments
 (0)