blob: e312b20899d12db432a174696b212d1f4127a0b8 [file] [log] [blame]
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001// Copyright 2012 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
31#include "zone.h"
danno@chromium.orgfa458e42012-02-01 10:48:36 +000032
33#include "counters.h"
34#include "isolate.h"
35#include "utils.h"
ager@chromium.orga74f0da2008-12-03 16:05:52 +000036#include "v8-counters.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
kasperl@chromium.org71affb52009-05-26 05:44:31 +000038namespace v8 {
39namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
41
42inline void* Zone::New(int size) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000043 ASSERT(scope_nesting_ > 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044 // Round up the requested size to fit the alignment.
45 size = RoundUp(size, kAlignment);
46
danno@chromium.org50d79f32011-11-11 12:01:18 +000047 // 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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055 // Check if the requested size is available without expanding.
56 Address result = position_;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +000057
58 if (size > limit_ - position_) {
59 result = NewExpand(size);
60 } else {
61 position_ += size;
62 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000063
64 // Check that the result has the proper alignment and return it.
65 ASSERT(IsAddressAligned(result, kAlignment, 0));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000066 allocation_size_ += size;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000067 return reinterpret_cast<void*>(result);
68}
69
70
ager@chromium.org41826e72009-03-30 13:30:57 +000071template <typename T>
72T* Zone::NewArray(int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000073 return static_cast<T*>(New(length * sizeof(T)));
ager@chromium.org41826e72009-03-30 13:30:57 +000074}
75
76
ager@chromium.orga74f0da2008-12-03 16:05:52 +000077bool 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;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000084 isolate_->counters()->zone_segment_bytes()->Set(segment_bytes_allocated_);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000085}
86
87
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000088template <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.
rossberg@chromium.org400388e2012-06-06 09:29:22 +000093 SplayTree<Config, ZoneAllocationPolicy>::ResetRoot();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +000094}
95
96
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +000097void* ZoneObject::operator new(size_t size, Zone* zone) {
98 return zone->New(static_cast<int>(size));
99}
100
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000101inline void* ZoneAllocationPolicy::New(size_t size) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000102 ASSERT(zone_);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000103 return zone_->New(static_cast<int>(size));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000104}
105
106
107template <typename T>
108void* ZoneList<T>::operator new(size_t size, Zone* zone) {
109 return zone->New(static_cast<int>(size));
110}
111
112
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000113ZoneScope::ZoneScope(Zone* zone, ZoneScopeMode mode)
114 : zone_(zone), mode_(mode) {
115 zone_->scope_nesting_++;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000116}
117
118
119bool ZoneScope::ShouldDeleteOnExit() {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000120 return zone_->scope_nesting_ == 1 && mode_ == DELETE_ON_EXIT;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000121}
122
123
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124} } // namespace v8::internal
125
126#endif // V8_ZONE_INL_H_