1// Copyright 2012 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 "zone.h"
32
33#include "counters.h"
34#include "isolate.h"
35#include "utils.h"
36#include "v8-counters.h"
37
38namespace v8 {
39namespace internal {
40
41
42inline void* Zone::New(int size) {
43  ASSERT(ZoneScope::nesting() > 0);
44  // Round up the requested size to fit the alignment.
45  size = RoundUp(size, kAlignment);
46
47  // If the allocation size is divisible by 8 then we return an 8-byte aligned
48  // address.
49  if (kPointerSize == 4 && kAlignment == 4) {
50    position_ += ((~size) & 4) & (reinterpret_cast<intptr_t>(position_) & 4);
51  } else {
52    ASSERT(kAlignment >= kPointerSize);
53  }
54
55  // Check if the requested size is available without expanding.
56  Address result = position_;
57
58  if (size > limit_ - position_) {
59     result = NewExpand(size);
60  } else {
61     position_ += size;
62  }
63
64  // Check that the result has the proper alignment and return it.
65  ASSERT(IsAddressAligned(result, kAlignment, 0));
66  allocation_size_ += size;
67  return reinterpret_cast<void*>(result);
68}
69
70
71template <typename T>
72T* Zone::NewArray(int length) {
73  return static_cast<T*>(New(length * sizeof(T)));
74}
75
76
77bool Zone::excess_allocation() {
78  return segment_bytes_allocated_ > zone_excess_limit_;
79}
80
81
82void Zone::adjust_segment_bytes_allocated(int delta) {
83  segment_bytes_allocated_ += delta;
84  isolate_->counters()->zone_segment_bytes()->Set(segment_bytes_allocated_);
85}
86
87
88template <typename Config>
89ZoneSplayTree<Config>::~ZoneSplayTree() {
90  // Reset the root to avoid unneeded iteration over all tree nodes
91  // in the destructor.  For a zone-allocated tree, nodes will be
92  // freed by the Zone.
93  SplayTree<Config, ZoneListAllocationPolicy>::ResetRoot();
94}
95
96
97// TODO(isolates): for performance reasons, this should be replaced with a new
98//                 operator that takes the zone in which the object should be
99//                 allocated.
100void* ZoneObject::operator new(size_t size) {
101  return ZONE->New(static_cast<int>(size));
102}
103
104void* ZoneObject::operator new(size_t size, Zone* zone) {
105  return zone->New(static_cast<int>(size));
106}
107
108
109inline void* ZoneListAllocationPolicy::New(int size) {
110  return ZONE->New(size);
111}
112
113
114template <typename T>
115void* ZoneList<T>::operator new(size_t size) {
116  return ZONE->New(static_cast<int>(size));
117}
118
119
120template <typename T>
121void* ZoneList<T>::operator new(size_t size, Zone* zone) {
122  return zone->New(static_cast<int>(size));
123}
124
125
126ZoneScope::ZoneScope(Isolate* isolate, ZoneScopeMode mode)
127    : isolate_(isolate), mode_(mode) {
128  isolate_->zone()->scope_nesting_++;
129}
130
131
132bool ZoneScope::ShouldDeleteOnExit() {
133  return isolate_->zone()->scope_nesting_ == 1 && mode_ == DELETE_ON_EXIT;
134}
135
136
137int ZoneScope::nesting() {
138  return Isolate::Current()->zone()->scope_nesting_;
139}
140
141
142} }  // namespace v8::internal
143
144#endif  // V8_ZONE_INL_H_
145