Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Allocator ¶
type Allocator[T any] interface { // purpose: grabs an item from our storage - like reaching into a box to pull out a toy // params: none // args: n/a // returns: (item T, ok bool) - the item we found and whether we actually got one // raises: none Get() (T, bool) // purpose: returns an item back to storage so we can reuse it later - like putting a toy back in the box // params: item T - the thing we want to return // args: takes the item and stores it for future use // returns: success bool - true if we could store it, false if storage is full // raises: none Put(item T) bool // purpose: tells us how many items are currently available to grab // params: none // args: n/a // returns: count int - number of items ready to use // raises: none Available() int // purpose: tells us the maximum number of items this allocator can hold at once // params: none // args: n/a // returns: cap int - the total capacity // raises: none Capacity() int }
purpose: this is the blueprint for all allocators in our system - it tells us what operations
any allocator must support, like getting an item, returning it, and checking stats
params: none (this is an interface) args: n/a returns: n/a raises: n/a
type BaseAllocator ¶
type BaseAllocator[T any] struct { // contains filtered or unexported fields }
purpose: a basic storage structure that can hold multiple items of any type in a safe way params: none (this is a type definition) args: n/a returns: n/a raises: n/a
func NewBaseAllocator ¶
func NewBaseAllocator[T any](capacity int) *BaseAllocator[T]
purpose: creates a new basic allocator with a specific size limit params: capacity int - how many items we can store at most args: uses the capacity to create storage space for that many items returns: pointer to BaseAllocator - a fresh allocator ready to use raises: none
func (*BaseAllocator[T]) Available ¶
func (a *BaseAllocator[T]) Available() int
purpose: tells us how many items are ready to grab right now params: none args: safely checks the current count returns: count int - number of available items raises: none
func (*BaseAllocator[T]) Capacity ¶
func (a *BaseAllocator[T]) Capacity() int
purpose: tells us the total storage capacity params: none args: just returns the capacity we set when creating this allocator returns: cap int - maximum number of items we can hold raises: none
func (*BaseAllocator[T]) Get ¶
func (a *BaseAllocator[T]) Get() (T, bool)
purpose: safely retrieves an item from the allocator's storage params: none args: locks the storage, checks if anything is available, and removes one item returns: (item T, ok bool) - the item we got and whether we successfully got one raises: none
func (*BaseAllocator[T]) Put ¶
func (a *BaseAllocator[T]) Put(item T) bool
purpose: safely returns an item to the allocator for future reuse params: item T - the thing we want to put back args: locks the storage, checks if there's room, and adds the item to our collection returns: success bool - true if we stored it, false if we're full raises: none
type FreeListAllocator ¶
type FreeListAllocator[T any] struct { // contains filtered or unexported fields }
purpose: a smart storage system that keeps track of empty slots using a linked chain of free spots,
making it super fast to find where to put things
params: none (this is a type definition) args: n/a returns: n/a raises: n/a
func NewFreeListAllocator ¶
func NewFreeListAllocator[T any](capacity int) *FreeListAllocator[T]
purpose: creates a new freelist allocator that can hold up to a certain number of items params: capacity int - the maximum number of items we can store args: sets up storage and builds a chain of all available slots returns: pointer to FreeListAllocator - a fresh allocator ready to go raises: none
func (*FreeListAllocator[T]) Available ¶
func (f *FreeListAllocator[T]) Available() int
purpose: counts how many items are currently available to grab params: none args: walks through the free chain and counts each link returns: count int - number of available items raises: none
func (*FreeListAllocator[T]) Capacity ¶
func (f *FreeListAllocator[T]) Capacity() int
purpose: tells us the maximum capacity of this allocator params: none args: just returns the capacity value we set at creation returns: cap int - total capacity raises: none
func (*FreeListAllocator[T]) Get ¶
func (f *FreeListAllocator[T]) Get() (T, bool)
purpose: grabs an available item from the freelist by following the chain of free slots params: none args: locks storage, follows the free chain to find an open slot, returns the item there returns: (item T, ok bool) - the item we found and whether we succeeded raises: none
func (*FreeListAllocator[T]) Put ¶
func (f *FreeListAllocator[T]) Put(item T) bool
purpose: puts an item back into an available free slot params: item T - the thing we're returning to storage args: finds a free spot, stores the item there, and updates our free chain returns: success bool - true if we stored it successfully raises: none
type LockFreeAllocator ¶
type LockFreeAllocator[T any] struct { // contains filtered or unexported fields }
purpose: an advanced storage system that doesn't use locks - it uses atomic operations to let
multiple goroutines work at the same time without blocking each other
params: none (this is a type definition) args: n/a returns: n/a raises: n/a
func NewLockFreeAllocator ¶
func NewLockFreeAllocator[T any](capacity int) *LockFreeAllocator[T]
purpose: creates a new lock-free allocator with a given capacity params: capacity int - maximum number of items to store args: sets up the allocator but starts empty (items added via Put) returns: pointer to LockFreeAllocator - a fresh lock-free allocator raises: none
func (*LockFreeAllocator[T]) Available ¶
func (l *LockFreeAllocator[T]) Available() int
purpose: tells us how many items are currently stored params: none args: reads the atomic counter returns: count int - number of available items raises: none
func (*LockFreeAllocator[T]) Capacity ¶
func (l *LockFreeAllocator[T]) Capacity() int
purpose: returns the maximum capacity of this allocator params: none args: returns the capacity value set at creation returns: cap int - maximum capacity raises: none
func (*LockFreeAllocator[T]) Get ¶
func (l *LockFreeAllocator[T]) Get() (T, bool)
purpose: grabs an item from the lock-free chain without using locks params: none args: uses atomic operations to safely remove the head node and return its item returns: (item T, ok bool) - the item we got and whether we succeeded raises: none
func (*LockFreeAllocator[T]) Put ¶
func (l *LockFreeAllocator[T]) Put(item T) bool
purpose: adds an item to the lock-free chain without using locks params: item T - the thing we want to store args: creates a new node, and uses atomic operations to add it to the chain returns: success bool - true if we added it, false if we're at capacity raises: none
type StackAllocator ¶
type StackAllocator[T any] struct { // contains filtered or unexported fields }
purpose: a simple last-in-first-out storage system - like a stack of plates where you always
take from the top and put back on top
params: none (this is a type definition) args: n/a returns: n/a raises: n/a
func NewStackAllocator ¶
func NewStackAllocator[T any](capacity int) *StackAllocator[T]
purpose: creates a new stack allocator with a specific maximum size params: capacity int - how many items we can stack up at most args: creates an empty stack with room for that many items returns: pointer to StackAllocator - a brand new stack ready to use raises: none
func (*StackAllocator[T]) Available ¶
func (s *StackAllocator[T]) Available() int
purpose: counts how many items are currently in the stack params: none args: safely checks the length of our items list returns: count int - number of items in the stack raises: none
func (*StackAllocator[T]) Capacity ¶
func (s *StackAllocator[T]) Capacity() int
purpose: tells us the maximum number of items we can stack params: none args: returns the capacity we set when creating the stack returns: cap int - maximum stack size raises: none
func (*StackAllocator[T]) Get ¶
func (s *StackAllocator[T]) Get() (T, bool)
purpose: pops an item off the top of our stack params: none args: safely locks the stack, checks if anything is there, and removes the top item returns: (item T, ok bool) - the item from the top and whether we got one raises: none
func (*StackAllocator[T]) Put ¶
func (s *StackAllocator[T]) Put(item T) bool
purpose: pushes an item onto the top of our stack params: item T - the thing we want to add to the stack args: checks if there's room, then adds it to the top returns: success bool - true if we added it, false if the stack is full raises: none