memory_allocation_test.cc 5.94 KB
Newer Older
Michael Schmid committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
/*
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <memory_allocation_test.h>
#include <stdint.h>

namespace embb {
namespace base {
namespace test {

MemoryAllocationTest::MemoryAllocationTest() {
  CreateUnit("ClassAllocationTest")
    .Add(&MemoryAllocationTest::ClassAllocationTest, this);
  CreateUnit("AllocatorTest").Add(&MemoryAllocationTest::AllocatorTest, this);
}

void MemoryAllocationTest::AllocatorTest() {
  static const int alloc_iterations = 10000;

  ::std::vector < DummyClassForAllocatorTests,
    embb::base::AllocatorCacheAligned< DummyClassForAllocatorTests > >
    aligned_allocating_vector;

  ::std::vector < DummyClassForAllocatorTests,
    embb::base::Allocator< DummyClassForAllocatorTests > >
    unaligned_allocating_vector;

  for (unsigned int i = 0;
    i != static_cast<unsigned int>(alloc_iterations);
    ++i) {
    DummyClassForAllocatorTests d;
    d.A = 0xF10000 | i;
    d.F = 0xF20000 | i;
    aligned_allocating_vector.push_back(d);
    unaligned_allocating_vector.push_back(d);
  }

  for (unsigned int i = 0;
    i != static_cast<unsigned int>(alloc_iterations);
    ++i) {
    unsigned int A = 0xF10000 | i;
    unsigned int F = 0xF20000 | i;
    PT_ASSERT_EQ(aligned_allocating_vector[i].A, A);
    PT_ASSERT_EQ(aligned_allocating_vector[i].F, F);
    PT_ASSERT_EQ(unaligned_allocating_vector[i].A, A);
    PT_ASSERT_EQ(unaligned_allocating_vector[i].F, F);
  }
}

void MemoryAllocationTest::ClassAllocationTest() {
  ::std::vector< DummyClassForAlignedAllocation* > aligned_allocs;
  ::std::vector< DummyClassForUnalignedAllocation* > unaligned_allocs;

  size_t expected = 0;

  static const unsigned int alloc_iterations = 10;

  for (unsigned int i = 0; i != alloc_iterations; ++i) {
    aligned_allocs.push_back( new DummyClassForAlignedAllocation );
    //write something
    aligned_allocs[i]->b = 0xF000 | i;
#ifdef EMBB_DEBUG
    size_t n = (sizeof(DummyClassForAlignedAllocation)
      + (EMBB_PLATFORM_CACHE_LINE_SIZE - 1)) / EMBB_PLATFORM_CACHE_LINE_SIZE;
    expected += (n + 1)*EMBB_PLATFORM_CACHE_LINE_SIZE +
      (sizeof(size_t) * 3 - 1);
#endif // else EMBB_DEBUG

    //check that the memory is aligned!
    PT_ASSERT_EQ((uintptr_t)aligned_allocs[i] % EMBB_PLATFORM_CACHE_LINE_SIZE,
      (uintptr_t)0);
  }

  PT_ASSERT_EQ(embb::base::Allocation::AllocatedBytes(), expected);

  //delete!
  expected = 0;
  for (unsigned int i = 0; i != alloc_iterations; ++i) {
    //check if written correctly
    PT_ASSERT_EQ(aligned_allocs[i]->b, 0xF000 | i);
    delete aligned_allocs[i];
  }

  //everything should be deleted
  PT_ASSERT_EQ(embb::base::Allocation::AllocatedBytes(), expected);

  for (unsigned int i = 0; i != alloc_iterations; ++i) {
    unaligned_allocs.push_back(new DummyClassForUnalignedAllocation);
    //write something
    unaligned_allocs[i]->b = 0xA000 | i;


#ifdef EMBB_DEBUG
    expected += sizeof(DummyClassForUnalignedAllocation) + 2 * sizeof(size_t);
#endif // else EMBB_DEBUG
  }

  PT_ASSERT_EQ(embb::base::Allocation::AllocatedBytes(), expected);

  //delete!
  expected = 0;
  for (unsigned int i = 0; i != alloc_iterations; ++i) {
    //check if written correctly
    PT_ASSERT_EQ(unaligned_allocs[i]->b, 0xA000 | i);
    delete unaligned_allocs[i];
  }

  //everything should be deleted
  PT_ASSERT_EQ(embb::base::Allocation::AllocatedBytes(), expected);


  DummyClassForAlignedAllocation * aligned_allocated =
    new DummyClassForAlignedAllocation[alloc_iterations];
#ifdef EMBB_DEBUG
    size_t n = (sizeof(DummyClassForAlignedAllocation)*alloc_iterations
      + (EMBB_PLATFORM_CACHE_LINE_SIZE - 1)) / EMBB_PLATFORM_CACHE_LINE_SIZE;
    expected += (n + 1)*EMBB_PLATFORM_CACHE_LINE_SIZE +
      (sizeof(size_t) * 3 - 1);
#endif // else EMBB_DEBUG

    // This assert does _not_ hold, but is left for documentation.
    // It is not guaranteed that the pointer to the array is aligned.
    // See the documentation of the overloaded new[] operator in
    // class MemoryAllocation.
    // PT_ASSERT_EQ((uintptr_t)aligned_allocated %
    //   EMBB_PLATFORM_CACHE_LINE_SIZE, 0);

    //delete!
    expected = 0;
    delete[] aligned_allocated;

    PT_ASSERT_EQ(embb::base::Allocation::AllocatedBytes(), expected);

    DummyClassForUnalignedAllocation * unaligned_allocated =
      new DummyClassForUnalignedAllocation[alloc_iterations];
#ifdef EMBB_DEBUG
    expected += sizeof(DummyClassForUnalignedAllocation)*alloc_iterations
      + 2 * sizeof(size_t);
#endif // else EMBB_DEBUG

    PT_ASSERT(embb::base::Allocation::AllocatedBytes() >= expected);

    //delete!
    expected = 0;
    delete[] unaligned_allocated;

    PT_ASSERT_EQ(embb::base::Allocation::AllocatedBytes(), expected);
}

} // namespace test
} // namespace base
} // namespace embb