blob: 119b087c1963a416e811479e02dcd6c1ea828404 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000028#include "../include/v8stdint.h"
29#include "globals.h"
30#include "checks.h"
31#include "allocation.h"
32#include "utils.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037void* Malloced::New(size_t size) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038 void* result = malloc(size);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000039 if (result == NULL) {
40 v8::internal::FatalProcessOutOfMemory("Malloced operator new");
41 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042 return result;
43}
44
45
46void Malloced::Delete(void* p) {
47 free(p);
48}
49
50
51void Malloced::FatalProcessOutOfMemory() {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000052 v8::internal::FatalProcessOutOfMemory("Out of memory");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +000085 int length = StrLength(str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000086 char* result = NewArray<char>(length + 1);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000087 memcpy(result, str, length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088 result[length] = '\0';
89 return result;
90}
91
92
ager@chromium.orgc4c92722009-11-18 14:12:51 +000093char* StrNDup(const char* str, int n) {
94 int length = StrLength(str);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000095 if (n < length) length = n;
96 char* result = NewArray<char>(length + 1);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000097 memcpy(result, str, length);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000098 result[length] = '\0';
99 return result;
100}
101
102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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