blob: 851cd61ffcf5be28b3890dd9ffa2ed73a607ede0 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/allocation.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006
7#include <stdlib.h> // For free, malloc.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/base/bits.h"
9#include "src/base/logging.h"
10#include "src/base/platform/platform.h"
11#include "src/utils.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012#include "src/v8.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013
14#if V8_LIBC_BIONIC
15#include <malloc.h> // NOLINT
16#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000017
18namespace v8 {
19namespace internal {
20
Steve Blocka7e24c12009-10-30 11:49:00 +000021void* Malloced::New(size_t size) {
Steve Blocka7e24c12009-10-30 11:49:00 +000022 void* result = malloc(size);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080023 if (result == NULL) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000024 V8::FatalProcessOutOfMemory("Malloced operator new");
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080025 }
Steve Blocka7e24c12009-10-30 11:49:00 +000026 return result;
27}
28
29
30void Malloced::Delete(void* p) {
31 free(p);
32}
33
34
Steve Blocka7e24c12009-10-30 11:49:00 +000035#ifdef DEBUG
36
37static void* invalid = static_cast<void*>(NULL);
38
39void* Embedded::operator new(size_t size) {
40 UNREACHABLE();
41 return invalid;
42}
43
44
45void Embedded::operator delete(void* p) {
46 UNREACHABLE();
47}
48
49
50void* AllStatic::operator new(size_t size) {
51 UNREACHABLE();
52 return invalid;
53}
54
55
56void AllStatic::operator delete(void* p) {
57 UNREACHABLE();
58}
59
60#endif
61
62
63char* StrDup(const char* str) {
Steve Blockd0582a62009-12-15 09:54:21 +000064 int length = StrLength(str);
Steve Blocka7e24c12009-10-30 11:49:00 +000065 char* result = NewArray<char>(length + 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 MemCopy(result, str, length);
Steve Blocka7e24c12009-10-30 11:49:00 +000067 result[length] = '\0';
68 return result;
69}
70
71
Steve Blockd0582a62009-12-15 09:54:21 +000072char* StrNDup(const char* str, int n) {
73 int length = StrLength(str);
Steve Blocka7e24c12009-10-30 11:49:00 +000074 if (n < length) length = n;
75 char* result = NewArray<char>(length + 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076 MemCopy(result, str, length);
Steve Blocka7e24c12009-10-30 11:49:00 +000077 result[length] = '\0';
78 return result;
79}
80
81
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082void* AlignedAlloc(size_t size, size_t alignment) {
83 DCHECK_LE(V8_ALIGNOF(void*), alignment);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040084 DCHECK(base::bits::IsPowerOfTwo64(alignment));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085 void* ptr;
86#if V8_OS_WIN
87 ptr = _aligned_malloc(size, alignment);
88#elif V8_LIBC_BIONIC
89 // posix_memalign is not exposed in some Android versions, so we fall back to
90 // memalign. See http://code.google.com/p/android/issues/detail?id=35391.
91 ptr = memalign(alignment, size);
92#else
93 if (posix_memalign(&ptr, alignment, size)) ptr = NULL;
94#endif
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000095 if (ptr == NULL) V8::FatalProcessOutOfMemory("AlignedAlloc");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 return ptr;
Steve Blocka7e24c12009-10-30 11:49:00 +000097}
98
99
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100void AlignedFree(void *ptr) {
101#if V8_OS_WIN
102 _aligned_free(ptr);
103#elif V8_LIBC_BIONIC
104 // Using free is not correct in general, but for V8_LIBC_BIONIC it is.
105 free(ptr);
106#else
107 free(ptr);
108#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000109}
110
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111} // namespace internal
112} // namespace v8