13c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/*
23c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Copyright (C) 2014 Square, Inc.
33c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller *
43c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Licensed under the Apache License, Version 2.0 (the "License");
53c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * you may not use this file except in compliance with the License.
63c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * You may obtain a copy of the License at
73c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller *
83c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller *      http://www.apache.org/licenses/LICENSE-2.0
93c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller *
103c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Unless required by applicable law or agreed to in writing, software
113c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * distributed under the License is distributed on an "AS IS" BASIS,
123c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * See the License for the specific language governing permissions and
143c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * limitations under the License.
153c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */
163c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerpackage okio;
173c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
183c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/**
193c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * A collection of unused segments, necessary to avoid GC churn and zero-fill.
203c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * This pool is a thread-safe static singleton.
213c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */
223c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerfinal class SegmentPool {
233c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /** The maximum number of bytes to pool. */
243c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  // TODO: Is 64 KiB a good maximum size? Do we ever have that many idle segments?
253c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  static final long MAX_SIZE = 64 * 1024; // 64 KiB.
263c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
273c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /** Singly-linked list of segments. */
28a2cab72aa5ff730ba2ae987b45398faafffeb505Neil Fuller  static Segment next;
293c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
303c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /** Total bytes in this pool. */
31a2cab72aa5ff730ba2ae987b45398faafffeb505Neil Fuller  static long byteCount;
323c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
333c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  private SegmentPool() {
343c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  }
353c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
36a2cab72aa5ff730ba2ae987b45398faafffeb505Neil Fuller  static Segment take() {
37a2cab72aa5ff730ba2ae987b45398faafffeb505Neil Fuller    synchronized (SegmentPool.class) {
383c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller      if (next != null) {
393c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller        Segment result = next;
403c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller        next = result.next;
413c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller        result.next = null;
423c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller        byteCount -= Segment.SIZE;
433c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller        return result;
443c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller      }
453c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    }
463c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    return new Segment(); // Pool is empty. Don't zero-fill while holding a lock.
473c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  }
483c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
49a2cab72aa5ff730ba2ae987b45398faafffeb505Neil Fuller  static void recycle(Segment segment) {
503c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    if (segment.next != null || segment.prev != null) throw new IllegalArgumentException();
51a2cab72aa5ff730ba2ae987b45398faafffeb505Neil Fuller    if (segment.shared) return; // This segment cannot be recycled.
52a2cab72aa5ff730ba2ae987b45398faafffeb505Neil Fuller    synchronized (SegmentPool.class) {
533c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller      if (byteCount + Segment.SIZE > MAX_SIZE) return; // Pool is full.
543c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller      byteCount += Segment.SIZE;
553c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller      segment.next = next;
563c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller      segment.pos = segment.limit = 0;
573c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller      next = segment;
583c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    }
593c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  }
603c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller}
61