Manual provides abstractions and implementations to work with manual memory management.
This repo is intended to be used to demonstrate manual memory management principles to students in the Go programming language.
// Allocator is the interface of a manual memory allocator.
type Allocator interface {
// Malloc allocates a slab of memory of argument number of bytes.
// The pointer returned points to the start address of the slab.
// If the memory fails to allocate then nil is returned.
Malloc(sizeInBytes int) unsafe.Pointer
// Free receives a point previously allocated by Malloc and frees it.
// After the memory is freed the pointer should be discarded and no other operation done with it.
Free(toBeFreed unsafe.Pointer) error
}
func newAllocator() manual.Allocator {
var ta manual.TestAllocator // Ready to use as zero value.
ta.SetMaxMemory(1024)
return &ta
}
func doWork(alloc manual.Allocator) error {
slice := manual.Malloc[int](alloc, 20)
if slice == nil {
return errors.New("allocation failed")
}
// do work with slice.
err := manual.Free(alloc, slice)
return err
}
How to install package with newer versions of Go (+1.16):
go mod download github.com/soypat/manual@latest