From fd23dbf5c6e2d72efe9d50283b84a5f1eb8c186a Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 12 Aug 2013 07:29:16 +0200 Subject: [PATCH] unix: clean up __attribute__((quux)) usage Macro-ify __attribute__((destructor)) and __attribute__((unused)). The macros are no-ops when the compiler does not support function attributes. --- src/unix/internal.h | 26 ++++++++++++++++++++------ src/unix/proctitle.c | 3 +-- src/unix/threadpool.c | 5 +---- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/unix/internal.h b/src/unix/internal.h index 2c4e3e91..5816be0e 100644 --- a/src/unix/internal.h +++ b/src/unix/internal.h @@ -67,6 +67,21 @@ } \ while (0) +/* The __clang__ and __INTEL_COMPILER checks are superfluous because they + * define __GNUC__. They are here to convey to you, dear reader, that these + * macros are enabled when compiling with clang or icc. + */ +#if defined(__clang__) || \ + defined(__GNUC__) || \ + defined(__INTEL_COMPILER) || \ + defined(__SUNPRO_C) +# define UV_DESTRUCTOR(declaration) __attribute__((destructor)) declaration +# define UV_UNUSED(declaration) __attribute__((unused)) declaration +#else +# define UV_DESTRUCTOR(declaration) declaration +# define UV_UNUSED(declaration) declaration +#endif + #if defined(__linux__) # define UV__POLLIN UV__EPOLLIN # define UV__POLLOUT UV__EPOLLOUT @@ -242,21 +257,20 @@ static const int kFSEventStreamEventFlagItemIsSymlink = 0x00040000; #endif /* defined(__APPLE__) */ -__attribute__((unused)) -static void uv__req_init(uv_loop_t* loop, uv_req_t* req, uv_req_type type) { +UV_UNUSED(static void uv__req_init(uv_loop_t* loop, + uv_req_t* req, + uv_req_type type)) { req->type = type; uv__req_register(loop, req); } #define uv__req_init(loop, req, type) \ uv__req_init((loop), (uv_req_t*)(req), (type)) -__attribute__((unused)) -static void uv__update_time(uv_loop_t* loop) { +UV_UNUSED(static void uv__update_time(uv_loop_t* loop)) { loop->time = uv__hrtime() / 1000000; } -__attribute__((unused)) -static char* uv__basename_r(const char* path) { +UV_UNUSED(static char* uv__basename_r(const char* path)) { char* s; s = strrchr(path, '/'); diff --git a/src/unix/proctitle.c b/src/unix/proctitle.c index 8ffebb14..16b05237 100644 --- a/src/unix/proctitle.c +++ b/src/unix/proctitle.c @@ -96,8 +96,7 @@ int uv_get_process_title(char* buffer, size_t size) { } -__attribute__((destructor)) -static void free_args_mem(void) { +UV_DESTRUCTOR(static void free_args_mem(void)) { free(args_mem); /* Keep valgrind happy. */ args_mem = NULL; } diff --git a/src/unix/threadpool.c b/src/unix/threadpool.c index 5a3a5f5b..7923250a 100644 --- a/src/unix/threadpool.c +++ b/src/unix/threadpool.c @@ -129,9 +129,7 @@ static void init_once(void) { } -#if defined(__GNUC__) -__attribute__((destructor)) -static void cleanup(void) { +UV_DESTRUCTOR(static void cleanup(void)) { unsigned int i; if (initialized == 0) @@ -153,7 +151,6 @@ static void cleanup(void) { nthreads = 0; initialized = 0; } -#endif void uv__work_submit(uv_loop_t* loop,