Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1643)

Unified Diff: src/zone.cc

Issue 885813002: Minor refactoring for Zone class and friends. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/zone.h ('k') | src/zone-inl.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/zone.cc
diff --git a/src/zone.cc b/src/zone.cc
index e52523dd96d11fae0af557491e301d5ecb2730bd..41eff835901d202d5b7ae3f6d25328d64244a4ca 100644
--- a/src/zone.cc
+++ b/src/zone.cc
@@ -2,14 +2,43 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include <string.h>
+#include "src/zone.h"
-#include "src/v8.h"
-#include "src/zone-inl.h"
+#include <cstring>
+
+#ifdef V8_USE_ADDRESS_SANITIZER
+#include <sanitizer/asan_interface.h>
+#endif // V8_USE_ADDRESS_SANITIZER
namespace v8 {
namespace internal {
+namespace {
+
+#if V8_USE_ADDRESS_SANITIZER
+
+const int kASanRedzoneBytes = 24; // Must be a multiple of 8.
+
+#else
+
+#define ASAN_POISON_MEMORY_REGION(start, size) \
+ do { \
+ USE(start); \
+ USE(size); \
+ } while (false)
+
+#define ASAN_UNPOISON_MEMORY_REGION(start, size) \
+ do { \
+ USE(start); \
+ USE(size); \
+ } while (false)
+
+const int kASanRedzoneBytes = 0;
+
+#endif // V8_USE_ADDRESS_SANITIZER
+
+} // namespace
+
// Segments represent chunks of memory: They have starting address
// (encoded in the this pointer) and a size in bytes. Segments are
@@ -25,7 +54,7 @@ class Segment {
}
Segment* next() const { return next_; }
- void clear_next() { next_ = NULL; }
+ void clear_next() { next_ = nullptr; }
int size() const { return size_; }
int capacity() const { return size_ - sizeof(Segment); }
@@ -49,7 +78,7 @@ Zone::Zone()
segment_bytes_allocated_(0),
position_(0),
limit_(0),
- segment_head_(NULL) {}
+ segment_head_(nullptr) {}
Zone::~Zone() {
@@ -75,24 +104,16 @@ void* Zone::New(int size) {
// Check if the requested size is available without expanding.
Address result = position_;
- int size_with_redzone =
-#ifdef V8_USE_ADDRESS_SANITIZER
- size + kASanRedzoneBytes;
-#else
- size;
-#endif
-
+ const int size_with_redzone = size + kASanRedzoneBytes;
if (size_with_redzone > limit_ - position_) {
- result = NewExpand(size_with_redzone);
+ result = NewExpand(size_with_redzone);
} else {
- position_ += size_with_redzone;
+ position_ += size_with_redzone;
}
-#ifdef V8_USE_ADDRESS_SANITIZER
Address redzone_position = result + size;
DCHECK(redzone_position + kASanRedzoneBytes == position_);
ASAN_POISON_MEMORY_REGION(redzone_position, kASanRedzoneBytes);
-#endif
// Check that the result has the proper alignment and return it.
DCHECK(IsAddressAligned(result, kAlignment, 0));
@@ -108,12 +129,12 @@ void Zone::DeleteAll() {
#endif
// Find a segment with a suitable size to keep around.
- Segment* keep = NULL;
+ Segment* keep = nullptr;
// Traverse the chained list of segments, zapping (in debug mode)
// and freeing every segment except the one we wish to keep.
- for (Segment* current = segment_head_; current != NULL; ) {
+ for (Segment* current = segment_head_; current;) {
Segment* next = current->next();
- if (keep == NULL && current->size() <= kMaximumKeptSegmentSize) {
+ if (!keep && current->size() <= kMaximumKeptSegmentSize) {
// Unlink the segment we wish to keep from the list.
keep = current;
keep->clear_next();
@@ -134,7 +155,7 @@ void Zone::DeleteAll() {
// variables 'position' and 'limit' to prepare for future allocate
// attempts. Otherwise, we must clear the position and limit to
// force a new segment to be allocated on demand.
- if (keep != NULL) {
+ if (keep) {
Address start = keep->start();
position_ = RoundUp(start, kAlignment);
limit_ = keep->end();
@@ -160,8 +181,8 @@ void Zone::DeleteKeptSegment() {
static const unsigned char kZapDeadByte = 0xcd;
#endif
- DCHECK(segment_head_ == NULL || segment_head_->next() == NULL);
- if (segment_head_ != NULL) {
+ DCHECK(segment_head_ == nullptr || segment_head_->next() == nullptr);
+ if (segment_head_ != nullptr) {
int size = segment_head_->size();
#ifdef DEBUG
// Un-poison first so the zapping doesn't trigger ASan complaints.
@@ -170,7 +191,7 @@ void Zone::DeleteKeptSegment() {
memset(segment_head_, kZapDeadByte, size);
#endif
DeleteSegment(segment_head_, size);
- segment_head_ = NULL;
+ segment_head_ = nullptr;
}
DCHECK(segment_bytes_allocated_ == 0);
@@ -181,8 +202,8 @@ void Zone::DeleteKeptSegment() {
// of the segment chain. Returns the new segment.
Segment* Zone::NewSegment(int size) {
Segment* result = reinterpret_cast<Segment*>(Malloced::New(size));
- adjust_segment_bytes_allocated(size);
- if (result != NULL) {
+ segment_bytes_allocated_ += size;
+ if (result != nullptr) {
result->Initialize(segment_head_, size);
segment_head_ = result;
}
@@ -192,7 +213,7 @@ Segment* Zone::NewSegment(int size) {
// Deletes the given segment. Does not touch the segment chain.
void Zone::DeleteSegment(Segment* segment, int size) {
- adjust_segment_bytes_allocated(-size);
+ segment_bytes_allocated_ -= size;
Malloced::Delete(segment);
}
@@ -208,7 +229,7 @@ Address Zone::NewExpand(int size) {
// except that we employ a maximum segment size when we delete. This
// is to avoid excessive malloc() and free() overhead.
Segment* head = segment_head_;
- const size_t old_size = (head == NULL) ? 0 : head->size();
+ const size_t old_size = (head == nullptr) ? 0 : head->size();
static const size_t kSegmentOverhead = sizeof(Segment) + kAlignment;
const size_t new_size_no_overhead = size + (old_size << 1);
size_t new_size = kSegmentOverhead + new_size_no_overhead;
@@ -216,8 +237,8 @@ Address Zone::NewExpand(int size) {
// Guard against integer overflow.
if (new_size_no_overhead < static_cast<size_t>(size) ||
new_size < static_cast<size_t>(kSegmentOverhead)) {
- V8::FatalProcessOutOfMemory("Zone");
- return NULL;
+ FatalProcessOutOfMemory("Zone");
+ return nullptr;
}
if (new_size < static_cast<size_t>(kMinimumSegmentSize)) {
new_size = kMinimumSegmentSize;
@@ -229,13 +250,13 @@ Address Zone::NewExpand(int size) {
new_size = Max(min_new_size, static_cast<size_t>(kMaximumSegmentSize));
}
if (new_size > INT_MAX) {
- V8::FatalProcessOutOfMemory("Zone");
- return NULL;
+ FatalProcessOutOfMemory("Zone");
+ return nullptr;
}
Segment* segment = NewSegment(static_cast<int>(new_size));
- if (segment == NULL) {
- V8::FatalProcessOutOfMemory("Zone");
- return NULL;
+ if (segment == nullptr) {
+ FatalProcessOutOfMemory("Zone");
+ return nullptr;
}
// Recompute 'top' and 'limit' based on the new segment.
@@ -244,15 +265,12 @@ Address Zone::NewExpand(int size) {
// Check for address overflow.
// (Should not happen since the segment is guaranteed to accomodate
// size bytes + header and alignment padding)
- if (reinterpret_cast<uintptr_t>(position_)
- < reinterpret_cast<uintptr_t>(result)) {
- V8::FatalProcessOutOfMemory("Zone");
- return NULL;
- }
+ DCHECK_GE(reinterpret_cast<uintptr_t>(position_),
+ reinterpret_cast<uintptr_t>(result));
limit_ = segment->end();
DCHECK(position_ <= limit_);
return result;
}
-
-} } // namespace v8::internal
+} // namespace internal
+} // namespace v8
« no previous file with comments | « src/zone.h ('k') | src/zone-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698