Skip to content

Commit

Permalink
remove check for __builtin_expect
Browse files Browse the repository at this point in the history
Use `__has_builtin` to check at compile time if the feature is
supported. This macro is supported by both clang and GCC (as of 10).
GCC 10 was released ~2020 [1], which is ~4 years old now.  In the case
that the compiler in use is not new enough, we still provide the
fallback so that the code will compile but without the additional hints
for the branch probability (which also tends to be accounted for by
modern branch predictors).  This removes the final use of the `config.h`
header and the configure time checks.  Remove the use of `config.h` from
the code base.

[1] https://gcc.gnu.org/releases.html
  • Loading branch information
compnerd authored and jgm committed Jan 4, 2024
1 parent b451234 commit abff3b6
Show file tree
Hide file tree
Showing 19 changed files with 41 additions and 51 deletions.
10 changes: 0 additions & 10 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,6 @@ if(CMARK_SHARED OR CMARK_STATIC)

endif()

# Feature tests
include(CheckCSourceCompiles)
CHECK_C_SOURCE_COMPILES(
"int main() { __builtin_expect(0,0); return 0; }"
HAVE___BUILTIN_EXPECT)

CONFIGURE_FILE(
${CMAKE_CURRENT_SOURCE_DIR}/config.h.in
${CMAKE_CURRENT_BINARY_DIR}/config.h)

if(CMARK_LIB_FUZZER)
add_executable(cmark-fuzz ../test/cmark-fuzz.c ${LIBRARY_SOURCES})
cmark_add_compile_options(cmark-fuzz)
Expand Down
1 change: 0 additions & 1 deletion src/blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <stdlib.h>

#include "cmark_ctype.h"
#include "config.h"
#include "parser.h"
#include "cmark.h"
#include "node.h"
Expand Down
1 change: 0 additions & 1 deletion src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "cmark_ctype.h"
#include "buffer.h"

Expand Down
8 changes: 4 additions & 4 deletions src/buffer.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef CMARK_BUFFER_H
#define CMARK_BUFFER_H

#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include "config.h"
#include <string.h>

#include "cmark.h"

#ifdef __cplusplus
Expand Down
1 change: 0 additions & 1 deletion src/commonmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "cmark.h"
#include "node.h"
#include "buffer.h"
Expand Down
14 changes: 0 additions & 14 deletions src/config.h.in

This file was deleted.

10 changes: 1 addition & 9 deletions src/houdini.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@ extern "C" {
#endif

#include <stdint.h>
#include "config.h"
#include "buffer.h"

#ifdef HAVE___BUILTIN_EXPECT
#define likely(x) __builtin_expect((x), 1)
#define unlikely(x) __builtin_expect((x), 0)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif
#include "buffer.h"

#ifdef HOUDINI_USE_LOCALE
#define _isxdigit(c) isxdigit(c)
Expand Down
11 changes: 11 additions & 0 deletions src/houdini_href_e.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

#include "houdini.h"

#if !defined(__has_builtin)
# define __has_builtin(b) 0
#endif

#if !__has_builtin(__builtin_expect)
# define __builtin_expect(e, v) (e)
#endif

#define likely(e) __builtin_expect((e), 1)
#define unlikely(e) __builtin_expect((e), 0)

/*
* The following characters will not be escaped:
*
Expand Down
11 changes: 11 additions & 0 deletions src/houdini_html_e.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

#include "houdini.h"

#if !defined(__has_builtin)
# define __has_builtin(b) 0
#endif

#if !__has_builtin(__builtin_expect)
# define __builtin_expect(e, v) (e)
#endif

#define likely(e) __builtin_expect((e), 1)
#define unlikely(e) __builtin_expect((e), 0)

/**
* According to the OWASP rules:
*
Expand Down
11 changes: 11 additions & 0 deletions src/houdini_html_u.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
#include "utf8.h"
#include "entities.inc"

#if !defined(__has_builtin)
# define __has_builtin(b) 0
#endif

#if !__has_builtin(__builtin_expect)
# define __builtin_expect(e, v) (e)
#endif

#define likely(e) __builtin_expect((e), 1)
#define unlikely(e) __builtin_expect((e), 0)

/* Binary tree lookup code for entities added by JGM */

static const unsigned char *S_lookup(int i, int low, int hi,
Expand Down
1 change: 0 additions & 1 deletion src/html.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <string.h>

#include "cmark_ctype.h"
#include "config.h"
#include "cmark.h"
#include "node.h"
#include "buffer.h"
Expand Down
1 change: 0 additions & 1 deletion src/inlines.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <string.h>

#include "cmark_ctype.h"
#include "config.h"
#include "node.h"
#include "parser.h"
#include "references.h"
Expand Down
1 change: 0 additions & 1 deletion src/iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <stdbool.h>
#include <stdlib.h>

#include "config.h"
#include "node.h"
#include "cmark.h"
#include "iterator.h"
Expand Down
1 change: 0 additions & 1 deletion src/latex.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "cmark.h"
#include "node.h"
#include "buffer.h"
Expand Down
6 changes: 3 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "config.h"

#include "cmark.h"
#include "node.h"

Expand Down
1 change: 0 additions & 1 deletion src/man.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "cmark.h"
#include "node.h"
#include "buffer.h"
Expand Down
1 change: 0 additions & 1 deletion src/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "node.h"

static void S_node_unlink(cmark_node *node);
Expand Down
1 change: 0 additions & 1 deletion src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ extern "C" {
#include <stdint.h>
#include <stdio.h>

#include "config.h"
#include "cmark.h"
#include "buffer.h"

Expand Down
1 change: 0 additions & 1 deletion src/xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "cmark.h"
#include "node.h"
#include "buffer.h"
Expand Down

0 comments on commit abff3b6

Please sign in to comment.