blob: fc7e1b0fbdcbf5016e4be14c110d2c5487b5a8cb [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _CUTILS_AREF_H_
18#define _CUTILS_AREF_H_
19
20#include <stddef.h>
21
22#ifdef ANDROID_SMP
23#include <cutils/atomic-inline.h>
24#else
25#include <cutils/atomic.h>
26#endif
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#define AREF_TO_ITEM(aref, container, member) \
33 (container *) (((char*) (aref)) - offsetof(container, member))
34
35struct aref
36{
37 volatile int32_t count;
38};
39
40static inline void aref_init(struct aref *r)
41{
42 r->count = 1;
43}
44
45static inline int32_t aref_count(struct aref *r)
46{
47 return r->count;
48}
49
50static inline void aref_get(struct aref *r)
51{
52 android_atomic_inc(&r->count);
53}
54
55static inline void aref_put(struct aref *r, void (*release)(struct aref *))
56{
57 if (android_atomic_dec(&r->count) == 1)
58 release(r);
59}
60
61#ifdef __cplusplus
62}
63#endif
64
65#endif // _CUTILS_AREF_H_