malloc

package
v0.0.0-debug-20260702 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MB = 1 << 20
	GB = 1 << 30
)

Variables

This section is empty.

Functions

func NewFixedSizeMakeAllocator

func NewFixedSizeMakeAllocator(size uint64) (ret *fixedSizeMakeAllocator)

func NewFixedSizeMmapAllocator added in v1.2.1

func NewFixedSizeMmapAllocator(
	size uint64,
) (ret *fixedSizeMmapAllocator)

func NewFixedSizeSyncPoolAllocator added in v1.2.1

func NewFixedSizeSyncPoolAllocator(size uint64) (ret *fixedSizeSyncPoolAllocator)

func SetDefaultConfig added in v1.2.1

func SetDefaultConfig(delta Config)

func SetMpoolProfilingHandler

func SetMpoolProfilingHandler(fn func(bool))

SetMpoolProfilingHandler registers the function to call when the MpoolProfiling config field changes via SetDefaultConfig. This indirection avoids a malloc→mpool import cycle.

func WithTempDefaultConfig

func WithTempDefaultConfig(config Config, fn func())

func WriteProfileData added in v1.2.4

func WriteProfileData(w io.Writer) error

Types

type AllocateInfo added in v1.2.1

type AllocateInfo struct {
	Deallocator Deallocator
	Size        uint64
}

type Allocator added in v1.2.1

type Allocator interface {
	Allocate(size uint64, hint Hints) ([]byte, Deallocator, error)
}

func GetDefault added in v1.2.1

func GetDefault(defaultConfig *Config) Allocator

type AtomicInteger added in v1.2.1

type AtomicInteger[T any] interface {
	Add(T) T
	Load() T
	Swap(T) T
}

type CAllocator added in v1.2.1

type CAllocator struct {
	// contains filtered or unexported fields
}

func NewCAllocator added in v1.2.1

func NewCAllocator() *CAllocator

func (*CAllocator) Allocate added in v1.2.1

func (c *CAllocator) Allocate(size uint64, hints Hints) ([]byte, Deallocator, error)

func (*CAllocator) Deallocate added in v1.2.1

func (c *CAllocator) Deallocate(slice []byte)

type CheckedAllocator added in v1.2.1

type CheckedAllocator[U Allocator] struct {
	// contains filtered or unexported fields
}

func NewCheckedAllocator added in v1.2.1

func NewCheckedAllocator[U Allocator](
	upstream U,
) *CheckedAllocator[U]

func (*CheckedAllocator[U]) Allocate added in v1.2.1

func (c *CheckedAllocator[U]) Allocate(size uint64, hints Hints) ([]byte, Deallocator, error)

type Class added in v1.2.1

type Class[T FixedSizeAllocator] struct {
	// contains filtered or unexported fields
}

type ClassAllocator added in v1.2.1

type ClassAllocator[T FixedSizeAllocator] struct {
	// contains filtered or unexported fields
}

func NewClassAllocator added in v1.2.1

func NewClassAllocator[T FixedSizeAllocator](
	newAllocator func(size uint64) T,
) *ClassAllocator[T]

func (*ClassAllocator[T]) Allocate added in v1.2.1

func (c *ClassAllocator[T]) Allocate(size uint64, hints Hints) ([]byte, Deallocator, error)

type ClosureDeallocator added in v1.2.3

type ClosureDeallocator[T any, P interface {
	*T
	TraitHolder
}] struct {
	// contains filtered or unexported fields
}

func (*ClosureDeallocator[T, P]) As added in v1.2.3

func (a *ClosureDeallocator[T, P]) As(target Trait) bool

func (*ClosureDeallocator[T, P]) Deallocate added in v1.2.3

func (a *ClosureDeallocator[T, P]) Deallocate()

func (*ClosureDeallocator[T, P]) SetArgument added in v1.2.3

func (a *ClosureDeallocator[T, P]) SetArgument(arg T)

type ClosureDeallocatorPool added in v1.2.3

type ClosureDeallocatorPool[T any, P interface {
	*T
	TraitHolder
}] struct {
	// contains filtered or unexported fields
}

func NewClosureDeallocatorPool added in v1.2.3

func NewClosureDeallocatorPool[T any, P interface {
	*T
	TraitHolder
}](
	deallocateFunc func(Hints, *T),
) *ClosureDeallocatorPool[T, P]

func (*ClosureDeallocatorPool[T, P]) Get added in v1.2.3

func (c *ClosureDeallocatorPool[T, P]) Get(args T) Deallocator

func (*ClosureDeallocatorPool[T, P]) Get2 added in v1.2.3

func (c *ClosureDeallocatorPool[T, P]) Get2(fn func(*T) T) Deallocator

type Config added in v1.2.1

type Config struct {

	// Allocator controls which allocator to use
	Allocator *string `toml:"allocator"`

	// CheckFraction controls the fraction of checked deallocations
	// On average, 1 / fraction of deallocations will be checked for double free or missing free
	CheckFraction *uint32 `toml:"check-fraction"`

	// FullStackFraction controls the fraction of full stack collecting in profile sampling
	// On average, 1 / fraction of allocations will be sampled with full stack information
	FullStackFraction *uint32 `toml:"full-stack-fraction"`

	// EnableMetrics indicates whether to expose metrics to prometheus
	EnableMetrics *bool `toml:"enable-metrics"`

	HashmapSoftLimit *uint64 `toml:"hashmap-soft-limit"`
	HashmapHardLimit *uint64 `toml:"hashmap-hard-limit"`

	// MpoolProfiling enables per-allocation stack tracking for off-heap
	// mpool allocations. Tracked allocations appear in the malloc profiler.
	MpoolProfiling *bool `toml:"mpool-profiling"`
}

func GetDefaultConfig

func GetDefaultConfig() Config

type CounterInteger added in v1.2.1

type CounterInteger interface {
	int32 | int64 | uint32 | uint64
}

type Deallocator added in v1.2.1

type Deallocator interface {
	TraitHolder
	Deallocate()
}

func ChainDeallocator added in v1.2.1

func ChainDeallocator(dec1 Deallocator, dec2 Deallocator) Deallocator

type FixedSizeAllocator added in v1.2.1

type FixedSizeAllocator interface {
	Allocate(hint Hints, clearSize uint64) ([]byte, Deallocator, error)
}

type Freezer added in v1.2.3

type Freezer struct {
	// contains filtered or unexported fields
}

func (*Freezer) IsTrait added in v1.2.3

func (*Freezer) IsTrait()

type FuncDeallocator added in v1.2.1

type FuncDeallocator func()

func (FuncDeallocator) As added in v1.2.3

func (FuncDeallocator) As(target Trait) bool

func (FuncDeallocator) Deallocate added in v1.2.1

func (f FuncDeallocator) Deallocate()

type FunctionKey added in v1.2.1

type FunctionKey struct {
	Name string
}

type HeapSampleValues added in v1.2.1

type HeapSampleValues struct {
	Objects struct {
		Allocated ShardedCounter[uint64, atomic.Uint64, *atomic.Uint64]
		Inuse     ShardedCounter[int64, atomic.Int64, *atomic.Int64]
	}
	Bytes struct {
		Allocated ShardedCounter[uint64, atomic.Uint64, *atomic.Uint64]
		Inuse     ShardedCounter[int64, atomic.Int64, *atomic.Int64]
	}
}

func (*HeapSampleValues) DefaultSampleType added in v1.2.1

func (h *HeapSampleValues) DefaultSampleType() string

func (*HeapSampleValues) Init added in v1.2.1

func (h *HeapSampleValues) Init()

func (*HeapSampleValues) SampleTypes added in v1.2.1

func (h *HeapSampleValues) SampleTypes() []*profile.ValueType

func (*HeapSampleValues) Values added in v1.2.1

func (h *HeapSampleValues) Values() []int64

type Hints added in v1.2.1

type Hints uint64
const (
	NoHints    Hints = 0
	DoNotReuse Hints = 1 << iota
	NoClear
)

type InuseTrackingAllocator

type InuseTrackingAllocator[U Allocator] struct {
	// contains filtered or unexported fields
}

func NewInuseTrackingAllocator

func NewInuseTrackingAllocator[U Allocator](
	upstream U,
	onChange func(uint64) error,
) (ret *InuseTrackingAllocator[U])

func (*InuseTrackingAllocator[U]) Allocate

func (s *InuseTrackingAllocator[U]) Allocate(size uint64, hints Hints) ([]byte, Deallocator, error)

type LeaksTracker added in v1.2.3

type LeaksTracker struct {
	// contains filtered or unexported fields
}

func (*LeaksTracker) ReportLeaks added in v1.2.3

func (t *LeaksTracker) ReportLeaks(w io.Writer) (leaks bool)

type LeaksTrackingAllocator added in v1.2.3

type LeaksTrackingAllocator[U Allocator] struct {
	// contains filtered or unexported fields
}

func NewLeaksTrackingAllocator added in v1.2.3

func NewLeaksTrackingAllocator[U Allocator](
	upstream U,
	tracker *LeaksTracker,
) (ret *LeaksTrackingAllocator[U])

func (*LeaksTrackingAllocator[U]) Allocate added in v1.2.3

func (t *LeaksTrackingAllocator[U]) Allocate(size uint64, hints Hints) ([]byte, Deallocator, error)

type LocationKey added in v1.2.1

type LocationKey struct {
	File     string
	Line     int
	Function string
}

type ManagedAllocator added in v1.2.3

type ManagedAllocator[U Allocator] struct {
	// contains filtered or unexported fields
}

func NewManagedAllocator added in v1.2.3

func NewManagedAllocator[U Allocator](
	upstream U,
) *ManagedAllocator[U]

func (*ManagedAllocator[U]) Allocate added in v1.2.3

func (m *ManagedAllocator[U]) Allocate(size uint64, hints Hints) ([]byte, error)

func (*ManagedAllocator[U]) Deallocate added in v1.2.3

func (m *ManagedAllocator[U]) Deallocate(slice []byte)

type MetricsAllocator added in v1.2.1

type MetricsAllocator[U Allocator] struct {
	// contains filtered or unexported fields
}

func NewMetricsAllocator added in v1.2.1

func NewMetricsAllocator[U Allocator](
	upstream U,
	allocateBytesCounter prometheus.Counter,
	inuseBytesGauge prometheus.Gauge,
	allocateObjectsCounter prometheus.Counter,
	inuseObjectsGauge prometheus.Gauge,
	absoluteInuseGauge prometheus.Gauge,
) *MetricsAllocator[U]

func (*MetricsAllocator[U]) Allocate added in v1.2.1

func (m *MetricsAllocator[U]) Allocate(size uint64, hints Hints) ([]byte, Deallocator, error)

type MmapInfo added in v1.2.3

type MmapInfo struct {
	Addr   unsafe.Pointer
	Length uint64
}

func (*MmapInfo) IsTrait added in v1.2.3

func (*MmapInfo) IsTrait()

type ProfileAllocator added in v1.2.1

type ProfileAllocator[U Allocator] struct {
	// contains filtered or unexported fields
}

func NewProfileAllocator added in v1.2.1

func NewProfileAllocator[U Allocator](
	upstream U,
	profiler *Profiler[HeapSampleValues, *HeapSampleValues],
	fraction uint32,
) *ProfileAllocator[U]

func (*ProfileAllocator[U]) Allocate added in v1.2.1

func (p *ProfileAllocator[U]) Allocate(size uint64, hints Hints) ([]byte, Deallocator, error)

type Profiler added in v1.2.1

type Profiler[T any, P interface {
	*T
	SampleValues[P]
}] struct {
	// contains filtered or unexported fields
}

func GlobalProfiler

func GlobalProfiler() *Profiler[HeapSampleValues, *HeapSampleValues]

GlobalProfiler returns the global heap profiler so external packages (e.g. mpool) can record off-heap allocations into the same profile.

func NewProfiler added in v1.2.1

func NewProfiler[T any, P interface {
	*T
	SampleValues[P]
}](name string) *Profiler[T, P]

func (*Profiler[T, P]) Sample added in v1.2.1

func (p *Profiler[T, P]) Sample(
	skip int,
	fullStackFraction uint32,
) P

func (*Profiler[T, P]) Write added in v1.2.1

func (p *Profiler[T, P]) Write(w io.Writer) error

type RandomAllocator added in v1.2.3

type RandomAllocator[A, B Allocator] struct {
	// contains filtered or unexported fields
}

RandomAllocator calls upstream1 or upstream2 in certain probability

func NewRandomAllocator added in v1.2.3

func NewRandomAllocator[A, B Allocator](
	upstream1 A,
	upstream2 B,
	upstream2Fraction uint32,
) *RandomAllocator[A, B]

func (*RandomAllocator[A, B]) Allocate added in v1.2.3

func (r *RandomAllocator[A, B]) Allocate(size uint64, hint Hints) ([]byte, Deallocator, error)

type ReadOnlyAllocator added in v1.2.3

type ReadOnlyAllocator[U Allocator] struct {
	// contains filtered or unexported fields
}

func NewReadOnlyAllocator added in v1.2.3

func NewReadOnlyAllocator[U Allocator](
	upstream U,
) *ReadOnlyAllocator[U]

func (*ReadOnlyAllocator[U]) Allocate added in v1.2.3

func (r *ReadOnlyAllocator[U]) Allocate(size uint64, hint Hints) ([]byte, Deallocator, error)

type SampleInfo added in v1.2.1

type SampleInfo[T any] struct {
	Values    T
	Locations []*profile.Location
}

type SampleValues added in v1.2.1

type SampleValues[T any] interface {
	Init()
	SampleTypes() []*profile.ValueType
	DefaultSampleType() string
	Values() []int64
}

type ShardedAllocator added in v1.2.1

type ShardedAllocator[T Allocator] []allocatorShard[T]

func NewShardedAllocator added in v1.2.1

func NewShardedAllocator[T Allocator](
	numShards int,
	newShard func() T,
) ShardedAllocator[T]

func (ShardedAllocator[T]) Allocate added in v1.2.1

func (s ShardedAllocator[T]) Allocate(size uint64, hints Hints) ([]byte, Deallocator, error)

type ShardedCounter added in v1.2.1

type ShardedCounter[T CounterInteger, A any, P interface {
	*A
	AtomicInteger[T]
}] struct {
	// contains filtered or unexported fields
}

func NewShardedCounter added in v1.2.1

func NewShardedCounter[T CounterInteger, A any, P interface {
	*A
	AtomicInteger[T]
}](shards int) *ShardedCounter[T, A, P]

func (*ShardedCounter[T, A, P]) Add added in v1.2.1

func (s *ShardedCounter[T, A, P]) Add(v T)

func (*ShardedCounter[T, A, P]) Each

func (s *ShardedCounter[T, A, P]) Each(fn func(P))

func (*ShardedCounter[T, A, P]) Load added in v1.2.1

func (s *ShardedCounter[T, A, P]) Load() (ret T)

type SimpleCAllocator

type SimpleCAllocator struct {
	// contains filtered or unexported fields
}

func NewSimpleCAllocator

func NewSimpleCAllocator(
	allocateBytesCounter prometheus.Counter,
	inuseBytesGauge prometheus.Gauge,
	allocateObjectsCounter prometheus.Counter,
	inuseObjectsGauge prometheus.Gauge,
	absoluteInuseGauge prometheus.Gauge,
) *SimpleCAllocator

func (*SimpleCAllocator) Allocate

func (sca *SimpleCAllocator) Allocate(size uint64) ([]byte, error)

Allocate clears the memory, calloc(size, 1)

func (*SimpleCAllocator) Deallocate

func (sca *SimpleCAllocator) Deallocate(slice []byte, size uint64)

func (*SimpleCAllocator) Malloc

func (sca *SimpleCAllocator) Malloc(size uint64) ([]byte, error)

Malloc does not clear the memory.

func (*SimpleCAllocator) ReallocZero

func (sca *SimpleCAllocator) ReallocZero(old []byte, size uint64) ([]byte, error)

RreallocZero realloc(ptr, size) and zeros the memory

type SizeBoundedAllocator added in v1.2.1

type SizeBoundedAllocator[U Allocator] struct {
	// contains filtered or unexported fields
}

func NewSizeBoundedAllocator added in v1.2.1

func NewSizeBoundedAllocator[U Allocator](
	upstream U,
	maxSize uint64,
	counter *atomic.Uint64,
) (ret *SizeBoundedAllocator[U])

func (*SizeBoundedAllocator[U]) Allocate added in v1.2.1

func (s *SizeBoundedAllocator[U]) Allocate(size uint64, hints Hints) ([]byte, Deallocator, error)

type Stacktrace

type Stacktrace unique.Handle[_PCs]

func GetStacktrace

func GetStacktrace(skip int) Stacktrace

func (Stacktrace) String

func (s Stacktrace) String() string

type Trait added in v1.2.3

type Trait interface {
	IsTrait()
}

type TraitHolder added in v1.2.3

type TraitHolder interface {
	As(trait Trait) bool
}

Jump to

Keyboard shortcuts

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