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 
23 TEST(DenseSlabAlloc, Basic) {
24   typedef DenseSlabAlloc<int, 128, 128> Alloc;
25   typedef Alloc::Cache Cache;
26   typedef Alloc::IndexT IndexT;
27   const int N = 1000;
28 
29   Alloc alloc;
30   Cache cache;
31   alloc.InitCache(&cache);
32 
33   IndexT blocks[N];
34   for (int ntry = 0; ntry < 3; ntry++) {
35     for (int i = 0; i < N; i++) {
36       IndexT idx = alloc.Alloc(&cache);
37       blocks[i] = idx;
38       EXPECT_NE(idx, 0U);
39       int *v = alloc.Map(idx);
40       *v = i;
41     }
42 
43     for (int i = 0; i < N; i++) {
44       IndexT idx = blocks[i];
45       int *v = alloc.Map(idx);
46       EXPECT_EQ(*v, i);
47       alloc.Free(&cache, idx);
48     }
49 
50     alloc.FlushCache(&cache);
51   }
52 }
53 
54 }  // namespace __tsan
55