/thirdparty/breakpad/client/mac/handler/protected_memory_allocator.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 85 lines · 21 code · 12 blank · 52 comment · 0 complexity · 668d9432f2f6a44b80190bb94496a98b MD5 · raw file

  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. #ifndef PROTECTED_MEMORY_ALLOCATOR_H__
  41. #define PROTECTED_MEMORY_ALLOCATOR_H__
  42. #include <mach/mach.h>
  43. //
  44. class ProtectedMemoryAllocator {
  45. public:
  46. ProtectedMemoryAllocator(vm_size_t pool_size);
  47. ~ProtectedMemoryAllocator();
  48. // Returns a pointer to an allocation of size n within the pool.
  49. // Fails by returning NULL is no more space is available.
  50. // Please note that the pointers returned from this method should not
  51. // be freed in any way (for example by calling free() on them ).
  52. char * Allocate(vm_size_t n);
  53. // Returns the base address of the allocation pool.
  54. char * GetBaseAddress() { return (char*)base_address_; }
  55. // Returns the size of the allocation pool, including allocated
  56. // plus free space.
  57. vm_size_t GetTotalSize() { return pool_size_; }
  58. // Returns the number of bytes already allocated in the pool.
  59. vm_size_t GetAllocatedSize() { return next_alloc_offset_; }
  60. // Returns the number of bytes available for allocation.
  61. vm_size_t GetFreeSize() { return pool_size_ - next_alloc_offset_; }
  62. // Makes the entire allocation pool read-only including, of course,
  63. // all allocations made from the pool.
  64. kern_return_t Protect();
  65. // Makes the entire allocation pool read/write.
  66. kern_return_t Unprotect();
  67. private:
  68. vm_size_t pool_size_;
  69. vm_address_t base_address_;
  70. vm_size_t next_alloc_offset_;
  71. bool valid_;
  72. };
  73. #endif // PROTECTED_MEMORY_ALLOCATOR_H__