/sparrowhawk/foundation/ESFSharedAllocator.h

http://github.com/jtblatt/duderino · C Header · 161 lines · 33 code · 19 blank · 109 comment · 0 complexity · 0391a4ebc5f89e523813c04b18c7e3c1 MD5 · raw file

  1. /** @file ESFSharedAllocator.h
  2. * @brief A ESFAllocator decorator that can synchronize access to any allocator
  3. *
  4. * Copyright (c) 2009 Yahoo! Inc.
  5. * The copyrights embodied in the content of this file are licensed by Yahoo! Inc.
  6. * under the BSD (revised) open source license.
  7. *
  8. * Derived from code that is Copyright (c) 2009 Joshua Blatt and offered under both
  9. * BSD and Apache 2.0 licenses (http://sourceforge.net/projects/sparrowhawk/).
  10. *
  11. * $Author: blattj $
  12. * $Date: 2009/05/25 21:51:08 $
  13. * $Name: $
  14. * $Revision: 1.3 $
  15. */
  16. #ifndef ESF_SHARED_ALLOCATOR_H
  17. #define ESF_SHARED_ALLOCATOR_H
  18. #ifndef ESF_CONFIG_H
  19. #include <ESFConfig.h>
  20. #endif
  21. #ifndef ESF_ALLOCATOR_H
  22. #include <ESFAllocator.h>
  23. #endif
  24. #ifndef ESF_MUTEX_H
  25. #include <ESFMutex.h>
  26. #endif
  27. /** ESFSharedAllocator is a decorator that can synchronize access
  28. * to any other allocator with a mutex.
  29. *
  30. * @ingroup allocator
  31. */
  32. class ESFSharedAllocator: public ESFAllocator {
  33. public:
  34. /** Constructor.
  35. *
  36. * @param allocator The decorated allocator to synchronize.
  37. */
  38. ESFSharedAllocator(ESFAllocator *allocator);
  39. /** Destructor. If initialized, the allocator will call the its destroy
  40. * method. The destroy method may or may not return the allocator's
  41. * managed memory to its source allocator.
  42. */
  43. virtual ~ESFSharedAllocator();
  44. /** Allocate a word-aligned memory block of at least size bytes.
  45. *
  46. * @param size The minimum number of bytes to allocate.
  47. * @return a word-aligned memory block of at least size bytes if
  48. * successful, NULL otherwise.
  49. */
  50. virtual void *allocate(ESFUWord size);
  51. /** Deallocate a memory block allocated by this allocator or by its
  52. * failover allocators.
  53. *
  54. * @param block The block to deallocate (pointer to a pointer). If
  55. * freed, the block pointer will be set to NULL.
  56. * @return ESF_SUCCESS if the block was successfully deallocated, another
  57. * error code otherwise. ESF_NOT_OWNER will be returned if the
  58. * block was not allocated by this allocator.
  59. */
  60. virtual ESFError deallocate(void *block);
  61. /** Get the overhead in bytes of any additional control structures
  62. * attached to an allocated block. This can be used to optimize
  63. * allocation requests for some allocators. Power of two allocators,
  64. * for example, will always round the size requested + the overhead up
  65. * to the next power of two. If the caller always requests powers of
  66. * two, then the allocator can waste a lot of memory. If, however, the
  67. * caller requests a power of two minus the allocator overhead, then
  68. * the allocator will return the optimal amount of memory.
  69. *
  70. * @return the allocator's overhead in bytes for each allocation.
  71. */
  72. virtual ESFUWord getOverhead();
  73. /** Initialize this memory pool.
  74. *
  75. * @return ESF_SUCCESS if successful, another error code otherwise.
  76. */
  77. virtual ESFError initialize();
  78. /** Destroy this allocator. The allocator will return all of the memory
  79. * it manages to its source allocator and return itself to a state where
  80. * initialize could be called again. All memory should be returned to
  81. * the allocator before calling its destroy method. Some implementations
  82. * may refuse to shutdown if they have outstanding allocations.
  83. *
  84. * @return ESF_SUCCESS if the allocator could destroy itself, another
  85. * error code otherwise. ESF_IN_USE will be returned if the
  86. * allocator has handed out memory that has not been returned.
  87. * @see initialize.
  88. */
  89. virtual ESFError destroy();
  90. /** Get the allocator's current initialization state.
  91. *
  92. * @return ESF_SUCCESS if the allocator is initialized,
  93. * ESF_NOT_INITIALIZED if the allocator has not been initialized,
  94. * another error code if an error occurred determining the
  95. * allocator's current state.
  96. * @see initialize.
  97. */
  98. virtual ESFError isInitialized();
  99. /** Set another allocator to be used if this allocator cannot fulfill a
  100. * allocate request. This failover allocator will be initialized lazily.
  101. * If a failover allocator is not set, an allocator will simply return
  102. * NULL if it cannot fulfill an allocation request. Destroying an
  103. * allocator with registered failover allocators must also destroy those
  104. * failover allocators if they have been initialized. Failover allocators
  105. * may be chained. An allocator is responsible for determining whether
  106. * a block of memory to be deallocated came from itself or from its
  107. * failover allocator and take the appropriate action. Allocators should
  108. * never be used as failover allocators for themselves to avoid
  109. * infinite recursion.
  110. *
  111. * @param allocator The failover allocator. Set to NULL to clear an
  112. * already registered failover allocator.
  113. * @return ESF_SUCCESS if successful, another error code otherwise.
  114. */
  115. virtual ESFError setFailoverAllocator(ESFAllocator *allocator);
  116. /** Get the failover allocator used by this allocator.
  117. *
  118. * @param allocator The failover allocator to get (pointer to a pointer).
  119. * This pointer will be set to NULL if no failover allocator exists.
  120. * @return ESF_SUCCESS if successful, another error code otherwise. Note
  121. * that if no failover allocator exists, ESF_SUCCESS will be returned
  122. * and the allocator argument will be set to NULL.
  123. * @see setFailoverAllocator
  124. */
  125. virtual ESFError getFailoverAllocator(ESFAllocator **allocator);
  126. /** Placement new.
  127. *
  128. * @param size The size of the object.
  129. * @param allocator The source of the object's memory.
  130. * @return The new object or NULL of the memory allocation failed.
  131. */
  132. inline void *operator new(size_t size, ESFAllocator *allocator) {
  133. return allocator->allocate(size);
  134. }
  135. private:
  136. // Disabled
  137. ESFSharedAllocator(const ESFSharedAllocator &);
  138. ESFSharedAllocator &operator=(const ESFSharedAllocator &);
  139. ESFAllocator *_allocator;
  140. ESFMutex _mutex;
  141. };
  142. #endif /* ! ESF_SHARED_ALLOCATOR_H */