summaryrefslogtreecommitdiff
path: root/deps/v8/src/codegen/aligned-slot-allocator.h
blob: 1abb7117138f6077d7c45a0bec6944d5f8645967 (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
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_CODEGEN_ALIGNED_SLOT_ALLOCATOR_H_
#define V8_CODEGEN_ALIGNED_SLOT_ALLOCATOR_H_

#include "src/base/macros.h"
#include "src/base/platform/platform.h"
#include "src/common/globals.h"

namespace v8 {
namespace internal {

// An aligned slot allocator. Allocates groups of 1, 2, or 4 slots such that the
// first slot of the group is aligned to the group size. The allocator can also
// allocate unaligned groups of arbitrary size, and an align the number of slots
// to 1, 2, or 4 slots. The allocator tries to be as thrifty as possible by
// reusing alignment padding slots in subsequent smaller slot allocations.
class V8_EXPORT_PRIVATE AlignedSlotAllocator {
 public:
  // Slots are always multiples of pointer-sized units.
  static constexpr int kSlotSize = kSystemPointerSize;

  static int NumSlotsForWidth(int bytes) {
    DCHECK_GT(bytes, 0);
    return (bytes + kSlotSize - 1) / kSlotSize;
  }

  AlignedSlotAllocator() = default;

  // Allocates |n| slots, where |n| must be 1, 2, or 4. Padding slots may be
  // inserted for alignment.
  // Returns the starting index of the slots, which is evenly divisible by |n|.
  int Allocate(int n);

  // Gets the starting index of the slots that would be returned by Allocate(n).
  int NextSlot(int n) const;

  // Allocates the given number of slots at the current end of the slot area,
  // and returns the starting index of the slots. This resets any fragment
  // slots, so subsequent allocations will be after the end of this one.
  // AllocateUnaligned(0) can be used to partition the slot area, for example
  // to make sure tagged values follow untagged values on a Frame.
  int AllocateUnaligned(int n);

  // Aligns the slot area so that future allocations begin at the alignment.
  // Returns the number of slots needed to align the slot area.
  int Align(int n);

  // Returns the size of the slot area, in slots. This will be greater than any
  // already allocated slot index.
  int Size() const { return size_; }

 private:
  static constexpr int kInvalidSlot = -1;

  static bool IsValid(int slot) { return slot > kInvalidSlot; }

  int next1_ = kInvalidSlot;
  int next2_ = kInvalidSlot;
  int next4_ = 0;
  int size_ = 0;

  DISALLOW_NEW_AND_DELETE()
};

}  // namespace internal
}  // namespace v8

#endif  // V8_CODEGEN_ALIGNED_SLOT_ALLOCATOR_H_