diff options
author | Erik Schnetter <schnetter@gmail.com> | 2013-04-27 00:16:44 -0400 |
---|---|---|
committer | Erik Schnetter <schnetter@gmail.com> | 2013-04-27 00:16:44 -0400 |
commit | 9b4e5841e2f30bded4cfed65e37c0ff2393250e8 (patch) | |
tree | d9f89e953cd7dacc3caacaf70246bbab6af5edd9 /Carpet/CarpetLib/src/mem.cc | |
parent | fa53c27c1de5d1afd5bc9d904e8a1858b06bebc4 (diff) |
CarpetLib: Correct alignment mechanism when allocating memory
Handle the case where new returns memory with an alignment less than the type's size.
Diffstat (limited to 'Carpet/CarpetLib/src/mem.cc')
-rw-r--r-- | Carpet/CarpetLib/src/mem.cc | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/Carpet/CarpetLib/src/mem.cc b/Carpet/CarpetLib/src/mem.cc index 89d8fb211..df374ae3e 100644 --- a/Carpet/CarpetLib/src/mem.cc +++ b/Carpet/CarpetLib/src/mem.cc @@ -105,6 +105,7 @@ mem (size_t const vectorlength, size_t const nelems, size_t const max_cache_linesize = get_max_cache_linesize(); size_t const vector_size = CCTK_REAL_VEC_SIZE * sizeof(T); size_t const alignment = align_up(max_cache_linesize, vector_size); + assert(alignment >= 1); // Safety check assert(alignment <= 1024); // Assume optimistically that operator new returns well-aligned @@ -123,7 +124,8 @@ mem (size_t const vectorlength, size_t const nelems, } else { allocate_with_alignment: // Operator new needs manual alignment - size_t const max_padding = alignment / sizeof(T) - 1; + size_t const max_padding = div_up(alignment, sizeof(T)); + assert(ptrdiff_t(max_padding) >= 0); storage_base_ = new T [vectorlength * nelems + max_padding]; storage_ = (T*) (size_t(storage_base_ + max_padding) & ~(alignment-1)); assert(size_t(storage_) >= size_t(storage_base_ ) and |