-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fbf0eab
commit 23e6aad
Showing
3 changed files
with
329 additions
and
168 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package alloc | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func newRing[T any](capacity uint64) (*ring[T], []T) { | ||
addresses := make([]T, capacity) | ||
return &ring[T]{ | ||
addresses: addresses, | ||
capacity: capacity, | ||
commitPtr: capacity, | ||
}, addresses | ||
} | ||
|
||
type ring[T any] struct { | ||
addresses []T | ||
|
||
capacity uint64 | ||
allocPtr, commitPtr, deallocPtr uint64 | ||
} | ||
|
||
func (r *ring[T]) Allocate() (T, error) { | ||
if r.allocPtr == r.commitPtr { | ||
var t T | ||
return t, errors.New("no free address to allocate") | ||
} | ||
if r.allocPtr == r.capacity { | ||
r.allocPtr = 0 | ||
} | ||
a := r.addresses[r.allocPtr] | ||
r.allocPtr++ | ||
return a, nil | ||
} | ||
|
||
func (r *ring[T]) Deallocate(item T) { | ||
if r.deallocPtr == r.capacity { | ||
r.deallocPtr = 0 | ||
} | ||
if r.deallocPtr == r.allocPtr { | ||
// This is really critical because it means that we deallocated more than allocated. | ||
panic("no space left in the ring for deallocation") | ||
} | ||
|
||
r.addresses[r.deallocPtr] = item | ||
r.deallocPtr++ | ||
} | ||
|
||
func (r *ring[T]) Commit() { | ||
r.commitPtr = r.deallocPtr - 1 | ||
} |
Oops, something went wrong.