summaryrefslogtreecommitdiff
path: root/libc/test/src/__support/freestore_test.cpp
blob: 39292b6a1211be8a4ea0184a7aa9c5f26e034201 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//===-- Unittests for a freestore -------------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include <stddef.h>

#include "src/__support/freestore.h"
#include "test/UnitTest/Test.h"

using LIBC_NAMESPACE::Block;
using LIBC_NAMESPACE::FreeList;
using LIBC_NAMESPACE::FreeStore;
using LIBC_NAMESPACE::FreeTrie;
using LIBC_NAMESPACE::cpp::byte;
using LIBC_NAMESPACE::cpp::optional;

// Inserting or removing blocks too small to be tracked does nothing.
TEST(LlvmLibcFreeStore, TooSmall) {
  byte mem[1024];
  optional<Block *> maybeBlock = Block::init(mem);
  ASSERT_TRUE(maybeBlock.has_value());
  Block *too_small = *maybeBlock;
  maybeBlock = too_small->split(Block::PREV_FIELD_SIZE);
  ASSERT_TRUE(maybeBlock.has_value());
  // On platforms with high alignment the smallest legal block may be large
  // enough for a node.
  if (too_small->outer_size() >= sizeof(Block) + sizeof(FreeList::Node))
    return;
  Block *remainder = *maybeBlock;

  FreeStore store;
  store.set_range({0, 4096});
  store.insert(too_small);
  store.insert(remainder);

  EXPECT_EQ(store.remove_best_fit(too_small->inner_size()), remainder);
  store.remove(too_small);
}

TEST(LlvmLibcFreeStore, RemoveBestFit) {
  byte mem[1024];
  optional<Block *> maybeBlock = Block::init(mem);
  ASSERT_TRUE(maybeBlock.has_value());

  Block *smallest = *maybeBlock;
  maybeBlock = smallest->split(sizeof(FreeList::Node) + Block::PREV_FIELD_SIZE);
  ASSERT_TRUE(maybeBlock.has_value());

  Block *largest_small = *maybeBlock;
  maybeBlock = largest_small->split(
      sizeof(FreeTrie::Node) + Block::PREV_FIELD_SIZE - alignof(max_align_t));
  ASSERT_TRUE(maybeBlock.has_value());
  if (largest_small->inner_size() == smallest->inner_size())
    largest_small = smallest;
  ASSERT_GE(largest_small->inner_size(), smallest->inner_size());

  Block *remainder = *maybeBlock;

  FreeStore store;
  store.set_range({0, 4096});
  store.insert(smallest);
  if (largest_small != smallest)
    store.insert(largest_small);
  store.insert(remainder);

  // Find exact match for smallest.
  ASSERT_EQ(store.remove_best_fit(smallest->inner_size()), smallest);
  store.insert(smallest);

  // Find exact match for largest.
  ASSERT_EQ(store.remove_best_fit(largest_small->inner_size()), largest_small);
  store.insert(largest_small);

  // Search small list for best fit.
  Block *next_smallest = largest_small == smallest ? remainder : largest_small;
  ASSERT_EQ(store.remove_best_fit(smallest->inner_size() + 1), next_smallest);
  store.insert(next_smallest);

  // Continue search for best fit to large blocks.
  EXPECT_EQ(store.remove_best_fit(largest_small->inner_size() + 1), remainder);
}

TEST(LlvmLibcFreeStore, Remove) {
  byte mem[1024];
  optional<Block *> maybeBlock = Block::init(mem);
  ASSERT_TRUE(maybeBlock.has_value());

  Block *small = *maybeBlock;
  maybeBlock = small->split(sizeof(FreeList::Node) + Block::PREV_FIELD_SIZE);
  ASSERT_TRUE(maybeBlock.has_value());

  Block *remainder = *maybeBlock;

  FreeStore store;
  store.set_range({0, 4096});
  store.insert(small);
  store.insert(remainder);

  store.remove(remainder);
  ASSERT_EQ(store.remove_best_fit(remainder->inner_size()),
            static_cast<Block *>(nullptr));
  store.remove(small);
  ASSERT_EQ(store.remove_best_fit(small->inner_size()),
            static_cast<Block *>(nullptr));
}