blob: 406bfff0ca5cc514dfd75ace51e7218f1a4a1722 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2/* Copyright (c) 2006, Google Inc.
3 * All rights reserved.
Brian Silverman20350ac2021-11-17 18:19:55 -08004 *
Austin Schuh745610d2015-09-06 18:19:50 -07005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
Brian Silverman20350ac2021-11-17 18:19:55 -08008 *
Austin Schuh745610d2015-09-06 18:19:50 -07009 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
Brian Silverman20350ac2021-11-17 18:19:55 -080018 *
Austin Schuh745610d2015-09-06 18:19:50 -070019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#if !defined(_BASE_LOW_LEVEL_ALLOC_H_)
33#define _BASE_LOW_LEVEL_ALLOC_H_
34
35// A simple thread-safe memory allocator that does not depend on
36// mutexes or thread-specific data. It is intended to be used
37// sparingly, and only when malloc() would introduce an unwanted
38// dependency, such as inside the heap-checker.
39
40#include <config.h>
41#include <stddef.h> // for size_t
42#include "base/basictypes.h"
43
44class LowLevelAlloc {
45 public:
Brian Silverman20350ac2021-11-17 18:19:55 -080046 class PagesAllocator {
47 public:
48 virtual ~PagesAllocator();
49 virtual void *MapPages(int32 flags, size_t size) = 0;
50 virtual void UnMapPages(int32 flags, void *addr, size_t size) = 0;
51 };
52
53 static PagesAllocator *GetDefaultPagesAllocator(void);
54
Austin Schuh745610d2015-09-06 18:19:50 -070055 struct Arena; // an arena from which memory may be allocated
56
57 // Returns a pointer to a block of at least "request" bytes
58 // that have been newly allocated from the specific arena.
59 // for Alloc() call the DefaultArena() is used.
60 // Returns 0 if passed request==0.
61 // Does not return 0 under other circumstances; it crashes if memory
62 // is not available.
63 static void *Alloc(size_t request)
64 ATTRIBUTE_SECTION(malloc_hook);
65 static void *AllocWithArena(size_t request, Arena *arena)
66 ATTRIBUTE_SECTION(malloc_hook);
67
68 // Deallocates a region of memory that was previously allocated with
69 // Alloc(). Does nothing if passed 0. "s" must be either 0,
70 // or must have been returned from a call to Alloc() and not yet passed to
71 // Free() since that call to Alloc(). The space is returned to the arena
72 // from which it was allocated.
73 static void Free(void *s) ATTRIBUTE_SECTION(malloc_hook);
74
75 // ATTRIBUTE_SECTION(malloc_hook) for Alloc* and Free
76 // are to put all callers of MallocHook::Invoke* in this module
77 // into special section,
78 // so that MallocHook::GetCallerStackTrace can function accurately.
79
80 // Create a new arena.
81 // The root metadata for the new arena is allocated in the
82 // meta_data_arena; the DefaultArena() can be passed for meta_data_arena.
83 // These values may be ored into flags:
84 enum {
85 // Report calls to Alloc() and Free() via the MallocHook interface.
86 // Set in the DefaultArena.
87 kCallMallocHook = 0x0001,
88
89 // Make calls to Alloc(), Free() be async-signal-safe. Not set in
90 // DefaultArena().
91 kAsyncSignalSafe = 0x0002,
92
93 // When used with DefaultArena(), the NewArena() and DeleteArena() calls
94 // obey the flags given explicitly in the NewArena() call, even if those
95 // flags differ from the settings in DefaultArena(). So the call
96 // NewArena(kAsyncSignalSafe, DefaultArena()) is itself async-signal-safe,
97 // as well as generatating an arena that provides async-signal-safe
98 // Alloc/Free.
99 };
100 static Arena *NewArena(int32 flags, Arena *meta_data_arena);
101
Brian Silverman20350ac2021-11-17 18:19:55 -0800102 // note: pages allocator will never be destroyed and allocated pages will never be freed
103 // When allocator is NULL, it's same as NewArena
104 static Arena *NewArenaWithCustomAlloc(int32 flags, Arena *meta_data_arena, PagesAllocator *allocator);
105
Austin Schuh745610d2015-09-06 18:19:50 -0700106 // Destroys an arena allocated by NewArena and returns true,
107 // provided no allocated blocks remain in the arena.
108 // If allocated blocks remain in the arena, does nothing and
109 // returns false.
110 // It is illegal to attempt to destroy the DefaultArena().
111 static bool DeleteArena(Arena *arena);
112
113 // The default arena that always exists.
114 static Arena *DefaultArena();
115
116 private:
117 LowLevelAlloc(); // no instances
118};
119
120#endif