Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:00:51

0001 // Copyright 2017 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 
0016 #ifndef ABSL_BASE_INTERNAL_LOW_LEVEL_ALLOC_H_
0017 #define ABSL_BASE_INTERNAL_LOW_LEVEL_ALLOC_H_
0018 
0019 // A simple thread-safe memory allocator that does not depend on
0020 // mutexes or thread-specific data.  It is intended to be used
0021 // sparingly, and only when malloc() would introduce an unwanted
0022 // dependency, such as inside the heap-checker, or the Mutex
0023 // implementation.
0024 
0025 // IWYU pragma: private, include "base/low_level_alloc.h"
0026 
0027 #include <sys/types.h>
0028 
0029 #include <cstdint>
0030 
0031 #include "absl/base/attributes.h"
0032 #include "absl/base/config.h"
0033 
0034 // LowLevelAlloc requires that the platform support low-level
0035 // allocation of virtual memory. Platforms lacking this cannot use
0036 // LowLevelAlloc.
0037 #ifdef ABSL_LOW_LEVEL_ALLOC_MISSING
0038 #error ABSL_LOW_LEVEL_ALLOC_MISSING cannot be directly set
0039 #elif !defined(ABSL_HAVE_MMAP) && !defined(_WIN32)
0040 #define ABSL_LOW_LEVEL_ALLOC_MISSING 1
0041 #endif
0042 
0043 // Using LowLevelAlloc with kAsyncSignalSafe isn't supported on Windows or
0044 // asm.js / WebAssembly.
0045 // See https://kripken.github.io/emscripten-site/docs/porting/pthreads.html
0046 // for more information.
0047 #ifdef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
0048 #error ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING cannot be directly set
0049 #elif defined(_WIN32) || defined(__asmjs__) || defined(__wasm__) || \
0050     defined(__hexagon__)
0051 #define ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING 1
0052 #endif
0053 
0054 #include <cstddef>
0055 
0056 #include "absl/base/port.h"
0057 
0058 namespace absl {
0059 ABSL_NAMESPACE_BEGIN
0060 namespace base_internal {
0061 
0062 class LowLevelAlloc {
0063  public:
0064   struct Arena;       // an arena from which memory may be allocated
0065 
0066   // Returns a pointer to a block of at least "request" bytes
0067   // that have been newly allocated from the specific arena.
0068   // for Alloc() call the DefaultArena() is used.
0069   // Returns 0 if passed request==0.
0070   // Does not return 0 under other circumstances; it crashes if memory
0071   // is not available.
0072   static void *Alloc(size_t request) ABSL_ATTRIBUTE_SECTION(malloc_hook);
0073   static void *AllocWithArena(size_t request, Arena *arena)
0074       ABSL_ATTRIBUTE_SECTION(malloc_hook);
0075 
0076   // Deallocates a region of memory that was previously allocated with
0077   // Alloc().   Does nothing if passed 0.   "s" must be either 0,
0078   // or must have been returned from a call to Alloc() and not yet passed to
0079   // Free() since that call to Alloc().  The space is returned to the arena
0080   // from which it was allocated.
0081   static void Free(void *s) ABSL_ATTRIBUTE_SECTION(malloc_hook);
0082 
0083   // ABSL_ATTRIBUTE_SECTION(malloc_hook) for Alloc* and Free
0084   // are to put all callers of MallocHook::Invoke* in this module
0085   // into special section,
0086   // so that MallocHook::GetCallerStackTrace can function accurately.
0087 
0088   // Create a new arena.
0089   // The root metadata for the new arena is allocated in the
0090   // meta_data_arena; the DefaultArena() can be passed for meta_data_arena.
0091   // These values may be ored into flags:
0092   enum {
0093     // Report calls to Alloc() and Free() via the MallocHook interface.
0094     // Set in the DefaultArena.
0095     kCallMallocHook = 0x0001,
0096 
0097 #ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
0098     // Make calls to Alloc(), Free() be async-signal-safe. Not set in
0099     // DefaultArena(). Not supported on all platforms.
0100     kAsyncSignalSafe = 0x0002,
0101 #endif
0102   };
0103   // Construct a new arena.  The allocation of the underlying metadata honors
0104   // the provided flags.  For example, the call NewArena(kAsyncSignalSafe)
0105   // is itself async-signal-safe, as well as generatating an arena that provides
0106   // async-signal-safe Alloc/Free.
0107   static Arena *NewArena(uint32_t flags);
0108 
0109   // Destroys an arena allocated by NewArena and returns true,
0110   // provided no allocated blocks remain in the arena.
0111   // If allocated blocks remain in the arena, does nothing and
0112   // returns false.
0113   // It is illegal to attempt to destroy the DefaultArena().
0114   static bool DeleteArena(Arena *arena);
0115 
0116   // The default arena that always exists.
0117   static Arena *DefaultArena();
0118 
0119  private:
0120   LowLevelAlloc();      // no instances
0121 };
0122 
0123 }  // namespace base_internal
0124 ABSL_NAMESPACE_END
0125 }  // namespace absl
0126 
0127 #endif  // ABSL_BASE_INTERNAL_LOW_LEVEL_ALLOC_H_