code touch-up for recent c language standard
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
hyung-hwan 2024-04-28 13:32:11 +09:00
parent 9271fae745
commit f928ce5ec8
12 changed files with 129 additions and 20 deletions

View File

@ -31,6 +31,13 @@
#include <hawk-xma.h> #include <hawk-xma.h>
#include <hawk-glob.h> #include <hawk-glob.h>
#if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
#endif
#if !defined(_XOPEN_SOURCE)
# define _XOPEN_SOURCE 700
#endif
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>
@ -38,7 +45,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <locale.h> #include <locale.h>
// #define ENABLE_CALLBACK TODO: enable this back /* #define ENABLE_CALLBACK TODO: enable this back */
#define ABORT(label) goto label #define ABORT(label) goto label
#if defined(_WIN32) #if defined(_WIN32)
@ -436,7 +443,7 @@ static void dprint_return (hawk_rtx_t* rtx, hawk_val_t* ret)
} }
else else
{ {
str = hawk_rtx_valtooocstrdup (rtx, ret, &len); str = hawk_rtx_valtooocstrdup(rtx, ret, &len);
if (str == HAWK_NULL) if (str == HAWK_NULL)
{ {
hawk_logfmt (hawk, HAWK_LOG_STDERR,HAWK_T("[RETURN] - ***OUT OF MEMORY***\n")); hawk_logfmt (hawk, HAWK_LOG_STDERR,HAWK_T("[RETURN] - ***OUT OF MEMORY***\n"));

View File

@ -31,6 +31,13 @@
#include <hawk-utl.h> #include <hawk-utl.h>
#include <hawk-std.h> #include <hawk-std.h>
#if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
#endif
#if !defined(_XOPEN_SOURCE)
# define _XOPEN_SOURCE 700
#endif
#include <locale.h> #include <locale.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>

View File

@ -31,6 +31,9 @@
#elif defined(__DOS__) #elif defined(__DOS__)
/* TODO: */ /* TODO: */
#else #else
# if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
# endif
# include "syscall.h" # include "syscall.h"
# include <sys/socket.h> # include <sys/socket.h>
# if defined(HAVE_SYS_IOCTL_H) # if defined(HAVE_SYS_IOCTL_H)
@ -152,7 +155,7 @@ int hawk_gem_bcstrtoifindex (hawk_gem_t* gem, const hawk_bch_t* ptr, unsigned in
if (x >= 0) if (x >= 0)
{ {
#if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) #if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) || defined(ifr_ifindex)
*index = ifr.ifr_ifindex; *index = ifr.ifr_ifindex;
#else #else
*index = ifr.ifr_index; *index = ifr.ifr_index;
@ -242,7 +245,7 @@ int hawk_gem_bcharstoifindex (hawk_gem_t* gem, const hawk_bch_t* ptr, hawk_oow_t
if (x >= 0) if (x >= 0)
{ {
#if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) #if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) || defined(ifr_ifindex)
*index = ifr.ifr_ifindex; *index = ifr.ifr_ifindex;
#else #else
*index = ifr.ifr_index; *index = ifr.ifr_index;
@ -326,7 +329,7 @@ int hawk_gem_ucstrtoifindex (hawk_gem_t* gem, const hawk_uch_t* ptr, unsigned in
if (x >= 0) if (x >= 0)
{ {
#if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) #if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) || defined(ifr_ifindex)
*index = ifr.ifr_ifindex; *index = ifr.ifr_ifindex;
#else #else
*index = ifr.ifr_index; *index = ifr.ifr_index;
@ -419,7 +422,7 @@ int hawk_gem_ucharstoifindex (hawk_gem_t* gem, const hawk_uch_t* ptr, hawk_oow_t
if (x >= 0) if (x >= 0)
{ {
#if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) #if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) || defined(ifr_ifindex)
*index = ifr.ifr_ifindex; *index = ifr.ifr_ifindex;
#else #else
*index = ifr.ifr_index; *index = ifr.ifr_index;
@ -506,7 +509,7 @@ int hawk_gem_ifindextobcstr (hawk_gem_t* gem, unsigned int index, hawk_bch_t* bu
} }
HAWK_MEMSET (&ifr, 0, HAWK_SIZEOF(ifr)); HAWK_MEMSET (&ifr, 0, HAWK_SIZEOF(ifr));
#if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) #if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) || defined(ifr_ifindex)
ifr.ifr_ifindex = index; ifr.ifr_ifindex = index;
#else #else
ifr.ifr_index = index; ifr.ifr_index = index;
@ -587,7 +590,7 @@ int hawk_gem_ifindextoucstr (hawk_gem_t* gem, unsigned int index, hawk_uch_t* bu
} }
HAWK_MEMSET (&ifr, 0, HAWK_SIZEOF(ifr)); HAWK_MEMSET (&ifr, 0, HAWK_SIZEOF(ifr));
#if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) #if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) || defined(ifr_ifindex)
ifr.ifr_ifindex = index; ifr.ifr_ifindex = index;
#else #else
ifr.ifr_index = index; ifr.ifr_index = index;

View File

@ -23,6 +23,8 @@
*/ */
#if 1 #if 1
#define _GNU_SOURCE
#include "hawk-prv.h" #include "hawk-prv.h"
#include "skad-prv.h" #include "skad-prv.h"
#include <hawk-sio.h> #include <hawk-sio.h>
@ -50,6 +52,9 @@
#elif defined(__DOS__) #elif defined(__DOS__)
/* TODO: */ /* TODO: */
#else #else
# if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
# endif
# include "syscall.h" # include "syscall.h"
# include <sys/socket.h> # include <sys/socket.h>
# include <netinet/in.h> # include <netinet/in.h>
@ -639,7 +644,7 @@ static int get_ifcfg (hawk_gem_t* gem, int s, hawk_ifcfg_t* cfg, struct ifreq* i
return -1; return -1;
} }
#if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) #if defined(HAVE_STRUCT_IFREQ_IFR_IFINDEX) || defined(ifr_ifindex)
cfg->index = ifr->ifr_ifindex; cfg->index = ifr->ifr_ifindex;
#else #else
cfg->index = ifr->ifr_index; cfg->index = ifr->ifr_index;

View File

@ -122,6 +122,10 @@
# define HAWK_UNUSED # define HAWK_UNUSED
#endif #endif
#if !defined(__has_builtin)
# define __has_builtin(v) 0
#endif
/* ========================================================================= /* =========================================================================
* STATIC ASSERTION * STATIC ASSERTION
* =========================================================================*/ * =========================================================================*/
@ -729,8 +733,16 @@ struct hawk_ntime_t
* The HAWK_ALIGNOF() macro returns the alignment size of a structure. * The HAWK_ALIGNOF() macro returns the alignment size of a structure.
* Note that this macro may not work reliably depending on the type given. * Note that this macro may not work reliably depending on the type given.
*/ */
#define HAWK_ALIGNOF(type) HAWK_OFFSETOF(struct { hawk_uint8_t d1; type d2; }, d2) #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L) /* C23 */
/*(sizeof(struct { hawk_uint8_t d1; type d2; }) - sizeof(type))*/ #define HAWK_ALIGNOF(type) alignof(type)
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */
#define HAWK_ALIGNOF(type) _Alignof(type)
#elif defined(__cplusplus) && (__cplusplus >= 201103L) /* C++11 */
#define HAWK_ALIGNOF(type) alignof(type)
#else
#define HAWK_ALIGNOF(type) HAWK_OFFSETOF(struct { hcl_uint8_t d1; type d2; }, d2)
/*(sizeof(struct { hcl_uint8_t d1; type d2; }) - sizeof(type))*/
#endif
#if defined(__cplusplus) #if defined(__cplusplus)
# if (__cplusplus >= 201103L) /* C++11 */ # if (__cplusplus >= 201103L) /* C++11 */
@ -1222,6 +1234,16 @@ typedef enum hawk_log_mask_t hawk_log_mask_t;
#define HAWK_IS_UNALIGNED_POW2(x,y) ((x) & ((y) - 1)) #define HAWK_IS_UNALIGNED_POW2(x,y) ((x) & ((y) - 1))
#define HAWK_IS_ALIGNED_POW2(x,y) (!HAWK_IS_UNALIGNED_POW2(x,y)) #define HAWK_IS_ALIGNED_POW2(x,y) (!HAWK_IS_UNALIGNED_POW2(x,y))
#if defined(__cplusplus) || (defined(__STDC_VERSION__) && (__STDC_VERSION__>=199901L))
/* array index */
#define HAWK_AID(x) [x]=
/* struct field name */
#define HAWK_SFN(x) .x=
#else
#define HAWK_AID(x)
#define HAWK_SFN(x)
#endif
/* ========================================================================= /* =========================================================================
* COMPILER FEATURE TEST MACROS * COMPILER FEATURE TEST MACROS
* =========================================================================*/ * =========================================================================*/

View File

@ -598,11 +598,10 @@ static HAWK_INLINE void HAWK_RTX_STACK_POP (hawk_rtx_t* rtx)
#define HAWK_RTX_STACK_POP(rtx) ((rtx)->stack_top--) #define HAWK_RTX_STACK_POP(rtx) ((rtx)->stack_top--)
#endif #endif
#define HAWK_RTX_INIT_REF_VAL(refval, _id, _adr, _nrefs) \ #define HAWK_RTX_INIT_REF_VAL(refval, _id, _adr, _nrefs) \
do { \ do { \
(refval)->v_type = HAWK_VAL_REF; \
(refval)->v_refs = (_nrefs); \ (refval)->v_refs = (_nrefs); \
(refval)->v_type = HAWK_VAL_REF; \
(refval)->v_static = 0; \ (refval)->v_static = 0; \
(refval)->v_nstr = 0; \ (refval)->v_nstr = 0; \
(refval)->v_gc = 0; \ (refval)->v_gc = 0; \

View File

@ -157,26 +157,40 @@ static HAWK_INLINE hawk_gch_t* hawk_val_to_gch(hawk_val_t* v)
/** /**
* The #HAWK_VAL_HDR defines the common header for a value. * The #HAWK_VAL_HDR defines the common header for a value.
* Three common fields are: * Three common fields are:
* - v_type - type of a value from #hawk_val_type_t
* - v_refs - reference count * - v_refs - reference count
* - v_type - type of a value from #hawk_val_type_t
* - v_static - static value indicator * - v_static - static value indicator
* - v_nstr - numeric string marker, 1 -> integer, 2 -> floating-point number * - v_nstr - numeric string marker, 1 -> integer, 2 -> floating-point number
* - v_gc - used for garbage collection together with v_refs * - v_gc - used for garbage collection together with v_refs
*
* [IMPORTANT]
* if you change the order of these fields, you must ensure that statically
* defined values follow this order strictly. for example, hawk_nil, hawk_zls,
* hawk_zlbs are defined statically in val.c
*/ */
/* /*
#define HAWK_VAL_HDR \ #define HAWK_VAL_HDR \
unsigned int v_type: 4; \
unsigned int v_refs: 24; \ unsigned int v_refs: 24; \
unsigned int v_type: 4; \
unsigned int v_static: 1; \ unsigned int v_static: 1; \
unsigned int v_nstr: 2; \ unsigned int v_nstr: 2; \
unsigned int v_gc: 1 unsigned int v_gc: 1
*/
#define HAWK_VAL_HDR \ #define HAWK_VAL_HDR \
hawk_uintptr_t v_type: 4; \
hawk_uintptr_t v_refs: ((HAWK_SIZEOF_UINTPTR_T * 8) - 8); \ hawk_uintptr_t v_refs: ((HAWK_SIZEOF_UINTPTR_T * 8) - 8); \
hawk_uintptr_t v_type: 4; \
hawk_uintptr_t v_static: 1; \ hawk_uintptr_t v_static: 1; \
hawk_uintptr_t v_nstr: 2; \ hawk_uintptr_t v_nstr: 2; \
hawk_uintptr_t v_gc: 1 hawk_uintptr_t v_gc: 1
*/
/* regardless of architecture, use 4-byte reference count */
#define HAWK_VAL_HDR \
hawk_uint32_t v_refs; \
hawk_uint8_t v_type: 4; \
hawk_uint8_t v_static: 1; \
hawk_uint8_t v_nstr: 2; \
hawk_uint8_t v_gc: 1
/** /**
* The hawk_val_t type is an abstract value type. A value commonly contains: * The hawk_val_t type is an abstract value type. A value commonly contains:

View File

@ -25,6 +25,9 @@
#include "mod-math.h" #include "mod-math.h"
#include "hawk-prv.h" #include "hawk-prv.h"
#if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
#endif
#include <stdlib.h> #include <stdlib.h>
#include <sys/time.h> #include <sys/time.h>
#include <math.h> #include <math.h>

View File

@ -40,6 +40,9 @@
/* implement this */ /* implement this */
#else #else
# if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
# endif
# include "syscall.h" # include "syscall.h"
# if defined(AIX) && defined(__GNUC__) # if defined(AIX) && defined(__GNUC__)
typedef int crid_t; typedef int crid_t;

View File

@ -37,6 +37,9 @@
# include <io.h> # include <io.h>
# include <errno.h> # include <errno.h>
#else #else
# if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
# endif
# include "syscall.h" # include "syscall.h"
# if defined(HAVE_SPAWN_H) # if defined(HAVE_SPAWN_H)
# include <spawn.h> # include <spawn.h>

View File

@ -39,6 +39,9 @@
# include <dos.h> # include <dos.h>
# include <time.h> # include <time.h>
#else #else
# if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
# endif
# include "syscall.h" # include "syscall.h"
# if defined(HAVE_SYS_TIME_H) # if defined(HAVE_SYS_TIME_H)
# include <sys/time.h> # include <sys/time.h>

View File

@ -26,11 +26,35 @@
#define CHUNKSIZE HAWK_VAL_CHUNK_SIZE #define CHUNKSIZE HAWK_VAL_CHUNK_SIZE
static hawk_val_nil_t hawk_nil = { HAWK_VAL_NIL, 0, 1, 0, 0 }; /* this isn't necessary as the implemenation is context(rtx)-centric
* leave it here for experiment.
#define USE_ATOMIC_REFCNT */
static hawk_val_nil_t hawk_nil = {
HAWK_SFN(v_refs) 0,
HAWK_SFN(v_type) HAWK_VAL_NIL,
HAWK_SFN(v_static) 1,
HAWK_SFN(v_nstr) 0,
HAWK_SFN(v_gc) 0
};
/* zero-length string */ /* zero-length string */
static hawk_val_str_t hawk_zls = { HAWK_VAL_STR, 0, 1, 0, 0, { HAWK_T(""), 0 } }; static hawk_val_str_t hawk_zls = {
HAWK_SFN(v_refs) 0,
HAWK_SFN(v_type) HAWK_VAL_STR,
HAWK_SFN(v_static) 1,
HAWK_SFN(v_nstr) 0,
HAWK_SFN(v_gc) 0,
HAWK_SFN(val) { HAWK_T(""), 0 }
};
/* zero-length byte string */ /* zero-length byte string */
static hawk_val_mbs_t hawk_zlbs = { HAWK_VAL_MBS, 0, 1, 0, 0, { HAWK_BT(""), 0 } }; static hawk_val_mbs_t hawk_zlbs = {
HAWK_SFN(v_refs) 0,
HAWK_SFN(v_type) HAWK_VAL_MBS,
HAWK_SFN(v_static) 1,
HAWK_SFN(v_nstr) 0,
HAWK_SFN(v_gc) 0,
HAWK_SFN(val) { HAWK_BT(""), 0 }
};
hawk_val_t* hawk_val_nil = (hawk_val_t*)&hawk_nil; hawk_val_t* hawk_val_nil = (hawk_val_t*)&hawk_nil;
hawk_val_t* hawk_val_zls = (hawk_val_t*)&hawk_zls; hawk_val_t* hawk_val_zls = (hawk_val_t*)&hawk_zls;
@ -1635,7 +1659,12 @@ void hawk_rtx_refupval (hawk_rtx_t* rtx, hawk_val_t* val)
#if defined(DEBUG_VAL) #if defined(DEBUG_VAL)
hawk_logfmt (hawk_rtx_gethawk(rtx), HAWK_LOG_STDERR, HAWK_T("ref up [ptr=%p] [count=%d] - [%O]\n"), val, (int)val->v_refs, val); hawk_logfmt (hawk_rtx_gethawk(rtx), HAWK_LOG_STDERR, HAWK_T("ref up [ptr=%p] [count=%d] - [%O]\n"), val, (int)val->v_refs, val);
#endif #endif
#if defined(USE_ATOMIC_REFCNT) && __has_builtin(__atomic_fetch_add)
__atomic_fetch_add(&val->v_refs, 1, __ATOMIC_RELAXED);
#else
val->v_refs++; val->v_refs++;
#endif
} }
} }
@ -1652,11 +1681,18 @@ void hawk_rtx_refdownval (hawk_rtx_t* rtx, hawk_val_t* val)
/* the reference count of a value should be greater than zero for it to be decremented. check the source code for any bugs */ /* the reference count of a value should be greater than zero for it to be decremented. check the source code for any bugs */
HAWK_ASSERT (val->v_refs > 0); HAWK_ASSERT (val->v_refs > 0);
#if defined(USE_ATOMIC_REFCNT) && __has_builtin(__atomic_fetch_sub)
if (__atomic_fetch_sub(&val->v_refs, 1, __ATOMIC_RELAXED) == 1)
{
hawk_rtx_freeval (rtx, val, HAWK_RTX_FREEVAL_CACHE);
}
#else
val->v_refs--; val->v_refs--;
if (val->v_refs <= 0) if (val->v_refs <= 0)
{ {
hawk_rtx_freeval (rtx, val, HAWK_RTX_FREEVAL_CACHE); hawk_rtx_freeval (rtx, val, HAWK_RTX_FREEVAL_CACHE);
} }
#endif
} }
} }
@ -1669,7 +1705,11 @@ void hawk_rtx_refdownval_nofree (hawk_rtx_t* rtx, hawk_val_t* val)
/* the reference count of a value should be greater than zero for it to be decremented. check the source code for any bugs */ /* the reference count of a value should be greater than zero for it to be decremented. check the source code for any bugs */
HAWK_ASSERT (val->v_refs > 0); HAWK_ASSERT (val->v_refs > 0);
#if defined(USE_ATOMIC_REFCNT) && __has_builtin(__atomic_fetch_sub)
__atomic_fetch_sub(&val->v_refs, 1, __ATOMIC_RELAXED);
#else
val->v_refs--; val->v_refs--;
#endif
} }
} }