From 17eb4d56ff29b72e82e29a39d3e1b133ed32c943 Mon Sep 17 00:00:00 2001 From: Steven Dee Date: Thu, 11 Dec 2014 22:08:20 -0500 Subject: [PATCH 1/5] Add visibility declarations --- SConstruct | 2 +- src/export.h | 17 +++++++++++ src/hammer.h | 83 ++++++++++++++++++++++++++-------------------------- 3 files changed, 60 insertions(+), 42 deletions(-) create mode 100644 src/export.h diff --git a/SConstruct b/SConstruct index 4cf48a3a..ceeef4af 100644 --- a/SConstruct +++ b/SConstruct @@ -45,7 +45,7 @@ env['backendsincpath'] = calcInstallPath("$prefix", "include", "hammer", "backen env['pkgconfigpath'] = calcInstallPath("$prefix", "lib", "pkgconfig") env.ScanReplace('libhammer.pc.in') -env.MergeFlags("-std=gnu99 -Wall -Wextra -Werror -Wno-unused-parameter -Wno-attributes -Wno-unused-variable") +env.MergeFlags("-std=gnu99 -fvisibility=hidden -Wall -Wextra -Werror -Wno-unused-parameter -Wno-attributes -Wno-unused-variable") if env['PLATFORM'] == 'darwin': env.Append(SHLINKFLAGS = '-install_name ' + env["libpath"] + '/${TARGET.file}') diff --git a/src/export.h b/src/export.h new file mode 100644 index 00000000..2e28aad8 --- /dev/null +++ b/src/export.h @@ -0,0 +1,17 @@ +#if defined _MSC_VER +# if defined BUILDING_HAMMER_DLL +# define H_EXPORT __declspec(dllexport) +# else + /* + * there is a slight performance gain in using __declspec(dllimport) here, + * but doing so would break static linking against hammer. + */ +# define H_EXPORT extern +# endif +#else +# if __GNUC__ >= 4 +# define H_EXPORT __attribute__ ((visibility ("default"))) +# else +# define H_EXPORT +# endif +#endif diff --git a/src/hammer.h b/src/hammer.h index b0ce75d2..bb9f704f 100644 --- a/src/hammer.h +++ b/src/hammer.h @@ -24,6 +24,7 @@ #include #include #include "allocator.h" +#include "export.h" #define BYTE_BIG_ENDIAN 0x1 #define BIT_BIG_ENDIAN 0x2 @@ -197,47 +198,47 @@ typedef struct HBenchmarkResults_ { // {{{ Preprocessor definitions #define HAMMER_FN_DECL_NOARG(rtype_t, name) \ - rtype_t name(void); \ - rtype_t name##__m(HAllocator* mm__) + H_EXPORT rtype_t name(void); \ + H_EXPORT rtype_t name##__m(HAllocator* mm__) #define HAMMER_FN_DECL(rtype_t, name, ...) \ - rtype_t name(__VA_ARGS__); \ - rtype_t name##__m(HAllocator* mm__, __VA_ARGS__) + H_EXPORT rtype_t name(__VA_ARGS__); \ + H_EXPORT rtype_t name##__m(HAllocator* mm__, __VA_ARGS__) #define HAMMER_FN_DECL_ATTR(attr, rtype_t, name, ...) \ - rtype_t name(__VA_ARGS__) attr; \ - rtype_t name##__m(HAllocator* mm__, __VA_ARGS__) attr + H_EXPORT rtype_t name(__VA_ARGS__) attr; \ + H_EXPORT rtype_t name##__m(HAllocator* mm__, __VA_ARGS__) attr #ifndef SWIG #define HAMMER_FN_DECL_VARARGS(rtype_t, name, ...) \ - rtype_t name(__VA_ARGS__, ...); \ - rtype_t name##__m(HAllocator* mm__, __VA_ARGS__, ...); \ - rtype_t name##__mv(HAllocator* mm__, __VA_ARGS__, va_list ap); \ - rtype_t name##__v(__VA_ARGS__, va_list ap); \ - rtype_t name##__a(void *args[]); \ - rtype_t name##__ma(HAllocator *mm__, void *args[]) + H_EXPORT rtype_t name(__VA_ARGS__, ...); \ + H_EXPORT rtype_t name##__m(HAllocator* mm__, __VA_ARGS__, ...); \ + H_EXPORT rtype_t name##__mv(HAllocator* mm__, __VA_ARGS__, va_list ap); \ + H_EXPORT rtype_t name##__v(__VA_ARGS__, va_list ap); \ + H_EXPORT rtype_t name##__a(void *args[]); \ + H_EXPORT rtype_t name##__ma(HAllocator *mm__, void *args[]) // Note: this drops the attributes on the floor for the __v versions #define HAMMER_FN_DECL_VARARGS_ATTR(attr, rtype_t, name, ...) \ - rtype_t name(__VA_ARGS__, ...) attr; \ - rtype_t name##__m(HAllocator* mm__, __VA_ARGS__, ...) attr; \ - rtype_t name##__mv(HAllocator* mm__, __VA_ARGS__, va_list ap); \ - rtype_t name##__v(__VA_ARGS__, va_list ap); \ - rtype_t name##__a(void *args[]); \ - rtype_t name##__ma(HAllocator *mm__, void *args[]) + H_EXPORT rtype_t name(__VA_ARGS__, ...) attr; \ + H_EXPORT rtype_t name##__m(HAllocator* mm__, __VA_ARGS__, ...) attr; \ + H_EXPORT rtype_t name##__mv(HAllocator* mm__, __VA_ARGS__, va_list ap); \ + H_EXPORT rtype_t name##__v(__VA_ARGS__, va_list ap); \ + H_EXPORT rtype_t name##__a(void *args[]); \ + H_EXPORT rtype_t name##__ma(HAllocator *mm__, void *args[]) #else #define HAMMER_FN_DECL_VARARGS(rtype_t, name, params...) \ - rtype_t name(params, ...); \ - rtype_t name##__m(HAllocator* mm__, params, ...); \ - rtype_t name##__a(void *args[]); \ - rtype_t name##__ma(HAllocator *mm__, void *args[]) + H_EXPORT rtype_t name(params, ...); \ + H_EXPORT rtype_t name##__m(HAllocator* mm__, params, ...); \ + H_EXPORT rtype_t name##__a(void *args[]); \ + H_EXPORT rtype_t name##__ma(HAllocator *mm__, void *args[]) // Note: this drops the attributes on the floor for the __v versions #define HAMMER_FN_DECL_VARARGS_ATTR(attr, rtype_t, name, params...) \ - rtype_t name(params, ...); \ - rtype_t name##__m(HAllocator* mm__, params, ...); \ - rtype_t name##__a(void *args[]); \ - rtype_t name##__ma(HAllocator *mm__, void *args[]) + H_EXPORT rtype_t name(params, ...); \ + H_EXPORT rtype_t name##__m(HAllocator* mm__, params, ...); \ + H_EXPORT rtype_t name##__a(void *args[]); \ + H_EXPORT rtype_t name##__ma(HAllocator *mm__, void *args[]) #endif // SWIG // }}} @@ -673,12 +674,12 @@ HAMMER_FN_DECL(void, h_parse_result_free, HParseResult *result); * Format token into a compact unambiguous form. Useful for parser test cases. * Caller is responsible for freeing the result. */ -char* h_write_result_unamb(const HParsedToken* tok); +H_EXPORT char* h_write_result_unamb(const HParsedToken* tok); /** * Format token to the given output stream. Indent starting at * [indent] spaces, with [delta] spaces between levels. */ -void h_pprint(FILE* stream, const HParsedToken* tok, int indent, int delta); +H_EXPORT void h_pprint(FILE* stream, const HParsedToken* tok, int indent, int delta); /** * Build parse tables for the given parser backend. See the @@ -692,48 +693,48 @@ HAMMER_FN_DECL(int, h_compile, HParser* parser, HParserBackend backend, const vo /** * TODO: Document me */ -HBitWriter *h_bit_writer_new(HAllocator* mm__); +H_EXPORT HBitWriter *h_bit_writer_new(HAllocator* mm__); /** * TODO: Document me */ -void h_bit_writer_put(HBitWriter* w, uint64_t data, size_t nbits); +H_EXPORT void h_bit_writer_put(HBitWriter* w, uint64_t data, size_t nbits); /** * TODO: Document me * Must not free [w] until you're done with the result. * [len] is in bytes. */ -const uint8_t* h_bit_writer_get_buffer(HBitWriter* w, size_t *len); +H_EXPORT const uint8_t* h_bit_writer_get_buffer(HBitWriter* w, size_t *len); /** * TODO: Document me */ -void h_bit_writer_free(HBitWriter* w); +H_EXPORT void h_bit_writer_free(HBitWriter* w); // General-purpose actions for use with h_action // XXX to be consolidated with glue.h when merged upstream -HParsedToken *h_act_first(const HParseResult *p, void* userdata); -HParsedToken *h_act_second(const HParseResult *p, void* userdata); -HParsedToken *h_act_last(const HParseResult *p, void* userdata); -HParsedToken *h_act_flatten(const HParseResult *p, void* userdata); -HParsedToken *h_act_ignore(const HParseResult *p, void* userdata); +H_EXPORT HParsedToken *h_act_first(const HParseResult *p, void* userdata); +H_EXPORT HParsedToken *h_act_second(const HParseResult *p, void* userdata); +H_EXPORT HParsedToken *h_act_last(const HParseResult *p, void* userdata); +H_EXPORT HParsedToken *h_act_flatten(const HParseResult *p, void* userdata); +H_EXPORT HParsedToken *h_act_ignore(const HParseResult *p, void* userdata); // {{{ Benchmark functions HAMMER_FN_DECL(HBenchmarkResults *, h_benchmark, HParser* parser, HParserTestcase* testcases); -void h_benchmark_report(FILE* stream, HBenchmarkResults* results); +H_EXPORT void h_benchmark_report(FILE* stream, HBenchmarkResults* results); //void h_benchmark_dump_optimized_code(FILE* stream, HBenchmarkResults* results); // }}} // {{{ Token type registry /// Allocate a new, unused (as far as this function knows) token type. -HTokenType h_allocate_token_type(const char* name); +H_EXPORT HTokenType h_allocate_token_type(const char* name); /// Get the token type associated with name. Returns -1 if name is unkown -HTokenType h_get_token_type_number(const char* name); +H_EXPORT HTokenType h_get_token_type_number(const char* name); /// Get the name associated with token_type. Returns NULL if the token type is unkown -const char* h_get_token_type_name(HTokenType token_type); +H_EXPORT const char* h_get_token_type_name(HTokenType token_type); // }}} #ifdef __cplusplus From 48a50322f60409af41ac7eeeec8219e08f13e863 Mon Sep 17 00:00:00 2001 From: Steven Dee Date: Thu, 11 Dec 2014 22:22:31 -0500 Subject: [PATCH 2/5] Add export.h to build and other stuff --- contrib/freebsd/pkg-plist | 1 + contrib/netbsd/PLIST | 1 + examples/Makefile | 8 ++++---- src/Makefile | 4 ++-- src/SConscript | 1 + src/bindings/python/setup.py | 1 + 6 files changed, 10 insertions(+), 6 deletions(-) diff --git a/contrib/freebsd/pkg-plist b/contrib/freebsd/pkg-plist index c751ea1e..ad99e5ad 100644 --- a/contrib/freebsd/pkg-plist +++ b/contrib/freebsd/pkg-plist @@ -3,6 +3,7 @@ include/hammer/allocator.h include/hammer/parsers/parser_internal.h include/hammer/backends/regex.h include/hammer/backends/contextfree.h +include/hammer/export.h include/hammer/glue.h include/hammer/hammer.h lib/libhammer.so diff --git a/contrib/netbsd/PLIST b/contrib/netbsd/PLIST index 1efea540..6ce84351 100644 --- a/contrib/netbsd/PLIST +++ b/contrib/netbsd/PLIST @@ -4,6 +4,7 @@ include/hammer/allocator.h include/hammer/parsers/parser_internal.h include/hammer/backends/regex.h include/hammer/backends/contextfree.h +include/hammer/export.h include/hammer/glue.h include/hammer/hammer.h lib/libhammer.so diff --git a/examples/Makefile b/examples/Makefile index 663a2144..f577d7f0 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -22,9 +22,9 @@ dns: LDFLAGS:=-L../src -lhammer $(LDFLAGS) dns: dns.o rr.o dns_common.o $(call hush, "Linking $@") $(CC) -o $@ $^ $(LDFLAGS) -dns.o: ../src/hammer.h dns_common.h ../src/glue.h -rr.o: ../src/hammer.h rr.h dns_common.h ../src/glue.h -dns_common.o: ../src/hammer.h dns_common.h ../src/glue.h +dns.o: ../src/hammer.h dns_common.h ../src/glue.h ../src/export.h +rr.o: ../src/hammer.h rr.h dns_common.h ../src/glue.h ../src/export.h +dns_common.o: ../src/hammer.h dns_common.h ../src/glue.h ../src/export.h base64: LDFLAGS:=-L../src -lhammer $(LDFLAGS) base64: base64.o @@ -38,4 +38,4 @@ base64_sem2: LDFLAGS:=-L../src -lhammer $(LDFLAGS) base64_sem2: base64_sem2.o $(call hush, "Linking $@") $(CC) -o $@ $^ $(LDFLAGS) -base64%.o: ../src/hammer.h ../src/glue.h +base64%.o: ../src/hammer.h ../src/glue.h ../src/export.h diff --git a/src/Makefile b/src/Makefile index 9ce6d9f8..eedd9513 100644 --- a/src/Makefile +++ b/src/Makefile @@ -77,8 +77,8 @@ all: libhammer.a libhammer.a: $(HAMMER_PARTS) bitreader.o: test_suite.h -hammer.o: hammer.h -glue.o: hammer.h glue.h +hammer.o: hammer.h export.h allocator.h +glue.o: hammer.h glue.h export.h allocator.h all: libhammer.a diff --git a/src/SConscript b/src/SConscript index 38ace12a..5a8a1d00 100644 --- a/src/SConscript +++ b/src/SConscript @@ -5,6 +5,7 @@ Import('env testruns') dist_headers = [ "hammer.h", "allocator.h", + "export.h", "glue.h", "internal.h" ] diff --git a/src/bindings/python/setup.py b/src/bindings/python/setup.py index 308283c8..7e4e0445 100644 --- a/src/bindings/python/setup.py +++ b/src/bindings/python/setup.py @@ -21,6 +21,7 @@ '-I../../'], define_macros=[('SWIG', None)], depends=['allocator.h', + 'export.h', 'glue.h', 'hammer.h', 'internal.h',], From 4596ec609d384f17748e4d41cff8ada8ecd62004 Mon Sep 17 00:00:00 2001 From: Steven Dee Date: Thu, 11 Dec 2014 22:23:23 -0500 Subject: [PATCH 3/5] Remove trailing whitespace --- src/bindings/python/setup.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bindings/python/setup.py b/src/bindings/python/setup.py index 7e4e0445..4a5abd98 100644 --- a/src/bindings/python/setup.py +++ b/src/bindings/python/setup.py @@ -20,9 +20,9 @@ swig_opts=['-DHAMMER_INTERNAL__NO_STDARG_H', '-I../../'], define_macros=[('SWIG', None)], - depends=['allocator.h', + depends=['allocator.h', 'export.h', - 'glue.h', + 'glue.h', 'hammer.h', 'internal.h',], extra_compile_args=['-fPIC', @@ -30,7 +30,7 @@ include_dirs=['../../'], library_dirs=['../../'], libraries=['hammer'],)], - + py_modules=['hammer'], ) From 391958e6f376c89c28b39d0779bb2677cb5201d8 Mon Sep 17 00:00:00 2001 From: Steven Dee Date: Thu, 11 Dec 2014 22:36:42 -0500 Subject: [PATCH 4/5] also export allocator.h + ifndef guard on export.h --- src/allocator.h | 13 +++++++------ src/export.h | 5 +++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/allocator.h b/src/allocator.h index 803d89fe..d38d5f0c 100644 --- a/src/allocator.h +++ b/src/allocator.h @@ -18,6 +18,7 @@ #ifndef HAMMER_ALLOCATOR__H__ #define HAMMER_ALLOCATOR__H__ #include +#include "export.h" #ifdef __cplusplus extern "C" { @@ -32,21 +33,21 @@ typedef struct HAllocator_ { typedef struct HArena_ HArena ; // hidden implementation -HArena *h_new_arena(HAllocator* allocator, size_t block_size); // pass 0 for default... +H_EXPORT HArena *h_new_arena(HAllocator* allocator, size_t block_size); // pass 0 for default... #ifndef SWIG -void* h_arena_malloc(HArena *arena, size_t count) __attribute__(( malloc, alloc_size(2) )); +H_EXPORT void* h_arena_malloc(HArena *arena, size_t count) __attribute__(( malloc, alloc_size(2) )); #else -void* h_arena_malloc(HArena *arena, size_t count); +H_EXPORT void* h_arena_malloc(HArena *arena, size_t count); #endif -void h_arena_free(HArena *arena, void* ptr); // For future expansion, with alternate memory managers. -void h_delete_arena(HArena *arena); +H_EXPORT void h_arena_free(HArena *arena, void* ptr); // For future expansion, with alternate memory managers. +H_EXPORT void h_delete_arena(HArena *arena); typedef struct { size_t used; size_t wasted; } HArenaStats; -void h_allocator_stats(HArena *arena, HArenaStats *stats); +H_EXPORT void h_allocator_stats(HArena *arena, HArenaStats *stats); #ifdef __cplusplus } diff --git a/src/export.h b/src/export.h index 2e28aad8..64f44231 100644 --- a/src/export.h +++ b/src/export.h @@ -1,3 +1,6 @@ +#ifndef HAMMER_EXPORT__H__ +#define HAMMER_EXPORT__H__ + #if defined _MSC_VER # if defined BUILDING_HAMMER_DLL # define H_EXPORT __declspec(dllexport) @@ -15,3 +18,5 @@ # define H_EXPORT # endif #endif + +#endif // #ifndef HAMMER_EXPORT__H__ From cf7fbcb75cbeaa048666c0f646812cc46c20657f Mon Sep 17 00:00:00 2001 From: Steven Dee Date: Thu, 11 Dec 2014 22:37:01 -0500 Subject: [PATCH 5/5] Fix comment --- src/allocator.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/allocator.h b/src/allocator.h index d38d5f0c..39802b8b 100644 --- a/src/allocator.h +++ b/src/allocator.h @@ -53,4 +53,4 @@ H_EXPORT void h_allocator_stats(HArena *arena, HArenaStats *stats); } #endif -#endif // #ifndef LIB_ALLOCATOR__H__ +#endif // #ifndef HAMMER_ALLOCATOR__H__