diff --git a/src/allocation.cc b/src/allocation.cc index 27415c6525..119b087c19 100644 --- a/src/allocation.cc +++ b/src/allocation.cc @@ -25,58 +25,16 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "v8.h" -#include "isolate.h" -#include "allocation.h" - -/* TODO(isolates): this is what's included in bleeding_edge - including of v8.h was replaced with these in - http://codereview.chromium.org/5005001/ - we need Isolate and Isolate needs a lot more so I'm including v8.h back. #include "../include/v8stdint.h" #include "globals.h" #include "checks.h" #include "allocation.h" #include "utils.h" -*/ namespace v8 { namespace internal { -#ifdef DEBUG - -NativeAllocationChecker::NativeAllocationChecker( - NativeAllocationChecker::NativeAllocationAllowed allowed) - : allowed_(allowed) { - if (allowed == DISALLOW) { - Isolate* isolate = Isolate::Current(); - isolate->set_allocation_disallowed(isolate->allocation_disallowed() + 1); - } -} - - -NativeAllocationChecker::~NativeAllocationChecker() { - Isolate* isolate = Isolate::Current(); - if (allowed_ == DISALLOW) { - isolate->set_allocation_disallowed(isolate->allocation_disallowed() - 1); - } - ASSERT(isolate->allocation_disallowed() >= 0); -} - - -bool NativeAllocationChecker::allocation_allowed() { - // TODO(isolates): either find a way to make this work that doesn't - // require initializing an isolate before we can use malloc or drop - // it completely. - return true; - // return Isolate::Current()->allocation_disallowed() == 0; -} - -#endif // DEBUG - - void* Malloced::New(size_t size) { - ASSERT(NativeAllocationChecker::allocation_allowed()); void* result = malloc(size); if (result == NULL) { v8::internal::FatalProcessOutOfMemory("Malloced operator new"); @@ -142,77 +100,6 @@ char* StrNDup(const char* str, int n) { } -void Isolate::PreallocatedStorageInit(size_t size) { - ASSERT(free_list_.next_ == &free_list_); - ASSERT(free_list_.previous_ == &free_list_); - PreallocatedStorage* free_chunk = - reinterpret_cast(new char[size]); - free_list_.next_ = free_list_.previous_ = free_chunk; - free_chunk->next_ = free_chunk->previous_ = &free_list_; - free_chunk->size_ = size - sizeof(PreallocatedStorage); - preallocated_storage_preallocated_ = true; -} - - -void* Isolate::PreallocatedStorageNew(size_t size) { - if (!preallocated_storage_preallocated_) { - return FreeStoreAllocationPolicy::New(size); - } - ASSERT(free_list_.next_ != &free_list_); - ASSERT(free_list_.previous_ != &free_list_); - - size = (size + kPointerSize - 1) & ~(kPointerSize - 1); - // Search for exact fit. - for (PreallocatedStorage* storage = free_list_.next_; - storage != &free_list_; - storage = storage->next_) { - if (storage->size_ == size) { - storage->Unlink(); - storage->LinkTo(&in_use_list_); - return reinterpret_cast(storage + 1); - } - } - // Search for first fit. - for (PreallocatedStorage* storage = free_list_.next_; - storage != &free_list_; - storage = storage->next_) { - if (storage->size_ >= size + sizeof(PreallocatedStorage)) { - storage->Unlink(); - storage->LinkTo(&in_use_list_); - PreallocatedStorage* left_over = - reinterpret_cast( - reinterpret_cast(storage + 1) + size); - left_over->size_ = storage->size_ - size - sizeof(PreallocatedStorage); - ASSERT(size + left_over->size_ + sizeof(PreallocatedStorage) == - storage->size_); - storage->size_ = size; - left_over->LinkTo(&free_list_); - return reinterpret_cast(storage + 1); - } - } - // Allocation failure. - ASSERT(false); - return NULL; -} - - -// We don't attempt to coalesce. -void Isolate::PreallocatedStorageDelete(void* p) { - if (p == NULL) { - return; - } - if (!preallocated_storage_preallocated_) { - FreeStoreAllocationPolicy::Delete(p); - return; - } - PreallocatedStorage* storage = reinterpret_cast(p) - 1; - ASSERT(storage->next_->previous_ == storage); - ASSERT(storage->previous_->next_ == storage); - storage->Unlink(); - storage->LinkTo(&free_list_); -} - - void PreallocatedStorage::LinkTo(PreallocatedStorage* other) { next_ = other->next_; other->next_->previous_ = this; diff --git a/src/allocation.h b/src/allocation.h index d7bbbb8769..75aba35d8c 100644 --- a/src/allocation.h +++ b/src/allocation.h @@ -39,25 +39,6 @@ namespace internal { // processing. void FatalProcessOutOfMemory(const char* message); -// A class that controls whether allocation is allowed. This is for -// the C++ heap only! -class NativeAllocationChecker { - public: - enum NativeAllocationAllowed { ALLOW, DISALLOW }; -#ifdef DEBUG - explicit NativeAllocationChecker(NativeAllocationAllowed allowed); - ~NativeAllocationChecker(); - static bool allocation_allowed(); - private: - // This flag applies to this particular instance. - NativeAllocationAllowed allowed_; -#else - explicit inline NativeAllocationChecker(NativeAllocationAllowed allowed) {} - static inline bool allocation_allowed() { return true; } -#endif -}; - - // Superclass for classes managed with new & delete. class Malloced { public: @@ -101,7 +82,6 @@ class AllStatic { template static T* NewArray(int size) { - ASSERT(NativeAllocationChecker::allocation_allowed()); T* result = new T[size]; if (result == NULL) Malloced::FatalProcessOutOfMemory(); return result; diff --git a/src/isolate.cc b/src/isolate.cc index 3d166ee8c2..a163532130 100644 --- a/src/isolate.cc +++ b/src/isolate.cc @@ -168,6 +168,77 @@ void Isolate::PreallocatedMemoryThreadStop() { } +void Isolate::PreallocatedStorageInit(size_t size) { + ASSERT(free_list_.next_ == &free_list_); + ASSERT(free_list_.previous_ == &free_list_); + PreallocatedStorage* free_chunk = + reinterpret_cast(new char[size]); + free_list_.next_ = free_list_.previous_ = free_chunk; + free_chunk->next_ = free_chunk->previous_ = &free_list_; + free_chunk->size_ = size - sizeof(PreallocatedStorage); + preallocated_storage_preallocated_ = true; +} + + +void* Isolate::PreallocatedStorageNew(size_t size) { + if (!preallocated_storage_preallocated_) { + return FreeStoreAllocationPolicy::New(size); + } + ASSERT(free_list_.next_ != &free_list_); + ASSERT(free_list_.previous_ != &free_list_); + + size = (size + kPointerSize - 1) & ~(kPointerSize - 1); + // Search for exact fit. + for (PreallocatedStorage* storage = free_list_.next_; + storage != &free_list_; + storage = storage->next_) { + if (storage->size_ == size) { + storage->Unlink(); + storage->LinkTo(&in_use_list_); + return reinterpret_cast(storage + 1); + } + } + // Search for first fit. + for (PreallocatedStorage* storage = free_list_.next_; + storage != &free_list_; + storage = storage->next_) { + if (storage->size_ >= size + sizeof(PreallocatedStorage)) { + storage->Unlink(); + storage->LinkTo(&in_use_list_); + PreallocatedStorage* left_over = + reinterpret_cast( + reinterpret_cast(storage + 1) + size); + left_over->size_ = storage->size_ - size - sizeof(PreallocatedStorage); + ASSERT(size + left_over->size_ + sizeof(PreallocatedStorage) == + storage->size_); + storage->size_ = size; + left_over->LinkTo(&free_list_); + return reinterpret_cast(storage + 1); + } + } + // Allocation failure. + ASSERT(false); + return NULL; +} + + +// We don't attempt to coalesce. +void Isolate::PreallocatedStorageDelete(void* p) { + if (p == NULL) { + return; + } + if (!preallocated_storage_preallocated_) { + FreeStoreAllocationPolicy::Delete(p); + return; + } + PreallocatedStorage* storage = reinterpret_cast(p) - 1; + ASSERT(storage->next_->previous_ == storage); + ASSERT(storage->previous_->next_ == storage); + storage->Unlink(); + storage->LinkTo(&free_list_); +} + + Isolate* Isolate::default_isolate_ = NULL; Thread::LocalStorageKey Isolate::isolate_key_; Thread::LocalStorageKey Isolate::thread_id_key_; diff --git a/src/isolate.h b/src/isolate.h index a2716a011d..03a4866f45 100644 --- a/src/isolate.h +++ b/src/isolate.h @@ -292,8 +292,6 @@ typedef List DebugObjectCache; /* Assembler state. */ \ /* A previously allocated buffer of kMinimalBufferSize bytes, or NULL. */ \ V(byte*, assembler_spare_buffer, NULL) \ - /*This static counter ensures that NativeAllocationCheckers can be nested.*/ \ - V(int, allocation_disallowed, 0) \ V(FatalErrorCallback, exception_behavior, NULL) \ V(v8::Debug::MessageHandler, message_handler, NULL) \ /* To distinguish the function templates, so that we can find them in the */ \ diff --git a/src/objects.cc b/src/objects.cc index a0d1536bbb..8cb36e91f9 100644 --- a/src/objects.cc +++ b/src/objects.cc @@ -4514,7 +4514,6 @@ SmartPointer String::ToCString(AllowNullsFlag allow_nulls, int offset, int length, int* length_return) { - ASSERT(NativeAllocationChecker::allocation_allowed()); if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { return SmartPointer(NULL); } @@ -4594,7 +4593,6 @@ const uc16* String::GetTwoByteData(unsigned start) { SmartPointer String::ToWideCString(RobustnessFlag robust_flag) { - ASSERT(NativeAllocationChecker::allocation_allowed()); if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { return SmartPointer(); } diff --git a/src/parser.cc b/src/parser.cc index eefe7cf541..13e0c33c01 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -579,7 +579,7 @@ Parser::Parser(Handle