1/* 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#include <sys/cdefs.h> 22 23#include <cutils/atomic.h> 24 25__BEGIN_DECLS 26 27#define AREF_TO_ITEM(aref, container, member) \ 28 (container *) (((char*) (aref)) - offsetof(container, member)) 29 30struct aref 31{ 32 volatile int32_t count; 33}; 34 35static inline void aref_init(struct aref *r) 36{ 37 r->count = 1; 38} 39 40static inline int32_t aref_count(struct aref *r) 41{ 42 return r->count; 43} 44 45static inline void aref_get(struct aref *r) 46{ 47 android_atomic_inc(&r->count); 48} 49 50static inline void aref_put(struct aref *r, void (*release)(struct aref *)) 51{ 52 if (android_atomic_dec(&r->count) == 1) 53 release(r); 54} 55 56__END_DECLS 57 58#endif // _CUTILS_AREF_H_ 59