1 //===-- tsan_dense_alloc_test.cc ------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "tsan_dense_alloc.h"
14 #include "tsan_rtl.h"
15 #include "tsan_mman.h"
16 #include "gtest/gtest.h"
17 
18 #include <stdlib.h>
19 #include <stdint.h>
20 #include <map>
21 
22 namespace __tsan {
23 
TEST(DenseSlabAlloc,Basic)24 TEST(DenseSlabAlloc, Basic) {
25   typedef DenseSlabAlloc<int, 128, 128> Alloc;
26   typedef Alloc::Cache Cache;
27   typedef Alloc::IndexT IndexT;
28   const int N = 1000;
29 
30   Alloc alloc;
31   Cache cache;
32   alloc.InitCache(&cache);
33 
34   IndexT blocks[N];
35   for (int ntry = 0; ntry < 3; ntry++) {
36     for (int i = 0; i < N; i++) {
37       IndexT idx = alloc.Alloc(&cache);
38       blocks[i] = idx;
39       EXPECT_NE(idx, 0U);
40       int *v = alloc.Map(idx);
41       *v = i;
42     }
43 
44     for (int i = 0; i < N; i++) {
45       IndexT idx = blocks[i];
46       int *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