zone-segment.h 2.02 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 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_ZONE_ZONE_SEGMENT_H_
#define V8_ZONE_ZONE_SEGMENT_H_

8
#include "src/init/v8.h"
9 10 11 12 13 14 15 16 17

// Segments represent chunks of memory: They have starting address
// (encoded in the this pointer) and a size in bytes. Segments are
// chained together forming a LIFO structure with the newest segment
// available as segment_head_. Segments are allocated using malloc()
// and de-allocated using free().
namespace v8 {
namespace internal {

18 19
// Forward declarations.
class AccountingAllocator;
20 21 22 23
class Zone;

class Segment {
 public:
24 25 26
  Zone* zone() const { return zone_; }
  void set_zone(Zone* const zone) { zone_ = zone; }

27 28 29
  Segment* next() const { return next_; }
  void set_next(Segment* const next) { next_ = next; }

30 31 32 33 34 35
  // {total_size} returns the allocated size including the bookkeeping bytes of
  // the {Segment}.
  size_t total_size() const { return size_; }

  // {capacity} returns the number of storage bytes in this {Segment}, i.e.
  // {end() - start()}.
36 37 38 39 40
  size_t capacity() const { return size_ - sizeof(Segment); }

  Address start() const { return address(sizeof(Segment)); }
  Address end() const { return address(size_); }

heimbuef's avatar
heimbuef committed
41 42 43 44 45
  // Zap the contents of the segment (but not the header).
  void ZapContents();
  // Zaps the header and makes the segment unusable this way.
  void ZapHeader();

46
 private:
47 48 49 50 51
  // Segments are only created by the AccountingAllocator.
  friend class AccountingAllocator;

  explicit Segment(size_t size) : size_(size) {}

heimbuef's avatar
heimbuef committed
52 53 54 55
#ifdef DEBUG
  // Constant byte value used for zapping dead memory in debug mode.
  static const unsigned char kZapDeadByte = 0xcd;
#endif
56

57
  // Computes the address of the nth byte in this segment.
58 59 60
  Address address(size_t n) const {
    return reinterpret_cast<Address>(this) + n;
  }
61

62 63 64
  Zone* zone_ = nullptr;
  Segment* next_ = nullptr;
  const size_t size_;
65
};
66

67 68 69 70
}  // namespace internal
}  // namespace v8

#endif  // V8_ZONE_ZONE_SEGMENT_H_