/
githubmirror
/
nvme-cli
Обзор
Документация
Войти
/
githubmirror
/
nvme-cli
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
shared/cleanup.h
55 строк
1 KB
Daniel Wagner
shared: move fd/FILE cleanup helpers out of util/cleanup.h
07 авг 2026, 16:15
07 авг 2026, 16:15
3c1a641
Код
Авторство
О чём код?
/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * This file is part of nvme-cli. * Copyright (c) 2026 Dell Technologies Inc. or its subsidiaries. * * Authors: Martin Belanger <martin.belanger@dell.com> */ #pragma once #include <dirent.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define __cleanup(fn) __attribute__((cleanup(fn))) #define DECLARE_CLEANUP_FUNC(name, type) \ void name(type *__p) #define DEFINE_CLEANUP_FUNC(name, type, free_fn)\ DECLARE_CLEANUP_FUNC(name, type) \ { \ if (*__p) \ free_fn(*__p); \ } static inline void shr_freep(void *p) { free(*(void **)p); } #define __cleanup_free __cleanup(shr_freep) struct dirents { struct dirent **ents; int num; }; static inline void cleanup_dirents(struct dirents *ents) { while (ents->num > 0) free(ents->ents[--ents->num]); free(ents->ents); } #define __cleanup_dirents __cleanup(cleanup_dirents) static inline void shr_cleanup_fd(int *fd) { if (*fd > STDERR_FILENO) close(*fd); } #define __cleanup_fd __cleanup(shr_cleanup_fd) static inline DEFINE_CLEANUP_FUNC(shr_cleanup_file, FILE *, fclose) #define __cleanup_file __cleanup(shr_cleanup_file)