diff options
author | Dean Deng <deandeng@google.com> | 2020-08-06 11:23:09 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-08-06 11:40:03 -0700 |
commit | b918d97850075cc353abf7b590b4e807e2984ab0 (patch) | |
tree | 47287d65c71de15a1840bc979ddf492e29ab537c /pkg/refs_vfs2/refs.go | |
parent | 0d69bfa8aacb864274e11b5c66f19cd8c0d07e5c (diff) |
Add reference counting utility to VFS2.
The utility has several differences from the VFS1 equivalent:
- There are no weak references, which have a significant overhead
- In order to print useful debug messages with the type of the reference-
counted object, we use a generic Refs object with the owner type as a
template parameter. In vfs1, this was accomplished by storing a type name
and caller stack directly in the ref count (as in vfs1), which increases the
struct size by 6x. (Note that the caller stack was needed because fs types
like Dirent were shared by all fs implementations; in vfs2, each impl has
its own data structures, so this is no longer necessary.)
Updates #1486.
PiperOrigin-RevId: 325271469
Diffstat (limited to 'pkg/refs_vfs2/refs.go')
-rw-r--r-- | pkg/refs_vfs2/refs.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/pkg/refs_vfs2/refs.go b/pkg/refs_vfs2/refs.go new file mode 100644 index 000000000..ee01b17b0 --- /dev/null +++ b/pkg/refs_vfs2/refs.go @@ -0,0 +1,36 @@ +// Copyright 2020 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package refs defines an interface for a reference-counted object. +package refs + +import ( + "gvisor.dev/gvisor/pkg/context" +) + +// RefCounter is the interface to be implemented by objects that are reference +// counted. +type RefCounter interface { + // IncRef increments the reference counter on the object. + IncRef() + + // DecRef decrements the object's reference count. Users of refs_template.Refs + // may specify a destructor to be called once the reference count reaches zero. + DecRef(ctx context.Context) + + // TryIncRef attempts to increment the reference count, but may fail if all + // references have already been dropped, in which case it returns false. If + // true is returned, then a valid reference is now held on the object. + TryIncRef() bool +} |