summaryrefslogtreecommitdiff
path: root/lib/tsan/tests/unit/tsan_dense_alloc_test.cc
blob: 8c544b0d737c96b70befb4d38364b6a75ca22f3c (plain)
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
//===-- tsan_dense_alloc_test.cc ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// 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