vxwMemPartLib.h
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:8k
源码类别:

VxWorks

开发平台:

C/C++

  1. // VXWMemPart/vxwMemPartLib.h - memory partition classes
  2. // Copyright 1995-1999 Wind River Systems, Inc.
  3. // modification history
  4. // --------------------
  5. // 01c,08mar99,jdi  doc: fixed wrong cross-references.
  6. // 01b,23feb99,fle  doc : made it refgen compliant
  7. // 01c,21feb99,jdi  added library section, checked in documentation.
  8. // 01b,03oct95,rhp  documented.
  9. // 01a,15jun95,srh  written.
  10. // DESCRIPTION
  11. // The `VXWMemPart' class provides core facilities for managing the
  12. // allocation of blocks of memory from ranges of memory called memory
  13. // partitions.
  14. //
  15. // The allocation of memory, using routines such as
  16. // VXWMemPart::alloc(), is done with a first-fit algorithm.  Adjacent
  17. // blocks of memory are coalesced when they are freed with
  18. // VXWMemPart::free().  There is also a routine provided for
  19. // allocating memory aligned to a specified boundary from a specific
  20. // memory partition, VXWMemPart::alignedAlloc().
  21. //
  22. // CAVEATS
  23. // Architectures have various alignment constraints.  To provide
  24. // optimal performance, VXWMemPart::alloc() returns a pointer to a
  25. // buffer having the appropriate alignment for the architecture in
  26. // use.  The portion of the allocated buffer reserved for system
  27. // bookkeeping, known as the overhead, may vary depending on the
  28. // architecture.
  29. //
  30. // .TS
  31. // center,tab(|);
  32. // lf3 cf3 cf3
  33. // a n n .
  34. // Architecture | Boundary | Overhead
  35. // _
  36. //  68K   | 4  | 8
  37. //  SPARC | 8  | 12
  38. //  MIPS  | 8  | 12
  39. //  i960  | 16 | 16
  40. // .TE
  41. //
  42. // INCLUDE FILES: vxwMemPartLib.h
  43. //
  44. // SEE ALSO: VXWSem
  45. //
  46. // SECTION: 1C
  47. //
  48. #ifndef vxwMemPartLib_h
  49. #define vxwMemPartLib_h
  50. #include "vxWorks.h"
  51. #include "memLib.h"
  52. #include "stdlib.h"
  53. #include "private/memPartLibP.h"
  54. #include "vxwObject.h"
  55. #include "vxwErr.h"
  56. class VXWMemPart : virtual public VXWIdObject
  57.     {
  58.   public:
  59. //_ VXWMemPart Public Constructors
  60. ///////////////////////////////////////////////////////////////////////////////
  61. //
  62. // VXWMemPart::VXWMemPart - create a memory partition
  63. //
  64. // This constructor creates a new memory partition containing a specified
  65. // memory pool.  Partitions can be created to manage
  66. // any number of separate memory pools.
  67. //
  68. // NOTE
  69. // The descriptor for the new partition is allocated out of the system memory
  70. // partition (i.e., with malloc()).
  71. //
  72. // RETURNS: N/A.
  73.     VXWMemPart (char *pool, unsigned poolSize)
  74. : partid_ (memPartCreate (pool, poolSize))
  75. {
  76. if (partid_ == NULL)
  77.     vxwThrowErrno ();
  78. }
  79. //_ VXWMemPart Public Member Functions
  80. ///////////////////////////////////////////////////////////////////////////////
  81. //
  82. // VXWMemPart::addToPool - add memory to a memory partition
  83. //
  84. // This routine adds memory to its memory partition.
  85. // The new memory added need not be contiguous with
  86. // memory previously assigned to the partition.
  87. //
  88. // RETURNS: OK or ERROR.
  89.     STATUS addToPool (char *pool, unsigned poolSize)
  90. {
  91. return memPartAddToPool (partid_, pool, poolSize);
  92. }
  93. ///////////////////////////////////////////////////////////////////////////////
  94. //
  95. // VXWMemPart::alignedAlloc - allocate aligned memory from partition
  96. //
  97. // This routine allocates a buffer of size <nBytes> from its
  98. // partition.  Additionally, it ensures that the allocated buffer begins on a
  99. // memory address evenly divisible by <alignment>.  The <alignment> parameter
  100. // must be a power of 2.
  101. //
  102. // RETURNS:
  103. // A pointer to the newly allocated block, or NULL if the buffer cannot be
  104. // allocated.
  105.     void * alignedAlloc (unsigned nBytes, unsigned alignment)
  106. {
  107. return memPartAlignedAlloc (partid_, nBytes, alignment);
  108. }
  109. ///////////////////////////////////////////////////////////////////////////////
  110. //
  111. // VXWMemPart::alloc - allocate a block of memory from partition
  112. //
  113. // This routine allocates a block of memory from its partition. 
  114. // The size of the block allocated is equal to or greater than <nBytes>.  
  115. //
  116. // RETURNS:
  117. // A pointer to a block, or NULL if the call fails.
  118. //
  119. // SEE ALSO: VXWMemPart::free()
  120.     void * alloc (unsigned nBytes)
  121. {
  122. return memPartAlloc (partid_, nBytes);
  123. }
  124. ///////////////////////////////////////////////////////////////////////////////
  125. //
  126. // VXWMemPart::findMax - find the size of the largest available free block
  127. //
  128. // This routine searches for the largest block in the memory partition free
  129. // list and returns its size.
  130. //
  131. // RETURNS: The size, in bytes, of the largest available block.
  132.     int findMax () const
  133. {
  134. return memPartFindMax (partid_);
  135. }
  136. ///////////////////////////////////////////////////////////////////////////////
  137. //
  138. // VXWMemPart::free - free a block of memory in partition
  139. //
  140. // This routine returns to the partition's free memory list a block of 
  141. // memory previously allocated with VXWMemPart::alloc().
  142. //
  143. // RETURNS: OK, or ERROR if the block is invalid.
  144. //
  145. // SEE ALSO: VXWMemPart::alloc()
  146.     STATUS free (char *pBlock)
  147. {
  148. return memPartFree (partid_, pBlock);
  149. }
  150. ///////////////////////////////////////////////////////////////////////////////
  151. //
  152. // VXWMemPart::info - get partition information
  153. //
  154. // This routine takes a pointer to a MEM_PART_STATS structure.
  155. // All the parameters of the structure are filled in with the current partition
  156. // information.
  157. //
  158. // RETURNS: OK if the structure has valid data, otherwise ERROR.
  159. //
  160. // SEE ALSO: VXWMemPart::show()
  161.     STATUS info (MEM_PART_STATS *pPartStats) const
  162. {
  163. return memPartInfoGet (partid_, pPartStats);
  164. }
  165. ///////////////////////////////////////////////////////////////////////////////
  166. //
  167. // VXWMemPart::options - set the debug options for memory partition
  168. //
  169. // This routine sets the debug options for its memory partition.
  170. // Two kinds of errors are detected: attempts to allocate more memory
  171. // than is available, and bad blocks found when memory is freed.  In both
  172. // cases, the error status is returned.  There are four error-handling
  173. // options that can be individually selected:
  174. // 
  175. // .iP "MEM_ALLOC_ERROR_LOG_FLAG" 8
  176. // Log a message when there is an error in allocating memory.
  177. // .iP "MEM_ALLOC_ERROR_SUSPEND_FLAG"
  178. // Suspend the task when there is an error in allocating memory (unless
  179. // the task was spawned with the VX_UNBREAKABLE option, in which case it
  180. // cannot be suspended).
  181. // .iP "MEM_BLOCK_ERROR_LOG_FLAG"
  182. // Log a message when there is an error in freeing memory.
  183. // .iP "MEM_BLOCK_ERROR_SUSPEND_FLAG"
  184. // Suspend the task when there is an error in freeing memory (unless
  185. // the task was spawned with the VX_UNBREAKABLE option, in which case it
  186. // cannot be suspended).
  187. // .LP
  188. // 
  189. // These options are discussed in detail in the library manual entry for
  190. // memLib.
  191. // 
  192. // RETURNS: OK or ERROR.
  193.     STATUS options (unsigned options)
  194. {
  195. return memPartOptionsSet (partid_, options);
  196. }
  197. ///////////////////////////////////////////////////////////////////////////////
  198. //
  199. // VXWMemPart::realloc - reallocate a block of memory in partition
  200. //
  201. // This routine changes the size of a specified block of memory and returns a
  202. // pointer to the new block.  The contents that fit inside the new size (or
  203. // old size if smaller) remain unchanged.  The memory alignment of the new
  204. // block is not guaranteed to be the same as the original block.
  205. //
  206. // If <pBlock> is NULL, this call is equivalent to VXWMemPart::alloc().
  207. //
  208. // RETURNS:
  209. // A pointer to the new block of memory, or NULL if the call fails.
  210.     void * realloc (char *pBlock, int nBytes)
  211. {
  212. return memPartRealloc (partid_, pBlock, nBytes);
  213. }
  214. ///////////////////////////////////////////////////////////////////////////////
  215. //
  216. // VXWMemPart::show - show partition blocks and statistics
  217. //
  218. // This routine displays statistics about the available and allocated memory
  219. // in its memory partition.  It shows the number of bytes, the number
  220. // of blocks, and the average block size in both free and allocated memory,
  221. // and also the maximum block size of free memory.  It also shows the number
  222. // of blocks currently allocated and the average allocated block size.
  223. //
  224. // In addition, if <type> is 1, the routine displays a list of all the blocks
  225. // in the free list of the specified partition.
  226. //
  227. // RETURNS: OK or ERROR.
  228.     STATUS show (int type = 0) const
  229. {
  230. return memPartShow (partid_, type);
  231. }
  232.   protected:
  233.     VXWMemPart ()
  234. {
  235. }
  236.     VXWMemPart (PART_ID aPartId)
  237. : partid_ (aPartId)
  238. {
  239. }
  240.     VXWMemPart (const VXWMemPart & aMemPart)
  241. : partid_ (aMemPart.partid_)
  242. {
  243. }
  244.     VXWMemPart & operator = (const VXWMemPart & aMemPart)
  245. {
  246. partid_ = aMemPart.partid_;
  247. return *this;
  248. }
  249.     ~VXWMemPart ()
  250. {
  251. }
  252.     virtual void * myValue ();
  253.     PART_ID partid_;
  254.     };
  255. #endif /* ifndef vxwMemPartLib_h */