-
Notifications
You must be signed in to change notification settings - Fork 391
/
RingBufferDescriptor.java
107 lines (92 loc) · 3.5 KB
/
RingBufferDescriptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright 2014-2024 Real Logic Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.agrona.concurrent.ringbuffer;
import org.agrona.BitUtil;
/**
* Layout description for the underlying buffer used by a {@link RingBuffer}. The buffer consists
* of a ring of messages which is a power of 2 in size, followed by a trailer section containing state
* information for the producers and consumers of the ring.
*/
public final class RingBufferDescriptor
{
/**
* Offset within the trailer for where the tail value is stored.
*/
public static final int TAIL_POSITION_OFFSET;
/**
* Offset within the trailer for where the head cache value is stored.
*/
public static final int HEAD_CACHE_POSITION_OFFSET;
/**
* Offset within the trailer for where the head value is stored.
*/
public static final int HEAD_POSITION_OFFSET;
/**
* Offset within the trailer for where the correlation counter value is stored.
*/
public static final int CORRELATION_COUNTER_OFFSET;
/**
* Offset within the trailer for where the consumer heartbeat time value is stored.
*/
public static final int CONSUMER_HEARTBEAT_OFFSET;
/**
* Total length of the trailer in bytes.
*/
public static final int TRAILER_LENGTH;
static
{
int offset = 0;
offset += (BitUtil.CACHE_LINE_LENGTH * 2);
TAIL_POSITION_OFFSET = offset;
offset += (BitUtil.CACHE_LINE_LENGTH * 2);
HEAD_CACHE_POSITION_OFFSET = offset;
offset += (BitUtil.CACHE_LINE_LENGTH * 2);
HEAD_POSITION_OFFSET = offset;
offset += (BitUtil.CACHE_LINE_LENGTH * 2);
CORRELATION_COUNTER_OFFSET = offset;
offset += (BitUtil.CACHE_LINE_LENGTH * 2);
CONSUMER_HEARTBEAT_OFFSET = offset;
offset += (BitUtil.CACHE_LINE_LENGTH * 2);
TRAILER_LENGTH = offset;
}
private RingBufferDescriptor()
{
}
/**
* Check the buffer capacity is the correct size (a power of 2 + {@link RingBufferDescriptor#TRAILER_LENGTH})
* and that it satisfies minimum capacity size.
*
* @param capacity to be checked.
* @param minCapacity minimum required capacity of the ring buffer.
* @return the capacity to store the data in the ring buffer, i.e. {@code capacity - TRAILER_LENGTH}.
* @throws IllegalArgumentException if the buffer capacity is incorrect.
*/
public static int checkCapacity(final int capacity, final int minCapacity)
{
final int dataCapacity = capacity - TRAILER_LENGTH;
if (!BitUtil.isPowerOfTwo(dataCapacity))
{
throw new IllegalArgumentException(
"capacity must be a positive power of 2 + TRAILER_LENGTH: capacity=" + capacity);
}
if (dataCapacity < minCapacity)
{
throw new IllegalArgumentException(
"insufficient capacity: minCapacity=" + (minCapacity + TRAILER_LENGTH) + ", capacity=" + capacity);
}
return dataCapacity;
}
}