| Index: base/allocator/allocator_shim.h
|
| diff --git a/base/allocator/allocator_shim.h b/base/allocator/allocator_shim.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..904377a11eabed1afe2b7911cbb5ad606b80c700
|
| --- /dev/null
|
| +++ b/base/allocator/allocator_shim.h
|
| @@ -0,0 +1,93 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#ifndef BASE_ALLOCATOR_ALLOCATOR_SHIM_H_
|
| +#define BASE_ALLOCATOR_ALLOCATOR_SHIM_H_
|
| +
|
| +#include <stddef.h>
|
| +
|
| +#include "base/base_export.h"
|
| +
|
| +namespace base {
|
| +namespace allocator {
|
| +
|
| +// Allocator Shim API. Allows to to:
|
| +// - Configure the behavior of the allocator (what to do on OOM failures).
|
| +// - Install new hooks (AllocatorDispatch) in the allocator chain.
|
| +
|
| +// When this shim layer is enabled, the route of an allocation is as-follows:
|
| +//
|
| +// [allocator_shim_override_*.h] Intercept malloc() / operator new calls:
|
| +// The override_* headers define the symbols required to intercept calls to
|
| +// malloc() and operator new (if not overridden by specific C++ classes).
|
| +//
|
| +// [allocator_shim.cc] Routing allocation calls to the shim:
|
| +// The headers above route the calls to the internal ShimMalloc(), ShimFree(),
|
| +// ShimCppNew() etc. methods defined in allocator_shim.cc.
|
| +// These methods will: (1) forward the allocation call to the front of the
|
| +// AllocatorDispatch chain. (2) perform security hardenings (e.g., might
|
| +// call std::new_handler on OOM failure).
|
| +//
|
| +// [allocator_shim_default_dispatch_to_*.cc] The AllocatorDispatch chain:
|
| +// It is a singly linked list where each element is a struct with function
|
| +// pointers (|malloc_function|, |free_function|, etc). Normally the chain
|
| +// consists of a single AllocatorDispatch element, herein called
|
| +// the "default dispatch", which is statically defined at build time and
|
| +// ultimately routes the calls to the actual allocator defined by the build
|
| +// config (tcmalloc, glibc, ...).
|
| +//
|
| +// It is possible to dynamically insert (and remove) further AllocatorDispatch
|
| +// stages to the front of the chain, for debugging / profiling purposes.
|
| +
|
| +struct AllocatorDispatch {
|
| + using AllocFn = void*(size_t size, const AllocatorDispatch* self);
|
| + using AllocZeroInitializedFn = void*(size_t n,
|
| + size_t size,
|
| + const AllocatorDispatch* self);
|
| + using AllocAlignedFn = void*(size_t alignment,
|
| + size_t size,
|
| + const AllocatorDispatch* self);
|
| + using ReallocFn = void*(void* address,
|
| + size_t size,
|
| + const AllocatorDispatch* self);
|
| + using FreeFn = void(void* address, const AllocatorDispatch* self);
|
| +
|
| + AllocFn* const alloc_function;
|
| + AllocZeroInitializedFn* const alloc_zero_initialized_function;
|
| + AllocAlignedFn* const alloc_aligned_function;
|
| + ReallocFn* const realloc_function;
|
| + FreeFn* const free_function;
|
| +
|
| + const AllocatorDispatch* next;
|
| +
|
| + // |default_dispatch| is statically defined by one (and only one) of the
|
| + // allocator_shim_default_dispatch_to_*.cc files, depending on the build
|
| + // configuration.
|
| + static const AllocatorDispatch default_dispatch;
|
| +};
|
| +
|
| +// When true makes malloc behave like new, w.r.t calling the new_handler if
|
| +// the allocation fails (see set_new_mode() in Windows).
|
| +BASE_EXPORT void SetCallNewHandlerOnMallocFailure(bool value);
|
| +
|
| +// Allocates |size| bytes or returns nullptr. It does NOT call the new_handler,
|
| +// regardless of SetCallNewHandlerOnMallocFailure().
|
| +BASE_EXPORT void* UncheckedAlloc(size_t size);
|
| +
|
| +// Inserts |dispatch| in front of the allocator chain. This method is NOT
|
| +// thread-safe w.r.t concurrent invocations of Insert/RemoveAllocatorDispatch().
|
| +// The callers have the responsibility of linearizing the changes to the chain
|
| +// (or more likely call these always on the same thread).
|
| +BASE_EXPORT void InsertAllocatorDispatch(AllocatorDispatch* dispatch);
|
| +
|
| +// Removes |dispatch| from the allocator chain.
|
| +// It is not safe to free |dispatch| after this call, as other threads might be
|
| +// still derefercing it. In other words, make the AllocatorDispatch a static
|
| +// variable in the caller and expect spurious calls even after this returns.
|
| +BASE_EXPORT void RemoveAllocatorDispatch(AllocatorDispatch* dispatch);
|
| +
|
| +} // namespace allocator
|
| +} // namespace base
|
| +
|
| +#endif // BASE_ALLOCATOR_ALLOCATOR_SHIM_H_
|
|
|