Documentation ¶
Overview ¶
Package memory implements a memory allocator.
Changelog ¶
2017-10-03 Added alternative, unsafe.Pointer-based API.
Benchmarks ¶
Intel® Core™ i5-4670 CPU @ 3.40GHz × 4
goos: linux goarch: amd64 pkg: github.com/cznic/memory BenchmarkFree16-4 100000000 15.3 ns/op 0 B/op 0 allocs/op BenchmarkFree32-4 100000000 21.3 ns/op 0 B/op 0 allocs/op BenchmarkFree64-4 50000000 35.9 ns/op 0 B/op 0 allocs/op BenchmarkCalloc16-4 50000000 26.6 ns/op 0 B/op 0 allocs/op BenchmarkCalloc32-4 50000000 30.1 ns/op 0 B/op 0 allocs/op BenchmarkCalloc64-4 30000000 38.1 ns/op 0 B/op 0 allocs/op BenchmarkGoCalloc16-4 50000000 29.3 ns/op 16 B/op 1 allocs/op BenchmarkGoCalloc32-4 50000000 30.4 ns/op 32 B/op 1 allocs/op BenchmarkGoCalloc64-4 30000000 37.9 ns/op 64 B/op 1 allocs/op BenchmarkMalloc16-4 100000000 15.4 ns/op 0 B/op 0 allocs/op BenchmarkMalloc32-4 100000000 15.6 ns/op 0 B/op 0 allocs/op BenchmarkMalloc64-4 100000000 15.9 ns/op 0 B/op 0 allocs/op BenchmarkUnsafeFree16-4 100000000 14.4 ns/op 0 B/op 0 allocs/op BenchmarkUnsafeFree32-4 100000000 20.4 ns/op 0 B/op 0 allocs/op BenchmarkUnsafeFree64-4 50000000 34.1 ns/op 0 B/op 0 allocs/op BenchmarkUnsafeCalloc16-4 50000000 23.2 ns/op 0 B/op 0 allocs/op BenchmarkUnsafeCalloc32-4 50000000 28.0 ns/op 0 B/op 0 allocs/op BenchmarkUnsafeCalloc64-4 50000000 34.1 ns/op 0 B/op 0 allocs/op BenchmarkUnsafeMalloc16-4 100000000 13.8 ns/op 0 B/op 0 allocs/op BenchmarkUnsafeMalloc32-4 100000000 14.2 ns/op 0 B/op 0 allocs/op BenchmarkUnsafeMalloc64-4 100000000 14.0 ns/op 0 B/op 0 allocs/op PASS ok github.com/cznic/memory 229.054s
Index ¶
- func UintptrUsableSize(p uintptr) (r int)
- func UnsafeUsableSize(p unsafe.Pointer) (r int)
- func UsableSize(p *byte) (r int)
- type Allocator
- func (a *Allocator) Calloc(size int) (r []byte, err error)
- func (a *Allocator) Close() (err error)
- func (a *Allocator) Free(b []byte) (err error)
- func (a *Allocator) Malloc(size int) (r []byte, err error)
- func (a *Allocator) Realloc(b []byte, size int) (r []byte, err error)
- func (a *Allocator) UintptrCalloc(size int) (r uintptr, err error)
- func (a *Allocator) UintptrFree(p uintptr) (err error)
- func (a *Allocator) UintptrMalloc(size int) (r uintptr, err error)
- func (a *Allocator) UintptrRealloc(p uintptr, size int) (r uintptr, err error)
- func (a *Allocator) UnsafeCalloc(size int) (r unsafe.Pointer, err error)
- func (a *Allocator) UnsafeFree(p unsafe.Pointer) (err error)
- func (a *Allocator) UnsafeMalloc(size int) (r unsafe.Pointer, err error)
- func (a *Allocator) UnsafeRealloc(p unsafe.Pointer, size int) (r unsafe.Pointer, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func UintptrUsableSize ¶
UintptrUsableSize is like UsableSize except its argument is an uintptr, which must have been returned from UintptrCalloc, UintptrMalloc or UintptrRealloc.
func UnsafeUsableSize ¶
UnsafeUsableSize is like UsableSize except its argument is an unsafe.Pointer, which must have been returned from UnsafeCalloc, UnsafeMalloc or UnsafeRealloc.
func UsableSize ¶
UsableSize reports the size of the memory block allocated at p, which must point to the first byte of a slice returned from Calloc, Malloc or Realloc. The allocated memory block size can be larger than the size originally requested from Calloc, Malloc or Realloc.
Types ¶
type Allocator ¶
type Allocator struct {
// contains filtered or unexported fields
}
Allocator allocates and frees memory. Its zero value is ready for use.
func (*Allocator) Close ¶
Close releases all OS resources used by a and sets it to its zero value.
It's not necessary to Close the Allocator when exiting a process.
func (*Allocator) Free ¶
Free deallocates memory (as in C.free). The argument of Free must have been acquired from Calloc or Malloc or Realloc.
func (*Allocator) Malloc ¶
Malloc allocates size bytes and returns a byte slice of the allocated memory. The memory is not initialized. Malloc panics for size < 0 and returns (nil, nil) for zero size.
It's ok to reslice the returned slice but the result of appending to it cannot be passed to Free or Realloc as it may refer to a different backing array afterwards.
func (*Allocator) Realloc ¶
Realloc changes the size of the backing array of b to size bytes or returns an error, if any. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size is larger than the old size, the added memory will not be initialized. If b's backing array is of zero size, then the call is equivalent to Malloc(size), for all values of size; if size is equal to zero, and b's backing array is not of zero size, then the call is equivalent to Free(b). Unless b's backing array is of zero size, it must have been returned by an earlier call to Malloc, Calloc or Realloc. If the area pointed to was moved, a Free(b) is done.
func (*Allocator) UintptrCalloc ¶
UintptrCalloc is like Calloc except it returns an uintptr.
func (*Allocator) UintptrFree ¶
UintptrFree is like Free except its argument is an uintptr, which must have been acquired from UintptrCalloc or UintptrMalloc or UintptrRealloc.
func (*Allocator) UintptrMalloc ¶
UintptrMalloc is like Malloc except it returns an uinptr.
func (*Allocator) UintptrRealloc ¶
UintptrRealloc is like Realloc except its first argument is an uintptr, which must have been returned from UintptrCalloc, UintptrMalloc or UintptrRealloc.
func (*Allocator) UnsafeCalloc ¶
UnsafeCalloc is like Calloc except it returns an unsafe.Pointer.
func (*Allocator) UnsafeFree ¶
UnsafeFree is like Free except its argument is an unsafe.Pointer, which must have been acquired from UnsafeCalloc or UnsafeMalloc or UnsafeRealloc.
func (*Allocator) UnsafeMalloc ¶
UnsafeMalloc is like Malloc except it returns an unsafe.Pointer.