Skip to content

Commit 8301aa0

Browse files
committed
tree-wide: use DEFINE_TRIVIAL_REF_UNREF_FUNC() macro or friends where applicable
1 parent a6a0859 commit 8301aa0

34 files changed

+176
-763
lines changed

src/basic/bpf-program.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,8 @@ int bpf_program_new(uint32_t prog_type, BPFProgram **ret) {
2929
return 0;
3030
}
3131

32-
BPFProgram *bpf_program_ref(BPFProgram *p) {
33-
if (!p)
34-
return NULL;
35-
36-
assert(p->n_ref > 0);
37-
p->n_ref++;
38-
39-
return p;
40-
}
41-
42-
BPFProgram *bpf_program_unref(BPFProgram *p) {
43-
if (!p)
44-
return NULL;
45-
46-
assert(p->n_ref > 0);
47-
p->n_ref--;
48-
49-
if (p->n_ref > 0)
50-
return NULL;
32+
static BPFProgram *bpf_program_free(BPFProgram *p) {
33+
assert(p);
5134

5235
/* Unfortunately, the kernel currently doesn't implicitly detach BPF programs from their cgroups when the last
5336
* fd to the BPF program is closed. This has nasty side-effects since this means that abnormally terminated
@@ -66,6 +49,8 @@ BPFProgram *bpf_program_unref(BPFProgram *p) {
6649
return mfree(p);
6750
}
6851

52+
DEFINE_TRIVIAL_REF_UNREF_FUNC(BPFProgram, bpf_program, bpf_program_free);
53+
6954
int bpf_program_add_instructions(BPFProgram *p, const struct bpf_insn *instructions, size_t count) {
7055

7156
assert(p);

src/basic/format-table.c

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -171,32 +171,16 @@ Table *table_new_internal(const char *first_header, ...) {
171171
return TAKE_PTR(t);
172172
}
173173

174-
static TableData *table_data_unref(TableData *d) {
175-
if (!d)
176-
return NULL;
177-
178-
assert(d->n_ref > 0);
179-
d->n_ref--;
180-
181-
if (d->n_ref > 0)
182-
return NULL;
174+
static TableData *table_data_free(TableData *d) {
175+
assert(d);
183176

184177
free(d->formatted);
185178
return mfree(d);
186179
}
187180

181+
DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(TableData, table_data, table_data_free);
188182
DEFINE_TRIVIAL_CLEANUP_FUNC(TableData*, table_data_unref);
189183

190-
static TableData *table_data_ref(TableData *d) {
191-
if (!d)
192-
return NULL;
193-
194-
assert(d->n_ref > 0);
195-
d->n_ref++;
196-
197-
return d;
198-
}
199-
200184
Table *table_unref(Table *t) {
201185
size_t i;
202186

src/core/dynamic-user.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
/* Takes a value generated randomly or by hashing and turns it into a UID in the right range */
2121
#define UID_CLAMP_INTO_RANGE(rnd) (((uid_t) (rnd) % (DYNAMIC_UID_MAX - DYNAMIC_UID_MIN + 1)) + DYNAMIC_UID_MIN)
2222

23+
DEFINE_PRIVATE_TRIVIAL_REF_FUNC(DynamicUser, dynamic_user);
24+
2325
static DynamicUser* dynamic_user_free(DynamicUser *d) {
2426
if (!d)
2527
return NULL;
@@ -531,16 +533,6 @@ int dynamic_user_current(DynamicUser *d, uid_t *ret) {
531533
return 0;
532534
}
533535

534-
static DynamicUser* dynamic_user_ref(DynamicUser *d) {
535-
if (!d)
536-
return NULL;
537-
538-
assert(d->n_ref > 0);
539-
d->n_ref++;
540-
541-
return d;
542-
}
543-
544536
static DynamicUser* dynamic_user_unref(DynamicUser *d) {
545537
if (!d)
546538
return NULL;

src/core/socket.c

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -547,33 +547,17 @@ static SocketPeer *socket_peer_new(void) {
547547
return p;
548548
}
549549

550-
SocketPeer *socket_peer_ref(SocketPeer *p) {
551-
if (!p)
552-
return NULL;
553-
554-
assert(p->n_ref > 0);
555-
p->n_ref++;
556-
557-
return p;
558-
}
559-
560-
SocketPeer *socket_peer_unref(SocketPeer *p) {
561-
if (!p)
562-
return NULL;
563-
564-
assert(p->n_ref > 0);
565-
566-
p->n_ref--;
567-
568-
if (p->n_ref > 0)
569-
return NULL;
550+
static SocketPeer *socket_peer_free(SocketPeer *p) {
551+
assert(p);
570552

571553
if (p->socket)
572554
set_remove(p->socket->peers_by_address, p);
573555

574556
return mfree(p);
575557
}
576558

559+
DEFINE_TRIVIAL_REF_UNREF_FUNC(SocketPeer, socket_peer, socket_peer_free);
560+
577561
int socket_acquire_peer(Socket *s, int fd, SocketPeer **p) {
578562
_cleanup_(socket_peer_unrefp) SocketPeer *remote = NULL;
579563
SocketPeer sa = {}, *i;

src/journal-remote/journal-remote-write.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Writer* writer_new(RemoteServer *server) {
3434
return w;
3535
}
3636

37-
Writer* writer_free(Writer *w) {
37+
static Writer* writer_free(Writer *w) {
3838
if (!w)
3939
return NULL;
4040

@@ -54,19 +54,7 @@ Writer* writer_free(Writer *w) {
5454
return mfree(w);
5555
}
5656

57-
Writer* writer_unref(Writer *w) {
58-
if (w && (-- w->n_ref <= 0))
59-
writer_free(w);
60-
61-
return NULL;
62-
}
63-
64-
Writer* writer_ref(Writer *w) {
65-
if (w)
66-
assert_se(++ w->n_ref >= 2);
67-
68-
return w;
69-
}
57+
DEFINE_TRIVIAL_REF_UNREF_FUNC(Writer, writer, writer_free);
7058

7159
int writer_write(Writer *w,
7260
struct iovec_wrapper *iovw,

src/journal-remote/journal-remote-write.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ typedef struct Writer {
2020
} Writer;
2121

2222
Writer* writer_new(RemoteServer* server);
23-
Writer* writer_free(Writer *w);
24-
2523
Writer* writer_ref(Writer *w);
2624
Writer* writer_unref(Writer *w);
2725

src/journal/mmap-cache.c

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,6 @@ MMapCache* mmap_cache_new(void) {
8585
return m;
8686
}
8787

88-
MMapCache* mmap_cache_ref(MMapCache *m) {
89-
assert(m);
90-
assert(m->n_ref > 0);
91-
92-
m->n_ref++;
93-
return m;
94-
}
95-
9688
static void window_unlink(Window *w) {
9789
Context *c;
9890

@@ -278,7 +270,7 @@ static void context_free(Context *c) {
278270
free(c);
279271
}
280272

281-
static void mmap_cache_free(MMapCache *m) {
273+
static MMapCache *mmap_cache_free(MMapCache *m) {
282274
int i;
283275

284276
assert(m);
@@ -292,22 +284,10 @@ static void mmap_cache_free(MMapCache *m) {
292284
while (m->unused)
293285
window_free(m->unused);
294286

295-
free(m);
287+
return mfree(m);
296288
}
297289

298-
MMapCache* mmap_cache_unref(MMapCache *m) {
299-
300-
if (!m)
301-
return NULL;
302-
303-
assert(m->n_ref > 0);
304-
305-
m->n_ref--;
306-
if (m->n_ref == 0)
307-
mmap_cache_free(m);
308-
309-
return NULL;
310-
}
290+
DEFINE_TRIVIAL_REF_UNREF_FUNC(MMapCache, mmap_cache, mmap_cache_free);
311291

312292
static int make_room(MMapCache *m) {
313293
assert(m);

src/libsystemd-network/ndisc-router.c

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,7 @@
1515
#include "ndisc-router.h"
1616
#include "strv.h"
1717

18-
_public_ sd_ndisc_router* sd_ndisc_router_ref(sd_ndisc_router *rt) {
19-
if (!rt)
20-
return NULL;
21-
22-
assert(rt->n_ref > 0);
23-
rt->n_ref++;
24-
25-
return rt;
26-
}
27-
28-
_public_ sd_ndisc_router* sd_ndisc_router_unref(sd_ndisc_router *rt) {
29-
if (!rt)
30-
return NULL;
31-
32-
assert(rt->n_ref > 0);
33-
rt->n_ref--;
34-
35-
if (rt->n_ref > 0)
36-
return NULL;
37-
38-
return mfree(rt);
39-
}
18+
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_ndisc_router, sd_ndisc_router, mfree);
4019

4120
sd_ndisc_router *ndisc_router_new(size_t raw_size) {
4221
sd_ndisc_router *rt;

src/libsystemd-network/sd-dhcp-client.c

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,27 +1951,8 @@ sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client) {
19511951
return client->event;
19521952
}
19531953

1954-
sd_dhcp_client *sd_dhcp_client_ref(sd_dhcp_client *client) {
1955-
1956-
if (!client)
1957-
return NULL;
1958-
1959-
assert(client->n_ref >= 1);
1960-
client->n_ref++;
1961-
1962-
return client;
1963-
}
1964-
1965-
sd_dhcp_client *sd_dhcp_client_unref(sd_dhcp_client *client) {
1966-
1967-
if (!client)
1968-
return NULL;
1969-
1970-
assert(client->n_ref >= 1);
1971-
client->n_ref--;
1972-
1973-
if (client->n_ref > 0)
1974-
return NULL;
1954+
static sd_dhcp_client *dhcp_client_free(sd_dhcp_client *client) {
1955+
assert(client);
19751956

19761957
log_dhcp_client(client, "FREE");
19771958

@@ -1990,6 +1971,8 @@ sd_dhcp_client *sd_dhcp_client_unref(sd_dhcp_client *client) {
19901971
return mfree(client);
19911972
}
19921973

1974+
DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_client, sd_dhcp_client, dhcp_client_free);
1975+
19931976
int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize) {
19941977
_cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL;
19951978

src/libsystemd-network/sd-dhcp-lease.c

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -246,27 +246,8 @@ int sd_dhcp_lease_get_vendor_specific(sd_dhcp_lease *lease, const void **data, s
246246
return 0;
247247
}
248248

249-
sd_dhcp_lease *sd_dhcp_lease_ref(sd_dhcp_lease *lease) {
250-
251-
if (!lease)
252-
return NULL;
253-
254-
assert(lease->n_ref >= 1);
255-
lease->n_ref++;
256-
257-
return lease;
258-
}
259-
260-
sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) {
261-
262-
if (!lease)
263-
return NULL;
264-
265-
assert(lease->n_ref >= 1);
266-
lease->n_ref--;
267-
268-
if (lease->n_ref > 0)
269-
return NULL;
249+
static sd_dhcp_lease *dhcp_lease_free(sd_dhcp_lease *lease) {
250+
assert(lease);
270251

271252
while (lease->private_options) {
272253
struct sd_dhcp_raw_option *option = lease->private_options;
@@ -288,6 +269,8 @@ sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) {
288269
return mfree(lease);
289270
}
290271

272+
DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_lease, sd_dhcp_lease, dhcp_lease_free);
273+
291274
static int lease_parse_u32(const uint8_t *option, size_t len, uint32_t *ret, uint32_t min) {
292275
assert(option);
293276
assert(ret);

0 commit comments

Comments
 (0)