Line data Source code
1 : // Copyright 2017 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/zone/zone-allocator.h"
6 : #include "testing/gtest/include/gtest/gtest.h"
7 :
8 : namespace v8 {
9 : namespace internal {
10 :
11 15443 : TEST(RecyclingZoneAllocator, ReuseSameSize) {
12 2 : AccountingAllocator accounting_allocator;
13 2 : Zone zone(&accounting_allocator, ZONE_NAME);
14 : RecyclingZoneAllocator<int> zone_allocator(&zone);
15 :
16 1 : int* allocated = zone_allocator.allocate(10);
17 : zone_allocator.deallocate(allocated, 10);
18 1 : CHECK_EQ(zone_allocator.allocate(10), allocated);
19 1 : }
20 :
21 15443 : TEST(RecyclingZoneAllocator, ReuseSmallerSize) {
22 2 : AccountingAllocator accounting_allocator;
23 2 : Zone zone(&accounting_allocator, ZONE_NAME);
24 : RecyclingZoneAllocator<int> zone_allocator(&zone);
25 :
26 1 : int* allocated = zone_allocator.allocate(100);
27 : zone_allocator.deallocate(allocated, 100);
28 1 : CHECK_EQ(zone_allocator.allocate(10), allocated);
29 1 : }
30 :
31 15443 : TEST(RecyclingZoneAllocator, DontReuseTooSmallSize) {
32 2 : AccountingAllocator accounting_allocator;
33 2 : Zone zone(&accounting_allocator, ZONE_NAME);
34 : RecyclingZoneAllocator<int> zone_allocator(&zone);
35 :
36 : // The sizeof(FreeBlock) will be larger than a single int, so we can't keep
37 : // store the free list in the deallocated block.
38 1 : int* allocated = zone_allocator.allocate(1);
39 : zone_allocator.deallocate(allocated, 1);
40 1 : CHECK_NE(zone_allocator.allocate(1), allocated);
41 1 : }
42 :
43 15443 : TEST(RecyclingZoneAllocator, ReuseMultipleSize) {
44 2 : AccountingAllocator accounting_allocator;
45 2 : Zone zone(&accounting_allocator, ZONE_NAME);
46 : RecyclingZoneAllocator<int> zone_allocator(&zone);
47 :
48 1 : int* allocated1 = zone_allocator.allocate(10);
49 1 : int* allocated2 = zone_allocator.allocate(20);
50 1 : int* allocated3 = zone_allocator.allocate(30);
51 : zone_allocator.deallocate(allocated1, 10);
52 : zone_allocator.deallocate(allocated2, 20);
53 : zone_allocator.deallocate(allocated3, 30);
54 1 : CHECK_EQ(zone_allocator.allocate(10), allocated3);
55 1 : CHECK_EQ(zone_allocator.allocate(10), allocated2);
56 1 : CHECK_EQ(zone_allocator.allocate(10), allocated1);
57 1 : }
58 :
59 15443 : TEST(RecyclingZoneAllocator, DontChainSmallerSizes) {
60 2 : AccountingAllocator accounting_allocator;
61 2 : Zone zone(&accounting_allocator, ZONE_NAME);
62 : RecyclingZoneAllocator<int> zone_allocator(&zone);
63 :
64 1 : int* allocated1 = zone_allocator.allocate(10);
65 1 : int* allocated2 = zone_allocator.allocate(5);
66 1 : int* allocated3 = zone_allocator.allocate(10);
67 : zone_allocator.deallocate(allocated1, 10);
68 : zone_allocator.deallocate(allocated2, 5);
69 : zone_allocator.deallocate(allocated3, 10);
70 1 : CHECK_EQ(zone_allocator.allocate(5), allocated3);
71 1 : CHECK_EQ(zone_allocator.allocate(5), allocated1);
72 1 : CHECK_NE(zone_allocator.allocate(5), allocated2);
73 1 : }
74 :
75 : } // namespace internal
76 9264 : } // namespace v8
|