blob: 119b087c1963a416e811479e02dcd6c1ea828404 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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) Wang8a31eba2010-12-06 19:01:33 -080028#include "../include/v8stdint.h"
29#include "globals.h"
30#include "checks.h"
31#include "allocation.h"
32#include "utils.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000033
34namespace v8 {
35namespace internal {
36
Steve Blocka7e24c12009-10-30 11:49:00 +000037void* Malloced::New(size_t size) {
Steve Blocka7e24c12009-10-30 11:49:00 +000038 void* result = malloc(size);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080039 if (result == NULL) {
40 v8::internal::FatalProcessOutOfMemory("Malloced operator new");
41 }
Steve Blocka7e24c12009-10-30 11:49:00 +000042 return result;
43}
44
45
46void Malloced::Delete(void* p) {
47 free(p);
48}
49
50
51void Malloced::FatalProcessOutOfMemory() {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080052 v8::internal::FatalProcessOutOfMemory("Out of memory");
Steve Blocka7e24c12009-10-30 11:49:00 +000053}
54
55
56#ifdef DEBUG
57
58static void* invalid = static_cast<void*>(NULL);
59
60void* Embedded::operator new(size_t size) {
61 UNREACHABLE();
62 return invalid;
63}
64
65
66void Embedded::operator delete(void* p) {
67 UNREACHABLE();
68}
69
70
71void* AllStatic::operator new(size_t size) {
72 UNREACHABLE();
73 return invalid;
74}
75
76
77void AllStatic::operator delete(void* p) {
78 UNREACHABLE();
79}
80
81#endif
82
83
84char* StrDup(const char* str) {
Steve Blockd0582a62009-12-15 09:54:21 +000085 int length = StrLength(str);
Steve Blocka7e24c12009-10-30 11:49:00 +000086 char* result = NewArray<char>(length + 1);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080087 memcpy(result, str, length);
Steve Blocka7e24c12009-10-30 11:49:00 +000088 result[length] = '\0';
89 return result;
90}
91
92
Steve Blockd0582a62009-12-15 09:54:21 +000093char* StrNDup(const char* str, int n) {
94 int length = StrLength(str);
Steve Blocka7e24c12009-10-30 11:49:00 +000095 if (n < length) length = n;
96 char* result = NewArray<char>(length + 1);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080097 memcpy(result, str, length);
Steve Blocka7e24c12009-10-30 11:49:00 +000098 result[length] = '\0';
99 return result;
100}
101
102
Steve Blocka7e24c12009-10-30 11:49:00 +0000103void PreallocatedStorage::LinkTo(PreallocatedStorage* other) {
104 next_ = other->next_;
105 other->next_->previous_ = this;
106 previous_ = other;
107 other->next_ = this;
108}
109
110
111void PreallocatedStorage::Unlink() {
112 next_->previous_ = previous_;
113 previous_->next_ = next_;
114}
115
116
117PreallocatedStorage::PreallocatedStorage(size_t size)
118 : size_(size) {
119 previous_ = next_ = this;
120}
121
122} } // namespace v8::internal