github.com/matrixorigin/matrixone@v0.7.0/pkg/common/mpool/freelist.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package mpool
    16  
    17  import (
    18  	"sync/atomic"
    19  	"unsafe"
    20  )
    21  
    22  // Poor man's sync.Pool.  golang's sync.Pool is not good for our usage
    23  // because it has no guanrantees, it may silently drop entries.
    24  //
    25  // The freelist is implemented as a ring buffer.  Both put and get spin.
    26  type freelist struct {
    27  	cap        int32
    28  	head, tail atomic.Int32
    29  	ptrs       []unsafe.Pointer
    30  }
    31  
    32  func make_freelist(cap int32) *freelist {
    33  	var fl freelist
    34  	fl.cap = cap
    35  	// +1 so that we can tell empty from full.
    36  	fl.ptrs = make([]unsafe.Pointer, cap+1)
    37  	return &fl
    38  }
    39  
    40  // ring buffer next
    41  func (fl *freelist) next(i int32) int32 {
    42  	if i == fl.cap {
    43  		return 0
    44  	}
    45  	return i + 1
    46  }
    47  
    48  // put always succeeds.  It is caller's job to make sure
    49  // we never overflow the freelist
    50  func (fl *freelist) put(ptr unsafe.Pointer) {
    51  	for {
    52  		curr := fl.tail.Load()
    53  		next := fl.next(curr)
    54  		ok := fl.tail.CompareAndSwap(curr, next)
    55  		if ok {
    56  			atomic.StorePointer(&fl.ptrs[curr], ptr)
    57  			return
    58  		}
    59  	}
    60  }
    61  
    62  // get may return nil if there is no free item
    63  func (fl *freelist) get() unsafe.Pointer {
    64  	for {
    65  		pos := fl.head.Load()
    66  		if pos == fl.tail.Load() {
    67  			// empty
    68  			return nil
    69  		}
    70  		next := fl.next(pos)
    71  		ptr := atomic.LoadPointer(&fl.ptrs[pos])
    72  		ok := fl.head.CompareAndSwap(pos, next)
    73  		if ok {
    74  			return ptr
    75  		}
    76  	}
    77  }