xref: /llvm-project/compiler-rt/lib/tsan/tests/unit/tsan_dense_alloc_test.cpp (revision aff73487c986b519aac1e0b7fee6d7bef72e4002)
1 //===-- tsan_dense_alloc_test.cpp -----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 //
11 //===----------------------------------------------------------------------===//
12 #include "tsan_dense_alloc.h"
13 #include "tsan_rtl.h"
14 #include "tsan_mman.h"
15 #include "gtest/gtest.h"
16 
17 #include <stdlib.h>
18 #include <stdint.h>
19 #include <map>
20 
21 namespace __tsan {
22 
TEST(DenseSlabAlloc,Basic)23 TEST(DenseSlabAlloc, Basic) {
24   typedef u64 T;
25   typedef DenseSlabAlloc<T, 128, 128> Alloc;
26   typedef Alloc::Cache Cache;
27   typedef Alloc::IndexT IndexT;
28   const T N = 1000;
29 
30   Alloc alloc("test");
31   Cache cache;
32   alloc.InitCache(&cache);
33 
34   IndexT blocks[N];
35   for (int ntry = 0; ntry < 3; ntry++) {
36     for (T i = 0; i < N; i++) {
37       IndexT idx = alloc.Alloc(&cache);
38       blocks[i] = idx;
39       EXPECT_NE(idx, 0U);
40       T *v = alloc.Map(idx);
41       *v = i;
42     }
43 
44     for (T i = 0; i < N; i++) {
45       IndexT idx = blocks[i];
46       T *v = alloc.Map(idx);
47       EXPECT_EQ(*v, i);
48       alloc.Free(&cache, idx);
49     }
50 
51     alloc.FlushCache(&cache);
52   }
53 }
54 
55 }  // namespace __tsan
56