カスタムバイトバッファキュー
5514 ワード
設計上:
(1)可変長バイトキューに読み書きカーソル制御を追加する.
(2)タイプがダイナミックな長さのコンテナの場合:
(2-1)リードカーソルを移動し、読み取り可能データが残っていない場合はリードライトカーソルをリセットし、読み取り可能データがある場合は可変長バイトキュー内に読み取り可能データを移動する.
(2-2)書き込みカーソルを移動すると、書き込みスペースが不足すると、バイトキューのサイズが動的に広がります.
(3)タイプが静的長さコンテナの場合,静的バイト配列サイズは変更できない.
(4)静的および動的文字キューは同じ対外インタフェースを保持する.
次のように適用されます.
動的長さタイプカスタムバイトバッファキューは、スイートインタフェースがデータを受信および送信するバッファに使用できます.
静的長さタイプカスタムバイトバッファキューは、単一の命令キャッシュおよび処理に使用できます.
1.カスタムバイトバッファキューテンプレート実装
テンプレートタイプは実装され、実際のタイプは長くなるstlのvectorまたは文字配列char[]であり、外部に一貫したインタフェースを提供することができる.長いタイプになるには、読み書きカーソルを管理する必要があります.
2、動的にメモリを割り当てるバッファキュー
ダイナミックメモリのバッファ、ダイナミック拡張可能なバッファサイズ
定義:
typedef ByteBuffer> t_BufferCmdQueue;
コンストラクション関数、メモリを動的に割り当てるバッファ、サイズをいつでも拡張できます(テンプレートバイアス)
バッファメモリを再整理し、バッファにデータを書き込み、バッファサイズが不足している場合はバッファサイズを再調整し、サイズ調整はtrunkSizeの整数倍で原則として増加する
(sizeはバッファにどれだけのデータが書き込まれているか、テンプレートが特化している)
ただし、メモリブロック単位サイズ(64 kバイト)
3、静的にメモリを割り当てるバッファキュー
静的サイズのバッファは、スタック空間配列でメモリを割り当て、一時的な変数の取得に使用されます.
定義:
コンストラクション関数、静的配列のバッファ、サイズは随時変更できません(テンプレートバイアス)
静的な大きなスタック文字キュー、サイズ:
(1)可変長バイトキューに読み書きカーソル制御を追加する.
(2)タイプがダイナミックな長さのコンテナの場合:
(2-1)リードカーソルを移動し、読み取り可能データが残っていない場合はリードライトカーソルをリセットし、読み取り可能データがある場合は可変長バイトキュー内に読み取り可能データを移動する.
(2-2)書き込みカーソルを移動すると、書き込みスペースが不足すると、バイトキューのサイズが動的に広がります.
(3)タイプが静的長さコンテナの場合,静的バイト配列サイズは変更できない.
(4)静的および動的文字キューは同じ対外インタフェースを保持する.
次のように適用されます.
動的長さタイプカスタムバイトバッファキューは、スイートインタフェースがデータを受信および送信するバッファに使用できます.
静的長さタイプカスタムバイトバッファキューは、単一の命令キャッシュおよび処理に使用できます.
1.カスタムバイトバッファキューテンプレート実装
テンプレートタイプは実装され、実際のタイプは長くなるstlのvectorまたは文字配列char[]であり、外部に一貫したインタフェースを提供することができる.長いタイプになるには、読み書きカーソルを管理する必要があります.
template <typename _type>//_type
class ByteBuffer
{
public:
ByteBuffer();
/**
* \
* \buf
* \size
*/
inline void put(const unsigned char *buf, const unsigned int size);
/**
* \
* wr_reserve(size)
* \
*/
inline unsigned char *wr_buf();
/**
* \
* \
*/
inline unsigned char *rd_buf();
/**
* \
* \
*/
inline bool rd_ready() const;
/**
*
* \
*/
inline unsigned int rd_size() const;
/**
* \ ,
* \size
*/
inline void rd_flip(unsigned int size);
/**
*
* \
*/
inline unsigned int wr_size() const;
/**
* \description ,
* \param size
*/
inline void wr_flip(const unsigned int size);
/**
* ,
*/
inline void reset();
/**
* \description
* \return
*/
inline unsigned int maxSize() const;
/**
* \description , , , ,
* trunkSize
* \param size
*/
inline void wr_reserve(const unsigned int size);
private:
//
unsigned int _maxSize;
//
unsigned int _offPtr;
//
unsigned int _currPtr;
//
_type _buffer;
};
template <typename _type>
void ByteBuffer<_type>::put(const unsigned char *buf, const unsigned int size)
{
//
wr_reserve(size);
bcopy(buf, &_buffer[_currPtr], size);
_currPtr += size;
}
template <typename _type>
unsigned char *ByteBuffer<_type>::wr_buf()
{
return &_buffer[_currPtr];
}
template <typename _type>
unsigned char *ByteBuffer<_type>::rd_buf()
{
return &_buffer[_offPtr];
}
template <typename _type>
bool ByteBuffer<_type>::rd_ready() const
{
return _currPtr > _offPtr;
}
template <typename _type>
unsigned int ByteBuffer<_type>::rd_size() const
{
return _currPtr - _offPtr;
}
template <typename _type>
void ByteBuffer<_type>::rd_flip(unsigned int size)
{
_offPtr += size;
if (_currPtr > _offPtr)//
{
unsigned int tmp = _currPtr - _offPtr;//
if(_offPtr >= tmp)//
{
memmove(&_buffer[0], &_buffer[_offPtr], tmp);
_offPtr = 0; //
_currPtr = tmp;
}
}
else//
{
_offPtr = 0;
_currPtr = 0;
}
}
//
template <typename _type>
unsigned int ByteBuffer<_type>::wr_size() const
{
return _maxSize - _currPtr;
}
//
template <typename _type>
void ByteBuffer<_type>::wr_flip(const unsigned int size)
{
_currPtr += size;
}
//
template <typename _type>
void ByteBuffer<_type>::reset()
{
_offPtr = 0;
_currPtr = 0;
}
//
template <typename _type>
unsigned int ByteBuffer<_type>::maxSize() const
{
return _maxSize;
}
2、動的にメモリを割り当てるバッファキュー
ダイナミックメモリのバッファ、ダイナミック拡張可能なバッファサイズ
定義:
typedef ByteBuffer
コンストラクション関数、メモリを動的に割り当てるバッファ、サイズをいつでも拡張できます(テンプレートバイアス)
template <>
t_BufferCmdQueue::ByteBuffer()
: _maxSize(trunkSize), _offPtr(0), _currPtr(0), _buffer(_maxSize)
{
}
バッファメモリを再整理し、バッファにデータを書き込み、バッファサイズが不足している場合はバッファサイズを再調整し、サイズ調整はtrunkSizeの整数倍で原則として増加する
(sizeはバッファにどれだけのデータが書き込まれているか、テンプレートが特化している)
template <> inline void t_BufferCmdQueue::wr_reserve(const unsigned int size)
{
if (wr_size() < size + 8)//
{
_maxSize += (trunkSize * trunkCount(size + 8));//
_buffer.resize(_maxSize,0);//
}
}
ただし、メモリブロック単位サイズ(64 kバイト)
const unsigned int trunkSize = 64 * 1024;
// size (size )
#define trunkCount(size) (((size) + trunkSize - 1) / trunkSize)
3、静的にメモリを割り当てるバッファキュー
静的サイズのバッファは、スタック空間配列でメモリを割り当て、一時的な変数の取得に使用されます.
定義:
typedef ByteBuffer<unsigned char [PACKET_ZIP_BUFFER]> t_StackCmdQueue;
コンストラクション関数、静的配列のバッファ、サイズは随時変更できません(テンプレートバイアス)
template <>
t_StackCmdQueue::ByteBuffer()
: _maxSize(PACKET_ZIP_BUFFER), _offPtr(0), _currPtr(0)
{
}
template <> inline void t_StackCmdQueue::wr_reserve(const unsigned int size)
{
// ,
//
assert(wr_size() >= size);
}
静的な大きなスタック文字キュー、サイズ:
// ,zip_size
#define unzip_size(zip_size) ((zip_size) * 120 / 100 + 12)
const unsigned int PACKET_ZIP_BUFFER=unzip_size(trunkSize - 1) + sizeof(unsigned int) + 8;/**< */