memory

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2018 License: BSD-3-Clause Imports: 6 Imported by: 14

README

memory

Package memory implements a memory allocator.

Installation

$ go get modernc.org/memory

Documentation: godoc.org/modernc.org/memory

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: modernc.org/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  	modernc.org/memory	229.054s

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UintptrUsableSize

func UintptrUsableSize(p uintptr) (r int)

UintptrUsableSize is like UsableSize except its argument is an uintptr, which must have been returned from UintptrCalloc, UintptrMalloc or UintptrRealloc.

func UnsafeUsableSize

func UnsafeUsableSize(p unsafe.Pointer) (r int)

UnsafeUsableSize is like UsableSize except its argument is an unsafe.Pointer, which must have been returned from UnsafeCalloc, UnsafeMalloc or UnsafeRealloc.

func UsableSize

func UsableSize(p *byte) (r int)

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) Calloc

func (a *Allocator) Calloc(size int) (r []byte, err error)

Calloc is like Malloc except the allocated memory is zeroed.

func (*Allocator) Close

func (a *Allocator) Close() (err error)

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

func (a *Allocator) Free(b []byte) (err error)

Free deallocates memory (as in C.free). The argument of Free must have been acquired from Calloc or Malloc or Realloc.

func (*Allocator) Malloc

func (a *Allocator) Malloc(size int) (r []byte, err error)

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

func (a *Allocator) Realloc(b []byte, size int) (r []byte, err error)

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

func (a *Allocator) UintptrCalloc(size int) (r uintptr, err error)

UintptrCalloc is like Calloc except it returns an uintptr.

func (*Allocator) UintptrFree

func (a *Allocator) UintptrFree(p uintptr) (err error)

UintptrFree is like Free except its argument is an uintptr, which must have been acquired from UintptrCalloc or UintptrMalloc or UintptrRealloc.

func (*Allocator) UintptrMalloc

func (a *Allocator) UintptrMalloc(size int) (r uintptr, err error)

UintptrMalloc is like Malloc except it returns an uinptr.

func (*Allocator) UintptrRealloc

func (a *Allocator) UintptrRealloc(p uintptr, size int) (r uintptr, err error)

UintptrRealloc is like Realloc except its first argument is an uintptr, which must have been returned from UintptrCalloc, UintptrMalloc or UintptrRealloc.

func (*Allocator) UnsafeCalloc

func (a *Allocator) UnsafeCalloc(size int) (r unsafe.Pointer, err error)

UnsafeCalloc is like Calloc except it returns an unsafe.Pointer.

func (*Allocator) UnsafeFree

func (a *Allocator) UnsafeFree(p unsafe.Pointer) (err error)

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

func (a *Allocator) UnsafeMalloc(size int) (r unsafe.Pointer, err error)

UnsafeMalloc is like Malloc except it returns an unsafe.Pointer.

func (*Allocator) UnsafeRealloc

func (a *Allocator) UnsafeRealloc(p unsafe.Pointer, size int) (r unsafe.Pointer, err error)

UnsafeRealloc is like Realloc except its first argument is an unsafe.Pointer, which must have been returned from UnsafeCalloc, UnsafeMalloc or UnsafeRealloc.

Jump to

Keyboard shortcuts

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