|
1 | 1 | #include "ByteBuffer.h" |
| 2 | +#include <iostream> |
2 | 3 |
|
3 | 4 | ByteBuffer::ByteBuffer(bufsize_t v_size, bufsize_t v_padding) |
4 | | - : content(NULL), contentSize(v_size), padding(v_padding), |
5 | | - originalSize(v_size) |
| 5 | + : content(nullptr), contentSize(0), padding(v_padding), |
| 6 | + originalSize(0) |
6 | 7 | { |
7 | 8 | if (v_size == 0) throw BufferException("Zero size requested"); |
8 | 9 |
|
9 | | - this->content = allocContent(v_size, v_padding); |
| 10 | + this->content = allocContent(v_size, v_padding); |
10 | 11 | this->contentSize = v_size; |
| 12 | + this->originalSize = v_size; |
11 | 13 | } |
12 | 14 |
|
13 | 15 | ByteBuffer::ByteBuffer(BYTE *v_content, bufsize_t v_size, bufsize_t v_padding) |
14 | | - : content(NULL), contentSize(v_size), padding(v_padding), |
15 | | - originalSize(v_size) |
| 16 | + : content(nullptr), contentSize(0), padding(v_padding), |
| 17 | + originalSize(0) |
16 | 18 | { |
17 | 19 | if (v_size == 0) throw BufferException("Zero size requested"); |
18 | 20 |
|
19 | | - this->content = allocContent(v_size, v_padding); |
20 | | - this->contentSize = v_size; |
21 | | - ::memcpy(this->content, v_content, v_size); |
| 21 | + this->content = allocContent(v_size, v_padding); |
| 22 | + if (this->content) { |
| 23 | + this->contentSize = v_size; |
| 24 | + this->originalSize = v_size; |
| 25 | + ::memcpy(this->content, v_content, v_size); |
| 26 | + } |
22 | 27 | } |
23 | 28 |
|
24 | 29 | ByteBuffer::ByteBuffer(AbstractByteBuffer *v_parent, offset_t v_offset, bufsize_t v_size, bufsize_t v_padding) |
25 | 30 | : content(NULL), contentSize(0), padding(0), |
26 | 31 | originalSize(0) |
27 | 32 | { |
28 | | - if (v_parent == NULL) throw BufferException("Cannot make subBuffer for NULL buffer!"); |
29 | | - if (v_size == 0) throw BufferException("Cannot make 0 size buffer!"); |
| 33 | + if (!v_parent) throw BufferException("Cannot make subBuffer for NULL buffer!"); |
| 34 | + if (!v_size) throw BufferException("Cannot make 0 size buffer!"); |
30 | 35 |
|
31 | 36 | bufsize_t parentSize = v_parent->getContentSize(); |
32 | 37 |
|
33 | 38 | bufsize_t copySize = v_size < parentSize ? v_size : parentSize; |
34 | 39 | bufsize_t allocSize = v_size > parentSize ? v_size : parentSize; |
35 | 40 |
|
36 | 41 | BYTE *bContent = v_parent->getContentAt(v_offset, copySize); |
37 | | - if (bContent == NULL) throw BufferException("Cannot make Buffer for NULL content!"); |
| 42 | + if (!bContent) throw BufferException("Cannot make Buffer for NULL content!"); |
38 | 43 |
|
39 | 44 | this->content = allocContent(allocSize, v_padding); |
40 | | - this->contentSize = allocSize; |
41 | | - this->originalSize = this->contentSize; |
42 | | - ::memcpy(this->content, bContent, copySize); |
| 45 | + if (this->content) { |
| 46 | + this->contentSize = allocSize; |
| 47 | + this->originalSize = this->contentSize; |
| 48 | + ::memcpy(this->content, bContent, copySize); |
| 49 | + } |
43 | 50 | } |
44 | 51 |
|
45 | 52 | BYTE* ByteBuffer::allocContent(bufsize_t v_size, bufsize_t v_padding) |
46 | 53 | { |
47 | | - if (v_size == 0) throw BufferException("Zero size requested"); |
48 | | - bufsize_t allocSize = v_size + v_padding; |
49 | | - BYTE* content = new (std::nothrow) BYTE[allocSize]; |
50 | | - if (content == NULL) { |
| 54 | + if (!v_size) throw BufferException("Zero size requested"); |
| 55 | + |
| 56 | + const bufsize_t allocSize = v_size + v_padding; |
| 57 | + std::cerr << "Trying to resize. Ptr: " << std::hex << (void*)content << " New size: " << allocSize << std::endl; |
| 58 | + |
| 59 | + const bufsize_t sizeDiff = (allocSize > this->contentSize) ? (allocSize - this->contentSize) : 0; |
| 60 | + BYTE* content = reinterpret_cast<BYTE*>(::realloc((void*)this->content, allocSize)); |
| 61 | + |
| 62 | + if (!content) { |
| 63 | + std::cerr << "Error!" << std::endl; |
51 | 64 | throw BufferException("Cannot allocate buffer of size: 0x" + QString::number(allocSize, 16)); |
52 | 65 | } |
53 | | - ::memset(content, 0, allocSize); |
| 66 | + if (sizeDiff) { |
| 67 | + std::cerr << "Ptr: " << std::hex << (void*)content << " Additional size: " << sizeDiff << " BaseContentSize: " << this->contentSize << std::endl; |
| 68 | + ::memset(content + this->contentSize, 0, sizeDiff); |
| 69 | + } else { |
| 70 | + std::cerr << "Ptr: " << std::hex << (void*)content << " New size: " << allocSize << std::endl; |
| 71 | + } |
| 72 | + /*if (sizeDiff) { |
| 73 | + // if some memory has been added to the existing buffer, initialize it with 0 |
| 74 | + ::memset(content + this->contentSize, 0, sizeDiff); |
| 75 | + }*/ |
54 | 76 | return content; |
55 | 77 | } |
56 | 78 |
|
57 | 79 | bool ByteBuffer::resize(bufsize_t newSize) |
58 | 80 | { |
59 | | - if (newSize == this->contentSize) return true; |
60 | | - |
61 | | - BYTE *newContent = NULL; |
| 81 | + if (newSize == this->contentSize) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + BYTE *newContent = nullptr; |
| 85 | + bool isOk = true; |
62 | 86 | try { |
63 | 87 | newContent = allocContent(newSize, this->padding); |
64 | | - } catch(BufferException) { |
65 | | - newContent = NULL; |
| 88 | + if (newContent) { |
| 89 | + this->content = newContent; |
| 90 | + this->contentSize = newSize; |
| 91 | + } |
| 92 | + } catch (BufferException &e) { |
| 93 | + isOk = false; |
66 | 94 | } |
67 | | - if (!newContent) return false; |
68 | | - |
69 | | - BYTE *oldContent = this->content; |
70 | | - bufsize_t oldSize = this->contentSize; |
71 | | - bufsize_t copySize = newSize < oldSize ? newSize : oldSize; |
72 | | - |
73 | | - ::memcpy(newContent, oldContent, copySize); |
74 | | - |
75 | | - this->content = newContent; |
76 | | - this->contentSize = newSize; |
77 | | - |
78 | | - delete []oldContent; |
79 | | - |
80 | | - return true; |
| 95 | + return isOk; |
81 | 96 | } |
82 | 97 |
|
83 | 98 | ByteBuffer::~ByteBuffer() |
84 | 99 | { |
85 | | - delete []this->content; |
| 100 | + std::cerr << "Ptr: " << std::hex << (void*)content << " BaseContentSize: " << this->contentSize << " : " << __FUNCTION__ << std::endl; |
| 101 | + ::free(this->content); |
86 | 102 | } |
87 | | - |
88 | | - |
89 | | - |
|
0 commit comments