AsyncBuffer.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CAT_ASYNC_BUFFER_HPP
  26. #define CAT_ASYNC_BUFFER_HPP
  27. #include <cat/Platform.hpp>
  28. #include <cat/port/FastDelegate.h>
  29. #if defined(CAT_OS_WINDOWS)
  30. # include <cat/port/WindowsInclude.hpp>
  31. #endif
  32. namespace cat {
  33. class ThreadPoolLocalStorage;
  34. class AsyncBuffer;
  35. typedef fastdelegate::FastDelegate4<ThreadPoolLocalStorage *, int, AsyncBuffer *, u32, bool> AsyncCallback;
  36. // Overlapped base object
  37. #if defined(CAT_OS_WINDOWS)
  38. typedef OVERLAPPED AsyncOv;
  39. #else
  40. #error "TODO"
  41. #endif
  42. /*
  43. AsyncBuffer: Utility object representing the buffers for a single I/O operation.
  44. This is flexible enough to represent network and file IO buffers.
  45. */
  46. class AsyncBuffer
  47. {
  48. private:
  49. AsyncOv _ov;
  50. AsyncCallback _callback;
  51. u8 *_data;
  52. u32 _data_bytes, _tag_bytes;
  53. u8 _tag[1];
  54. public:
  55. // Reset AsyncOv and set offset and callback
  56. CAT_INLINE void Reset(const AsyncCallback &callback, u64 offset = 0)
  57. {
  58. #if defined(CAT_OS_WINDOWS)
  59. _ov.hEvent = 0;
  60. _ov.Internal = 0;
  61. _ov.InternalHigh = 0;
  62. _ov.OffsetHigh = (u32)(offset >> 32);
  63. _ov.Offset = (u32)offset;
  64. #else
  65. #error "TODO"
  66. #endif
  67. _callback = callback;
  68. }
  69. public:
  70. static CAT_INLINE u32 OVERHEAD() { return (u32)(offsetof(AsyncBuffer, _tag)); }
  71. CAT_INLINE AsyncOv *GetOv() { return &_ov; }
  72. CAT_INLINE u64 GetOffset() { return ((u64)_ov.OffsetHigh << 32) | _ov.Offset; }
  73. CAT_INLINE bool Call(ThreadPoolLocalStorage *tls, int error, AsyncBuffer *buffer, u32 bytes)
  74. {
  75. if (!_callback) return true;
  76. return _callback(tls, error, buffer, bytes);
  77. }
  78. CAT_INLINE void Zero()
  79. {
  80. CAT_CLR(_data, _data_bytes);
  81. }
  82. public:
  83. CAT_INLINE u32 GetDataBytes() { return _data_bytes; }
  84. CAT_INLINE u8 *GetData() { return reinterpret_cast<u8*>( _data ); }
  85. template<class T>
  86. CAT_INLINE T *GetData() { return reinterpret_cast<T*>( _data ); }
  87. template<class T>
  88. CAT_INLINE T *GetData(T * &ptr) { return (ptr = reinterpret_cast<T*>( _data )); }
  89. public:
  90. CAT_INLINE u32 GetTagBytes() { return _tag_bytes; }
  91. CAT_INLINE u8 *GetTagData() { return _tag; }
  92. template<class T>
  93. CAT_INLINE T *GetTag() { return reinterpret_cast<T*>( _tag ); }
  94. template<class T>
  95. CAT_INLINE T *GetTag(T * &ptr) { return (ptr = reinterpret_cast<T*>( _tag )); }
  96. public:
  97. // Acquire with built-in data pointer
  98. static CAT_INLINE AsyncBuffer *Acquire(AsyncBuffer * &ptr, u32 data_bytes = 0, u32 tag_bytes = 0)
  99. {
  100. const u32 OVERHEAD_BYTES = (u32)(offsetof(AsyncBuffer, _tag));
  101. AsyncBuffer *buffer = reinterpret_cast<AsyncBuffer*>(
  102. RegionAllocator::ii->Acquire(OVERHEAD_BYTES + data_bytes + tag_bytes) );
  103. if (!buffer) return 0;
  104. buffer->_data_bytes = data_bytes;
  105. buffer->_tag_bytes = tag_bytes;
  106. buffer->_data = buffer->_tag + tag_bytes;
  107. return (ptr = buffer);
  108. }
  109. // Change number of data bytes allocated to the buffer
  110. // Returns a new data pointer that may be different from the old data pointer
  111. CAT_INLINE AsyncBuffer *Resize(u32 data_bytes)
  112. {
  113. const u32 OVERHEAD_BYTES = (u32)(offsetof(AsyncBuffer, _tag));
  114. AsyncBuffer *buffer = reinterpret_cast<AsyncBuffer*>(
  115. RegionAllocator::ii->Resize( this,
  116. OVERHEAD_BYTES + _tag_bytes + data_bytes) );
  117. if (!buffer) return 0;
  118. buffer->_data_bytes = data_bytes;
  119. buffer->_data = buffer->_tag + buffer->_tag_bytes;
  120. return buffer;
  121. }
  122. public:
  123. // Acquire with built-in data pointer
  124. static CAT_INLINE u8 *Acquire(u32 data_bytes = 0, u32 tag_bytes = 0)
  125. {
  126. AsyncBuffer *buffer;
  127. if (!Acquire(buffer, data_bytes, tag_bytes)) return 0;
  128. return buffer->_data;
  129. }
  130. // Acquire with built-in data pointer
  131. template<class T>
  132. static CAT_INLINE T *Acquire(T * &data, u32 tag_bytes = 0)
  133. {
  134. AsyncBuffer *buffer;
  135. if (!Acquire(buffer, sizeof(T), tag_bytes)) return 0;
  136. return (data = reinterpret_cast<T*>( buffer->_data ));
  137. }
  138. // Change number of data bytes allocated to the buffer
  139. // Returns a new data pointer that may be different from the old data pointer
  140. static CAT_INLINE u8 *Resize(void *vdata, u32 data_bytes)
  141. {
  142. u8 *data = reinterpret_cast<u8*>( vdata );
  143. const u32 OVERHEAD_BYTES = (u32)(offsetof(AsyncBuffer, _tag));
  144. if (!data) return Acquire(data_bytes);
  145. AsyncBuffer *buffer = reinterpret_cast<AsyncBuffer*>( data - OVERHEAD_BYTES );
  146. buffer = reinterpret_cast<AsyncBuffer*>(
  147. RegionAllocator::ii->Resize(buffer,
  148. OVERHEAD_BYTES + buffer->_tag_bytes + data_bytes) );
  149. if (!buffer) return 0;
  150. buffer->_data_bytes = data_bytes;
  151. return (buffer->_data = buffer->_tag + buffer->_tag_bytes);
  152. }
  153. public:
  154. // Wrap external data pointer
  155. static CAT_INLINE AsyncBuffer *Wrap(void *vdata, u32 data_bytes, u32 tag_bytes = 0)
  156. {
  157. u8 *data = reinterpret_cast<u8*>( vdata );
  158. const u32 OVERHEAD_BYTES = (u32)(offsetof(AsyncBuffer, _tag));
  159. AsyncBuffer *buffer = reinterpret_cast<AsyncBuffer*>(
  160. RegionAllocator::ii->Acquire(OVERHEAD_BYTES + tag_bytes) );
  161. if (!buffer) return 0;
  162. buffer->_data_bytes = data_bytes;
  163. buffer->_tag_bytes = tag_bytes;
  164. buffer->_data = data;
  165. return buffer;
  166. }
  167. public:
  168. // Only works on Acquired() buffers, not Wrap()ed buffers
  169. static CAT_INLINE AsyncBuffer *Promote(void *vdata)
  170. {
  171. u8 *data = reinterpret_cast<u8*>( vdata );
  172. const u32 OVERHEAD_BYTES = (u32)(offsetof(AsyncBuffer, _tag));
  173. if (!data) return 0;
  174. return reinterpret_cast<AsyncBuffer*>( data - OVERHEAD_BYTES );
  175. }
  176. public:
  177. // Release memory
  178. CAT_INLINE void Release()
  179. {
  180. RegionAllocator::ii->Release(this);
  181. }
  182. static CAT_INLINE void Release(AsyncBuffer *buffer)
  183. {
  184. RegionAllocator::ii->Release(buffer);
  185. }
  186. static CAT_INLINE void Release(void *vdata)
  187. {
  188. u8 *data = reinterpret_cast<u8*>( vdata );
  189. const u32 OVERHEAD_BYTES = (u32)(offsetof(AsyncBuffer, _tag));
  190. if (!data) return;
  191. AsyncBuffer *buffer = reinterpret_cast<AsyncBuffer*>( data - OVERHEAD_BYTES );
  192. RegionAllocator::ii->Release(buffer);
  193. }
  194. };
  195. } // namespace cat
  196. #endif // CAT_ASYNC_BUFFER_HPP
粤ICP备19079148号