github.com/dubbogo/gost@v1.14.0/bytes/slice_pool.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package gxbytes
    19  
    20  const (
    21  	minShift = 6
    22  	maxShift = 18
    23  )
    24  
    25  var defaultSlicePool *SlicePool
    26  
    27  func init() {
    28  	defaultSlicePool = NewSlicePool()
    29  }
    30  
    31  // SlicePool is []byte pools
    32  // Deprecated
    33  type SlicePool struct {
    34  	BytesPool
    35  }
    36  
    37  // newSlicePool returns SlicePool
    38  // Deprecated
    39  // instead by NewBytesPool
    40  func NewSlicePool() *SlicePool {
    41  	sizes := make([]int, 0, maxShift-minShift+1)
    42  	for i := minShift; i <= maxShift; i++ {
    43  		sizes = append(sizes, 1<<uint(i))
    44  	}
    45  	p := &SlicePool{
    46  		*NewBytesPool(sizes),
    47  	}
    48  	return p
    49  }
    50  
    51  // Get returns *[]byte from SlicePool
    52  func (p *SlicePool) Get(size int) *[]byte {
    53  	return p.AcquireBytes(size)
    54  }
    55  
    56  // Put returns *[]byte to SlicePool
    57  func (p *SlicePool) Put(bufp *[]byte) {
    58  	p.ReleaseBytes(bufp)
    59  }
    60  
    61  // GetBytes returns *[]byte from SlicePool
    62  // Deprecated
    63  // instead by AcquireBytes
    64  func GetBytes(size int) *[]byte {
    65  	return defaultSlicePool.Get(size)
    66  }
    67  
    68  // PutBytes Put *[]byte to SlicePool
    69  // Deprecated
    70  // instead by ReleaseBytes
    71  func PutBytes(buf *[]byte) {
    72  	defaultSlicePool.Put(buf)
    73  }