1 /*
2   Copyright 2021 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 #include <alloc.h>
26 #include <refcount.h>
27 #include <misc_lib.h>
28 #include <platform.h>
29 
RefCountNew(RefCount ** ref)30 void RefCountNew(RefCount **ref)
31 {
32     if (!ref)
33     {
34         return;
35     }
36     *ref = (RefCount *)xmalloc(sizeof(RefCount));
37     (*ref)->user_count = 0;
38     (*ref)->users = NULL;
39     (*ref)->last = NULL;
40 }
41 
RefCountDestroy(RefCount ** ref)42 void RefCountDestroy(RefCount **ref)
43 {
44     if (ref && *ref)
45     {
46         // Destroying a refcount which has more than one user is a bug, but we let it
47         // pass in production code (memory leak).
48         assert((*ref)->user_count <= 1);
49         if ((*ref)->user_count > 1)
50             return;
51         if ((*ref)->users)
52             free((*ref)->users);
53         free(*ref);
54         *ref = NULL;
55     }
56 }
57 
RefCountAttach(RefCount * ref,void * owner)58 void RefCountAttach(RefCount *ref, void *owner)
59 {
60     if (!ref || !owner)
61     {
62         ProgrammingError("Either refcount or owner is NULL (or both)");
63     }
64     ref->user_count++;
65     RefCountNode *node = (RefCountNode *)xmalloc(sizeof(RefCountNode));
66     node->next = NULL;
67     node->user = owner;
68     if (ref->last)
69     {
70         ref->last->next = node;
71         node->previous = ref->last;
72     }
73     else
74     {
75         ref->users = node;
76         node->previous = NULL;
77     }
78     ref->last = node;
79 }
80 
RefCountDetach(RefCount * ref,void * owner)81 void RefCountDetach(RefCount *ref, void *owner)
82 {
83     if (!ref || !owner)
84     {
85         ProgrammingError("Either refcount or owner is NULL (or both)");
86     }
87     if (ref->user_count <= 1)
88     {
89         /*
90          * Semantics: If 1 that means that we are the only users, if 0 nobody is using it.
91          * In either case it is safe to destroy the refcount.
92          */
93         return;
94     }
95     RefCountNode *p = NULL;
96     int found = 0;
97     for (p = ref->users; p; p = p->next)
98     {
99         if (p->user == owner)
100         {
101             found = 1;
102             if (p->previous && p->next)
103             {
104                 p->previous->next = p->next;
105                 p->next->previous = p->previous;
106             }
107             else if (p->previous && !p->next)
108             {
109                 // Last node
110                 p->previous->next = NULL;
111                 ref->last = p->previous;
112             }
113             else if (!p->previous && p->next)
114             {
115                 // First node
116                 ref->users = p->next;
117                 p->next->previous = NULL;
118             }
119             else
120             {
121                 // Only one node, we cannot detach from ourselves.
122                 return;
123             }
124             free(p);
125             break;
126         }
127     }
128     if (!found)
129     {
130         ProgrammingError("The object is not attached to the RefCount object");
131     }
132     ref->user_count--;
133 }
134 
RefCountIsShared(RefCount * ref)135 bool RefCountIsShared(RefCount *ref)
136 {
137     return ref && (ref->user_count > 1);
138 }
139 
RefCountIsEqual(RefCount * a,RefCount * b)140 bool RefCountIsEqual(RefCount *a, RefCount *b)
141 {
142     return (a && b) && (a == b);
143 }
144