1 //===-- sanitizer_persistent_allocator.h ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // A fast memory allocator that does not support free() nor realloc().
11 // All allocations are forever.
12 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_PERSISTENT_ALLOCATOR_H
15 #define SANITIZER_PERSISTENT_ALLOCATOR_H
17 #include "sanitizer_internal_defs.h"
18 #include "sanitizer_mutex.h"
19 #include "sanitizer_atomic.h"
20 #include "sanitizer_common.h"
22 namespace __sanitizer {
24 class PersistentAllocator {
26 void *alloc(uptr size);
29 void *tryAlloc(uptr size);
30 StaticSpinMutex mtx; // Protects alloc of new blocks for region allocator.
31 atomic_uintptr_t region_pos; // Region allocator for Node's.
32 atomic_uintptr_t region_end;
35 inline void *PersistentAllocator::tryAlloc(uptr size) {
36 // Optimisic lock-free allocation, essentially try to bump the region ptr.
38 uptr cmp = atomic_load(®ion_pos, memory_order_acquire);
39 uptr end = atomic_load(®ion_end, memory_order_acquire);
40 if (cmp == 0 || cmp + size > end) return nullptr;
41 if (atomic_compare_exchange_weak(®ion_pos, &cmp, cmp + size,
42 memory_order_acquire))
47 inline void *PersistentAllocator::alloc(uptr size) {
48 // First, try to allocate optimisitically.
49 void *s = tryAlloc(size);
51 // If failed, lock, retry and alloc new superblock.
52 SpinMutexLock l(&mtx);
56 atomic_store(®ion_pos, 0, memory_order_relaxed);
57 uptr allocsz = 64 * 1024;
58 if (allocsz < size) allocsz = size;
59 uptr mem = (uptr)MmapOrDie(allocsz, "stack depot");
60 atomic_store(®ion_end, mem + allocsz, memory_order_release);
61 atomic_store(®ion_pos, mem, memory_order_release);
65 extern PersistentAllocator thePersistentAllocator;
66 inline void *PersistentAlloc(uptr sz) {
67 return thePersistentAllocator.alloc(sz);
70 } // namespace __sanitizer
72 #endif // SANITIZER_PERSISTENT_ALLOCATOR_H