fsmp

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2023 License: MIT Imports: 3 Imported by: 0

README

Fast Fixed-Size Memory Poll (fsmp)

This is an implementation of the paper: computation_tools_2012_1_10_80006.pdf

Features:

  • no golang garbage collection
  • no loops (fast access times)
  • no recursive functions
  • little initialization overhead
  • little memory footprint (few dozen bytes)
  • straightforward and trouble-free algorithm
  • no-fragmentation
  • control and organization of memory
  • usage of a spinlock for fast concurrent access

Install

go get -u github.com/EinfachAndy/fsmp/

Usage

package main

import (
	"encoding/binary"

	"github.com/EinfachAndy/fsmp"
)

func main() {
	pool := fsmp.CreatePool(1, 8)
	b, err := pool.Allocate()
	if err == fsmp.ErrOutOfMemory {
		panic(err.Error())
	}
	value := uint64(1234)
	binary.LittleEndian.PutUint64(b, value)

	pool.DeAllocate(b)
}

License

fsmp source code is available under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOutOfMemory error = fmt.Errorf("no space left")
	ErrOutOfBound  error = fmt.Errorf("index is out of bound")
)

Functions

This section is empty.

Types

type Pool

type Pool struct {
	spinlock.Locker
	// contains filtered or unexported fields
}

func CreatePool

func CreatePool(numBlocks, blockSize uint) *Pool

CreatePool creates a new memory pool with a fixed number of blocks (numBlocks) and size of each block (blockSize).

func (*Pool) Allocate

func (p *Pool) Allocate() ([]byte, error)

Allocate returns a new slice where len and cap equals `blockSize`. If the limit of `numBlocks` concurrent allocations is reached, ErrOutOfMemory is returned.

func (*Pool) DeAllocate

func (p *Pool) DeAllocate(b []byte) error

DeAllocate releases a previous allocated slice. In case of passing an invalid slice, ErrOutOfBound is returned. Furthermore the released slice must have the same start position provided by allocate.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL