Commit 44a7aff56f13a0afb16e6b1abcf4b62e49f35863
Committed by
Jay Berkenbilt
1 parent
ae800361
Refactor Pl_Buffer
Base implementation of the buffer on std::basic_string<unsigned char>.
Showing
2 changed files
with
15 additions
and
36 deletions
include/qpdf/Pl_Buffer.hh
| @@ -35,7 +35,6 @@ | @@ -35,7 +35,6 @@ | ||
| 35 | 35 | ||
| 36 | #include <qpdf/Buffer.hh> | 36 | #include <qpdf/Buffer.hh> |
| 37 | #include <qpdf/Pipeline.hh> | 37 | #include <qpdf/Pipeline.hh> |
| 38 | -#include <qpdf/PointerHolder.hh> | ||
| 39 | 38 | ||
| 40 | #include <memory> | 39 | #include <memory> |
| 41 | 40 | ||
| @@ -80,12 +79,11 @@ class QPDF_DLL_CLASS Pl_Buffer: public Pipeline | @@ -80,12 +79,11 @@ class QPDF_DLL_CLASS Pl_Buffer: public Pipeline | ||
| 80 | ~Members() = default; | 79 | ~Members() = default; |
| 81 | 80 | ||
| 82 | private: | 81 | private: |
| 83 | - Members(); | 82 | + Members() = default; |
| 84 | Members(Members const&) = delete; | 83 | Members(Members const&) = delete; |
| 85 | 84 | ||
| 86 | - bool ready; | ||
| 87 | - std::shared_ptr<Buffer> data; | ||
| 88 | - size_t total_size; | 85 | + bool ready{true}; |
| 86 | + std::basic_string<unsigned char> data; | ||
| 89 | }; | 87 | }; |
| 90 | 88 | ||
| 91 | std::shared_ptr<Members> m; | 89 | std::shared_ptr<Members> m; |
libqpdf/Pl_Buffer.cc
| @@ -5,12 +5,6 @@ | @@ -5,12 +5,6 @@ | ||
| 5 | #include <stdlib.h> | 5 | #include <stdlib.h> |
| 6 | #include <string.h> | 6 | #include <string.h> |
| 7 | 7 | ||
| 8 | -Pl_Buffer::Members::Members() : | ||
| 9 | - ready(true), | ||
| 10 | - total_size(0) | ||
| 11 | -{ | ||
| 12 | -} | ||
| 13 | - | ||
| 14 | Pl_Buffer::Pl_Buffer(char const* identifier, Pipeline* next) : | 8 | Pl_Buffer::Pl_Buffer(char const* identifier, Pipeline* next) : |
| 15 | Pipeline(identifier, next), | 9 | Pipeline(identifier, next), |
| 16 | m(new Members()) | 10 | m(new Members()) |
| @@ -26,21 +20,7 @@ Pl_Buffer::~Pl_Buffer() | @@ -26,21 +20,7 @@ Pl_Buffer::~Pl_Buffer() | ||
| 26 | void | 20 | void |
| 27 | Pl_Buffer::write(unsigned char const* buf, size_t len) | 21 | Pl_Buffer::write(unsigned char const* buf, size_t len) |
| 28 | { | 22 | { |
| 29 | - if (this->m->data == nullptr) { | ||
| 30 | - this->m->data = std::make_shared<Buffer>(len); | ||
| 31 | - } | ||
| 32 | - size_t cur_size = this->m->data->getSize(); | ||
| 33 | - size_t left = cur_size - this->m->total_size; | ||
| 34 | - if (left < len) { | ||
| 35 | - size_t new_size = std::max(this->m->total_size + len, 2 * cur_size); | ||
| 36 | - auto b = std::make_shared<Buffer>(new_size); | ||
| 37 | - memcpy(b->getBuffer(), this->m->data->getBuffer(), this->m->total_size); | ||
| 38 | - this->m->data = b; | ||
| 39 | - } | ||
| 40 | - if (len) { | ||
| 41 | - memcpy(this->m->data->getBuffer() + this->m->total_size, buf, len); | ||
| 42 | - this->m->total_size += len; | ||
| 43 | - } | 23 | + this->m->data.append(buf, len); |
| 44 | this->m->ready = false; | 24 | this->m->ready = false; |
| 45 | 25 | ||
| 46 | if (getNext(true)) { | 26 | if (getNext(true)) { |
| @@ -64,12 +44,13 @@ Pl_Buffer::getBuffer() | @@ -64,12 +44,13 @@ Pl_Buffer::getBuffer() | ||
| 64 | throw std::logic_error("Pl_Buffer::getBuffer() called when not ready"); | 44 | throw std::logic_error("Pl_Buffer::getBuffer() called when not ready"); |
| 65 | } | 45 | } |
| 66 | 46 | ||
| 67 | - Buffer* b = new Buffer(this->m->total_size); | ||
| 68 | - if (this->m->total_size > 0) { | 47 | + auto size = this->m->data.length(); |
| 48 | + Buffer* b = new Buffer(size); | ||
| 49 | + if (size > 0) { | ||
| 69 | unsigned char* p = b->getBuffer(); | 50 | unsigned char* p = b->getBuffer(); |
| 70 | - memcpy(p, this->m->data->getBuffer(), this->m->total_size); | 51 | + memcpy(p, this->m->data.data(), size); |
| 71 | } | 52 | } |
| 72 | - this->m = std::shared_ptr<Members>(new Members()); | 53 | + this->m->data.clear(); |
| 73 | return b; | 54 | return b; |
| 74 | } | 55 | } |
| 75 | 56 | ||
| @@ -86,13 +67,13 @@ Pl_Buffer::getMallocBuffer(unsigned char** buf, size_t* len) | @@ -86,13 +67,13 @@ Pl_Buffer::getMallocBuffer(unsigned char** buf, size_t* len) | ||
| 86 | throw std::logic_error( | 67 | throw std::logic_error( |
| 87 | "Pl_Buffer::getMallocBuffer() called when not ready"); | 68 | "Pl_Buffer::getMallocBuffer() called when not ready"); |
| 88 | } | 69 | } |
| 89 | - | ||
| 90 | - *len = this->m->total_size; | ||
| 91 | - if (this->m->total_size > 0) { | ||
| 92 | - *buf = reinterpret_cast<unsigned char*>(malloc(this->m->total_size)); | ||
| 93 | - memcpy(*buf, this->m->data->getBuffer(), this->m->total_size); | 70 | + auto size = this->m->data.length(); |
| 71 | + *len = size; | ||
| 72 | + if (size > 0) { | ||
| 73 | + *buf = reinterpret_cast<unsigned char*>(malloc(size)); | ||
| 74 | + memcpy(*buf, this->m->data.data(), size); | ||
| 94 | } else { | 75 | } else { |
| 95 | *buf = nullptr; | 76 | *buf = nullptr; |
| 96 | } | 77 | } |
| 97 | - this->m = std::shared_ptr<Members>(new Members()); | 78 | + this->m->data.clear(); |
| 98 | } | 79 | } |