Skip to content

Commit 35a5e55

Browse files
byrootmatzbot
authored andcommitted
[ruby/json] Centralize macro definitions
ruby/json@1576ea7d47
1 parent 4a3d834 commit 35a5e55

File tree

5 files changed

+106
-140
lines changed

5 files changed

+106
-140
lines changed

ext/json/fbuffer/fbuffer.h

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,9 @@
11
#ifndef _FBUFFER_H_
22
#define _FBUFFER_H_
33

4-
#include "ruby.h"
5-
#include "ruby/encoding.h"
4+
#include "../json.h"
65
#include "../vendor/jeaiii-ltoa.h"
76

8-
/* shims */
9-
/* This is the fallback definition from Ruby 3.4 */
10-
11-
#ifndef RBIMPL_STDBOOL_H
12-
#if defined(__cplusplus)
13-
# if defined(HAVE_STDBOOL_H) && (__cplusplus >= 201103L)
14-
# include <cstdbool>
15-
# endif
16-
#elif defined(HAVE_STDBOOL_H)
17-
# include <stdbool.h>
18-
#elif !defined(HAVE__BOOL)
19-
typedef unsigned char _Bool;
20-
# define bool _Bool
21-
# define true ((_Bool)+1)
22-
# define false ((_Bool)+0)
23-
# define __bool_true_false_are_defined
24-
#endif
25-
#endif
26-
27-
#ifndef NOINLINE
28-
#if defined(__has_attribute) && __has_attribute(noinline)
29-
#define NOINLINE() __attribute__((noinline))
30-
#else
31-
#define NOINLINE()
32-
#endif
33-
#endif
34-
35-
#ifndef RB_UNLIKELY
36-
#define RB_UNLIKELY(expr) expr
37-
#endif
38-
39-
#ifndef RB_LIKELY
40-
#define RB_LIKELY(expr) expr
41-
#endif
42-
43-
#ifndef MAYBE_UNUSED
44-
# define MAYBE_UNUSED(x) x
45-
#endif
46-
47-
#ifdef RUBY_DEBUG
48-
#ifndef JSON_DEBUG
49-
#define JSON_DEBUG RUBY_DEBUG
50-
#endif
51-
#endif
52-
537
enum fbuffer_type {
548
FBUFFER_HEAP_ALLOCATED = 0,
559
FBUFFER_STACK_ALLOCATED = 1,
@@ -290,4 +244,4 @@ static VALUE fbuffer_finalize(FBuffer *fb)
290244
}
291245
}
292246

293-
#endif
247+
#endif // _FBUFFER_H_

ext/json/generator/generator.c

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "ruby.h"
1+
#include "../json.h"
22
#include "../fbuffer/fbuffer.h"
33
#include "../vendor/fpconv.c"
44

@@ -36,10 +36,6 @@ typedef struct JSON_Generator_StateStruct {
3636
bool strict;
3737
} JSON_Generator_State;
3838

39-
#ifndef RB_UNLIKELY
40-
#define RB_UNLIKELY(cond) (cond)
41-
#endif
42-
4339
static VALUE mJSON, cState, cFragment, eGeneratorError, eNestingError, Encoding_UTF_8;
4440

4541
static ID i_to_s, i_to_json, i_new, i_pack, i_unpack, i_create_id, i_extend, i_encode;
@@ -85,24 +81,18 @@ static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *d
8581

8682
static int usascii_encindex, utf8_encindex, binary_encindex;
8783

88-
#ifdef RBIMPL_ATTR_NORETURN
89-
RBIMPL_ATTR_NORETURN()
90-
#endif
91-
static void raise_generator_error_str(VALUE invalid_object, VALUE str)
84+
NORETURN(static void) raise_generator_error_str(VALUE invalid_object, VALUE str)
9285
{
9386
rb_enc_associate_index(str, utf8_encindex);
9487
VALUE exc = rb_exc_new_str(eGeneratorError, str);
9588
rb_ivar_set(exc, rb_intern("@invalid_object"), invalid_object);
9689
rb_exc_raise(exc);
9790
}
9891

99-
#ifdef RBIMPL_ATTR_NORETURN
100-
RBIMPL_ATTR_NORETURN()
101-
#endif
10292
#ifdef RBIMPL_ATTR_FORMAT
10393
RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
10494
#endif
105-
static void raise_generator_error(VALUE invalid_object, const char *fmt, ...)
95+
NORETURN(static void) raise_generator_error(VALUE invalid_object, const char *fmt, ...)
10696
{
10797
va_list args;
10898
va_start(args, fmt);
@@ -137,13 +127,7 @@ typedef struct _search_state {
137127
#endif /* HAVE_SIMD */
138128
} search_state;
139129

140-
#if (defined(__GNUC__ ) || defined(__clang__))
141-
#define FORCE_INLINE __attribute__((always_inline))
142-
#else
143-
#define FORCE_INLINE
144-
#endif
145-
146-
static inline FORCE_INLINE void search_flush(search_state *search)
130+
static ALWAYS_INLINE() void search_flush(search_state *search)
147131
{
148132
// Do not remove this conditional without profiling, specifically escape-heavy text.
149133
// escape_UTF8_char_basic will advance search->ptr and search->cursor (effectively a search_flush).
@@ -186,7 +170,7 @@ static inline unsigned char search_escape_basic(search_state *search)
186170
return 0;
187171
}
188172

189-
static inline FORCE_INLINE void escape_UTF8_char_basic(search_state *search)
173+
static ALWAYS_INLINE() void escape_UTF8_char_basic(search_state *search)
190174
{
191175
const unsigned char ch = (unsigned char)*search->ptr;
192176
switch (ch) {
@@ -273,7 +257,7 @@ static inline void escape_UTF8_char(search_state *search, unsigned char ch_len)
273257

274258
#ifdef HAVE_SIMD
275259

276-
static inline FORCE_INLINE char *copy_remaining_bytes(search_state *search, unsigned long vec_len, unsigned long len)
260+
static ALWAYS_INLINE() char *copy_remaining_bytes(search_state *search, unsigned long vec_len, unsigned long len)
277261
{
278262
// Flush the buffer so everything up until the last 'len' characters are unflushed.
279263
search_flush(search);
@@ -296,7 +280,7 @@ static inline FORCE_INLINE char *copy_remaining_bytes(search_state *search, unsi
296280

297281
#ifdef HAVE_SIMD_NEON
298282

299-
static inline FORCE_INLINE unsigned char neon_next_match(search_state *search)
283+
static ALWAYS_INLINE() unsigned char neon_next_match(search_state *search)
300284
{
301285
uint64_t mask = search->matches_mask;
302286
uint32_t index = trailing_zeros64(mask) >> 2;
@@ -410,7 +394,7 @@ static inline unsigned char search_escape_basic_neon(search_state *search)
410394

411395
#ifdef HAVE_SIMD_SSE2
412396

413-
static inline FORCE_INLINE unsigned char sse2_next_match(search_state *search)
397+
static ALWAYS_INLINE() unsigned char sse2_next_match(search_state *search)
414398
{
415399
int mask = search->matches_mask;
416400
int index = trailing_zeros(mask);
@@ -434,7 +418,7 @@ static inline FORCE_INLINE unsigned char sse2_next_match(search_state *search)
434418
#define TARGET_SSE2
435419
#endif
436420

437-
static inline TARGET_SSE2 FORCE_INLINE unsigned char search_escape_basic_sse2(search_state *search)
421+
static inline TARGET_SSE2 ALWAYS_INLINE() unsigned char search_escape_basic_sse2(search_state *search)
438422
{
439423
if (RB_UNLIKELY(search->has_matches)) {
440424
// There are more matches if search->matches_mask > 0.

ext/json/json.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#ifndef _JSON_H_
2+
#define _JSON_H_
3+
4+
#include "ruby.h"
5+
#include "ruby/encoding.h"
6+
7+
#if defined(RUBY_DEBUG) && RUBY_DEBUG
8+
# define JSON_ASSERT RUBY_ASSERT
9+
#else
10+
# ifdef JSON_DEBUG
11+
# include <assert.h>
12+
# define JSON_ASSERT(x) assert(x)
13+
# else
14+
# define JSON_ASSERT(x)
15+
# endif
16+
#endif
17+
18+
/* shims */
19+
20+
#if SIZEOF_UINT64_T == SIZEOF_LONG_LONG
21+
# define INT64T2NUM(x) LL2NUM(x)
22+
# define UINT64T2NUM(x) ULL2NUM(x)
23+
#elif SIZEOF_UINT64_T == SIZEOF_LONG
24+
# define INT64T2NUM(x) LONG2NUM(x)
25+
# define UINT64T2NUM(x) ULONG2NUM(x)
26+
#else
27+
# error No uint64_t conversion
28+
#endif
29+
30+
/* This is the fallback definition from Ruby 3.4 */
31+
#ifndef RBIMPL_STDBOOL_H
32+
#if defined(__cplusplus)
33+
# if defined(HAVE_STDBOOL_H) && (__cplusplus >= 201103L)
34+
# include <cstdbool>
35+
# endif
36+
#elif defined(HAVE_STDBOOL_H)
37+
# include <stdbool.h>
38+
#elif !defined(HAVE__BOOL)
39+
typedef unsigned char _Bool;
40+
# define bool _Bool
41+
# define true ((_Bool)+1)
42+
# define false ((_Bool)+0)
43+
# define __bool_true_false_are_defined
44+
#endif
45+
#endif
46+
47+
#ifndef NORETURN
48+
#define NORETURN(x) x
49+
#endif
50+
51+
#ifndef NOINLINE
52+
#if defined(__has_attribute) && __has_attribute(noinline)
53+
#define NOINLINE(x) __attribute__((noinline)) x
54+
#else
55+
#define NOINLINE(x) x
56+
#endif
57+
#endif
58+
59+
#ifndef ALWAYS_INLINE
60+
#if defined(__has_attribute) && __has_attribute(always_inline)
61+
#define ALWAYS_INLINE(x) inline __attribute__((always_inline)) x
62+
#else
63+
#define ALWAYS_INLINE(x) inline x
64+
#endif
65+
#endif
66+
67+
#ifndef RB_UNLIKELY
68+
#define RB_UNLIKELY(expr) expr
69+
#endif
70+
71+
#ifndef RB_LIKELY
72+
#define RB_LIKELY(expr) expr
73+
#endif
74+
75+
#ifndef MAYBE_UNUSED
76+
# define MAYBE_UNUSED(x) x
77+
#endif
78+
79+
#ifdef RUBY_DEBUG
80+
#ifndef JSON_DEBUG
81+
#define JSON_DEBUG RUBY_DEBUG
82+
#endif
83+
#endif
84+
85+
#endif // _JSON_H_

ext/json/parser/parser.c

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,7 @@
1-
#include "ruby.h"
2-
#include "ruby/encoding.h"
1+
#include "../json.h"
32
#include "../vendor/ryu.h"
4-
5-
/* shims */
6-
/* This is the fallback definition from Ruby 3.4 */
7-
8-
#ifndef RBIMPL_STDBOOL_H
9-
#if defined(__cplusplus)
10-
# if defined(HAVE_STDBOOL_H) && (__cplusplus >= 201103L)
11-
# include <cstdbool>
12-
# endif
13-
#elif defined(HAVE_STDBOOL_H)
14-
# include <stdbool.h>
15-
#elif !defined(HAVE__BOOL)
16-
typedef unsigned char _Bool;
17-
# define bool _Bool
18-
# define true ((_Bool)+1)
19-
# define false ((_Bool)+0)
20-
# define __bool_true_false_are_defined
21-
#endif
22-
#endif
23-
24-
#if SIZEOF_UINT64_T == SIZEOF_LONG_LONG
25-
# define INT64T2NUM(x) LL2NUM(x)
26-
# define UINT64T2NUM(x) ULL2NUM(x)
27-
#elif SIZEOF_UINT64_T == SIZEOF_LONG
28-
# define INT64T2NUM(x) LONG2NUM(x)
29-
# define UINT64T2NUM(x) ULONG2NUM(x)
30-
#else
31-
# error No uint64_t conversion
32-
#endif
33-
343
#include "../simd/simd.h"
354

36-
#ifndef RB_UNLIKELY
37-
#define RB_UNLIKELY(expr) expr
38-
#endif
39-
40-
#ifndef RB_LIKELY
41-
#define RB_LIKELY(expr) expr
42-
#endif
43-
445
static VALUE mJSON, eNestingError, Encoding_UTF_8;
456
static VALUE CNaN, CInfinity, CMinusInfinity;
467

@@ -985,17 +946,11 @@ static const bool string_scan_table[256] = {
985946
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
986947
};
987948

988-
#if (defined(__GNUC__ ) || defined(__clang__))
989-
#define FORCE_INLINE __attribute__((always_inline))
990-
#else
991-
#define FORCE_INLINE
992-
#endif
993-
994949
#ifdef HAVE_SIMD
995950
static SIMD_Implementation simd_impl = SIMD_NONE;
996951
#endif /* HAVE_SIMD */
997952

998-
static inline bool FORCE_INLINE string_scan(JSON_ParserState *state)
953+
static ALWAYS_INLINE() bool string_scan(JSON_ParserState *state)
999954
{
1000955
#ifdef HAVE_SIMD
1001956
#if defined(HAVE_SIMD_NEON)

0 commit comments

Comments
 (0)