1 /*
2  * Copyright 2012 The Netty Project
3  *
4  * The Netty Project licenses this file to you under the Apache License,
5  * version 2.0 (the "License"); you may not use this file except in compliance
6  * with the License. You may obtain a copy of the License at:
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16 module hunt.net.buffer.ByteBufAllocator;
17 
18 import hunt.net.buffer.ByteBuf;
19 import hunt.net.buffer.CompositeByteBuf;
20 
21 /**
22  * Implementations are responsible to allocate buffers. Implementations of this interface are expected to be
23  * thread-safe.
24  */
25 interface ByteBufAllocator {
26 
27     // ByteBufAllocator DEFAULT = ByteBufUtil.DEFAULT_ALLOCATOR;
28 
29     /**
30      * Allocate a {@link ByteBuf}. If it is a direct or heap buffer
31      * depends on the actual implementation.
32      */
33     ByteBuf buffer();
34 
35     /**
36      * Allocate a {@link ByteBuf} with the given initial capacity.
37      * If it is a direct or heap buffer depends on the actual implementation.
38      */
39     ByteBuf buffer(int initialCapacity);
40 
41     /**
42      * Allocate a {@link ByteBuf} with the given initial capacity and the given
43      * maximal capacity. If it is a direct or heap buffer depends on the actual
44      * implementation.
45      */
46     ByteBuf buffer(int initialCapacity, int maxCapacity);
47 
48     /**
49      * Allocate a {@link ByteBuf}, preferably a direct buffer which is suitable for I/O.
50      */
51     ByteBuf ioBuffer();
52 
53     /**
54      * Allocate a {@link ByteBuf}, preferably a direct buffer which is suitable for I/O.
55      */
56     ByteBuf ioBuffer(int initialCapacity);
57 
58     /**
59      * Allocate a {@link ByteBuf}, preferably a direct buffer which is suitable for I/O.
60      */
61     ByteBuf ioBuffer(int initialCapacity, int maxCapacity);
62 
63     /**
64      * Allocate a heap {@link ByteBuf}.
65      */
66     ByteBuf heapBuffer();
67 
68     /**
69      * Allocate a heap {@link ByteBuf} with the given initial capacity.
70      */
71     ByteBuf heapBuffer(int initialCapacity);
72 
73     /**
74      * Allocate a heap {@link ByteBuf} with the given initial capacity and the given
75      * maximal capacity.
76      */
77     ByteBuf heapBuffer(int initialCapacity, int maxCapacity);
78 
79     /**
80      * Allocate a direct {@link ByteBuf}.
81      */
82     ByteBuf directBuffer();
83 
84     /**
85      * Allocate a direct {@link ByteBuf} with the given initial capacity.
86      */
87     ByteBuf directBuffer(int initialCapacity);
88 
89     /**
90      * Allocate a direct {@link ByteBuf} with the given initial capacity and the given
91      * maximal capacity.
92      */
93     ByteBuf directBuffer(int initialCapacity, int maxCapacity);
94 
95     /**
96      * Allocate a {@link CompositeByteBuf}.
97      * If it is a direct or heap buffer depends on the actual implementation.
98      */
99     CompositeByteBuf compositeBuffer();
100 
101     /**
102      * Allocate a {@link CompositeByteBuf} with the given maximum number of components that can be stored in it.
103      * If it is a direct or heap buffer depends on the actual implementation.
104      */
105     CompositeByteBuf compositeBuffer(int maxNumComponents);
106 
107     /**
108      * Allocate a heap {@link CompositeByteBuf}.
109      */
110     CompositeByteBuf compositeHeapBuffer();
111 
112     /**
113      * Allocate a heap {@link CompositeByteBuf} with the given maximum number of components that can be stored in it.
114      */
115     CompositeByteBuf compositeHeapBuffer(int maxNumComponents);
116 
117     /**
118      * Allocate a direct {@link CompositeByteBuf}.
119      */
120     CompositeByteBuf compositeDirectBuffer();
121 
122     /**
123      * Allocate a direct {@link CompositeByteBuf} with the given maximum number of components that can be stored in it.
124      */
125     CompositeByteBuf compositeDirectBuffer(int maxNumComponents);
126 
127     /**
128      * Returns {@code true} if direct {@link ByteBuf}'s are pooled
129      */
130     bool isDirectBufferPooled();
131 
132     /**
133      * Calculate the new capacity of a {@link ByteBuf} that is used when a {@link ByteBuf} needs to expand by the
134      * {@code minNewCapacity} with {@code maxCapacity} as upper-bound.
135      */
136     int calculateNewCapacity(int minNewCapacity, int maxCapacity);
137  }