alloc_test.cc 6.17 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
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 176
 *
 * 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 <alloc_test.h>
#include <embb/base/c/memory_allocation.h>
#include <cassert>
#include <memory.h>

namespace embb {
namespace base {
namespace test {

AllocTest::AllocTest()
    : allocated_(partest::TestSuite::GetDefaultNumThreads()) {
  CreateUnit("Non-aligned")
    .Add(&AllocTest::TestNonAlignedAlloc, this,
         partest::TestSuite::GetDefaultNumThreads())
    .Post(&AllocTest::PostNonAligned, this);

  CreateUnit("Aligned")
    .Add(&AllocTest::TestAlignedAlloc, this,
         partest::TestSuite::GetDefaultNumThreads())
    .Post(&AllocTest::PostAligned, this);

  CreateUnit("Cache aligned")
    .Add(&AllocTest::TestCacheAlignedAlloc, this,
         partest::TestSuite::GetDefaultNumThreads())
    .Post(&AllocTest::PostCacheAligned, this);

  CreateUnit("AccessAllocatedMemory")
    .Add(&AllocTest::TestAccessAllocatedMemory, this)
    .Post(&AllocTest::PostAccessAllocatedMemory, this);

  CreateUnit("Mixed").Add(&AllocTest::TestMixedAllocs, this);
}

void AllocTest::TestAccessAllocatedMemory() {
  static const unsigned int memory_allocation_iterations = 1000;

  ::std::vector<void *> allocs_aligned;
  ::std::vector<void *> allocs_unaligned;

  for (unsigned int i = 0; i != memory_allocation_iterations; ++i) {
    allocs_aligned.push_back(embb_alloc_cache_aligned(8));
    allocs_unaligned.push_back(embb_alloc(8));
  }

  for (unsigned int i = 0; i != memory_allocation_iterations; ++i) {
    memcpy(allocs_aligned[i], &i, sizeof(int));
    memcpy(allocs_unaligned[i], &i, sizeof(int));
  }

  for (unsigned int i = 0; i != memory_allocation_iterations; ++i) {
    unsigned int alloc_aligned_test;
    unsigned int alloc_unaligned_test;
    memcpy(&alloc_aligned_test, allocs_aligned[i], sizeof(i));
    memcpy(&alloc_unaligned_test, allocs_unaligned[i], sizeof(i));

    PT_EXPECT_EQ(alloc_aligned_test, i);
    if (alloc_aligned_test != i) {
      PT_ASSERT(false);
    }
    if (alloc_unaligned_test != i) {
      PT_ASSERT(false);
    }
  }

  for (unsigned int i = 0; i != memory_allocation_iterations; ++i) {
    embb_free(allocs_unaligned[i]);
    embb_free_aligned(allocs_aligned[i]);
  }
}

void AllocTest::PostAccessAllocatedMemory() {
  size_t left = embb_get_bytes_allocated();
  PT_EXPECT_EQ(left, (size_t)0);
}

void AllocTest::TestNonAlignedAlloc() {
  size_t thread_num =
      partest::TestSuite::GetCurrentThreadID();
  assert(thread_num < allocated_.size());
  allocated_[thread_num] = NULL;
  allocated_[thread_num] = embb_alloc(1);
  PT_EXPECT_NE(allocated_[thread_num], static_cast<void*>(NULL));
  embb_free(allocated_[thread_num]);
}

void AllocTest::PostNonAligned() {
  size_t left = embb_get_bytes_allocated();
  PT_EXPECT_EQ(left, (size_t)0);
}

void AllocTest::TestAlignedAlloc() {
  size_t thread_num =
      partest::TestSuite::GetCurrentThreadID();
  assert(thread_num < allocated_.size());
  allocated_[thread_num] = NULL;
  allocated_[thread_num] = embb_alloc_aligned(2*sizeof(void*), 1);
  PT_EXPECT_NE(allocated_[thread_num], static_cast<void*>(NULL));
  embb_free_aligned(allocated_[thread_num]);
}

void AllocTest::PostAligned() {
  size_t left = embb_get_bytes_allocated();
  PT_EXPECT_EQ(left, (size_t)0);
}

void AllocTest::TestCacheAlignedAlloc() {
  size_t thread_num =
      partest::TestSuite::GetCurrentThreadID();
  assert(thread_num < allocated_.size());
  allocated_[thread_num] = NULL;
  allocated_[thread_num] = embb_alloc_cache_aligned(1);
  PT_EXPECT_NE(allocated_[thread_num], static_cast<void*>(NULL));
  embb_free_aligned(allocated_[thread_num]);
}

void AllocTest::PostCacheAligned() {
  size_t left = embb_get_bytes_allocated();
  PT_EXPECT_EQ(left, (size_t)0);
}

void AllocTest::TestMixedAllocs() {
  size_t expected = 0;
  size_t allocated = 0;

  // Plain memory allocation
  void* plain = NULL;
  plain = embb_alloc(2);
  PT_EXPECT_NE(plain, static_cast<void*>(NULL));
  allocated = embb_get_bytes_allocated();
#ifdef EMBB_DEBUG
  expected += 2 + 2*sizeof(size_t);
#endif // else EMBB_DEBUG
  PT_EXPECT_EQ(allocated, expected);

  // Aligned memory allocation
  void* aligned = NULL;
  aligned = embb_alloc_aligned(2*sizeof(void*), 2);
  PT_EXPECT_NE(aligned, static_cast<void*>(NULL));
  allocated = embb_get_bytes_allocated();
#ifdef EMBB_DEBUG
  expected += (1 + 1) * 2 * sizeof(void*) + 3 * sizeof(size_t) - 1;
#endif // else EMBB_DEBUG
  PT_EXPECT_EQ(allocated, expected);

  // Cache-aligned memory allocation
  void* cache_aligned = NULL;
  cache_aligned = embb_alloc_cache_aligned(2);
  PT_EXPECT_NE(cache_aligned, static_cast<void*>(NULL));
  allocated = embb_get_bytes_allocated();
#ifdef EMBB_DEBUG
177
  expected += (1 + 1) * EMBB_PLATFORM_CACHE_LINE_SIZE + 3 * sizeof(size_t) - 1;
178 179
#endif // else EMBB_DEBUG
  PT_EXPECT_EQ(allocated, expected);
180 181 182 183 184 185

  embb_free(plain);
  embb_free_aligned(aligned);
  embb_free_aligned(cache_aligned);

  PT_EXPECT(embb_get_bytes_allocated() == 0);
186 187 188 189 190
}

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