1 /* Copyright (c) 2006, Google Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #if !defined(_BASE_LOW_LEVEL_ALLOC_H_)
32 #define _BASE_LOW_LEVEL_ALLOC_H_
34 // A simple thread-safe memory allocator that does not depend on
35 // mutexes or thread-specific data. It is intended to be used
36 // sparingly, and only when malloc() would introduce an unwanted
37 // dependency, such as inside the heap-checker.
40 #include <stddef.h> // for size_t
41 #include "base/basictypes.h"
45 struct Arena
; // an arena from which memory may be allocated
47 // Returns a pointer to a block of at least "request" bytes
48 // that have been newly allocated from the specific arena.
49 // for Alloc() call the DefaultArena() is used.
50 // Returns 0 if passed request==0.
51 // Does not return 0 under other circumstances; it crashes if memory
53 static void *Alloc(size_t request
)
54 ATTRIBUTE_SECTION(malloc_hook
);
55 static void *AllocWithArena(size_t request
, Arena
*arena
)
56 ATTRIBUTE_SECTION(malloc_hook
);
58 // Deallocates a region of memory that was previously allocated with
59 // Alloc(). Does nothing if passed 0. "s" must be either 0,
60 // or must have been returned from a call to Alloc() and not yet passed to
61 // Free() since that call to Alloc(). The space is returned to the arena
62 // from which it was allocated.
63 static void Free(void *s
) ATTRIBUTE_SECTION(malloc_hook
);
65 // ATTRIBUTE_SECTION(malloc_hook) for Alloc* and Free
66 // are to put all callers of MallocHook::Invoke* in this module
67 // into special section,
68 // so that MallocHook::GetCallerStackTrace can function accurately.
70 // Create a new arena.
71 // The root metadata for the new arena is allocated in the
72 // meta_data_arena; the DefaultArena() can be passed for meta_data_arena.
73 // These values may be ored into flags:
75 // Report calls to Alloc() and Free() via the MallocHook interface.
76 // Set in the DefaultArena.
77 kCallMallocHook
= 0x0001,
79 // Make calls to Alloc(), Free() be async-signal-safe. Not set in
81 kAsyncSignalSafe
= 0x0002,
83 // When used with DefaultArena(), the NewArena() and DeleteArena() calls
84 // obey the flags given explicitly in the NewArena() call, even if those
85 // flags differ from the settings in DefaultArena(). So the call
86 // NewArena(kAsyncSignalSafe, DefaultArena()) is itself async-signal-safe,
87 // as well as generatating an arena that provides async-signal-safe
90 static Arena
*NewArena(int32 flags
, Arena
*meta_data_arena
);
92 // Destroys an arena allocated by NewArena and returns true,
93 // provided no allocated blocks remain in the arena.
94 // If allocated blocks remain in the arena, does nothing and
96 // It is illegal to attempt to destroy the DefaultArena().
97 static bool DeleteArena(Arena
*arena
);
99 // The default arena that always exists.
100 static Arena
*DefaultArena();
103 LowLevelAlloc(); // no instances