|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +#ifndef SHARE_GC_SHARED_BUFFERNODE_HPP |
| 26 | +#define SHARE_GC_SHARED_BUFFERNODE_HPP |
| 27 | + |
| 28 | +#include "gc/shared/freeListAllocator.hpp" |
| 29 | +#include "utilities/debug.hpp" |
| 30 | +#include "utilities/globalDefinitions.hpp" |
| 31 | +#include "utilities/lockFreeStack.hpp" |
| 32 | +#include "utilities/macros.hpp" |
| 33 | + |
| 34 | +#include <limits> |
| 35 | + |
| 36 | +class BufferNode { |
| 37 | + using InternalSizeType = LP64_ONLY(uint32_t) NOT_LP64(uint16_t); |
| 38 | + static_assert(sizeof(InternalSizeType) <= sizeof(size_t), "assumption"); |
| 39 | + |
| 40 | + InternalSizeType _index; |
| 41 | + InternalSizeType _capacity; |
| 42 | + BufferNode* volatile _next; |
| 43 | + void* _buffer[1]; // Pseudo flexible array member. |
| 44 | + |
| 45 | + BufferNode(InternalSizeType capacity) |
| 46 | + : _index(capacity), _capacity(capacity), _next(nullptr) |
| 47 | + {} |
| 48 | + |
| 49 | + ~BufferNode() = default; |
| 50 | + |
| 51 | + NONCOPYABLE(BufferNode); |
| 52 | + |
| 53 | + static size_t buffer_offset() { |
| 54 | + return offset_of(BufferNode, _buffer); |
| 55 | + } |
| 56 | + |
| 57 | +public: |
| 58 | + static constexpr size_t max_size() { |
| 59 | + return std::numeric_limits<InternalSizeType>::max(); |
| 60 | + } |
| 61 | + |
| 62 | + static BufferNode* volatile* next_ptr(BufferNode& bn) { return &bn._next; } |
| 63 | + typedef LockFreeStack<BufferNode, &next_ptr> Stack; |
| 64 | + |
| 65 | + BufferNode* next() const { return _next; } |
| 66 | + void set_next(BufferNode* n) { _next = n; } |
| 67 | + size_t index() const { return _index; } |
| 68 | + |
| 69 | + void set_index(size_t i) { |
| 70 | + assert(i <= capacity(), "precondition"); |
| 71 | + _index = static_cast<InternalSizeType>(i); |
| 72 | + } |
| 73 | + |
| 74 | + size_t capacity() const { return _capacity; } |
| 75 | + |
| 76 | + bool is_empty() const { return index() == capacity(); } |
| 77 | + size_t size() const { return capacity() - index(); } |
| 78 | + |
| 79 | + // Return the BufferNode containing the buffer, WITHOUT setting its index. |
| 80 | + static BufferNode* make_node_from_buffer(void** buffer) { |
| 81 | + char* base = reinterpret_cast<char*>(buffer) - buffer_offset(); |
| 82 | + return reinterpret_cast<BufferNode*>(base); |
| 83 | + } |
| 84 | + |
| 85 | + // Return the BufferNode containing the buffer, after setting its index. |
| 86 | + static BufferNode* make_node_from_buffer(void** buffer, size_t index) { |
| 87 | + BufferNode* node = make_node_from_buffer(buffer); |
| 88 | + node->set_index(index); |
| 89 | + return node; |
| 90 | + } |
| 91 | + |
| 92 | + // Return the buffer for node. |
| 93 | + static void** make_buffer_from_node(BufferNode *node) { |
| 94 | + // &_buffer[0] might lead to index out of bounds warnings. |
| 95 | + return reinterpret_cast<void**>( |
| 96 | + reinterpret_cast<char*>(node) + buffer_offset()); |
| 97 | + } |
| 98 | + |
| 99 | + class AllocatorConfig; |
| 100 | + class Allocator; // Free-list based allocator. |
| 101 | + class TestSupport; // Unit test support. |
| 102 | +}; |
| 103 | + |
| 104 | +// We use BufferNode::AllocatorConfig to set the allocation options for the |
| 105 | +// FreeListAllocator. |
| 106 | +class BufferNode::AllocatorConfig : public FreeListConfig { |
| 107 | + const size_t _buffer_capacity; |
| 108 | + |
| 109 | +public: |
| 110 | + explicit AllocatorConfig(size_t size); |
| 111 | + |
| 112 | + ~AllocatorConfig() = default; |
| 113 | + |
| 114 | + void* allocate() override; |
| 115 | + |
| 116 | + void deallocate(void* node) override; |
| 117 | + |
| 118 | + size_t buffer_capacity() const { return _buffer_capacity; } |
| 119 | +}; |
| 120 | + |
| 121 | +class BufferNode::Allocator { |
| 122 | + friend class TestSupport; |
| 123 | + |
| 124 | + AllocatorConfig _config; |
| 125 | + FreeListAllocator _free_list; |
| 126 | + |
| 127 | + NONCOPYABLE(Allocator); |
| 128 | + |
| 129 | +public: |
| 130 | + Allocator(const char* name, size_t buffer_capacity); |
| 131 | + ~Allocator() = default; |
| 132 | + |
| 133 | + size_t buffer_capacity() const { return _config.buffer_capacity(); } |
| 134 | + size_t free_count() const; |
| 135 | + BufferNode* allocate(); |
| 136 | + void release(BufferNode* node); |
| 137 | +}; |
| 138 | + |
| 139 | +#endif // SHARE_GC_SHARED_BUFFERNODE_HPP |
0 commit comments