| 1 | #ifndef _LTTNG_WRAPPER_KREF_H |
| 2 | #define _LTTNG_WRAPPER_KREF_H |
| 3 | |
| 4 | /* |
| 5 | * wrapper/kref.h |
| 6 | * |
| 7 | * wrapper around linux/kref.h. |
| 8 | * |
| 9 | * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; only version 2 of the License. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | * |
| 24 | * This wrapper code is derived from Linux 3.19.2 include/linux/list.h |
| 25 | * and include/linux/rculist.h, hence the GPLv2 license applied to this |
| 26 | * file. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/kref.h> |
| 30 | #include <linux/rculist.h> |
| 31 | |
| 32 | /* |
| 33 | * lttng_kref_get: get reference count, checking for overflow. |
| 34 | * |
| 35 | * Return 1 if reference is taken, 0 otherwise (overflow). |
| 36 | */ |
| 37 | static inline int lttng_kref_get(struct kref *kref) |
| 38 | { |
| 39 | if (atomic_add_unless(&kref->refcount, 1, INT_MAX) != INT_MAX) { |
| 40 | return 1; |
| 41 | } else { |
| 42 | return 0; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | #endif /* _LTTNG_WRAPPER_KREF_H */ |