memory_allocator

package
v0.0.0-...-1605a13 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsEnoughSpace

func IsEnoughSpace(end, len int) bool

IsEnoughSpace checks if there is enough space to allocate a block of memory from 'end' position to the given length.

func ParseOperation

func ParseOperation(payload []byte) byte

func TLLParser

func TLLParser(ttl uint32) time.Time

TLLParser converts a TTL value into a time.Time object.

Types

type Allocator

type Allocator struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Allocator is a memory allocator that manages a slice of bytes and keeps track of the next available index.

func New

func New(capacity int) *Allocator

New creates a new Allocator with the specified capacity.

func (*Allocator) AllocateBlock

func (a *Allocator) AllocateBlock() ([]byte, error)

AllocateBlock allocates a block of memory in the allocator. It locks the allocator for thread-safety and returns a slice of bytes or an error if there isn't enough space.

func (*Allocator) GetNext

func (a *Allocator) GetNext() int

GetNext returns the next available index in the allocator's memory.

type Key

type Key struct {
	// contains filtered or unexported fields
}

Key represents a stored object with its field, TTL (Time-To-Live), and a pointer to its node in the LRU list.

type ShardedStore

type ShardedStore struct {
	// contains filtered or unexported fields
}

func NewShardedStore

func NewShardedStore() *ShardedStore

func (*ShardedStore) Delete

func (s *ShardedStore) Delete(key string)

func (*ShardedStore) Load

func (s *ShardedStore) Load(key string) (Key, bool)

func (*ShardedStore) Store

func (s *ShardedStore) Store(key string, value Key)

type Slab

type Slab struct {
	sync.RWMutex // Mutex to protect access to the slab
	*Allocator   // Memory allocator associated with the slab
	// contains filtered or unexported fields
}

Slab represents a memory slab used for allocation.

func NewSlab

func NewSlab(slabSize, maxMemoryAllocate int, allocator *Allocator) Slab

NewSlab creates a new Slab with the specified size and allocator.

func (*Slab) AllocateMemory

func (s *Slab) AllocateMemory() ([]byte, error)

AllocateMemory allocates memory for the slab, either by reusing a free block or allocating a new page.

func (*Slab) FreeMemory

func (s *Slab) FreeMemory(ptr unsafe.Pointer)

func (*Slab) GetCurrentPage

func (s *Slab) GetCurrentPage() []byte

GetCurrentPage returns the current page of the slab.

func (*Slab) IsSlabActive

func (s *Slab) IsSlabActive() bool

IsSlabActive checks if the slab has an active memory page.

func (*Slab) UpdatePage

func (s *Slab) UpdatePage(dataBlock []byte)

type SlabManager

type SlabManager struct {
	JobCh []chan Transfer // Channel to receive transfer jobs for processing
	// contains filtered or unexported fields
}

SlabManager manages slabs, LRU (Least Recently Used) caches, and memory allocation.

func NewSlabManager

func NewSlabManager(slabs []Slab, numberOfWorker int) *SlabManager

NewSlabManager creates a new SlabManager with the provided slabs and starts worker goroutines.

func (*SlabManager) ChoseSlab

func (s *SlabManager) ChoseSlab(index int) *Slab

ChoseSlab returns the slab at the specified index.

func (*SlabManager) DeleteOperationFn

func (s *SlabManager) DeleteOperationFn(payload Transfer)

func (*SlabManager) Dispatch

func (s *SlabManager) Dispatch(payload Transfer)

func (*SlabManager) FreeSpace

func (s *SlabManager) FreeSpace(index, slabSize int) ([]byte, string)

FreeSpace frees space in the slab's LRU cache by removing the least recently used node.

func (*SlabManager) GetIndex

func (s *SlabManager) GetIndex(dataSize int) (int, int)

GetIndex performs a binary search to find the appropriate slab index based on the data size.

func (*SlabManager) GetLRUIndex

func (s *SlabManager) GetLRUIndex(index int) *link_list.DLL

GetLRUIndex returns the LRU cache at the specified index.

func (*SlabManager) GetOperationFn

func (s *SlabManager) GetOperationFn(payload Transfer)

func (*SlabManager) GetSlab

func (s *SlabManager) GetSlab(payloadSize int, conn net.Conn) ([]byte, int, error)

GetSlab allocates a slab of memory based on the payload size, handles errors, and frees space if necessary.

func (*SlabManager) GetSlabIndex

func (s *SlabManager) GetSlabIndex(index int) *Slab

GetSlabIndex returns the slab at the specified index.

func (*SlabManager) SetOperationFn

func (s *SlabManager) SetOperationFn(payload Transfer)

func (*SlabManager) Worker

func (s *SlabManager) Worker(jobCh <-chan Transfer)

type Transfer

type Transfer struct {
	// contains filtered or unexported fields
}

Transfer represents a data payload and connection information for a transfer task.

func NewTransfer

func NewTransfer(payload []byte, index int, conn io.Writer) Transfer

NewTransfer creates a new Transfer object with the specified payload, index, and connection.

Jump to

Keyboard shortcuts

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