xref: /aosp_15_r20/external/google-breakpad/src/client/mac/handler/protected_memory_allocator.h (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2006 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // ProtectedMemoryAllocator
30 //
31 // A very simple allocator class which allows allocation, but not deallocation.
32 // The allocations can be made read-only with the Protect() method.
33 // This class is NOT useful as a general-purpose memory allocation system,
34 // since it does not allow deallocation.  It is useful to use for a group
35 // of allocations which are created in the same time-frame and destroyed
36 // in the same time-frame.  It is useful for making allocations of memory
37 // which will not need to change often once initialized.  This memory can then
38 // be protected from memory smashers by calling the Protect() method.
39 
40 #ifndef PROTECTED_MEMORY_ALLOCATOR_H__
41 #define PROTECTED_MEMORY_ALLOCATOR_H__
42 
43 #include <mach/mach.h>
44 
45 //
46 class ProtectedMemoryAllocator {
47  public:
48   ProtectedMemoryAllocator(vm_size_t pool_size);
49   ~ProtectedMemoryAllocator();
50 
51   // Returns a pointer to an allocation of size n within the pool.
52   // Fails by returning NULL is no more space is available.
53   // Please note that the pointers returned from this method should not
54   // be freed in any way (for example by calling free() on them ).
55   char *         Allocate(vm_size_t n);
56 
57   // Returns the base address of the allocation pool.
GetBaseAddress()58   char *         GetBaseAddress() { return (char*)base_address_; }
59 
60   // Returns the size of the allocation pool, including allocated
61   // plus free space.
GetTotalSize()62   vm_size_t      GetTotalSize() { return pool_size_; }
63 
64   // Returns the number of bytes already allocated in the pool.
GetAllocatedSize()65   vm_size_t      GetAllocatedSize() { return next_alloc_offset_; }
66 
67   // Returns the number of bytes available for allocation.
GetFreeSize()68   vm_size_t      GetFreeSize() { return pool_size_ - next_alloc_offset_; }
69 
70   // Makes the entire allocation pool read-only including, of course,
71   // all allocations made from the pool.
72   kern_return_t  Protect();
73 
74   // Makes the entire allocation pool read/write.
75   kern_return_t  Unprotect();
76 
77  private:
78   vm_size_t      pool_size_;
79   vm_address_t   base_address_;
80   vm_size_t      next_alloc_offset_;
81   bool           valid_;
82 };
83 
84 #endif // PROTECTED_MEMORY_ALLOCATOR_H__
85