2828import io .questdb .client .std .Unsafe ;
2929
3030final class NativeSegmentList implements QuietCloseable {
31- static final int SEGMENT_SIZE = 16 ;
31+ static final int ENTRY_SIZE = 16 ;
3232
3333 private int capacity ;
3434 private long ptr ;
@@ -41,15 +41,49 @@ final class NativeSegmentList implements QuietCloseable {
4141
4242 NativeSegmentList (int initialCapacity ) {
4343 this .capacity = Math .max (initialCapacity , 4 );
44- this .ptr = Unsafe .malloc ((long ) capacity * SEGMENT_SIZE , MemoryTag .NATIVE_DEFAULT );
44+ this .ptr = Unsafe .malloc ((long ) capacity * ENTRY_SIZE , MemoryTag .NATIVE_DEFAULT );
45+ }
46+
47+ @ Override
48+ public void close () {
49+ if (ptr != 0 ) {
50+ Unsafe .free (ptr , (long ) capacity * ENTRY_SIZE , MemoryTag .NATIVE_DEFAULT );
51+ ptr = 0 ;
52+ capacity = 0 ;
53+ size = 0 ;
54+ totalLength = 0 ;
55+ }
56+ }
57+
58+ private void ensureCapacity (int required ) {
59+ if (required <= capacity ) {
60+ return ;
61+ }
62+
63+ int newCapacity = capacity ;
64+ while (newCapacity < required ) {
65+ if (newCapacity > Integer .MAX_VALUE / 2 ) {
66+ newCapacity = required ;
67+ break ;
68+ }
69+ newCapacity *= 2 ;
70+ }
71+
72+ ptr = Unsafe .realloc (
73+ ptr ,
74+ (long ) capacity * ENTRY_SIZE ,
75+ (long ) newCapacity * ENTRY_SIZE ,
76+ MemoryTag .NATIVE_DEFAULT
77+ );
78+ capacity = newCapacity ;
4579 }
4680
4781 void add (long address , long length ) {
4882 if (length <= 0 ) {
4983 return ;
5084 }
5185 ensureCapacity (size + 1 );
52- long segmentPtr = ptr + (long ) size * SEGMENT_SIZE ;
86+ long segmentPtr = ptr + (long ) size * ENTRY_SIZE ;
5387 Unsafe .getUnsafe ().putLong (segmentPtr , address );
5488 Unsafe .getUnsafe ().putLong (segmentPtr + 8 , length );
5589 size ++;
@@ -63,8 +97,8 @@ void appendFrom(NativeSegmentList other) {
6397 ensureCapacity (size + other .size );
6498 Unsafe .getUnsafe ().copyMemory (
6599 other .ptr ,
66- ptr + (long ) size * SEGMENT_SIZE ,
67- (long ) other .size * SEGMENT_SIZE
100+ ptr + (long ) size * ENTRY_SIZE ,
101+ (long ) other .size * ENTRY_SIZE
68102 );
69103 size += other .size ;
70104 totalLength += other .totalLength ;
@@ -82,42 +116,8 @@ long getTotalLength() {
82116 return totalLength ;
83117 }
84118
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-
96119 void reset () {
97120 size = 0 ;
98121 totalLength = 0 ;
99122 }
100-
101- private void ensureCapacity (int required ) {
102- if (required <= capacity ) {
103- return ;
104- }
105-
106- int newCapacity = capacity ;
107- while (newCapacity < required ) {
108- if (newCapacity > Integer .MAX_VALUE / 2 ) {
109- newCapacity = required ;
110- break ;
111- }
112- newCapacity *= 2 ;
113- }
114-
115- ptr = Unsafe .realloc (
116- ptr ,
117- (long ) capacity * SEGMENT_SIZE ,
118- (long ) newCapacity * SEGMENT_SIZE ,
119- MemoryTag .NATIVE_DEFAULT
120- );
121- capacity = newCapacity ;
122- }
123123}
0 commit comments