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 <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2018-12-15 17:42:37 +00:00
parent c2c16fb8a1
commit 0cebbc370a
No known key found for this signature in database
GPG Key ID: 37C999F617EA1A47
15 changed files with 174 additions and 147 deletions

View File

@ -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.
*/

View File

@ -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.
*/

View File

@ -29,6 +29,7 @@
#include "x.h"
#include "config.h"
#include "diagnostic.h"
#include "string_utils.h"
#define auto __auto_type

View File

@ -6,9 +6,11 @@
#include <stdbool.h>
#include "common.h"
#include "config.h"
#include "utils.h"
#include "c2.h"
#include "string_utils.h"
#include "config.h"
/**
* Parse a long number.

View File

@ -10,6 +10,7 @@
#include "common.h"
#include "config.h"
#include "string_utils.h"
/**
* Wrapper of libconfig's <code>config_lookup_int</code>.

View File

@ -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 *);

View File

@ -10,6 +10,7 @@
*/
#include "common.h"
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>

29
src/log.h Normal file
View File

@ -0,0 +1,29 @@
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2018 Yuxuan Shui <yshuiv7@gmail.com>
#pragma once
#include <stdio.h>
/// 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__)

View File

@ -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 = []

View File

@ -9,6 +9,7 @@
*
*/
#include "string_utils.h"
#include "opengl.h"
static inline int

63
src/string_utils.c Normal file
View File

@ -0,0 +1,63 @@
#include <string.h>
#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);
}

37
src/string_utils.h Normal file
View File

@ -0,0 +1,37 @@
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) Yuxuan Shui <yshuiv7@gmail.com>
#pragma once
#include <ctype.h>
#include <stddef.h>
#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)

View File

@ -5,7 +5,14 @@
#include <stddef.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <unistd.h>
#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`
///

View File

@ -12,6 +12,7 @@
#include "compton.h"
#include "c2.h"
#include "x.h"
#include "string_utils.h"
#include "win.h"

View File

@ -9,6 +9,7 @@
// FIXME shouldn't need this
#ifdef CONFIG_OPENGL
#define GL_GLEXT_PROTOTYPES
#include <GL/glx.h>
#endif