arena-test.c++ 9.9 KB
Newer Older
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
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "arena.h"
#include "debug.h"
Kenton Varda's avatar
Kenton Varda committed
26
#include "thread.h"
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 110 111 112 113 114 115 116 117
#include <gtest/gtest.h>
#include <stdint.h>

namespace kj {
namespace {

struct TestObject {
  TestObject() {
    index = count;
    KJ_ASSERT(index != throwAt);
    ++count;
  }
  TestObject(const TestObject& other) {
    KJ_ASSERT(other.index != throwAt);
    index = -1;
    copiedCount++;
  }
  ~TestObject() noexcept(false) {
    if (index == -1) {
      --copiedCount;
    } else {
      --count;
      EXPECT_EQ(index, count);
      KJ_ASSERT(count != throwAt);
    }
  }

  int index;

  static int count;
  static int copiedCount;
  static int throwAt;
};

int TestObject::count = 0;
int TestObject::copiedCount = 0;
int TestObject::throwAt = -1;

TEST(Arena, Object) {
  TestObject::count = 0;
  TestObject::throwAt = -1;

  {
    Arena arena;

    TestObject& obj1 = arena.allocate<TestObject>();
    TestObject& obj2 = arena.allocate<TestObject>();

    EXPECT_LT(&obj1, &obj2);

    EXPECT_EQ(2, TestObject::count);
  }

  EXPECT_EQ(0, TestObject::count);
}

TEST(Arena, TrivialObject) {
  Arena arena;

  int& i1 = arena.allocate<int>();
  int& i2 = arena.allocate<int>();

  // Trivial objects should be tightly-packed.
  EXPECT_EQ(&i1 + 1, &i2);
}

TEST(Arena, OwnObject) {
  TestObject::count = 0;
  TestObject::throwAt = -1;

  Arena arena;

  {
    Own<TestObject> obj1 = arena.allocateOwn<TestObject>();
    Own<TestObject> obj2 = arena.allocateOwn<TestObject>();
    EXPECT_LT(obj1.get(), obj2.get());

    EXPECT_EQ(2, TestObject::count);
  }

  EXPECT_EQ(0, TestObject::count);
}

TEST(Arena, Array) {
  TestObject::count = 0;
  TestObject::throwAt = -1;

  {
    Arena arena;
    ArrayPtr<TestObject> arr1 = arena.allocateArray<TestObject>(4);
    ArrayPtr<TestObject> arr2 = arena.allocateArray<TestObject>(2);
Kenton Varda's avatar
Kenton Varda committed
118 119
    EXPECT_EQ(4u, arr1.size());
    EXPECT_EQ(2u, arr2.size());
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    EXPECT_LE(arr1.end(), arr2.begin());
    EXPECT_EQ(6, TestObject::count);
  }

  EXPECT_EQ(0, TestObject::count);
}

TEST(Arena, TrivialArray) {
  Arena arena;
  ArrayPtr<int> arr1 = arena.allocateArray<int>(16);
  ArrayPtr<int> arr2 = arena.allocateArray<int>(8);

  // Trivial arrays should be tightly-packed.
  EXPECT_EQ(arr1.end(), arr2.begin());
}

TEST(Arena, OwnArray) {
  TestObject::count = 0;
  TestObject::throwAt = -1;

  Arena arena;

  {
    Array<TestObject> arr1 = arena.allocateOwnArray<TestObject>(4);
    Array<TestObject> arr2 = arena.allocateOwnArray<TestObject>(2);
Kenton Varda's avatar
Kenton Varda committed
145 146
    EXPECT_EQ(4u, arr1.size());
    EXPECT_EQ(2u, arr2.size());
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    EXPECT_LE(arr1.end(), arr2.begin());
    EXPECT_EQ(6, TestObject::count);
  }

  EXPECT_EQ(0, TestObject::count);
}

#ifndef KJ_NO_EXCEPTIONS

TEST(Arena, ObjectThrow) {
  TestObject::count = 0;
  TestObject::throwAt = 1;

  {
    Arena arena;

    arena.allocate<TestObject>();
    EXPECT_ANY_THROW(arena.allocate<TestObject>());
    EXPECT_EQ(1, TestObject::count);
  }

  EXPECT_EQ(0, TestObject::count);
}

TEST(Arena, ArrayThrow) {
  TestObject::count = 0;
  TestObject::throwAt = 2;

  {
    Arena arena;
    EXPECT_ANY_THROW(arena.allocateArray<TestObject>(4));
    EXPECT_EQ(2, TestObject::count);
  }

  EXPECT_EQ(0, TestObject::count);
}

#endif

TEST(Arena, Alignment) {
  Arena arena;

  char& c = arena.allocate<char>();
  long& l = arena.allocate<long>();
  char& c2 = arena.allocate<char>();
  ArrayPtr<char> arr = arena.allocateArray<char>(8);

Kenton Varda's avatar
Kenton Varda committed
194 195 196
  EXPECT_EQ(alignof(long) + sizeof(long), implicitCast<size_t>(&c2 - &c));
  EXPECT_EQ(alignof(long), implicitCast<size_t>(reinterpret_cast<char*>(&l) - &c));
  EXPECT_EQ(sizeof(char), implicitCast<size_t>(arr.begin() - &c2));
197 198 199 200
}

TEST(Arena, EndOfChunk) {
  union {
201
    byte scratch[64];
202 203 204 205
    uint64_t align;
  };
  Arena arena(arrayPtr(scratch, sizeof(scratch)));

206
  // First allocation will come from somewhere in the scratch space (after the chunk header).
207
  uint64_t& i = arena.allocate<uint64_t>();
208 209
  EXPECT_GE(reinterpret_cast<byte*>(&i), scratch);
  EXPECT_LT(reinterpret_cast<byte*>(&i), scratch + sizeof(scratch));
210

211
  // Next allocation will come at the next position.
212
  uint64_t& i2 = arena.allocate<uint64_t>();
213
  EXPECT_EQ(&i + 1, &i2);
214

215 216 217 218 219 220
  // Allocate the rest of the scratch space.
  size_t spaceLeft = scratch + sizeof(scratch) - reinterpret_cast<byte*>(&i2 + 1);
  ArrayPtr<byte> remaining = arena.allocateArray<byte>(spaceLeft);
  EXPECT_EQ(reinterpret_cast<byte*>(&i2 + 1), remaining.begin());

  // Next allocation comes from somewhere new.
221
  uint64_t& i3 = arena.allocate<uint64_t>();
222
  EXPECT_NE(remaining.end(), reinterpret_cast<byte*>(&i3));
223 224 225 226
}

TEST(Arena, EndOfChunkAlignment) {
  union {
227
    byte scratch[34];
228 229 230 231
    uint64_t align;
  };
  Arena arena(arrayPtr(scratch, sizeof(scratch)));

232 233 234 235 236 237 238 239
  // Figure out where we are...
  byte* start = arena.allocateArray<byte>(0).begin();

  // Allocate enough space so that we're 24 bytes into the scratch space.  (On 64-bit systems, this
  // should be zero.)
  arena.allocateArray<byte>(24 - (start - scratch));

  // Allocating a 16-bit integer works.  Now we're at 26 bytes; 8 bytes are left.
240
  uint16_t& i = arena.allocate<uint16_t>();
241
  EXPECT_EQ(scratch + 24, reinterpret_cast<byte*>(&i));
242

243 244
  // Although there is technically enough space to allocate a uint64, it is not aligned correctly,
  // so it will be allocated elsewhere instead.
245 246 247 248 249 250 251 252 253 254 255 256 257 258
  uint64_t& i2 = arena.allocate<uint64_t>();
  EXPECT_TRUE(reinterpret_cast<byte*>(&i2) < scratch ||
              reinterpret_cast<byte*>(&i2) > scratch + sizeof(scratch));
}

TEST(Arena, TooBig) {
  Arena arena(1024);

  byte& b1 = arena.allocate<byte>();

  ArrayPtr<byte> arr = arena.allocateArray<byte>(1024);

  byte& b2 = arena.allocate<byte>();

259 260 261 262 263
  // The array should not have been allocated anywhere near that first byte.
  EXPECT_TRUE(arr.begin() < &b1 || arr.begin() > &b1 + 512);

  // The next byte should have been allocated after the array.
  EXPECT_EQ(arr.end(), &b2);
264 265 266 267 268 269

  // Write to the array to make sure it's valid.
  memset(arr.begin(), 0xbe, arr.size());
}

TEST(Arena, MultiSegment) {
270 271
  // Sorry, this test makes assumptions about the size of ChunkHeader.
  Arena arena(sizeof(void*) == 4 ? 32 : 40);
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

  uint64_t& i1 = arena.allocate<uint64_t>();
  uint64_t& i2 = arena.allocate<uint64_t>();
  uint64_t& i3 = arena.allocate<uint64_t>();

  EXPECT_EQ(&i1 + 1, &i2);
  EXPECT_NE(&i2 + 1, &i3);

  i1 = 1234;
  i2 = 5678;
  i3 = 9012;
}

TEST(Arena, Constructor) {
  Arena arena;

Kenton Varda's avatar
Kenton Varda committed
288
  EXPECT_EQ(123u, arena.allocate<uint64_t>(123));
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  EXPECT_EQ("foo", arena.allocate<StringPtr>("foo", 3));
}

TEST(Arena, Strings) {
  Arena arena;

  StringPtr foo = arena.copyString("foo");
  StringPtr bar = arena.copyString("bar");
  StringPtr quux = arena.copyString("quux");
  StringPtr corge = arena.copyString("corge");

  EXPECT_EQ("foo", foo);
  EXPECT_EQ("bar", bar);
  EXPECT_EQ("quux", quux);
  EXPECT_EQ("corge", corge);

  EXPECT_EQ(foo.end() + 1, bar.begin());
  EXPECT_EQ(bar.end() + 1, quux.begin());
  EXPECT_EQ(quux.end() + 1, corge.begin());
}

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
struct ThreadTestObject {
  ThreadTestObject* next;
  void* owner;  // points into the owning thread's stack

  ThreadTestObject(ThreadTestObject* next, void* owner)
      : next(next), owner(owner) {}
  ~ThreadTestObject() { ++destructorCount; }

  static uint destructorCount;
};
uint ThreadTestObject::destructorCount = 0;

TEST(Arena, Threads) {
  // Test thread-safety.  We allocate objects in four threads simultaneously, verify that they
  // are not corrupted, then verify that their destructors are all called when the Arena is
  // destroyed.

  {
    MutexGuarded<Arena> arena;

    // Func to run in each thread.
    auto threadFunc = [&]() {
      int me;
      ThreadTestObject* head = nullptr;

      {
        auto lock = arena.lockShared();

        // Allocate a huge linked list.
        for (uint i = 0; i < 100000; i++) {
          head = &lock->allocate<ThreadTestObject>(head, &me);
        }
      }

      // Wait until all other threads are done before verifying.
      arena.lockExclusive();

      // Verify that the list hasn't been corrupted.
      while (head != nullptr) {
        ASSERT_EQ(&me, head->owner);
        head = head->next;
      }
    };

    {
      auto lock = arena.lockExclusive();
      Thread thread1(threadFunc);
      Thread thread2(threadFunc);
      Thread thread3(threadFunc);
      Thread thread4(threadFunc);

      // Wait for threads to be ready.
      usleep(10000);

      auto release = kj::mv(lock);
      // As we go out of scope, the lock will be released (since `release` is destroyed first),
      // allowing all the threads to start running.  We'll then join each thread.
    }

Kenton Varda's avatar
Kenton Varda committed
369
    EXPECT_EQ(0u, ThreadTestObject::destructorCount);
370 371
  }

Kenton Varda's avatar
Kenton Varda committed
372
  EXPECT_EQ(400000u, ThreadTestObject::destructorCount);
373 374
}

375 376
}  // namespace
}  // namespace kj