Skip to content

Commit c35a784

Browse files
calebsanderigaw
authored andcommitted
cleanup: add cleanup functions
Introduce _cleanup_free_ from nvme-cli to call free() on cleanup, _cleanup_file_ to call fclose(), _cleanup_dir_ to call closedir(), and _cleanup_fd_ to call close(). _cleanup_free_ generalizes __cleanup__(cleanup_charp), so remove it along with cleanup.c. Signed-off-by: Caleb Sander <[email protected]>
1 parent 33ea970 commit c35a784

5 files changed

Lines changed: 25 additions & 11 deletions

File tree

src/meson.build

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
# Authors: Martin Belanger <[email protected]>
77
#
88
sources = [
9-
'nvme/cleanup.c',
109
'nvme/nbft.c',
1110
'nvme/fabrics.c',
1211
'nvme/filters.c',
@@ -19,7 +18,6 @@ sources = [
1918
]
2019

2120
mi_sources = [
22-
'nvme/cleanup.c',
2321
'nvme/log.c',
2422
'nvme/mi.c',
2523
'nvme/mi-mctp.c',

src/nvme/cleanup.c

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/nvme/cleanup.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
#ifndef __CLEANUP_H
33
#define __CLEANUP_H
44

5+
#include <dirent.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <unistd.h>
9+
510
#define __cleanup__(fn) __attribute__((cleanup(fn)))
611

712
#define DECLARE_CLEANUP_FUNC(name, type) \
@@ -14,6 +19,23 @@ DECLARE_CLEANUP_FUNC(name, type) \
1419
free_fn(*__p); \
1520
}
1621

17-
DECLARE_CLEANUP_FUNC(cleanup_charp, char *);
22+
static inline void freep(void *p)
23+
{
24+
free(*(void **)p);
25+
}
26+
#define _cleanup_free_ __cleanup__(freep)
27+
28+
static inline DEFINE_CLEANUP_FUNC(cleanup_file, FILE *, fclose)
29+
#define _cleanup_file_ __cleanup__(cleanup_file)
30+
31+
static inline DEFINE_CLEANUP_FUNC(cleanup_dir, DIR *, closedir)
32+
#define _cleanup_dir_ __cleanup__(cleanup_dir)
33+
34+
static inline void cleanup_fd(int *fd)
35+
{
36+
if (*fd >= 0)
37+
close(*fd);
38+
}
39+
#define _cleanup_fd_ __cleanup__(cleanup_fd)
1840

1941
#endif

src/nvme/log.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ __nvme_msg(nvme_root_t r, int lvl,
4646
"[%s] <%s>%s ",
4747
"[%s] <%s> %s: ",
4848
};
49-
char *header __cleanup__(cleanup_charp) = NULL;
50-
char *message __cleanup__(cleanup_charp) = NULL;
49+
_cleanup_free_ char *header = NULL;
50+
_cleanup_free_ char *message = NULL;
5151
int idx = 0;
5252

5353
if (!r)

test/test-util.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <netdb.h>
2121
#include <string.h>
2222

23-
#include "nvme/cleanup.c" /* to resolve cleanup_charp() */
2423
#include "nvme/log.c" /* to resolve __nvme_msg() */
2524
#include "nvme/util.c"
2625

0 commit comments

Comments
 (0)