Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions offheap/object_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package offheap

import (
"fmt"
"reflect"
"unsafe"

"github.com/fmstephe/memorymanager/offheap/internal/pointerstore"
Expand All @@ -19,10 +19,8 @@ import (
// Go allocations objects acquired via AllocObject do _not_ have their contents
// zeroed out.
func AllocObject[T any](s *Store) RefObject[T] {
// TODO this is not fast - we _need_ to cache this type data
if err := containsNoPointers[T](); err != nil {
panic(fmt.Errorf("cannot allocate generic type containing pointers %w", err))
}
t := reflect.TypeFor[T]()
s.checker.checkType(t)

idx := indexForType[T]()

Expand Down
11 changes: 8 additions & 3 deletions offheap/object_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ import (
const defaultSlabSize = 1 << 13

type Store struct {
checker typeChecker
sizedStores []*pointerstore.Store
}

// Returns a new *Store.
//
// This store manages allocation and freeing of any offheap allocated objects.
func New() *Store {
return &Store{
sizedStores: initSizeStore(defaultSlabSize),
}
return newSized(defaultSlabSize)
}

// Returns a new *Store.
Expand All @@ -35,7 +34,13 @@ func New() *Store {
// small slab sizes to allow faster tests with reduced memory usage. Most users
// will probably prefer to use the default New() above.
func NewSized(slabSize int) *Store {
return newSized(slabSize)
}

// Returns a new *Store
func newSized(slabSize int) *Store {
return &Store{
checker: newTypeChecker(),
sizedStores: initSizeStore(slabSize),
}
}
Expand Down
117 changes: 0 additions & 117 deletions offheap/pointer_checker_test.go

This file was deleted.

7 changes: 3 additions & 4 deletions offheap/slice_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package offheap

import (
"fmt"
"reflect"
"unsafe"

"github.com/fmstephe/memorymanager/offheap/internal/pointerstore"
Expand All @@ -18,10 +19,8 @@ import (
// The contents of the slice will be arbitrary. Unlike Go slices acquired via
// AllocSlice do _not_ have their contents zeroed out.
func AllocSlice[T any](s *Store, length, requestedCapacity int) RefSlice[T] {
// TODO this is not fast - we _need_ to cache this type data
if err := containsNoPointers[T](); err != nil {
panic(fmt.Errorf("cannot allocate generic type containing pointers %w", err))
}
t := reflect.TypeFor[T]()
s.checker.checkType(t)

// Round the requested capacity up to a power of 2
actualCapacity := capacityForSlice(requestedCapacity)
Expand Down
45 changes: 42 additions & 3 deletions offheap/pointer_checker.go → offheap/type_checker.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Francis Michael Stephens. All rights reserved. Use of this
// Copyright 2025 Francis Michael Stephens. All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

Expand All @@ -8,8 +8,48 @@ import (
"fmt"
"reflect"
"strconv"
"sync"
)

type typeChecker struct {
typeCacheLock sync.RWMutex
typeCache map[reflect.Type]struct{}
}

func newTypeChecker() typeChecker {
return typeChecker{
typeCacheLock: sync.RWMutex{},
typeCache: map[reflect.Type]struct{}{},
}
}

func (c *typeChecker) checkType(t reflect.Type) {
if c.lookupTypeCache(t) {
return
}

if err := containsNoPointers(t); err != nil {
panic(fmt.Errorf("cannot allocate generic type containing pointers %w", err))
}

c.writeTypeToCache(t)
}

func (c *typeChecker) lookupTypeCache(t reflect.Type) bool {
c.typeCacheLock.RLock()
defer c.typeCacheLock.RUnlock()

_, ok := c.typeCache[t]
return ok
}

func (c *typeChecker) writeTypeToCache(t reflect.Type) {
c.typeCacheLock.Lock()
defer c.typeCacheLock.Unlock()

c.typeCache[t] = struct{}{}
}

type typePaths struct {
paths []string
}
Expand All @@ -35,8 +75,7 @@ func (p *typePaths) String() string {
return result[:len(result)-1]
}

func containsNoPointers[O any]() error {
t := reflect.TypeFor[O]()
func containsNoPointers(t reflect.Type) error {
paths := &typePaths{}
searchForPointers(t, "", paths)
if paths.Len() != 0 {
Expand Down
Loading