2 * ref.h: reference counting macros
4 * Copyright (C) 2007-2016 David Lutterkort
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Author: David Lutterkort <dlutter@redhat.com>
29 /* Reference counting for pointers to structs with a REF field of type ref_t
31 * When a pointer to such a struct is passed into a function that stores
32 * it, the function can either "receive ownership", meaning it does not
33 * increment the reference count, or it can "take ownership", meaning it
34 * increments the reference count. In the first case, the reference is now
35 * owned by wherever the function stored it, and not the caller anymore; in
36 * the second case, the caller and whereever the reference was stored both
39 // FIXME: This is not threadsafe; incr/decr ref needs to be protected
41 #define REF_MAX UINT_MAX
43 typedef unsigned int ref_t;
45 int ref_make_ref(void *ptrptr, size_t size, size_t ref_ofs);
47 #define make_ref(var) \
48 ref_make_ref(&(var), sizeof(*(var)), offsetof(typeof(*(var)), ref))
50 #define make_ref_err(var) if (make_ref(var) < 0) goto error
52 #define ref(s) (((s) == NULL || (s)->ref == REF_MAX) ? (s) : ((s)->ref++, (s)))
56 if ((s) != NULL && (s)->ref != REF_MAX) { \
57 assert((s)->ref > 0); \
58 if (--(s)->ref == 0) { \
59 /*memset(s, 255, sizeof(*s));*/ \
66 /* Make VAR uncollectable and pin it in memory for eternity */
67 #define ref_pin(var) (var)->ref = REF_MAX
74 * indent-tabs-mode: nil