|
| 1 | +// Copyright 2025 The go-ethereum Authors |
| 2 | +// This file is part of the go-ethereum library. |
| 3 | +// |
| 4 | +// The go-ethereum library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// The go-ethereum library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package common |
| 18 | + |
| 19 | +// Arena is an allocation primitive that allows individual allocations |
| 20 | +// out of a page of items and bulk de-allocations of last N allocations. |
| 21 | +// The most common way of using an Arena is to pair it up with a sync.Pool |
| 22 | +// of pages. |
| 23 | +type Arena[T any] struct { |
| 24 | + used uint32 |
| 25 | + pages [][]T |
| 26 | + |
| 27 | + PageSize uint32 |
| 28 | + NewPage func() any |
| 29 | + ReleasePage func(any) |
| 30 | +} |
| 31 | + |
| 32 | +// Alloc returns the next free item on the arena |
| 33 | +// Allocates a new page if needed |
| 34 | +func (a *Arena[T]) Alloc() *T { |
| 35 | + pageIndex := a.used / a.PageSize |
| 36 | + pageOffset := a.used % a.PageSize |
| 37 | + if pageOffset == 0 { |
| 38 | + a.pages = append(a.pages, a.NewPage().([]T)) |
| 39 | + } |
| 40 | + a.used++ |
| 41 | + return &a.pages[pageIndex][pageOffset] |
| 42 | +} |
| 43 | + |
| 44 | +// Used returns the number of items that live on this arena |
| 45 | +func (a *Arena[T]) Used() uint32 { |
| 46 | + return a.used |
| 47 | +} |
| 48 | + |
| 49 | +// Reset rollsback the active set of live elements to the given number |
| 50 | +func (a *Arena[T]) Reset(to uint32) { |
| 51 | + a.used = to |
| 52 | +} |
| 53 | + |
| 54 | +// Release releases all the pages that the arena currently owns |
| 55 | +func (a *Arena[T]) Release() { |
| 56 | + for _, page := range a.pages { |
| 57 | + a.ReleasePage(page) |
| 58 | + } |
| 59 | + a.pages = nil |
| 60 | + a.used = 0 |
| 61 | +} |
0 commit comments