Skip to content

Commit

Permalink
Reuse and simplify libhs/libty platform compatibility stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Koromix committed Aug 3, 2020
1 parent 8d158a2 commit 69ad806
Show file tree
Hide file tree
Showing 70 changed files with 244 additions and 511 deletions.
12 changes: 4 additions & 8 deletions src/libhs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ elseif(LINUX)
endif()

include(CheckSymbolExists)
check_symbol_exists(stpcpy string.h _HS_HAVE_STPCPY)
check_symbol_exists(asprintf stdio.h _HS_HAVE_ASPRINTF)
configure_file(config.h.in config.h)
check_symbol_exists(asprintf stdio.h HAVE_ASPRINTF)

find_package(Threads)
list(APPEND LIBHS_LINK_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
Expand All @@ -38,8 +36,6 @@ set(LIBHS_SOURCES array.c
common.c
common.h
common_priv.h
compat.c
compat_priv.h
device.c
device.h
device_priv.h
Expand Down Expand Up @@ -80,9 +76,9 @@ endif()
add_library(libhs STATIC ${LIBHS_SOURCES})
set_target_properties(libhs PROPERTIES OUTPUT_NAME hs)
target_link_libraries(libhs ${LIBHS_LINK_LIBRARIES})
# We need that for auto-generated file config.h
target_include_directories(libhs PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_compile_definitions(libhs PUBLIC _HS_HAVE_CONFIG_H)
if(HAVE_ASPRINTF)
target_compile_definitions(libhs PRIVATE HAVE_ASPRINTF)
endif()
enable_unity_build(libhs)

add_amalgamated_file(libhs "${CMAKE_BINARY_DIR}/libhs.h" libhs.h)
Expand Down
4 changes: 2 additions & 2 deletions src/libhs/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include "common.h"

HS_BEGIN_C
_HS_BEGIN_C

struct _hs_array {
void *values;
Expand Down Expand Up @@ -79,6 +79,6 @@ static inline int _hs_array_grow_(struct _hs_array *array, size_t value_size, si

void _hs_array_shrink_(struct _hs_array *array, size_t value_size);

HS_END_C
_HS_END_C

#endif
47 changes: 46 additions & 1 deletion src/libhs/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
See the LICENSE file for more details. */

#include "common_priv.h"
#include <stdarg.h>

static hs_log_handler_func *log_handler = hs_log_default_handler;
static void *log_handler_udata;
Expand Down Expand Up @@ -135,3 +134,49 @@ int hs_error(hs_error_code err, const char *fmt, ...)

return err;
}

int _hs_asprintf(char **strp, const char *fmt, ...)
{
va_list ap;
int r;

va_start(ap, fmt);
#ifdef HAVE_ASPRINTF
r = vasprintf(strp, fmt, ap);
#else
r = _hs_vasprintf(strp, fmt, ap);
#endif
va_end(ap);

return r;
}

int _hs_vasprintf(char **strp, const char *fmt, va_list ap)
{
#ifdef HAVE_ASPRINTF
return vasprintf(strp, fmt, ap);
#else
va_list ap_copy;
char *s;
int r;

va_copy(ap_copy, ap);
r = vsnprintf(NULL, 0, fmt, ap_copy);
if (r < 0)
return -1;
va_end(ap_copy);

s = (char *)malloc((size_t)r + 1);
if (!s)
return -1;

va_copy(ap_copy, ap);
r = vsnprintf(s, (size_t)r + 1, fmt, ap_copy);
if (r < 0)
return -1;
va_end(ap_copy);

*strp = s;
return r;
#endif
}
47 changes: 33 additions & 14 deletions src/libhs/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
#include <sys/types.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdarg.h>

#ifdef __cplusplus
#define HS_BEGIN_C extern "C" {
#define HS_END_C }
#define _HS_BEGIN_C extern "C" {
#define _HS_END_C }
#else
#define HS_BEGIN_C
#define HS_END_C
#define _HS_BEGIN_C
#define _HS_END_C
#endif

HS_BEGIN_C
_HS_BEGIN_C

typedef struct hs_device hs_device;
typedef struct hs_monitor hs_monitor;
Expand Down Expand Up @@ -57,13 +58,21 @@ typedef struct hs_match_spec hs_match_spec;
#define HS_VERSION_STRING "0.9.0"

#if defined(__GNUC__)
#define _HS_POSSIBLY_UNUSED __attribute__((__unused__))
#define _HS_THREAD_LOCAL __thread
#define _HS_ALIGN_OF(type) __alignof__(type)

#ifdef __MINGW_PRINTF_FORMAT
#define HS_PRINTF_FORMAT(fmt, first) __attribute__((__format__(__MINGW_PRINTF_FORMAT, fmt, first)))
#define _HS_PRINTF_FORMAT(fmt, first) __attribute__((__format__(__MINGW_PRINTF_FORMAT, fmt, first)))
#else
#define HS_PRINTF_FORMAT(fmt, first) __attribute__((__format__(__printf__, fmt, first)))
#define _HS_PRINTF_FORMAT(fmt, first) __attribute__((__format__(__printf__, fmt, first)))
#endif
#elif _MSC_VER >= 1900
#define HS_PRINTF_FORMAT(fmt, first)
#define _HS_POSSIBLY_UNUSED
#define _HS_THREAD_LOCAL __declspec(thread)
#define _HS_ALIGN_OF(type) __alignof(type)

#define _HS_PRINTF_FORMAT(fmt, first)

// HAVE_SSIZE_T is used this way by other projects
#ifndef HAVE_SSIZE_T
Expand All @@ -74,18 +83,28 @@ typedef __int64 ssize_t;
typedef long ssize_t;
#endif
#endif

#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#else
#error "This compiler is not supported"
#endif

#define _HS_CONCAT_HELPER(a, b) a ## b
#define _HS_CONCAT(a, b) _HS_CONCAT_HELPER(a, b)

#define _HS_UNIQUE_ID(prefix) _HS_CONCAT(prefix, __LINE__)

#define _hs_container_of(head, type, member) \
#define _HS_UNUSED(arg) ((void)(arg))
#define _HS_COUNTOF(a) (sizeof(a) / sizeof(*(a)))
#define _HS_MIN(a,b) ((a) < (b) ? (a) : (b))
#define _HS_MAX(a,b) ((a) > (b) ? (a) : (b))
#define _HS_ALIGN_SIZE(size, align) (((size) + (align) - 1) / (align) * (align))
#define _HS_ALIGN_SIZE_FOR_TYPE(size, type) _HS_ALIGN_SIZE((size), sizeof(type))
#define _HS_CONTAINER_OF(head, type, member) \
((type *)((char *)(head) - (size_t)(&((type *)0)->member)))

int _hs_asprintf(char **strp, const char *fmt, ...) _HS_PRINTF_FORMAT(2, 3);
int _hs_vasprintf(char **strp, const char *fmt, va_list ap);

#if defined(DOXYGEN)
/**
* @ingroup misc
Expand Down Expand Up @@ -208,7 +227,7 @@ void hs_log_set_handler(hs_log_handler_func *f, void *udata);
*
* @sa hs_log_set_handler() to use a custom callback function.
*/
void hs_log(hs_log_level level, const char *fmt, ...) HS_PRINTF_FORMAT(2, 3);
void hs_log(hs_log_level level, const char *fmt, ...) _HS_PRINTF_FORMAT(2, 3);

/** @} */

Expand Down Expand Up @@ -299,8 +318,8 @@ const char *hs_error_last_message();
* @sa hs_error_mask() to mask specific error codes.
* @sa hs_log_set_handler() to use a custom callback function.
*/
int hs_error(hs_error_code err, const char *fmt, ...) HS_PRINTF_FORMAT(2, 3);
int hs_error(hs_error_code err, const char *fmt, ...) _HS_PRINTF_FORMAT(2, 3);

HS_END_C
_HS_END_C

#endif
21 changes: 0 additions & 21 deletions src/libhs/common_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#define _HS_COMMON_PRIV_H

#include "common.h"
#include "compat_priv.h"
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
Expand All @@ -21,24 +20,4 @@
#include <stdlib.h>
#include <string.h>

#if defined(__GNUC__)
#define _HS_POSSIBLY_UNUSED __attribute__((__unused__))
#define _HS_THREAD_LOCAL __thread
#define _HS_ALIGN_OF(type) __alignof__(type)
#elif defined(_MSC_VER)
#define _HS_POSSIBLY_UNUSED
#define _HS_THREAD_LOCAL __declspec(thread)
#define _HS_ALIGN_OF(type) __alignof(type)

#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif

#define _HS_UNUSED(arg) ((void)(arg))

#define _HS_COUNTOF(a) (sizeof(a) / sizeof(*(a)))

#define _HS_ALIGN_SIZE(size, align) (((size) + (align) - 1) / (align) * (align))
#define _HS_ALIGN_SIZE_FOR_TYPE(size, type) _HS_ALIGN_SIZE((size), sizeof(type))

#endif
60 changes: 0 additions & 60 deletions src/libhs/compat.c

This file was deleted.

44 changes: 0 additions & 44 deletions src/libhs/compat_priv.h

This file was deleted.

12 changes: 0 additions & 12 deletions src/libhs/config.h.in

This file was deleted.

4 changes: 2 additions & 2 deletions src/libhs/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "common.h"
#include "htable.h"

HS_BEGIN_C
_HS_BEGIN_C

/**
* @defgroup device Device handling
Expand Down Expand Up @@ -242,6 +242,6 @@ hs_device *hs_port_get_device(const hs_port *port);
*/
hs_handle hs_port_get_poll_handle(const hs_port *port);

HS_END_C
_HS_END_C

#endif
2 changes: 1 addition & 1 deletion src/libhs/doc/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,7 @@ INCLUDE_FILE_PATTERNS =
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.

PREDEFINED = HS_PUBLIC= \
HS_PRINTF_FORMAT(a,b)= \
_HS_PRINTF_FORMAT(a,b)= \
DOXYGEN \
__posix__ \
__linux__ \
Expand Down
Loading

0 comments on commit 69ad806

Please sign in to comment.