memory_segment_local.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include <string>
  18. #include <map>
  19. namespace isc {
  20. namespace util {
  21. /// \brief malloc/free based Memory Segment class
  22. ///
  23. /// This class specifies a concrete implementation for a malloc/free
  24. /// based MemorySegment. Please see the MemorySegment class
  25. /// documentation for usage.
  26. class MemorySegmentLocal : public MemorySegment {
  27. public:
  28. /// \brief Constructor
  29. ///
  30. /// Creates a local memory segment object
  31. MemorySegmentLocal() : allocated_size_(0) {
  32. }
  33. /// \brief Destructor
  34. virtual ~MemorySegmentLocal() {}
  35. /// \brief Allocate/acquire a segment of memory. The source of the
  36. /// memory is libc's malloc().
  37. ///
  38. /// Throws <code>std::bad_alloc</code> if the implementation cannot
  39. /// allocate the requested storage.
  40. ///
  41. /// \param size The size of the memory requested in bytes.
  42. /// \return Returns pointer to the memory allocated.
  43. virtual void* allocate(size_t size);
  44. /// \brief Free/release a segment of memory.
  45. ///
  46. /// This method may throw <code>isc::OutOfRange</code> if \c size is
  47. /// not equal to the originally allocated size.
  48. ///
  49. /// \param ptr Pointer to the block of memory to free/release. This
  50. /// should be equal to a value returned by <code>allocate()</code>.
  51. /// \param size The size of the memory to be freed in bytes. This
  52. /// should be equal to the number of bytes originally allocated.
  53. virtual void deallocate(void* ptr, size_t size);
  54. /// \brief Check if all allocated memory was deallocated.
  55. ///
  56. /// \return Returns <code>true</code> if all allocated memory was
  57. /// deallocated, <code>false</code> otherwise.
  58. virtual bool allMemoryDeallocated() const;
  59. /// \brief Local segment version of getNamedAddress.
  60. ///
  61. /// There's a small chance this method could throw std::bad_alloc.
  62. /// It should be considered a fatal error.
  63. virtual NamedAddressResult getNamedAddressImpl(const char* name) const;
  64. /// \brief Local segment version of setNamedAddress.
  65. ///
  66. /// This version does not validate the given address to see whether it
  67. /// belongs to this segment.
  68. ///
  69. /// This implementation of this method always returns \c false (but the
  70. /// application should expect a return value of \c true unless it knows
  71. /// the memory segment class is \c MemorySegmentLocal and needs to
  72. /// exploit the fact).
  73. virtual bool setNamedAddressImpl(const char* name, void* addr);
  74. /// \brief Local segment version of clearNamedAddress.
  75. ///
  76. /// There's a small chance this method could throw std::bad_alloc.
  77. /// It should be considered a fatal error.
  78. virtual bool clearNamedAddressImpl(const char* name);
  79. private:
  80. // allocated_size_ can underflow, wrap around to max size_t (which
  81. // is unsigned). But because we only do a check against 0 and not a
  82. // relation comparison, this is okay.
  83. size_t allocated_size_;
  84. std::map<std::string, void*> named_addrs_;
  85. };
  86. } // namespace util
  87. } // namespace isc
  88. #endif // MEMORY_SEGMENT_LOCAL_H
  89. // Local Variables:
  90. // mode: c++
  91. // End: