Home | History | Annotate | Download | only in handler
      1 // Copyright (c) 2006, Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      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
     13 // distribution.
     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.
     17 //
     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.
     29 //
     30 // ProtectedMemoryAllocator
     31 //
     32 // A very simple allocator class which allows allocation, but not deallocation.
     33 // The allocations can be made read-only with the Protect() method.
     34 // This class is NOT useful as a general-purpose memory allocation system,
     35 // since it does not allow deallocation.  It is useful to use for a group
     36 // of allocations which are created in the same time-frame and destroyed
     37 // in the same time-frame.  It is useful for making allocations of memory
     38 // which will not need to change often once initialized.  This memory can then
     39 // be protected from memory smashers by calling the Protect() method.
     40 
     41 #ifndef PROTECTED_MEMORY_ALLOCATOR_H__
     42 #define PROTECTED_MEMORY_ALLOCATOR_H__
     43 
     44 #include <mach/mach.h>
     45 
     46 //
     47 class ProtectedMemoryAllocator {
     48  public:
     49   ProtectedMemoryAllocator(vm_size_t pool_size);
     50   ~ProtectedMemoryAllocator();
     51 
     52   // Returns a pointer to an allocation of size n within the pool.
     53   // Fails by returning NULL is no more space is available.
     54   // Please note that the pointers returned from this method should not
     55   // be freed in any way (for example by calling free() on them ).
     56   char *         Allocate(vm_size_t n);
     57 
     58   // Returns the base address of the allocation pool.
     59   char *         GetBaseAddress() { return (char*)base_address_; }
     60 
     61   // Returns the size of the allocation pool, including allocated
     62   // plus free space.
     63   vm_size_t      GetTotalSize() { return pool_size_; }
     64 
     65   // Returns the number of bytes already allocated in the pool.
     66   vm_size_t      GetAllocatedSize() { return next_alloc_offset_; }
     67 
     68   // Returns the number of bytes available for allocation.
     69   vm_size_t      GetFreeSize() { return pool_size_ - next_alloc_offset_; }
     70 
     71   // Makes the entire allocation pool read-only including, of course,
     72   // all allocations made from the pool.
     73   kern_return_t  Protect();
     74 
     75   // Makes the entire allocation pool read/write.
     76   kern_return_t  Unprotect();
     77 
     78  private:
     79   vm_size_t      pool_size_;
     80   vm_address_t   base_address_;
     81   vm_size_t      next_alloc_offset_;
     82   bool           valid_;
     83 };
     84 
     85 #endif // PROTECTED_MEMORY_ALLOCATOR_H__
     86