• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/allocation.h"
6 
7 #include <stdlib.h>  // For free, malloc.
8 #include "src/checks.h"
9 #include "src/platform.h"
10 #include "src/utils.h"
11 
12 #if V8_LIBC_BIONIC
13 #include <malloc.h>  // NOLINT
14 #endif
15 
16 namespace v8 {
17 namespace internal {
18 
New(size_t size)19 void* Malloced::New(size_t size) {
20   void* result = malloc(size);
21   if (result == NULL) {
22     v8::internal::FatalProcessOutOfMemory("Malloced operator new");
23   }
24   return result;
25 }
26 
27 
Delete(void * p)28 void Malloced::Delete(void* p) {
29   free(p);
30 }
31 
32 
FatalProcessOutOfMemory()33 void Malloced::FatalProcessOutOfMemory() {
34   v8::internal::FatalProcessOutOfMemory("Out of memory");
35 }
36 
37 
38 #ifdef DEBUG
39 
40 static void* invalid = static_cast<void*>(NULL);
41 
operator new(size_t size)42 void* Embedded::operator new(size_t size) {
43   UNREACHABLE();
44   return invalid;
45 }
46 
47 
operator delete(void * p)48 void Embedded::operator delete(void* p) {
49   UNREACHABLE();
50 }
51 
52 
operator new(size_t size)53 void* AllStatic::operator new(size_t size) {
54   UNREACHABLE();
55   return invalid;
56 }
57 
58 
operator delete(void * p)59 void AllStatic::operator delete(void* p) {
60   UNREACHABLE();
61 }
62 
63 #endif
64 
65 
StrDup(const char * str)66 char* StrDup(const char* str) {
67   int length = StrLength(str);
68   char* result = NewArray<char>(length + 1);
69   MemCopy(result, str, length);
70   result[length] = '\0';
71   return result;
72 }
73 
74 
StrNDup(const char * str,int n)75 char* StrNDup(const char* str, int n) {
76   int length = StrLength(str);
77   if (n < length) length = n;
78   char* result = NewArray<char>(length + 1);
79   MemCopy(result, str, length);
80   result[length] = '\0';
81   return result;
82 }
83 
84 
AlignedAlloc(size_t size,size_t alignment)85 void* AlignedAlloc(size_t size, size_t alignment) {
86   ASSERT(IsPowerOf2(alignment) && alignment >= V8_ALIGNOF(void*));  // NOLINT
87   void* ptr;
88 #if V8_OS_WIN
89   ptr = _aligned_malloc(size, alignment);
90 #elif V8_LIBC_BIONIC
91   // posix_memalign is not exposed in some Android versions, so we fall back to
92   // memalign. See http://code.google.com/p/android/issues/detail?id=35391.
93   ptr = memalign(alignment, size);
94 #else
95   if (posix_memalign(&ptr, alignment, size)) ptr = NULL;
96 #endif
97   if (ptr == NULL) FatalProcessOutOfMemory("AlignedAlloc");
98   return ptr;
99 }
100 
101 
AlignedFree(void * ptr)102 void AlignedFree(void *ptr) {
103 #if V8_OS_WIN
104   _aligned_free(ptr);
105 #elif V8_LIBC_BIONIC
106   // Using free is not correct in general, but for V8_LIBC_BIONIC it is.
107   free(ptr);
108 #else
109   free(ptr);
110 #endif
111 }
112 
113 } }  // namespace v8::internal
114