Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 | // 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 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 28 | #include "../include/v8stdint.h" |
| 29 | #include "globals.h" |
| 30 | #include "checks.h" |
| 31 | #include "allocation.h" |
| 32 | #include "utils.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | |
| 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | void* Malloced::New(size_t size) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | void* result = malloc(size); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 39 | if (result == NULL) { |
| 40 | v8::internal::FatalProcessOutOfMemory("Malloced operator new"); |
| 41 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | return result; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | void Malloced::Delete(void* p) { |
| 47 | free(p); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | void Malloced::FatalProcessOutOfMemory() { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 52 | v8::internal::FatalProcessOutOfMemory("Out of memory"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
| 56 | #ifdef DEBUG |
| 57 | |
| 58 | static void* invalid = static_cast<void*>(NULL); |
| 59 | |
| 60 | void* Embedded::operator new(size_t size) { |
| 61 | UNREACHABLE(); |
| 62 | return invalid; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | void Embedded::operator delete(void* p) { |
| 67 | UNREACHABLE(); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | void* AllStatic::operator new(size_t size) { |
| 72 | UNREACHABLE(); |
| 73 | return invalid; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void AllStatic::operator delete(void* p) { |
| 78 | UNREACHABLE(); |
| 79 | } |
| 80 | |
| 81 | #endif |
| 82 | |
| 83 | |
| 84 | char* StrDup(const char* str) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 85 | int length = StrLength(str); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | char* result = NewArray<char>(length + 1); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 87 | memcpy(result, str, length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | result[length] = '\0'; |
| 89 | return result; |
| 90 | } |
| 91 | |
| 92 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 93 | char* StrNDup(const char* str, int n) { |
| 94 | int length = StrLength(str); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | if (n < length) length = n; |
| 96 | char* result = NewArray<char>(length + 1); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 97 | memcpy(result, str, length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | result[length] = '\0'; |
| 99 | return result; |
| 100 | } |
| 101 | |
| 102 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 103 | void PreallocatedStorage::LinkTo(PreallocatedStorage* other) { |
| 104 | next_ = other->next_; |
| 105 | other->next_->previous_ = this; |
| 106 | previous_ = other; |
| 107 | other->next_ = this; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | void PreallocatedStorage::Unlink() { |
| 112 | next_->previous_ = previous_; |
| 113 | previous_->next_ = next_; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | PreallocatedStorage::PreallocatedStorage(size_t size) |
| 118 | : size_(size) { |
| 119 | previous_ = next_ = this; |
| 120 | } |
| 121 | |
| 122 | } } // namespace v8::internal |