v8/src/allocation.cc
honggyu.kp 446232f16b Strictly disable instantiation of AllStatic class
Since the intention of using AllStatic class is to provide classes that
only contain static method functions without member variables so it
doesn't have to be instantiated at all.

However, current implementation only disables dynamic instantiation, and
it can be detected at runtime by reaching UNREACHABLE().  And it can
still have instances allocated inside stack.

This blocks all those cases by deleting default constructor of AllStatic
class to prevent undesirable usage of it.

BUG=
R=jochen@chromium.org

Review-Url: https://codereview.chromium.org/2108273003
Cr-Commit-Position: refs/heads/master@{#37532}
2016-07-05 14:27:34 +00:00

102 lines
2.1 KiB
C++

// Copyright 2012 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/allocation.h"
#include <stdlib.h> // For free, malloc.
#include "src/base/bits.h"
#include "src/base/logging.h"
#include "src/base/platform/platform.h"
#include "src/utils.h"
#include "src/v8.h"
#if V8_LIBC_BIONIC
#include <malloc.h> // NOLINT
#endif
namespace v8 {
namespace internal {
void* Malloced::New(size_t size) {
void* result = malloc(size);
if (result == NULL) {
V8::FatalProcessOutOfMemory("Malloced operator new");
}
return result;
}
void Malloced::Delete(void* p) {
free(p);
}
#ifdef DEBUG
static void* invalid = static_cast<void*>(NULL);
void* Embedded::operator new(size_t size) {
UNREACHABLE();
return invalid;
}
void Embedded::operator delete(void* p) {
UNREACHABLE();
}
#endif
char* StrDup(const char* str) {
int length = StrLength(str);
char* result = NewArray<char>(length + 1);
MemCopy(result, str, length);
result[length] = '\0';
return result;
}
char* StrNDup(const char* str, int n) {
int length = StrLength(str);
if (n < length) length = n;
char* result = NewArray<char>(length + 1);
MemCopy(result, str, length);
result[length] = '\0';
return result;
}
void* AlignedAlloc(size_t size, size_t alignment) {
DCHECK_LE(V8_ALIGNOF(void*), alignment);
DCHECK(base::bits::IsPowerOfTwo64(alignment));
void* ptr;
#if V8_OS_WIN
ptr = _aligned_malloc(size, alignment);
#elif V8_LIBC_BIONIC
// posix_memalign is not exposed in some Android versions, so we fall back to
// memalign. See http://code.google.com/p/android/issues/detail?id=35391.
ptr = memalign(alignment, size);
#else
if (posix_memalign(&ptr, alignment, size)) ptr = NULL;
#endif
if (ptr == NULL) V8::FatalProcessOutOfMemory("AlignedAlloc");
return ptr;
}
void AlignedFree(void *ptr) {
#if V8_OS_WIN
_aligned_free(ptr);
#elif V8_LIBC_BIONIC
// Using free is not correct in general, but for V8_LIBC_BIONIC it is.
free(ptr);
#else
free(ptr);
#endif
}
} // namespace internal
} // namespace v8