blob: 6e2d558a60625a1fb055c6a5c52ffd5bc44ec622 [file] [log] [blame]
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000031#include "isolate.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "zone.h"
ager@chromium.orga74f0da2008-12-03 16:05:52 +000033#include "v8-counters.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035namespace v8 {
36namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
38
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000039AssertNoZoneAllocation::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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050inline void* Zone::New(int size) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000051 ASSERT(Isolate::Current()->zone_allow_allocation());
ager@chromium.orga74f0da2008-12-03 16:05:52 +000052 ASSERT(ZoneScope::nesting() > 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053 // 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));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000062 allocation_size_ += size;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000063 return reinterpret_cast<void*>(result);
64}
65
66
ager@chromium.org41826e72009-03-30 13:30:57 +000067template <typename T>
68T* Zone::NewArray(int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000069 return static_cast<T*>(New(length * sizeof(T)));
ager@chromium.org41826e72009-03-30 13:30:57 +000070}
71
72
ager@chromium.orga74f0da2008-12-03 16:05:52 +000073bool 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;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000080 isolate_->counters()->zone_segment_bytes()->Set(segment_bytes_allocated_);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000081}
82
83
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000084template <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();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +000090}
91
92
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000093// 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
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000100void* ZoneObject::operator new(size_t size, Zone* zone) {
101 return zone->New(static_cast<int>(size));
102}
103
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000104
105inline void* ZoneListAllocationPolicy::New(int size) {
106 return ZONE->New(size);
107}
108
109
danno@chromium.org40cb8782011-05-25 07:58:50 +0000110template <typename T>
111void* ZoneList<T>::operator new(size_t size) {
112 return ZONE->New(static_cast<int>(size));
113}
114
115
116template <typename T>
117void* ZoneList<T>::operator new(size_t size, Zone* zone) {
118 return zone->New(static_cast<int>(size));
119}
120
121
122ZoneScope::ZoneScope(Isolate* isolate, ZoneScopeMode mode)
123 : isolate_(isolate), mode_(mode) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000124 isolate_->zone()->scope_nesting_++;
125}
126
127
128bool ZoneScope::ShouldDeleteOnExit() {
129 return isolate_->zone()->scope_nesting_ == 1 && mode_ == DELETE_ON_EXIT;
130}
131
132
133int ZoneScope::nesting() {
134 return Isolate::Current()->zone()->scope_nesting_;
135}
136
137
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138} } // namespace v8::internal
139
140#endif // V8_ZONE_INL_H_