Chromium Code Reviews

Side by Side Diff: src/zone.cc

Issue 7660016: Fix memory leaks in ~Zone and ~Isolate (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
« src/zone.h ('K') | « src/zone.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 16 matching lines...)
27 27
28 #include "v8.h" 28 #include "v8.h"
29 29
30 #include "zone-inl.h" 30 #include "zone-inl.h"
31 #include "splay-tree-inl.h" 31 #include "splay-tree-inl.h"
32 32
33 namespace v8 { 33 namespace v8 {
34 namespace internal { 34 namespace internal {
35 35
36 36
37 Zone::Zone()
38 : zone_excess_limit_(256 * MB),
39 segment_bytes_allocated_(0),
40 position_(0),
41 limit_(0),
42 scope_nesting_(0),
43 segment_head_(NULL) {
44 }
45 unsigned Zone::allocation_size_ = 0;
46
47
48 ZoneScope::~ZoneScope() {
49 ASSERT_EQ(Isolate::Current(), isolate_);
50 if (ShouldDeleteOnExit()) isolate_->zone()->DeleteAll();
51 isolate_->zone()->scope_nesting_--;
52 }
53
54
55 // Segments represent chunks of memory: They have starting address 37 // Segments represent chunks of memory: They have starting address
56 // (encoded in the this pointer) and a size in bytes. Segments are 38 // (encoded in the this pointer) and a size in bytes. Segments are
57 // chained together forming a LIFO structure with the newest segment 39 // chained together forming a LIFO structure with the newest segment
58 // available as segment_head_. Segments are allocated using malloc() 40 // available as segment_head_. Segments are allocated using malloc()
59 // and de-allocated using free(). 41 // and de-allocated using free().
60 42
61 class Segment { 43 class Segment {
62 public: 44 public:
45 void Initialize(Segment* next, int size) {
46 next_ = next;
47 size_ = size;
48 }
49
63 Segment* next() const { return next_; } 50 Segment* next() const { return next_; }
64 void clear_next() { next_ = NULL; } 51 void clear_next() { next_ = NULL; }
65 52
66 int size() const { return size_; } 53 int size() const { return size_; }
67 int capacity() const { return size_ - sizeof(Segment); } 54 int capacity() const { return size_ - sizeof(Segment); }
68 55
69 Address start() const { return address(sizeof(Segment)); } 56 Address start() const { return address(sizeof(Segment)); }
70 Address end() const { return address(size_); } 57 Address end() const { return address(size_); }
71 58
72 private: 59 private:
73 // Computes the address of the nth byte in this segment. 60 // Computes the address of the nth byte in this segment.
74 Address address(int n) const { 61 Address address(int n) const {
75 return Address(this) + n; 62 return Address(this) + n;
76 } 63 }
77 64
78 Segment* next_; 65 Segment* next_;
79 int size_; 66 int size_;
67 };
80 68
81 friend class Zone; 69
82 }; 70 Zone::Zone()
71 : zone_excess_limit_(256 * MB),
72 segment_bytes_allocated_(0),
73 position_(0),
74 limit_(0),
75 scope_nesting_(0),
76 segment_head_(NULL) {
77 }
78 unsigned Zone::allocation_size_ = 0;
79
80 Zone::~Zone() {
81 if (segment_head_ != NULL) {
82 delete segment_head_;
Vitaly Repeshko 2011/08/16 17:52:32 This should use DeleteSegment.
Jakob Kummerow 2011/08/17 08:47:30 Done. This required putting the code into a separa
83 }
84 }
85
86 ZoneScope::~ZoneScope() {
87 ASSERT_EQ(Isolate::Current(), isolate_);
88 if (ShouldDeleteOnExit()) isolate_->zone()->DeleteAll();
89 isolate_->zone()->scope_nesting_--;
90 }
83 91
84 92
85 // Creates a new segment, sets it size, and pushes it to the front 93 // Creates a new segment, sets it size, and pushes it to the front
86 // of the segment chain. Returns the new segment. 94 // of the segment chain. Returns the new segment.
87 Segment* Zone::NewSegment(int size) { 95 Segment* Zone::NewSegment(int size) {
88 Segment* result = reinterpret_cast<Segment*>(Malloced::New(size)); 96 Segment* result = reinterpret_cast<Segment*>(Malloced::New(size));
89 adjust_segment_bytes_allocated(size); 97 adjust_segment_bytes_allocated(size);
90 if (result != NULL) { 98 if (result != NULL) {
91 result->next_ = segment_head_; 99 result->Initialize(segment_head_, size);
92 result->size_ = size;
93 segment_head_ = result; 100 segment_head_ = result;
94 } 101 }
95 return result; 102 return result;
96 } 103 }
97 104
98 105
99 // Deletes the given segment. Does not touch the segment chain. 106 // Deletes the given segment. Does not touch the segment chain.
100 void Zone::DeleteSegment(Segment* segment, int size) { 107 void Zone::DeleteSegment(Segment* segment, int size) {
101 adjust_segment_bytes_allocated(-size); 108 adjust_segment_bytes_allocated(-size);
102 Malloced::Delete(segment); 109 Malloced::Delete(segment);
(...skipping 84 matching lines...)
187 // Recompute 'top' and 'limit' based on the new segment. 194 // Recompute 'top' and 'limit' based on the new segment.
188 Address result = RoundUp(segment->start(), kAlignment); 195 Address result = RoundUp(segment->start(), kAlignment);
189 position_ = result + size; 196 position_ = result + size;
190 limit_ = segment->end(); 197 limit_ = segment->end();
191 ASSERT(position_ <= limit_); 198 ASSERT(position_ <= limit_);
192 return result; 199 return result;
193 } 200 }
194 201
195 202
196 } } // namespace v8::internal 203 } } // namespace v8::internal
OLDNEW
« src/zone.h ('K') | « src/zone.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine