memory_segment_local.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef MEMORY_SEGMENT_LOCAL_H
  15. #define MEMORY_SEGMENT_LOCAL_H
  16. #include <util/memory_segment.h>
  17. namespace isc {
  18. namespace util {
  19. /// \brief malloc/free based Memory Segment class
  20. ///
  21. /// This class specifies a concrete implementation for a malloc/free
  22. /// based MemorySegment. Please see the MemorySegment class
  23. /// documentation for usage.
  24. class MemorySegmentLocal : public MemorySegment {
  25. public:
  26. /// \brief Constructor
  27. ///
  28. /// Creates a local memory segment object
  29. MemorySegmentLocal() : allocated_size_(0) {
  30. }
  31. /// \brief Destructor
  32. virtual ~MemorySegmentLocal() {}
  33. /// \brief Allocate/acquire a segment of memory. The source of the
  34. /// memory is libc's malloc().
  35. ///
  36. /// Throws <code>std::bad_alloc</code> if the implementation cannot
  37. /// allocate the requested storage.
  38. ///
  39. /// \param size The size of the memory requested in bytes.
  40. /// \return Returns pointer to the memory allocated.
  41. virtual void* allocate(size_t size);
  42. /// \brief Free/release a segment of memory.
  43. ///
  44. /// This method may throw <code>isc::OutOfRange</code> if \c size is
  45. /// not equal to the originally allocated size.
  46. ///
  47. /// \param ptr Pointer to the block of memory to free/release. This
  48. /// should be equal to a value returned by <code>allocate()</code>.
  49. /// \param size The size of the memory to be freed in bytes. This
  50. /// should be equal to the number of bytes originally allocated.
  51. virtual void deallocate(void* ptr, size_t size);
  52. /// \brief Check if all allocated memory was deallocated.
  53. ///
  54. /// \return Returns <code>true</code> if all allocated memory was
  55. /// deallocated, <code>false</code> otherwise.
  56. virtual bool allMemoryDeallocated() const;
  57. private:
  58. // allocated_size_ can underflow, wrap around to max size_t (which
  59. // is unsigned). But because we only do a check against 0 and not a
  60. // relation comparison, this is okay.
  61. size_t allocated_size_;
  62. };
  63. } // namespace util
  64. } // namespace isc
  65. #endif // MEMORY_SEGMENT_LOCAL_H