1 // @generated Code generated by gen-atomicint.
2 3 // Copyright (c) 2020-2023 Uber Technologies, Inc.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 23 package atomic
24 25 import (
26 "encoding/json"
27 "strconv"
28 "sync/atomic"
29 )
30 31 // Uintptr is an atomic wrapper around uintptr.
32 type Uintptr struct {
33 _ nocmp // disallow non-atomic comparison
34 35 v uintptr
36 }
37 38 // NewUintptr creates a new Uintptr.
39 func NewUintptr(val uintptr) *Uintptr {
40 return &Uintptr{v: val}
41 }
42 43 // Load atomically loads the wrapped value.
44 func (i *Uintptr) Load() uintptr {
45 return atomic.LoadUintptr(&i.v)
46 }
47 48 // Add atomically adds to the wrapped uintptr and returns the new value.
49 func (i *Uintptr) Add(delta uintptr) uintptr {
50 return atomic.AddUintptr(&i.v, delta)
51 }
52 53 // Sub atomically subtracts from the wrapped uintptr and returns the new value.
54 func (i *Uintptr) Sub(delta uintptr) uintptr {
55 return atomic.AddUintptr(&i.v, ^(delta - 1))
56 }
57 58 // Inc atomically increments the wrapped uintptr and returns the new value.
59 func (i *Uintptr) Inc() uintptr {
60 return i.Add(1)
61 }
62 63 // Dec atomically decrements the wrapped uintptr and returns the new value.
64 func (i *Uintptr) Dec() uintptr {
65 return i.Sub(1)
66 }
67 68 // CAS is an atomic compare-and-swap.
69 //
70 // Deprecated: Use CompareAndSwap.
71 func (i *Uintptr) CAS(old, new uintptr) (swapped bool) {
72 return i.CompareAndSwap(old, new)
73 }
74 75 // CompareAndSwap is an atomic compare-and-swap.
76 func (i *Uintptr) CompareAndSwap(old, new uintptr) (swapped bool) {
77 return atomic.CompareAndSwapUintptr(&i.v, old, new)
78 }
79 80 // Store atomically stores the passed value.
81 func (i *Uintptr) Store(val uintptr) {
82 atomic.StoreUintptr(&i.v, val)
83 }
84 85 // Swap atomically swaps the wrapped uintptr and returns the old value.
86 func (i *Uintptr) Swap(val uintptr) (old uintptr) {
87 return atomic.SwapUintptr(&i.v, val)
88 }
89 90 // MarshalJSON encodes the wrapped uintptr into JSON.
91 func (i *Uintptr) MarshalJSON() ([]byte, error) {
92 return json.Marshal(i.Load())
93 }
94 95 // UnmarshalJSON decodes JSON into the wrapped uintptr.
96 func (i *Uintptr) UnmarshalJSON(b []byte) error {
97 var v uintptr
98 if err := json.Unmarshal(b, &v); err != nil {
99 return err
100 }
101 i.Store(v)
102 return nil
103 }
104 105 // String encodes the wrapped value as a string.
106 func (i *Uintptr) String() string {
107 v := i.Load()
108 return strconv.FormatUint(uint64(v), 10)
109 }
110