Categorygithub.com/pboyd/malloc
repositorypackage
0.9.0
Repository: https://github.com/pboyd/malloc.git
Documentation: pkg.go.dev

# README

Malloc

Go Reference

This is a proof-of-concept memory allocator for Go. A fixed-size chunk of memory is held by the arena and handed out as needed, similar to malloc(3).

Basic usage:

arena := malloc.NewArena(1024)
pointer := malloc.Malloc[SomeStruct](a)
defer malloc.Free(pointer)

// pointer is now a *SomeStruct allocated inside the arena.

It uses a fairly primitive first-fit algorithm to allocate memory. This is simple to implement for a proof of concept, but it has some drawbacks:

  • The smallest size that can be allocated is 16 bytes.
  • Sizes which aren't divisible by 16 will be rounded up.
  • It probably fragments easily.

Credits

The algorithm is taken from Donald Knuth's first-fit memory allocator in The Art of Computer Programming Vol. 1.