|
| 1 | +/* |
| 2 | + * ___ _ ____ ____ |
| 3 | + * / _ \ _ _ ___ ___| |_| _ \| __ ) |
| 4 | + * | | | | | | |/ _ \/ __| __| | | | _ \ |
| 5 | + * | |_| | |_| | __/\__ \ |_| |_| | |_) | |
| 6 | + * \__\_\\__,_|\___||___/\__|____/|____/ |
| 7 | + * |
| 8 | + * Copyright (c) 2014-2019 Appsicle |
| 9 | + * Copyright (c) 2019-2026 QuestDB |
| 10 | + * |
| 11 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | + * you may not use this file except in compliance with the License. |
| 13 | + * You may obtain a copy of the License at |
| 14 | + * |
| 15 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | + * |
| 17 | + * Unless required by applicable law or agreed to in writing, software |
| 18 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + * See the License for the specific language governing permissions and |
| 21 | + * limitations under the License. |
| 22 | + */ |
| 23 | + |
| 24 | +package io.questdb.client.cutlass.qwp.client; |
| 25 | + |
| 26 | +import io.questdb.client.std.MemoryTag; |
| 27 | +import io.questdb.client.std.QuietCloseable; |
| 28 | +import io.questdb.client.std.Unsafe; |
| 29 | + |
| 30 | +final class NativeSegmentList implements QuietCloseable { |
| 31 | + static final int SEGMENT_SIZE = 16; |
| 32 | + |
| 33 | + private int capacity; |
| 34 | + private long ptr; |
| 35 | + private int size; |
| 36 | + private long totalLength; |
| 37 | + |
| 38 | + NativeSegmentList() { |
| 39 | + this(16); |
| 40 | + } |
| 41 | + |
| 42 | + NativeSegmentList(int initialCapacity) { |
| 43 | + this.capacity = Math.max(initialCapacity, 4); |
| 44 | + this.ptr = Unsafe.malloc((long) capacity * SEGMENT_SIZE, MemoryTag.NATIVE_DEFAULT); |
| 45 | + } |
| 46 | + |
| 47 | + void add(long address, long length) { |
| 48 | + if (length <= 0) { |
| 49 | + return; |
| 50 | + } |
| 51 | + ensureCapacity(size + 1); |
| 52 | + long segmentPtr = ptr + (long) size * SEGMENT_SIZE; |
| 53 | + Unsafe.getUnsafe().putLong(segmentPtr, address); |
| 54 | + Unsafe.getUnsafe().putLong(segmentPtr + 8, length); |
| 55 | + size++; |
| 56 | + totalLength += length; |
| 57 | + } |
| 58 | + |
| 59 | + void appendFrom(NativeSegmentList other) { |
| 60 | + if (other.size == 0) { |
| 61 | + return; |
| 62 | + } |
| 63 | + ensureCapacity(size + other.size); |
| 64 | + Unsafe.getUnsafe().copyMemory( |
| 65 | + other.ptr, |
| 66 | + ptr + (long) size * SEGMENT_SIZE, |
| 67 | + (long) other.size * SEGMENT_SIZE |
| 68 | + ); |
| 69 | + size += other.size; |
| 70 | + totalLength += other.totalLength; |
| 71 | + } |
| 72 | + |
| 73 | + long getAddress() { |
| 74 | + return ptr; |
| 75 | + } |
| 76 | + |
| 77 | + int getSegmentCount() { |
| 78 | + return size; |
| 79 | + } |
| 80 | + |
| 81 | + long getTotalLength() { |
| 82 | + return totalLength; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void close() { |
| 87 | + if (ptr != 0) { |
| 88 | + Unsafe.free(ptr, (long) capacity * SEGMENT_SIZE, MemoryTag.NATIVE_DEFAULT); |
| 89 | + ptr = 0; |
| 90 | + capacity = 0; |
| 91 | + size = 0; |
| 92 | + totalLength = 0; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + void reset() { |
| 97 | + size = 0; |
| 98 | + totalLength = 0; |
| 99 | + } |
| 100 | + |
| 101 | + private void ensureCapacity(int required) { |
| 102 | + if (required <= capacity) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + int newCapacity = capacity; |
| 107 | + while (newCapacity < required) { |
| 108 | + newCapacity *= 2; |
| 109 | + } |
| 110 | + |
| 111 | + ptr = Unsafe.realloc( |
| 112 | + ptr, |
| 113 | + (long) capacity * SEGMENT_SIZE, |
| 114 | + (long) newCapacity * SEGMENT_SIZE, |
| 115 | + MemoryTag.NATIVE_DEFAULT |
| 116 | + ); |
| 117 | + capacity = newCapacity; |
| 118 | + } |
| 119 | +} |
0 commit comments