• 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 "MyTypes.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       ::MidFree(_data);
33       const size_t kMinSize = (size_t)1 << 16;
34       if (size < kMinSize)
35         size = kMinSize;
36       _size = 0;
37       _data = NULL;
38       _data = (Byte *)::MidAlloc(size);
39       if (_data)
40         _size = size;
41     }
42   }
43 };
44 
45 
46 class CAlignedBuffer
47 {
48   Byte *_data;
49   size_t _size;
50 
CLASS_NO_COPY(CAlignedBuffer)51   CLASS_NO_COPY(CAlignedBuffer)
52 
53 public:
54   CAlignedBuffer(): _data(NULL), _size(0) {}
~CAlignedBuffer()55   ~CAlignedBuffer()
56   {
57     ISzAlloc_Free(&g_AlignedAlloc, _data);
58   }
59 
Free()60   void Free()
61   {
62     ISzAlloc_Free(&g_AlignedAlloc, _data);
63     _data = NULL;
64     _size = 0;
65   }
66 
IsAllocated()67   bool IsAllocated() const { return _data != NULL; }
68   operator       Byte *()       { return _data; }
69   operator const Byte *() const { return _data; }
Size()70   size_t Size() const { return _size; }
71 
Alloc(size_t size)72   void Alloc(size_t size)
73   {
74     if (!_data || size != _size)
75     {
76       ISzAlloc_Free(&g_AlignedAlloc, _data);
77       _size = 0;
78       _data = NULL;
79       _data = (Byte *)ISzAlloc_Alloc(&g_AlignedAlloc, size);
80       if (_data)
81         _size = size;
82     }
83   }
84 
AllocAtLeast(size_t size)85   void AllocAtLeast(size_t size)
86   {
87     if (!_data || size > _size)
88     {
89       ISzAlloc_Free(&g_AlignedAlloc, _data);
90       _size = 0;
91       _data = NULL;
92       _data = (Byte *)ISzAlloc_Alloc(&g_AlignedAlloc, size);
93       if (_data)
94         _size = size;
95     }
96   }
97 };
98 
99 
100 #endif
101