[sheepdog] [PATCH v4 1/4] util: solve cyclic dependency of util.h and logger.h

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Thu Jul 11 05:16:24 CEST 2013


At Thu, 11 Jul 2013 10:48:10 +0900,
Hitoshi Mitake wrote:
> 
> Current sheepdog specialized assert() in util.h cannot be used by
> util.h itself because it depends on logger.h and logger.h depends on
> util.h, too.
> 
> This patch solves the cyclic dependency by introducing new header
> file compiler.h. logger.h doesn't require whole util.h so, the
> required part is extracted as compiler.h.
> 
> In addition, this patch modifies lib/strbuf.c because it is a assert()
> user but it didn't include util.h.
> 
> Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
> ---
> v4:
>  - introduce compiler.h for solving the cyclic dependency of util.h
>    and logger.h
> 
>  include/compiler.h |   20 ++++++++++++++++++++
>  include/logger.h   |    8 +++++++-
>  include/strbuf.h   |    1 +
>  include/util.h     |   10 +++-------
>  lib/strbuf.c       |    1 +
>  5 files changed, 32 insertions(+), 8 deletions(-)
>  create mode 100644 include/compiler.h
> 
> diff --git a/include/compiler.h b/include/compiler.h
> new file mode 100644
> index 0000000..1cd2440
> --- /dev/null
> +++ b/include/compiler.h
> @@ -0,0 +1,20 @@
> +/*
> + * Copyright (C) 2009-2013 Nippon Telegraph and Telephone Corporation.
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License version
> + * 2 as published by the Free Software Foundation.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program. If not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#ifndef SD_COMPILER_H
> +#define SD_COMPILER_H
> +
> +#define notrace __attribute__((no_instrument_function))
> +#define __packed __attribute((packed))
> +
> +#define __printf(a, b) __attribute__((format(printf, a, b)))
> +
> +#endif	/* SD_COMPILER_H */
> diff --git a/include/logger.h b/include/logger.h
> index 5c9aab6..c2c4237 100644
> --- a/include/logger.h
> +++ b/include/logger.h
> @@ -17,7 +17,7 @@
>  #include <stdbool.h>
>  #include <sys/syslog.h>
>  
> -#include "util.h"
> +#include "compiler.h"
>  
>  #define LOG_SPACE_SIZE (1 * 1024 * 1024)
>  #define LOG_SPACE_DEBUG_SIZE (32 * 1024 * 1024)
> @@ -81,4 +81,10 @@ void sd_backtrace(void);
>  		log_write(SDOG_DEBUG, __func__, __LINE__, fmt, ##args);	\
>  	} while (0)
>  
> +static inline void do_assert(const char *expr, const char *func, int line)
> +{
> +	log_write(SDOG_EMERG, func, line, "Asserting `%s' failed.", expr);
> +	abort();
> +}
> +
>  #endif	/* LOG_H */
> diff --git a/include/strbuf.h b/include/strbuf.h
> index 1500afc..62eada1 100644
> --- a/include/strbuf.h
> +++ b/include/strbuf.h
> @@ -8,6 +8,7 @@
>  #include <stdlib.h>
>  
>  #include "logger.h"
> +#include "util.h"
>  
>  struct strbuf {
>  	size_t alloc;
> diff --git a/include/util.h b/include/util.h
> index e70bb9f..5c3a0a0 100644
> --- a/include/util.h
> +++ b/include/util.h
> @@ -13,6 +13,7 @@
>  
>  #include "bitops.h"
>  #include "list.h"
> +#include "compiler.h"
>  
>  #define SECTOR_SIZE (1U << 9)
>  #define BLOCK_SIZE (1U << 12)
> @@ -39,11 +40,6 @@
>  #define __cpu_to_le32(x) bswap_32(x)
>  #endif
>  
> -#define notrace __attribute__((no_instrument_function))
> -#define __packed __attribute((packed))
> -
> -#define __printf(a, b)                  __attribute__((format(printf, a, b)))
> -
>  #define uninitialized_var(x) x = x
>  
>  static inline int before(uint32_t seq1, uint32_t seq2)
> @@ -167,8 +163,8 @@ int atomic_create_and_write(const char *path, char *buf, size_t len);
>  
>  #ifndef NDEBUG
>  
> -#define assert(expr) ((expr) ?						\
> -			(void)0 : panic("Asserting `%s' failed.", #expr))
> +#define assert(expr) ((expr) ?				\
> +			(void)0 : do_assert(#expr, __func__, __LINE__))

Why do we need to introduce do_assert?

In addition, I've encountered a compile error with the current master
branch.

cc1: warnings being treated as errors
In file included from event.c:20:
../include/util.h: In function 'refcount_dec':
../include/util.h:235: error: implicit declaration of function 'do_assert'
In file included from event.c:22:
../include/logger.h: At top level:
../include/logger.h:84: error: conflicting types for 'do_assert'
../include/logger.h:84: error: static declaration of 'do_assert' follows non-static declaration


Thanks,

Kazutaka



More information about the sheepdog mailing list