Menu

📰
0

Reddit - Please wait for verification

The Go Programming Language·/u/Revolutionary_Sir140·4 days ago
#A3SxLkQ6
Reading 0:00
15s threshold

package arena type MemoryArena[T any] struct { buffer []T offset uintptr } func NewMemoryArena[T any](size int) MemoryArena[T] { return &MemoryArena[T]{ buffer: make([]T, size), offset: 0, } } func (memoryarena MemoryArena[T]) Alloc(obj T) T { if memoryarena.offset >= uintptr(len(memoryarena.buffer)) { panic("Not enough space") } memoryarena.buffer[memoryarena.offset] = obj allocated := &memoryarena.buffer[memoryarena.offset] memoryarena.offset++ return allocated } func (memoryarena MemoryArena[T]) Reset() { memoryarena.offset = 0 var zero T for i := range memoryarena.buffer { memoryarena.buffer[i] = zero } } Hi, I've decided to not use AI and implemented simple memory arena where buffer is slice of generic type, I do realize that memory arena can be implemented using bytes and probably bytes is the best case scenario. I am looking for feedback, what should I add next? Maybe AllocArray or mutex? submitted by /u/Revolutionary_Sir140 [link] [comments]

Read More