1// Copyright 2011 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_ZONE_INL_H_
29#define V8_ZONE_INL_H_
30
31#include "isolate.h"
32#include "zone.h"
33#include "v8-counters.h"
34
35namespace v8 {
36namespace internal {
37
38
39AssertNoZoneAllocation::AssertNoZoneAllocation()
40    : prev_(Isolate::Current()->zone_allow_allocation()) {
41  Isolate::Current()->set_zone_allow_allocation(false);
42}
43
44
45AssertNoZoneAllocation::~AssertNoZoneAllocation() {
46  Isolate::Current()->set_zone_allow_allocation(prev_);
47}
48
49
50inline void* Zone::New(int size) {
51  ASSERT(Isolate::Current()->zone_allow_allocation());
52  ASSERT(ZoneScope::nesting() > 0);
53  // Round up the requested size to fit the alignment.
54  size = RoundUp(size, kAlignment);
55
56  // Check if the requested size is available without expanding.
57  Address result = position_;
58  if ((position_ += size) > limit_) result = NewExpand(size);
59
60  // Check that the result has the proper alignment and return it.
61  ASSERT(IsAddressAligned(result, kAlignment, 0));
62  allocation_size_ += size;
63  return reinterpret_cast<void*>(result);
64}
65
66
67template <typename T>
68T* Zone::NewArray(int length) {
69  return static_cast<T*>(New(length * sizeof(T)));
70}
71
72
73bool Zone::excess_allocation() {
74  return segment_bytes_allocated_ > zone_excess_limit_;
75}
76
77
78void Zone::adjust_segment_bytes_allocated(int delta) {
79  segment_bytes_allocated_ += delta;
80  isolate_->counters()->zone_segment_bytes()->Set(segment_bytes_allocated_);
81}
82
83
84template <typename Config>
85ZoneSplayTree<Config>::~ZoneSplayTree() {
86  // Reset the root to avoid unneeded iteration over all tree nodes
87  // in the destructor.  For a zone-allocated tree, nodes will be
88  // freed by the Zone.
89  SplayTree<Config, ZoneListAllocationPolicy>::ResetRoot();
90}
91
92
93// TODO(isolates): for performance reasons, this should be replaced with a new
94//                 operator that takes the zone in which the object should be
95//                 allocated.
96void* ZoneObject::operator new(size_t size) {
97  return ZONE->New(static_cast<int>(size));
98}
99
100void* ZoneObject::operator new(size_t size, Zone* zone) {
101  return zone->New(static_cast<int>(size));
102}
103
104
105inline void* ZoneListAllocationPolicy::New(int size) {
106  return ZONE->New(size);
107}
108
109
110ZoneScope::ZoneScope(ZoneScopeMode mode)
111    : isolate_(Isolate::Current()),
112      mode_(mode) {
113  isolate_->zone()->scope_nesting_++;
114}
115
116
117bool ZoneScope::ShouldDeleteOnExit() {
118  return isolate_->zone()->scope_nesting_ == 1 && mode_ == DELETE_ON_EXIT;
119}
120
121
122int ZoneScope::nesting() {
123  return Isolate::Current()->zone()->scope_nesting_;
124}
125
126
127} }  // namespace v8::internal
128
129#endif  // V8_ZONE_INL_H_
130