From 0cebbc370a00b9ba7aa1974f2cbd5fb8258908d2 Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Sat, 15 Dec 2018 17:42:37 +0000 Subject: [PATCH] Function movements * string functions mstr*() are moved to string_utils.c * allocation wrappers are moved to utils.h * printf_* functions are moved to log.h Signed-off-by: Yuxuan Shui --- src/c2.c | 3 +- src/common.h | 123 ----------------------------------------- src/compton.c | 1 + src/config.c | 4 +- src/config_libconfig.c | 1 + src/dbus.c | 4 +- src/dbus.h | 1 + src/log.h | 29 ++++++++++ src/meson.build | 2 +- src/opengl.c | 1 + src/string_utils.c | 63 +++++++++++++++++++++ src/string_utils.h | 37 +++++++++++++ src/utils.h | 50 ++++++++++------- src/win.c | 1 + src/win.h | 1 + 15 files changed, 174 insertions(+), 147 deletions(-) create mode 100644 src/log.h create mode 100644 src/string_utils.c create mode 100644 src/string_utils.h diff --git a/src/c2.c b/src/c2.c index 2b91b98..45b5368 100644 --- a/src/c2.c +++ b/src/c2.c @@ -30,6 +30,7 @@ #include "common.h" #include "win.h" #include "c2.h" +#include "string_utils.h" #define C2_MAX_LEVELS 10 @@ -218,8 +219,6 @@ static const c2_predef_t C2_PREDEFS[] = { [C2_L_PROLE ] = { "role" , C2_L_TSTRING , 0 }, }; -#define mstrncmp(s1, s2) strncmp((s1), (s2), strlen(s1)) - /** * Compare next word in a string with another string. */ diff --git a/src/common.h b/src/common.h index 911518c..d606eaf 100644 --- a/src/common.h +++ b/src/common.h @@ -64,8 +64,6 @@ #define MAX_ALPHA (255) -#define ARR_SIZE(arr) (sizeof(arr)/sizeof(arr[0])) - // === Includes === // For some special functions @@ -129,30 +127,6 @@ /// @brief Wrapper for gcc branch prediction builtin, for unlikely branch. #define unlikely(x) __builtin_expect(!!(x), 0) -/// Print out an error message. -#define printf_err(format, ...) \ - fprintf(stderr, format "\n", ## __VA_ARGS__) - -/// Print out an error message with function name. -#define printf_errf(format, ...) \ - printf_err("%s" format, __func__, ## __VA_ARGS__) - -/// Print out an error message with function name, and quit with a -/// specific exit code. -#define printf_errfq(code, format, ...) { \ - printf_err("%s" format, __func__, ## __VA_ARGS__); \ - exit(code); \ -} - -/// Print out a debug message. -#define printf_dbg(format, ...) \ - printf(format, ## __VA_ARGS__); \ - fflush(stdout) - -/// Print out a debug message with function name. -#define printf_dbgf(format, ...) \ - printf_dbg("%s" format, __func__, ## __VA_ARGS__) - // Use #s here to prevent macro expansion /// Macro used for shortening some debugging code. #define CASESTRRET(s) case s: return #s @@ -221,8 +195,6 @@ typedef long time_ms_t; typedef struct _c2_lptr c2_lptr_t; -// Or use cmemzero(). - /// Structure representing needed window updates. typedef struct { bool shadow : 1; @@ -998,37 +970,6 @@ print_backtrace(void) { // === Functions === -/** - * @brief Quit if the passed-in pointer is empty. - */ -static inline void * -allocchk_(const char *func_name, void *ptr) { - if (!ptr) { - printf_err("%s(): Failed to allocate memory.", func_name); - exit(1); - } - return ptr; -} - -/// @brief Wrapper of allocchk_(). -#define allocchk(ptr) allocchk_(__func__, ptr) - -/// @brief Wrapper of malloc(). -#define cmalloc(nmemb, type) ((type *) allocchk(malloc((nmemb) * sizeof(type)))) - -/// @brief Wrapper of calloc(). -#define ccalloc(nmemb, type) ((type *) allocchk(calloc((nmemb), sizeof(type)))) - -/// @brief Wrapper of ealloc(). -#define crealloc(ptr, nmemb, type) ((type *) allocchk(realloc((ptr), (nmemb) * sizeof(type)))) - -/// @brief Zero out the given memory block. -#define cmemzero(ptr, size) memset((ptr), 0, (size)) - -/// @brief Wrapper of cmemzero() that handles a pointer to a single item, for -/// convenience. -#define cmemzero_one(ptr) cmemzero((ptr), sizeof(*(ptr))) - /** * Return whether a struct timeval value is empty. */ @@ -1189,70 +1130,6 @@ print_timestamp(session_t *ps) { fprintf(stderr, "[ %5ld.%06ld ] ", diff.tv_sec, diff.tv_usec); } -/** - * Allocate the space and copy a string. - */ -static inline char * -mstrcpy(const char *src) { - char *str = cmalloc(strlen(src) + 1, char); - - strcpy(str, src); - - return str; -} - -/** - * Allocate the space and copy a string. - */ -static inline char * -mstrncpy(const char *src, unsigned len) { - char *str = cmalloc(len + 1, char); - - strncpy(str, src, len); - str[len] = '\0'; - - return str; -} - -/** - * Allocate the space and join two strings. - */ -static inline char * -mstrjoin(const char *src1, const char *src2) { - char *str = cmalloc(strlen(src1) + strlen(src2) + 1, char); - - strcpy(str, src1); - strcat(str, src2); - - return str; -} - -/** - * Allocate the space and join two strings; - */ -static inline char * -mstrjoin3(const char *src1, const char *src2, const char *src3) { - char *str = cmalloc(strlen(src1) + strlen(src2) - + strlen(src3) + 1, char); - - strcpy(str, src1); - strcat(str, src2); - strcat(str, src3); - - return str; -} - -/** - * Concatenate a string on heap with another string. - */ -static inline void -mstrextend(char **psrc1, const char *src2) { - *psrc1 = crealloc(*psrc1, (*psrc1 ? strlen(*psrc1): 0) + strlen(src2) + 1, - char); - - strcat(*psrc1, src2); -} - /** * Parse a VSync option argument. */ diff --git a/src/compton.c b/src/compton.c index 5323ae6..187e7d0 100644 --- a/src/compton.c +++ b/src/compton.c @@ -29,6 +29,7 @@ #include "x.h" #include "config.h" #include "diagnostic.h" +#include "string_utils.h" #define auto __auto_type diff --git a/src/config.c b/src/config.c index 18ce5c6..5f86b1e 100644 --- a/src/config.c +++ b/src/config.c @@ -6,9 +6,11 @@ #include #include "common.h" -#include "config.h" #include "utils.h" #include "c2.h" +#include "string_utils.h" + +#include "config.h" /** * Parse a long number. diff --git a/src/config_libconfig.c b/src/config_libconfig.c index bc4a177..32d483b 100644 --- a/src/config_libconfig.c +++ b/src/config_libconfig.c @@ -10,6 +10,7 @@ #include "common.h" #include "config.h" +#include "string_utils.h" /** * Wrapper of libconfig's config_lookup_int. diff --git a/src/dbus.c b/src/dbus.c index fe453e0..024df78 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -9,8 +9,10 @@ * */ -#include "dbus.h" #include "win.h" +#include "string_utils.h" + +#include "dbus.h" static DBusHandlerResult cdbus_process(DBusConnection *conn, DBusMessage *m, void *); diff --git a/src/dbus.h b/src/dbus.h index 8d4a982..af240dd 100644 --- a/src/dbus.h +++ b/src/dbus.h @@ -10,6 +10,7 @@ */ #include "common.h" + #include #include #include diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..6b6a216 --- /dev/null +++ b/src/log.h @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: MPL-2.0 +// Copyright (c) 2018 Yuxuan Shui + +#pragma once +#include + +/// Print out an error message. +#define printf_err(format, ...) \ + fprintf(stderr, format "\n", ## __VA_ARGS__) + +/// Print out an error message with function name. +#define printf_errf(format, ...) \ + printf_err("%s" format, __func__, ## __VA_ARGS__) + +/// Print out an error message with function name, and quit with a +/// specific exit code. +#define printf_errfq(code, format, ...) { \ + printf_err("%s" format, __func__, ## __VA_ARGS__); \ + exit(code); \ +} + +/// Print out a debug message. +#define printf_dbg(format, ...) \ + printf(format, ## __VA_ARGS__); \ + fflush(stdout) + +/// Print out a debug message with function name. +#define printf_dbgf(format, ...) \ + printf_dbg("%s" format, __func__, ## __VA_ARGS__) diff --git a/src/meson.build b/src/meson.build index f6596fe..bd874c4 100644 --- a/src/meson.build +++ b/src/meson.build @@ -4,7 +4,7 @@ deps = [ dependency('xcb', version: '>=1.9.2') ] -srcs = ['compton.c', 'win.c', 'c2.c', 'x.c', 'config.c', 'diagnostic.c'] +srcs = ['compton.c', 'win.c', 'c2.c', 'x.c', 'config.c', 'diagnostic.c', 'string_utils.c'] cflags = [] diff --git a/src/opengl.c b/src/opengl.c index 424a4a4..5e19435 100644 --- a/src/opengl.c +++ b/src/opengl.c @@ -9,6 +9,7 @@ * */ +#include "string_utils.h" #include "opengl.h" static inline int diff --git a/src/string_utils.c b/src/string_utils.c new file mode 100644 index 0000000..ad2dbde --- /dev/null +++ b/src/string_utils.c @@ -0,0 +1,63 @@ +#include + +#include "string_utils.h" +#include "utils.h" +/** + * Allocate the space and copy a string. + */ +char *mstrcpy(const char *src) { + char *str = cmalloc(strlen(src) + 1, char); + + strcpy(str, src); + + return str; +} + +/** + * Allocate the space and copy a string. + */ +char *mstrncpy(const char *src, unsigned len) { + char *str = cmalloc(len + 1, char); + + strncpy(str, src, len); + str[len] = '\0'; + + return str; +} + +/** + * Allocate the space and join two strings. + */ +char *mstrjoin(const char *src1, const char *src2) { + char *str = cmalloc(strlen(src1) + strlen(src2) + 1, char); + + strcpy(str, src1); + strcat(str, src2); + + return str; +} + +/** + * Allocate the space and join two strings; + */ +char * +mstrjoin3(const char *src1, const char *src2, const char *src3) { + char *str = cmalloc(strlen(src1) + strlen(src2) + + strlen(src3) + 1, char); + + strcpy(str, src1); + strcat(str, src2); + strcat(str, src3); + + return str; +} + +/** + * Concatenate a string on heap with another string. + */ +void mstrextend(char **psrc1, const char *src2) { + *psrc1 = crealloc(*psrc1, (*psrc1 ? strlen(*psrc1): 0) + strlen(src2) + 1, + char); + + strcat(*psrc1, src2); +} diff --git a/src/string_utils.h b/src/string_utils.h new file mode 100644 index 0000000..1dfbc1d --- /dev/null +++ b/src/string_utils.h @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: MPL-2.0 +// Copyright (c) Yuxuan Shui +#pragma once +#include +#include + +#define mstrncmp(s1, s2) strncmp((s1), (s2), strlen(s1)) + +char *mstrcpy(const char *src); +char *mstrncpy(const char *src, unsigned len); +char *mstrjoin(const char *src1, const char *src2); +char * +mstrjoin3(const char *src1, const char *src2, const char *src3); +void mstrextend(char **psrc1, const char *src2); + +static inline const char * +skip_space_const(const char *src) { + if (!src) + return NULL; + while (*src && isspace(*src)) + src++; + return src; +} + +static inline char * +skip_space_mut(char *src) { + if (!src) + return NULL; + while (*src && isspace(*src)) + src++; + return src; +} + +#define skip_space(x) _Generic((x), \ + char *: skip_space_mut, \ + const char *: skip_space_const \ +)(x) diff --git a/src/utils.h b/src/utils.h index eb4602f..5bede5a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -5,7 +5,14 @@ #include #include #include +#include #include +#include +#include + +#include "log.h" + +#define ARR_SIZE(arr) (sizeof(arr)/sizeof(arr[0])) #ifdef __FAST_MATH__ #warning Use of -ffast-math can cause rendering error or artifacts, \ @@ -94,28 +101,33 @@ normalize_d(double d) { return normalize_d_range(d, 0.0, 1.0); } -static inline const char * -skip_space_const(const char *src) { - if (!src) - return NULL; - while (*src && isspace(*src)) - src++; - return src; +/** + * @brief Quit if the passed-in pointer is empty. + */ +static inline void * +allocchk_(const char *func_name, void *ptr) { + if (!ptr) { + // Since memory allocation failed, we try to print + // this error message without any memory allocation. + const char msg[] = "(): Failed to allocate memory\n"; + write(STDERR_FILENO, func_name, strlen(func_name)); + write(STDERR_FILENO, msg, ARR_SIZE(msg)); + abort(); + } + return ptr; } -static inline char * -skip_space_mut(char *src) { - if (!src) - return NULL; - while (*src && isspace(*src)) - src++; - return src; -} +/// @brief Wrapper of allocchk_(). +#define allocchk(ptr) allocchk_(__func__, ptr) -#define skip_space(x) _Generic((x), \ - char *: skip_space_mut, \ - const char *: skip_space_const \ -)(x) +/// @brief Wrapper of malloc(). +#define cmalloc(nmemb, type) ((type *) allocchk(malloc((nmemb) * sizeof(type)))) + +/// @brief Wrapper of calloc(). +#define ccalloc(nmemb, type) ((type *) allocchk(calloc((nmemb), sizeof(type)))) + +/// @brief Wrapper of ealloc(). +#define crealloc(ptr, nmemb, type) ((type *) allocchk(realloc((ptr), (nmemb) * sizeof(type)))) /// RC_TYPE generates a reference counted type from `type` /// diff --git a/src/win.c b/src/win.c index 000a4ce..a1ba8c0 100644 --- a/src/win.c +++ b/src/win.c @@ -12,6 +12,7 @@ #include "compton.h" #include "c2.h" #include "x.h" +#include "string_utils.h" #include "win.h" diff --git a/src/win.h b/src/win.h index e0cedbe..ff65b17 100644 --- a/src/win.h +++ b/src/win.h @@ -9,6 +9,7 @@ // FIXME shouldn't need this #ifdef CONFIG_OPENGL +#define GL_GLEXT_PROTOTYPES #include #endif