-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync.go
More file actions
70 lines (60 loc) · 1.96 KB
/
Copy pathsync.go
File metadata and controls
70 lines (60 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) 2025 Andrey Kolkov and GoGPU Contributors
// SPDX-License-Identifier: MIT
//
// Based on OffsetAllocator by Sebastian Aaltonen
// https://github.com/sebbbi/OffsetAllocator
package galloc
import "sync"
// SyncAllocator is a thread-safe wrapper around [Allocator].
// All methods are protected by a sync.Mutex.
type SyncAllocator struct {
mu sync.Mutex
a *Allocator
}
// NewSync creates a thread-safe allocator that manages a contiguous range of
// size units with room for at most maxAllocs simultaneous allocations.
func NewSync(size, maxAllocs uint32) *SyncAllocator {
return &SyncAllocator{a: New(size, maxAllocs)}
}
// Allocate reserves a contiguous region of the given size.
// See [Allocator.Allocate] for details.
func (s *SyncAllocator) Allocate(size uint32) Allocation {
s.mu.Lock()
defer s.mu.Unlock()
return s.a.Allocate(size)
}
// AllocateAligned reserves a contiguous region at an aligned offset.
// See [Allocator.AllocateAligned] for details.
func (s *SyncAllocator) AllocateAligned(size, alignment uint32) Allocation {
s.mu.Lock()
defer s.mu.Unlock()
return s.a.AllocateAligned(size, alignment)
}
// Free releases a previously-made allocation.
// See [Allocator.Free] for details.
func (s *SyncAllocator) Free(alloc Allocation) {
s.mu.Lock()
defer s.mu.Unlock()
s.a.Free(alloc)
}
// AllocationSize returns the size stored for an allocation.
// See [Allocator.AllocationSize] for details.
func (s *SyncAllocator) AllocationSize(alloc Allocation) uint32 {
s.mu.Lock()
defer s.mu.Unlock()
return s.a.AllocationSize(alloc)
}
// StorageReport returns a summary of the allocator's free space.
// See [Allocator.StorageReport] for details.
func (s *SyncAllocator) StorageReport() StorageReport {
s.mu.Lock()
defer s.mu.Unlock()
return s.a.StorageReport()
}
// Reset clears all allocations and returns the allocator to its initial state.
// See [Allocator.Reset] for details.
func (s *SyncAllocator) Reset() {
s.mu.Lock()
defer s.mu.Unlock()
s.a.Reset()
}