|
| 1 | +/* vim:set ts=2 sw=2 sts=2 et: */ |
| 2 | +/** |
| 3 | + * \author Marcus Holland-Moritz ([email protected]) |
| 4 | + * \copyright Copyright (c) Marcus Holland-Moritz |
| 5 | + * |
| 6 | + * This file is part of dwarfs. |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the “Software”), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | + * SOFTWARE. |
| 25 | + * |
| 26 | + * SPDX-License-Identifier: MIT |
| 27 | + */ |
| 28 | + |
| 29 | +#pragma once |
| 30 | + |
| 31 | +#include <cstdint> |
| 32 | +#include <cstring> |
| 33 | +#include <span> |
| 34 | + |
| 35 | +namespace dwarfs::internal { |
| 36 | + |
| 37 | +class malloc_buffer { |
| 38 | + public: |
| 39 | + using value_type = uint8_t; |
| 40 | + |
| 41 | + malloc_buffer() = default; |
| 42 | + malloc_buffer(size_t size); |
| 43 | + malloc_buffer(void const* data, size_t size); |
| 44 | + malloc_buffer(std::span<value_type const> data); |
| 45 | + |
| 46 | + ~malloc_buffer(); |
| 47 | + |
| 48 | + malloc_buffer(malloc_buffer&&) = default; |
| 49 | + malloc_buffer& operator=(malloc_buffer&&) = default; |
| 50 | + |
| 51 | + bool empty() const { return size_ == 0; } |
| 52 | + |
| 53 | + size_t size() const { return size_; } |
| 54 | + size_t capacity() const { return capacity_; } |
| 55 | + |
| 56 | + value_type const* data() const { return data_; } |
| 57 | + value_type* data() { return data_; } |
| 58 | + |
| 59 | + void append(void const* data, size_t size) { |
| 60 | + reserve(size_ + size); |
| 61 | + copy(data_ + size_, data, size); |
| 62 | + size_ += size; |
| 63 | + } |
| 64 | + |
| 65 | + void clear() { size_ = 0; } |
| 66 | + |
| 67 | + void resize(size_t new_size) { |
| 68 | + reserve(new_size); |
| 69 | + size_ = new_size; |
| 70 | + } |
| 71 | + |
| 72 | + void reserve(size_t new_capacity) { |
| 73 | + if (new_capacity > capacity_) { |
| 74 | + grow(new_capacity); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + void shrink_to_fit(); |
| 79 | + |
| 80 | + private: |
| 81 | + void grow(size_t new_size); |
| 82 | + void resize_buffer(size_t new_size); |
| 83 | + static void copy(void* dest, void const* src, size_t size) { |
| 84 | + // TODO: try std::copy or even something custom |
| 85 | + std::memcpy(dest, src, size); |
| 86 | + } |
| 87 | + |
| 88 | + value_type* data_{nullptr}; |
| 89 | + size_t size_{0}; |
| 90 | + size_t capacity_{0}; |
| 91 | +}; |
| 92 | + |
| 93 | +} // namespace dwarfs::internal |
0 commit comments