aboutsummaryrefslogtreecommitdiff
path: root/contrib/compiler-rt/lib/tsan/tests/unit/tsan_dense_alloc_test.cc
blob: e848e48c6641d3c170befc892fd9761d2fbe3675 (plain) (blame)
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
//===-- tsan_dense_alloc_test.cc ------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of ThreadSanitizer (TSan), a race detector.
//
//===----------------------------------------------------------------------===//
#include "tsan_dense_alloc.h"
#include "tsan_rtl.h"
#include "tsan_mman.h"
#include "gtest/gtest.h"

#include <stdlib.h>
#include <stdint.h>
#include <map>

namespace __tsan {

TEST(DenseSlabAlloc, Basic) {
  typedef DenseSlabAlloc<int, 128, 128> Alloc;
  typedef Alloc::Cache Cache;
  typedef Alloc::IndexT IndexT;
  const int N = 1000;

  Alloc alloc;
  Cache cache;
  alloc.InitCache(&cache);

  IndexT blocks[N];
  for (int ntry = 0; ntry < 3; ntry++) {
    for (int i = 0; i < N; i++) {
      IndexT idx = alloc.Alloc(&cache);
      blocks[i] = idx;
      EXPECT_NE(idx, 0U);
      int *v = alloc.Map(idx);
      *v = i;
    }

    for (int i = 0; i < N; i++) {
      IndexT idx = blocks[i];
      int *v = alloc.Map(idx);
      EXPECT_EQ(*v, i);
      alloc.Free(&cache, idx);
    }

    alloc.FlushCache(&cache);
  }
}

}  // namespace __tsan