Panda3D
membuffer.I
1 #define MEMBUF_THRASH_SIZE 25
2 
3 /**
4  * Releases all resources(Memory USed) is locally allocated
5  */
6 inline void MemBuffer::
7 ClearBuffer(void) {
8  if (_BufferLocal == true) {
9  if (_Buffer != nullptr) {
10  delete[] _Buffer;
11  }
12 
13  _Buffer = nullptr;
14  }
15 }
16 
17 /**
18  * Locally allocate a new buffer
19  */
20 inline void MemBuffer::
21 AllocBuffer(size_t len) {
22  _Buffer = new char[len];
23  _BufferLocal = true;
24  _BufferLen = len;
25 }
26 
27 /**
28  * default constructor
29  */
30 inline MemBuffer::
31 MemBuffer(void) {
32  _Buffer = nullptr;
33  _BufferLocal = false;
34  _BufferLen = 0;
35 }
36 
37 /**
38  * Constructure to locall allocate a buffer
39  */
40 inline MemBuffer::
41 MemBuffer(size_t len) {
42  AllocBuffer(len);
43 }
44 
45 /**
46  * Constructure to use an external buffer
47  */
48 inline MemBuffer::
49 MemBuffer(char *data, size_t len) {
50  _BufferLocal = false;
51  _BufferLen = len;
52  _Buffer = data;
53 }
54 
55 /**
56  * CLean UP a mess on Deletion
57  */
58 inline MemBuffer::
60  ClearBuffer();
61 }
62 
63 /**
64  * Assigns a buffer
65  */
66 inline void MemBuffer::
67 SetBuffer(char * data, size_t len) {
68  if (_BufferLocal == true) {
69  ClearBuffer();
70  }
71 
72  _BufferLocal = false;
73  _BufferLen = len;
74  _Buffer = data;
75 }
76 
77 /**
78  * Grow a buffer is needed to get to a sertion size No care is made here to
79  * preserve convtent unlike a vector of chars
80  *
81  */
82 inline void MemBuffer::
83 GrowBuffer(size_t new_len) {
84  if (new_len >= _BufferLen) {
85  size_t len = new_len + MEMBUF_THRASH_SIZE;
86  len = len +len;
87 
88  char *tmp = new char[len];
89 
90  if (_Buffer != nullptr) {
91  memcpy(tmp,_Buffer,_BufferLen);
92  }
93 
94  ClearBuffer();
95 
96  _Buffer = tmp;
97  _BufferLocal = true;
98  _BufferLen = len;
99  }
100 }
101 
102 /**
103  * Access to the BUffer Size Information
104  */
105 inline size_t MemBuffer::
106 GetBufferSize(void) const {
107  return _BufferLen;
108 }
109 
110 /**
111  * Access to the actual BUffer
112  */
113 inline char *MemBuffer::
114 GetBuffer(void) {
115  return _Buffer;
116 }
117 
118 inline const char *MemBuffer::
119 GetBuffer(void) const {
120  return _Buffer;
121 }
122 
123 /**
124  *
125  */
126 inline bool MemBuffer::
127 InBufferRange(char *inpos) {
128  return (inpos >= _Buffer && inpos <= (_Buffer + _BufferLen));
129 }
void GrowBuffer(size_t len)
Grow a buffer is needed to get to a sertion size No care is made here to preserve convtent unlike a v...
Definition: membuffer.I:83
char * GetBuffer(void)
Access to the actual BUffer.
Definition: membuffer.I:114
size_t GetBufferSize(void) const
Access to the BUffer Size Information.
Definition: membuffer.I:106
virtual ~MemBuffer()
CLean UP a mess on Deletion.
Definition: membuffer.I:59
MemBuffer(void)
default constructor
Definition: membuffer.I:31
void SetBuffer(char *data, size_t len)
Assigns a buffer.
Definition: membuffer.I:67