• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Common/MyBuffer2.h
2 
3 #ifndef __COMMON_MY_BUFFER2_H
4 #define __COMMON_MY_BUFFER2_H
5 
6 #include "../../C/Alloc.h"
7 
8 #include "Defs.h"
9 
10 class CMidBuffer
11 {
12   Byte *_data;
13   size_t _size;
14 
CLASS_NO_COPY(CMidBuffer)15   CLASS_NO_COPY(CMidBuffer)
16 
17 public:
18   CMidBuffer(): _data(NULL), _size(0) {};
~CMidBuffer()19   ~CMidBuffer() { ::MidFree(_data); }
20 
Free()21   void Free() { ::MidFree(_data); _data = NULL; _size = 0; }
22 
IsAllocated()23   bool IsAllocated() const { return _data != NULL; }
24   operator       Byte *()       { return _data; }
25   operator const Byte *() const { return _data; }
Size()26   size_t Size() const { return _size; }
27 
AllocAtLeast(size_t size)28   void AllocAtLeast(size_t size)
29   {
30     if (!_data || size > _size)
31     {
32       const size_t kMinSize = (size_t)1 << 16;
33       if (size < kMinSize)
34         size = kMinSize;
35       ::MidFree(_data);
36       _size = 0;
37       _data = 0;
38       _data = (Byte *)::MidAlloc(size);
39       if (_data)
40         _size = size;
41     }
42   }
43 };
44 
45 #endif
46