blob: 96fd71f11bd3d55be11d7f157ee885e838c430be [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"
12
13#if V8_LIBC_BIONIC
14#include <malloc.h> // NOLINT
15#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000016
17namespace v8 {
18namespace internal {
19
Steve Blocka7e24c12009-10-30 11:49:00 +000020void* Malloced::New(size_t size) {
Steve Blocka7e24c12009-10-30 11:49:00 +000021 void* result = malloc(size);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080022 if (result == NULL) {
23 v8::internal::FatalProcessOutOfMemory("Malloced operator new");
24 }
Steve Blocka7e24c12009-10-30 11:49:00 +000025 return result;
26}
27
28
29void Malloced::Delete(void* p) {
30 free(p);
31}
32
33
34void Malloced::FatalProcessOutOfMemory() {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080035 v8::internal::FatalProcessOutOfMemory("Out of memory");
Steve Blocka7e24c12009-10-30 11:49:00 +000036}
37
38
39#ifdef DEBUG
40
41static void* invalid = static_cast<void*>(NULL);
42
43void* Embedded::operator new(size_t size) {
44 UNREACHABLE();
45 return invalid;
46}
47
48
49void Embedded::operator delete(void* p) {
50 UNREACHABLE();
51}
52
53
54void* AllStatic::operator new(size_t size) {
55 UNREACHABLE();
56 return invalid;
57}
58
59
60void AllStatic::operator delete(void* p) {
61 UNREACHABLE();
62}
63
64#endif
65
66
67char* StrDup(const char* str) {
Steve Blockd0582a62009-12-15 09:54:21 +000068 int length = StrLength(str);
Steve Blocka7e24c12009-10-30 11:49:00 +000069 char* result = NewArray<char>(length + 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070 MemCopy(result, str, length);
Steve Blocka7e24c12009-10-30 11:49:00 +000071 result[length] = '\0';
72 return result;
73}
74
75
Steve Blockd0582a62009-12-15 09:54:21 +000076char* StrNDup(const char* str, int n) {
77 int length = StrLength(str);
Steve Blocka7e24c12009-10-30 11:49:00 +000078 if (n < length) length = n;
79 char* result = NewArray<char>(length + 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080 MemCopy(result, str, length);
Steve Blocka7e24c12009-10-30 11:49:00 +000081 result[length] = '\0';
82 return result;
83}
84
85
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086void* AlignedAlloc(size_t size, size_t alignment) {
87 DCHECK_LE(V8_ALIGNOF(void*), alignment);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040088 DCHECK(base::bits::IsPowerOfTwo64(alignment));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089 void* ptr;
90#if V8_OS_WIN
91 ptr = _aligned_malloc(size, alignment);
92#elif V8_LIBC_BIONIC
93 // posix_memalign is not exposed in some Android versions, so we fall back to
94 // memalign. See http://code.google.com/p/android/issues/detail?id=35391.
95 ptr = memalign(alignment, size);
96#else
97 if (posix_memalign(&ptr, alignment, size)) ptr = NULL;
98#endif
99 if (ptr == NULL) FatalProcessOutOfMemory("AlignedAlloc");
100 return ptr;
Steve Blocka7e24c12009-10-30 11:49:00 +0000101}
102
103
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000104void AlignedFree(void *ptr) {
105#if V8_OS_WIN
106 _aligned_free(ptr);
107#elif V8_LIBC_BIONIC
108 // Using free is not correct in general, but for V8_LIBC_BIONIC it is.
109 free(ptr);
110#else
111 free(ptr);
112#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000113}
114
115} } // namespace v8::internal