15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Copyright (c) 2006, Google Inc.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * All rights reserved.
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * modification, are permitted provided that the following conditions are
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * met:
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     * Redistributions of source code must retain the above copyright
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * notice, this list of conditions and the following disclaimer.
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     * Redistributions in binary form must reproduce the above
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * in the documentation and/or other materials provided with the
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * distribution.
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * contributors may be used to endorse or promote products derived from
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * this software without specific prior written permission.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(_BASE_LOW_LEVEL_ALLOC_H_)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define _BASE_LOW_LEVEL_ALLOC_H_
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A simple thread-safe memory allocator that does not depend on
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// mutexes or thread-specific data.  It is intended to be used
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// sparingly, and only when malloc() would introduce an unwanted
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// dependency, such as inside the heap-checker.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <config.h>
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stddef.h>             // for size_t
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class LowLevelAlloc {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct Arena;       // an arena from which memory may be allocated
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a pointer to a block of at least "request" bytes
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that have been newly allocated from the specific arena.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for Alloc() call the DefaultArena() is used.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns 0 if passed request==0.
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Does not return 0 under other circumstances; it crashes if memory
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is not available.
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void *Alloc(size_t request)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ATTRIBUTE_SECTION(malloc_hook);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void *AllocWithArena(size_t request, Arena *arena)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ATTRIBUTE_SECTION(malloc_hook);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deallocates a region of memory that was previously allocated with
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Alloc().   Does nothing if passed 0.   "s" must be either 0,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // or must have been returned from a call to Alloc() and not yet passed to
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Free() since that call to Alloc().  The space is returned to the arena
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // from which it was allocated.
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void Free(void *s) ATTRIBUTE_SECTION(malloc_hook);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // ATTRIBUTE_SECTION(malloc_hook) for Alloc* and Free
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // are to put all callers of MallocHook::Invoke* in this module
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // into special section,
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // so that MallocHook::GetCallerStackTrace can function accurately.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Create a new arena.
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The root metadata for the new arena is allocated in the
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // meta_data_arena; the DefaultArena() can be passed for meta_data_arena.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These values may be ored into flags:
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum {
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Report calls to Alloc() and Free() via the MallocHook interface.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Set in the DefaultArena.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kCallMallocHook = 0x0001,
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Make calls to Alloc(), Free() be async-signal-safe.  Not set in
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // DefaultArena().
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kAsyncSignalSafe = 0x0002,
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // When used with DefaultArena(), the NewArena() and DeleteArena() calls
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // obey the flags given explicitly in the NewArena() call, even if those
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // flags differ from the settings in DefaultArena().  So the call
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // NewArena(kAsyncSignalSafe, DefaultArena()) is itself async-signal-safe,
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // as well as generatating an arena that provides async-signal-safe
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Alloc/Free.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Arena *NewArena(int32 flags, Arena *meta_data_arena);
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Destroys an arena allocated by NewArena and returns true,
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // provided no allocated blocks remain in the arena.
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If allocated blocks remain in the arena, does nothing and
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returns false.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It is illegal to attempt to destroy the DefaultArena().
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool DeleteArena(Arena *arena);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The default arena that always exists.
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Arena *DefaultArena();
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LowLevelAlloc();      // no instances
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
107