Skip to content

Commit 129c12f

Browse files
authored
Move locks closer to the protected part
1 parent 76708a3 commit 129c12f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

mem.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ func roundMultiple(n, k uint) uint {
4242
// POSIX libc's malloc. However, Alloc panics if an error occurs when
4343
// requesting more memory from the operating system.
4444
func Alloc(size uint) unsafe.Pointer {
45-
m.Lock()
46-
defer m.Unlock()
4745
if size == 0 {
4846
return nil
4947
}
48+
m.Lock()
49+
defer m.Unlock()
5050
// Iterate through linked list of headers.
5151
p := freep
5252
for {
@@ -97,11 +97,11 @@ func Alloc(size uint) unsafe.Pointer {
9797
// However, Free panics if an error occurs when releasing memory to
9898
// the operating system.
9999
func Free(p unsafe.Pointer) {
100-
m.Lock()
101-
defer m.Unlock()
102100
if p == nil {
103101
return
104102
}
103+
m.Lock()
104+
defer m.Unlock()
105105
h := (*header)(unsafe.Pointer(uintptr(p) - uintptr(szheader)))
106106
arcurr := h.arena
107107
h.allocated = false

0 commit comments

Comments
 (0)