1 /* 2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 #pragma once 26 27 #include <cstring> 28 #include <ostream> 29 #include <functional> 30 31 class PureMark { 32 public: 33 virtual char *getStart() = 0; 34 35 virtual ~PureMark() = default; 36 }; 37 38 class PureRange : public PureMark { 39 public: 40 virtual char *getEnd() = 0; 41 42 virtual size_t getSize() = 0; 43 44 ~PureRange() override { 45 } 46 }; 47 48 class Buffer : public PureRange { 49 size_t max; // max size before we need to realloc. 50 protected: 51 char *memory; 52 size_t size; // size requested 53 public: 54 Buffer(); 55 56 explicit Buffer(size_t size); 57 58 Buffer(const char *mem, size_t size); 59 60 explicit Buffer(const char *fileName); 61 62 explicit Buffer(const std::string &fileName); 63 64 void resize(size_t size); 65 66 void dump(std::ostream &s); 67 68 void dump(std::ostream &s, std::function<void(std::ostream &)> prefix); 69 70 size_t write(int fd); 71 72 size_t read(int fd, size_t size); 73 74 ~Buffer() override; 75 76 char *getStart() override; 77 78 char *getEnd() override; 79 80 size_t getSize() override; 81 82 std::string str(); 83 }; 84 85 class GrowableBuffer : public Buffer { 86 public: 87 GrowableBuffer(); 88 89 explicit GrowableBuffer(size_t size); 90 91 GrowableBuffer(char *mem, size_t size); 92 93 explicit GrowableBuffer(char *fileName); 94 95 void add(void *contents, size_t bytes); 96 97 void add(char c); 98 };