Use clear on map types wherever possible.

This is similar as pull request #9749 but for maps rather than slices.

PiperOrigin-RevId: 586504320
This commit is contained in:
Etienne Perot
2023-11-29 18:00:07 -08:00
committed by gVisor bot
parent be011b9bfe
commit 69e0c7643d
11 changed files with 16 additions and 13 deletions
+1 -1
View File
@@ -213,7 +213,7 @@ func (b *ProgramBuilder) resolveLabels() error {
b.instructions[s.line] = inst
}
}
b.labels = map[string]*label{}
clear(b.labels)
return nil
}
+1 -1
View File
@@ -390,7 +390,7 @@ func (c *Client) handleOne() {
for _, resp := range c.pending {
resp.done <- err
}
c.pending = make(map[Tag]*response)
clear(c.pending)
c.pendingMu.Unlock()
} else {
// Process the tag.
+2 -2
View File
@@ -221,7 +221,7 @@ func (c *devicesController) applyRule(id deviceID, p permission, allow bool) err
// If the device type is all, it will reset the rules for all.
if id.controllerType == wildcardDevice {
c.defaultAllow = allow
c.deviceRules = make(map[deviceID]permission)
clear(c.deviceRules)
return nil
}
if !p.valid() {
@@ -229,7 +229,7 @@ func (c *devicesController) applyRule(id deviceID, p permission, allow bool) err
}
if len(c.deviceRules) == 0 {
c.defaultAllow = allow
c.deviceRules = make(map[deviceID]permission)
clear(c.deviceRules)
}
if allow == c.defaultAllow {
return c.addRule(id, p)
+2 -2
View File
@@ -235,7 +235,7 @@ func (l *Lock) lock(uid UniqueID, ownerPID int32, t LockType, ofd bool) {
panic(fmt.Sprintf("lock: cannot downgrade write lock to read lock for uid %d, writer is %d", uid, l.Writer))
}
// Ensure that there is only one reader if upgrading.
l.Readers = make(map[UniqueID]OwnerInfo)
clear(l.Readers)
// Ensure that there is no longer a writer.
l.Writer = nil
}
@@ -258,7 +258,7 @@ func (l *Lock) lock(uid UniqueID, ownerPID int32, t LockType, ofd bool) {
}
}
// Ensure that there is only a writer.
l.Readers = make(map[UniqueID]OwnerInfo)
clear(l.Readers)
l.Writer = uid
l.WriterInfo = OwnerInfo{PID: ownerPID, OFD: ofd}
default:
+3
View File
@@ -56,6 +56,9 @@ func equals(e0, e1 []entry) bool {
func fill(entries []entry) LockSet {
l := LockSet{}
for _, e := range entries {
if e.Readers == nil {
e.Readers = make(map[UniqueID]OwnerInfo)
}
gap := l.FindGap(e.LockRange.Start)
if !gap.Ok() {
panic("cannot insert into existing segment")
+1 -2
View File
@@ -584,8 +584,7 @@ func (t *Task) exitPtrace() {
// this is consistent with Linux.
target.forgetTracerLocked()
}
// "nil maps cannot be saved"
t.ptraceTracees = make(map[*Task]struct{})
clear(t.ptraceTracees) // nil maps cannot be saved
if t.ptraceYAMAExceptionAdded {
delete(t.k.ptraceExceptions, t)
+1 -1
View File
@@ -174,7 +174,7 @@ func (r *runSyscallAfterExecStop) execute(t *Task) taskRunState {
for _, it := range t.tg.timers {
its = append(its, it)
}
t.tg.timers = make(map[linux.TimerID]*IntervalTimer)
clear(t.tg.timers)
t.tg.signalHandlers.mu.Unlock()
t.tg.pidns.owner.mu.Unlock()
for _, it := range its {
+1 -1
View File
@@ -321,7 +321,7 @@ func (tg *ThreadGroup) Release(ctx context.Context) {
for _, it := range tg.timers {
its = append(its, it)
}
tg.timers = make(map[linux.TimerID]*IntervalTimer) // nil maps can't be saved
clear(tg.timers) // nil maps can't be saved
// Disassociate from the tty if we have one.
if tg.tty != nil {
tg.tty.mu.Lock()
+2 -1
View File
@@ -992,7 +992,7 @@ func (vfs *VirtualFilesystem) PopDelayedDecRefs() []refs.RefCounter {
rcs = append(rcs, rc)
}
}
vfs.toDecRef = map[refs.RefCounter]int{}
clear(vfs.toDecRef)
return rcs
}
@@ -1023,6 +1023,7 @@ func (vfs *VirtualFilesystem) unlockMounts(ctx context.Context) {
return
}
toDecRef := vfs.toDecRef
// Can't use `clear` here as this would reference the same map as `toDecRef`.
vfs.toDecRef = map[refs.RefCounter]int{}
vfs.mountMu.Unlock()
for rc, refs := range toDecRef {
+1 -1
View File
@@ -247,7 +247,7 @@ func (n *neighborCache) clear() {
}
n.mu.dynamic.lru = neighborEntryList{}
n.mu.cache = make(map[tcpip.Address]*neighborEntry)
clear(n.mu.cache)
n.mu.dynamic.count = 0
}
+1 -1
View File
@@ -101,7 +101,7 @@ func (l *lockState) modify() {
l.stored = s
// Reset the used values.
l.used = make(map[ssa.Value]struct{})
clear(l.used)
// Copy the defers.
ds := make([]*ssa.Defer, len(l.defers))