View Javadoc
1   /*
2    * Copyright 2025 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    *   https://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  package io.netty.util.internal;
17  
18  import java.nio.ByteBuffer;
19  
20  final class DirectCleaner implements Cleaner {
21      @Override
22      public CleanableDirectBuffer allocate(int capacity) {
23          return new CleanableDirectBufferImpl(capacity);
24      }
25  
26      @Override
27      public CleanableDirectBuffer reallocate(CleanableDirectBuffer old, int newCapacity) {
28          int oldCapacity = old.buffer().capacity();
29          int delta = newCapacity - oldCapacity;
30          PlatformDependent.incrementMemoryCounter(delta);
31          try {
32              ByteBuffer newBuffer = PlatformDependent0.reallocateDirectNoCleaner(
33                      old.buffer(), newCapacity);
34              return new CleanableDirectBufferImpl(newBuffer);
35          } catch (Throwable e) {
36              PlatformDependent.decrementMemoryCounter(delta);
37              throw e;
38          }
39      }
40  
41      @Override
42      public void freeDirectBuffer(ByteBuffer buffer) {
43          PlatformDependent0.freeMemory(PlatformDependent0.directBufferAddress(buffer));
44      }
45  
46      @Override
47      public boolean hasExpensiveClean() {
48          return false;
49      }
50  
51      private static final class CleanableDirectBufferImpl implements CleanableDirectBuffer {
52          private final ByteBuffer buffer;
53  
54          // Used for normal allocation — allocates memory and increments counter
55          CleanableDirectBufferImpl(int capacity) {
56              PlatformDependent.incrementMemoryCounter(capacity);
57              try {
58                  this.buffer = PlatformDependent0.allocateDirectNoCleaner(capacity);
59              } catch (Throwable e) {
60                  PlatformDependent.decrementMemoryCounter(capacity);
61                  throw e;
62              }
63          }
64  
65          // Used for reallocation — memory already allocated, counter already adjusted
66          CleanableDirectBufferImpl(ByteBuffer buffer) {
67              this.buffer = buffer;
68          }
69  
70          @Override
71          public ByteBuffer buffer() {
72              return buffer;
73          }
74  
75          @Override
76          public void clean() {
77              int capacity = buffer.capacity();
78              PlatformDependent0.freeMemory(PlatformDependent0.directBufferAddress(buffer));
79              PlatformDependent.decrementMemoryCounter(capacity);
80          }
81      }
82  }