ff16dab3cc0f94af62baa5faa5906f772d2730e7
[platform/framework/web/crosswalk.git] / src / v8 / src / allocation.cc
1 // Copyright 2012 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
28 #include "allocation.h"
29
30 #include <stdlib.h>  // For free, malloc.
31 #include "checks.h"
32 #include "platform.h"
33 #include "utils.h"
34
35 #if V8_LIBC_BIONIC
36 #include <malloc.h>  // NOLINT
37 #endif
38
39 namespace v8 {
40 namespace internal {
41
42 void* Malloced::New(size_t size) {
43   void* result = malloc(size);
44   if (result == NULL) {
45     v8::internal::FatalProcessOutOfMemory("Malloced operator new");
46   }
47   return result;
48 }
49
50
51 void Malloced::Delete(void* p) {
52   free(p);
53 }
54
55
56 void Malloced::FatalProcessOutOfMemory() {
57   v8::internal::FatalProcessOutOfMemory("Out of memory");
58 }
59
60
61 #ifdef DEBUG
62
63 static void* invalid = static_cast<void*>(NULL);
64
65 void* Embedded::operator new(size_t size) {
66   UNREACHABLE();
67   return invalid;
68 }
69
70
71 void Embedded::operator delete(void* p) {
72   UNREACHABLE();
73 }
74
75
76 void* AllStatic::operator new(size_t size) {
77   UNREACHABLE();
78   return invalid;
79 }
80
81
82 void AllStatic::operator delete(void* p) {
83   UNREACHABLE();
84 }
85
86 #endif
87
88
89 char* StrDup(const char* str) {
90   int length = StrLength(str);
91   char* result = NewArray<char>(length + 1);
92   OS::MemCopy(result, str, length);
93   result[length] = '\0';
94   return result;
95 }
96
97
98 char* StrNDup(const char* str, int n) {
99   int length = StrLength(str);
100   if (n < length) length = n;
101   char* result = NewArray<char>(length + 1);
102   OS::MemCopy(result, str, length);
103   result[length] = '\0';
104   return result;
105 }
106
107
108 void* AlignedAlloc(size_t size, size_t alignment) {
109   ASSERT(IsPowerOf2(alignment) && alignment >= V8_ALIGNOF(void*));  // NOLINT
110   void* ptr;
111 #if V8_OS_WIN
112   ptr = _aligned_malloc(size, alignment);
113 #elif V8_LIBC_BIONIC
114   // posix_memalign is not exposed in some Android versions, so we fall back to
115   // memalign. See http://code.google.com/p/android/issues/detail?id=35391.
116   ptr = memalign(alignment, size);
117 #else
118   if (posix_memalign(&ptr, alignment, size)) ptr = NULL;
119 #endif
120   if (ptr == NULL) FatalProcessOutOfMemory("AlignedAlloc");
121   return ptr;
122 }
123
124
125 void AlignedFree(void *ptr) {
126 #if V8_OS_WIN
127   _aligned_free(ptr);
128 #elif V8_LIBC_BIONIC
129   // Using free is not correct in general, but for V8_LIBC_BIONIC it is.
130   free(ptr);
131 #else
132   free(ptr);
133 #endif
134 }
135
136 } }  // namespace v8::internal