github.com/matrixorigin/matrixone@v1.2.0/pkg/common/buffer/buffer.go (about)

     1  // Copyright 2021 - 2023 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 buffer
    16  
    17  import (
    18  	"unsafe"
    19  
    20  	"golang.org/x/sys/unix"
    21  )
    22  
    23  func New() *Buffer {
    24  	return new(Buffer)
    25  }
    26  
    27  func (b *Buffer) Free() {
    28  	b.Lock()
    29  	defer b.Unlock()
    30  	for i := range b.chunks {
    31  		unix.Munmap(b.chunks[i].data)
    32  	}
    33  	b.chunks = nil
    34  }
    35  
    36  func Alloc[T any](b *Buffer) *T {
    37  	var v T
    38  
    39  	data := b.alloc(int(unsafe.Sizeof(v)))
    40  	return (*T)(unsafe.Pointer(unsafe.SliceData(data)))
    41  }
    42  
    43  func Free[T any](b *Buffer, v *T) {
    44  	b.free(unsafe.Slice((*byte)(unsafe.Pointer(v)), unsafe.Sizeof(*v)))
    45  }
    46  
    47  func MakeSlice[T any](b *Buffer, len, cap int) []T {
    48  	var v T
    49  
    50  	data := b.alloc(int(unsafe.Sizeof(v)) * cap)
    51  	return unsafe.Slice((*T)(unsafe.Pointer(unsafe.SliceData(data))), cap)[:len]
    52  }
    53  
    54  func FreeSlice[T any](b *Buffer, vs []T) {
    55  	var v T
    56  
    57  	b.free(unsafe.Slice((*byte)(unsafe.Pointer(unsafe.SliceData(vs))),
    58  		cap(vs)*int(unsafe.Sizeof(v))))
    59  }
    60  
    61  func (b *Buffer) pop() *chunk {
    62  	b.Lock()
    63  	defer b.Unlock()
    64  	if len(b.chunks) == 0 {
    65  		return nil
    66  	}
    67  	c := b.chunks[0]
    68  	b.chunks = b.chunks[1:]
    69  	return c
    70  }
    71  
    72  func (b *Buffer) push(c *chunk) {
    73  	b.Lock()
    74  	defer b.Unlock()
    75  	b.chunks = append(b.chunks, c)
    76  }
    77  
    78  func (b *Buffer) newChunk() *chunk {
    79  	data, err := unix.Mmap(-1, 0, DefaultChunkBufferSize, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_ANON|unix.MAP_PRIVATE)
    80  	if err != nil {
    81  		panic(err)
    82  	}
    83  	c := (*chunk)(unsafe.Pointer(unsafe.SliceData(data)))
    84  	c.data = data
    85  	c.off = uint32(ChunkSize)
    86  	return c
    87  }
    88  
    89  func (b *Buffer) alloc(sz int) []byte {
    90  	c := b.pop()
    91  	if c == nil {
    92  		c = b.newChunk()
    93  	}
    94  	data := c.alloc(sz)
    95  	if data == nil {
    96  		c = b.newChunk()
    97  		data = c.alloc(sz)
    98  	}
    99  	b.push(c)
   100  	return data
   101  }
   102  
   103  func (b *Buffer) free(data []byte) {
   104  	ptr := *((*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(data)), -PointerSize)))
   105  	c := (*chunk)(ptr)
   106  	c.free()
   107  }