From 9d7ad2a67f781a80cec5dc7b2cfd1f9ecff7724a Mon Sep 17 00:00:00 2001 From: lurchi Date: Sun, 21 Aug 2016 09:58:36 +0200 Subject: [PATCH] Switch static inline to inline The linker does not create symbols for static functions so Rust FFI can't find them. All static inline function have been changed to 'inline' functions with an 'extern inline' declaration in the corresponding .c files. Other than Rust FFI seeing the functions this has other advantages. See [1] for a nice explanation. [1] https://gustedt.wordpress.com/2010/11/29/myth-and-reality-about-inline-in-c99/ --- include/psyc/match.h | 2 +- include/psyc/packet.h | 6 +-- include/psyc/parse.h | 68 ++++++++++++------------- include/psyc/render.h | 4 +- include/psyc/text.h | 8 +-- include/psyc/uniform.h | 2 +- include/psyc/variable.h | 6 +-- src/match.c | 4 ++ src/packet.c | 11 +++++ src/parse.c | 107 ++++++++++++++++++++++++++++++++++++++-- src/render.c | 4 +- src/text.c | 18 +++++++ src/uniform.c | 3 ++ src/variable.c | 9 ++++ 14 files changed, 198 insertions(+), 54 deletions(-) diff --git a/include/psyc/match.h b/include/psyc/match.h index f94aad2..d4cc27e 100644 --- a/include/psyc/match.h +++ b/include/psyc/match.h @@ -64,7 +64,7 @@ psyc_map_lookup (const PsycMap *map, size_t size, * Look up value associated with a key in a map with integer values. * @see psyc_map_lookup */ -static inline intptr_t +inline intptr_t psyc_map_lookup_int (const PsycMapInt *map, size_t size, const char *key, size_t keylen, PsycBool inherit) { diff --git a/include/psyc/packet.h b/include/psyc/packet.h index a319196..311d96d 100644 --- a/include/psyc/packet.h +++ b/include/psyc/packet.h @@ -249,7 +249,7 @@ typedef struct { /** * Return the number of digits a number has in its base 10 representation. */ -static inline size_t +inline size_t psyc_num_length (size_t n) { return n < 10 ? 1 : log10(n) + 1; @@ -259,7 +259,7 @@ psyc_num_length (size_t n) * \internal * Check if a modifier needs length. */ -static inline PsycModifierFlag +inline PsycModifierFlag psyc_modifier_length_check (PsycModifier *m) { if (m->value.length > 0 @@ -271,7 +271,7 @@ psyc_modifier_length_check (PsycModifier *m) } /** Initialize modifier */ -static inline void +inline void psyc_modifier_init (PsycModifier *m, PsycOperator oper, char *name, size_t namelen, char *value, size_t valuelen, PsycModifierFlag flag) diff --git a/include/psyc/parse.h b/include/psyc/parse.h index 0ae1ca8..7c71d8a 100644 --- a/include/psyc/parse.h +++ b/include/psyc/parse.h @@ -509,7 +509,7 @@ typedef struct { * @param flags Flags to be set for the parser, see PsycParseFlag. * @see PsycParseFlag */ -static inline void +inline void psyc_parse_state_init (PsycParseState *state, uint8_t flags) { memset(state, 0, sizeof(PsycParseState)); @@ -530,7 +530,7 @@ psyc_parse_state_init (PsycParseState *state, uint8_t flags) * @param length length of the data in bytes * @see PsycString */ -static inline void +inline void psyc_parse_buffer_set (PsycParseState *state, const char *buffer, size_t length) { state->buffer = (PsycString) {length, (char*)buffer}; @@ -545,7 +545,7 @@ psyc_parse_buffer_set (PsycParseState *state, const char *buffer, size_t length) /** * Initializes the list parser state. */ -static inline void +inline void psyc_parse_list_state_init (PsycParseListState *state) { memset(state, 0, sizeof(PsycParseListState)); @@ -554,7 +554,7 @@ psyc_parse_list_state_init (PsycParseListState *state) /** * Sets a new buffer in the list parser state struct with data to be parsed. */ -static inline void +inline void psyc_parse_list_buffer_set (PsycParseListState *state, const char *buffer, size_t length) { @@ -565,7 +565,7 @@ psyc_parse_list_buffer_set (PsycParseListState *state, /** * Initializes the dict parser state. */ -static inline void +inline void psyc_parse_dict_state_init (PsycParseDictState *state) { memset(state, 0, sizeof(PsycParseDictState)); @@ -574,7 +574,7 @@ psyc_parse_dict_state_init (PsycParseDictState *state) /** * Sets a new buffer in the dict parser state struct with data to be parsed. */ -static inline void +inline void psyc_parse_dict_buffer_set (PsycParseDictState *state, const char *buffer, size_t length) { @@ -585,7 +585,7 @@ psyc_parse_dict_buffer_set (PsycParseDictState *state, /** * Initializes the index parser state. */ -static inline void +inline void psyc_parse_index_state_init (PsycParseIndexState *state) { memset(state, 0, sizeof(PsycParseIndexState)); @@ -594,7 +594,7 @@ psyc_parse_index_state_init (PsycParseIndexState *state) /** * Sets a new buffer in the index parser state struct with data to be parsed. */ -static inline void +inline void psyc_parse_index_buffer_set (PsycParseIndexState *state, const char *buffer, size_t length) { @@ -605,7 +605,7 @@ psyc_parse_index_buffer_set (PsycParseIndexState *state, /** * Initializes the update modifier parser state. */ -static inline void +inline void psyc_parse_update_state_init (PsycParseUpdateState *state) { memset(state, 0, sizeof(PsycParseUpdateState)); @@ -614,7 +614,7 @@ psyc_parse_update_state_init (PsycParseUpdateState *state) /** * Sets a new buffer in the update modifier parser state struct with data to be parsed. */ -static inline void +inline void psyc_parse_update_buffer_set (PsycParseUpdateState *state, const char *buffer, size_t length) { @@ -622,49 +622,49 @@ psyc_parse_update_buffer_set (PsycParseUpdateState *state, state->cursor = 0; } -static inline size_t +inline size_t psyc_parse_content_length (PsycParseState *state) { return state->contentlen; } -static inline PsycBool +inline PsycBool psyc_parse_content_length_found (PsycParseState *state) { return (PsycBool) state->contentlen_found; } -static inline size_t +inline size_t psyc_parse_value_length (PsycParseState *state) { return state->valuelen; } -static inline PsycBool +inline PsycBool psyc_parse_value_length_found (PsycParseState *state) { return (PsycBool) state->valuelen_found; } -static inline size_t +inline size_t psyc_parse_cursor (PsycParseState *state) { return state->cursor; } -static inline size_t +inline size_t psyc_parse_buffer_length (PsycParseState *state) { return state->buffer.length; } -static inline size_t +inline size_t psyc_parse_remaining_length (PsycParseState *state) { return state->buffer.length - state->cursor; } -static inline const char * +inline const char * psyc_parse_remaining_buffer (PsycParseState *state) { return state->buffer.data + state->cursor; @@ -687,7 +687,7 @@ psyc_parse_remaining_buffer (PsycParseState *state) * in case of the body it will point to the data. */ #ifdef __INLINE_PSYC_PARSE -static inline +inline #endif PsycParseRC psyc_parse (PsycParseState *state, char *oper, @@ -705,30 +705,30 @@ psyc_parse (PsycParseState *state, char *oper, * @param elem It will point to the next element in the list. */ #ifdef __INLINE_PSYC_PARSE -static inline +inline #endif PsycParseListRC psyc_parse_list (PsycParseListState *state, PsycString *type, PsycString *elem); #ifdef __INLINE_PSYC_PARSE -static inline +inline #endif PsycParseDictRC psyc_parse_dict (PsycParseDictState *state, PsycString *type, PsycString *elem); #ifdef __INLINE_PSYC_PARSE -static inline +inline #endif PsycParseIndexRC psyc_parse_index (PsycParseIndexState *state, PsycString *idx); #ifdef __INLINE_PSYC_PARSE -static inline +inline #endif PsycParseUpdateRC psyc_parse_update (PsycParseUpdateState *state, char *oper, PsycString *value); -static inline size_t +inline size_t psyc_parse_int (const char *value, size_t len, int64_t *n) { size_t c = 0; @@ -753,7 +753,7 @@ psyc_parse_int (const char *value, size_t len, int64_t *n) return c; } -static inline size_t +inline size_t psyc_parse_uint (const char *value, size_t len, uint64_t *n) { size_t c = 0; @@ -767,7 +767,7 @@ psyc_parse_uint (const char *value, size_t len, uint64_t *n) return c; } -static inline size_t +inline size_t psyc_parse_list_index (const char *value, size_t len, int64_t *n) { if (!value || len == 0 || value[0] != '#') @@ -779,7 +779,7 @@ psyc_parse_list_index (const char *value, size_t len, int64_t *n) * Determines if the argument is a glyph. * Glyphs are: : = + - ? ! */ -static inline PsycBool +inline PsycBool psyc_is_oper (char g) { switch (g) { @@ -799,7 +799,7 @@ psyc_is_oper (char g) /** * Determines if the argument is numeric. */ -static inline char +inline char psyc_is_numeric (char c) { return c >= '0' && c <= '9'; @@ -808,7 +808,7 @@ psyc_is_numeric (char c) /** * Determines if the argument is alphabetic. */ -static inline char +inline char psyc_is_alpha (char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); @@ -817,7 +817,7 @@ psyc_is_alpha (char c) /** * Determines if the argument is alphanumeric. */ -static inline char +inline char psyc_is_alpha_numeric (char c) { return psyc_is_alpha(c) || psyc_is_numeric(c); @@ -827,7 +827,7 @@ psyc_is_alpha_numeric (char c) * Determines if the argument is a keyword character. * Keyword characters are: alphanumeric and _ */ -static inline char +inline char psyc_is_kw_char (char c) { return psyc_is_alpha_numeric(c) || c == '_'; @@ -837,7 +837,7 @@ psyc_is_kw_char (char c) * Determines if the argument is a name character. * Name characters are: see opaque_part in RFC 2396 */ -static inline char +inline char psyc_is_name_char (char c) { return psyc_is_alpha(c) || (c >= '$' && c <= ';') @@ -848,7 +848,7 @@ psyc_is_name_char (char c) * Determines if the argument is a hostname character. * Hostname characters are: alphanumeric and - */ -static inline char +inline char psyc_is_host_char (char c) { return psyc_is_alpha_numeric(c) || c == '.' || c == '-'; @@ -859,7 +859,7 @@ psyc_is_host_char (char c) * It should contain one or more keyword characters. * @return Number of characters parsed. */ -static inline size_t +inline size_t psyc_parse_keyword (const char *data, size_t len) { size_t c = 0; diff --git a/include/psyc/render.h b/include/psyc/render.h index 5226e68..621ea9a 100644 --- a/include/psyc/render.h +++ b/include/psyc/render.h @@ -66,7 +66,7 @@ typedef enum { * @see psyc_packet_length_set() */ #ifdef __INLINE_PSYC_RENDER -static inline +inline #endif PsycRenderRC psyc_render (PsycPacket *packet, char *buffer, size_t buflen); @@ -84,7 +84,7 @@ psyc_render_dict_key (PsycDictKey *elem, char *buffer, size_t buflen); * Render a PSYC list into a buffer. */ #ifdef __INLINE_PSYC_RENDER -static inline +inline #endif PsycRenderRC psyc_render_list (PsycList *list, char *buffer, size_t buflen); diff --git a/include/psyc/text.h b/include/psyc/text.h index 72475e6..fd1d64a 100644 --- a/include/psyc/text.h +++ b/include/psyc/text.h @@ -94,7 +94,7 @@ typedef PsycTextValueRC (*PsycTextCB) (void *cls, const char *name, size_t namel * @param buffer Output buffer where the rendered text is going to be written. * @param buflen Length of output buffer. */ -static inline void +inline void psyc_text_state_init (PsycTextState *state, char *tmpl, size_t tmplen, char *buffer, size_t buflen) @@ -120,7 +120,7 @@ psyc_text_state_init (PsycTextState *state, * @param clo Closing brace. * @param clolen Length of closing brace. */ -static inline void +inline void psyc_text_state_init_custom (PsycTextState *state, char *tmpl, size_t tmplen, char *buffer, size_t buflen, @@ -138,7 +138,7 @@ psyc_text_state_init_custom (PsycTextState *state, /** * Sets a new output buffer in the PSYC text state struct. */ -static inline void +inline void psyc_text_buffer_set (PsycTextState *state, char *buffer, size_t length) { state->buffer = (PsycString) { @@ -146,7 +146,7 @@ psyc_text_buffer_set (PsycTextState *state, char *buffer, size_t length) state->written = 0; } -static inline size_t +inline size_t psyc_text_bytes_written (PsycTextState *state) { return state->written; diff --git a/include/psyc/uniform.h b/include/psyc/uniform.h index 80488cf..d8c319e 100644 --- a/include/psyc/uniform.h +++ b/include/psyc/uniform.h @@ -112,7 +112,7 @@ psyc_uniform_parse (PsycUniform *uni, const char *buffer, size_t length); * * @return PsycEntityType on success, PSYC_ERROR on error. */ -static inline int +inline int psyc_entity_type (char entity) { switch (entity) { diff --git a/include/psyc/variable.h b/include/psyc/variable.h index 38981ba..a818bda 100644 --- a/include/psyc/variable.h +++ b/include/psyc/variable.h @@ -88,7 +88,7 @@ typedef enum { /** * Look up routing variable. */ -static inline PsycRoutingVar +inline PsycRoutingVar psyc_var_routing (const char *name, size_t len) { return (PsycRoutingVar) @@ -99,7 +99,7 @@ psyc_var_routing (const char *name, size_t len) /** * Get the type of variable name. */ -static inline PsycType +inline PsycType psyc_var_type (const char *name, size_t len) { return (PsycType) @@ -110,7 +110,7 @@ psyc_var_type (const char *name, size_t len) /** * Is this a list variable name? */ -static inline PsycBool +inline PsycBool psyc_var_is_list (const char *name, size_t len) { return len < 5 || memcmp(name, "_list", 5) != 0 || (len > 5 && name[5] != '_') diff --git a/src/match.c b/src/match.c index 4eb9f41..005e61e 100644 --- a/src/match.c +++ b/src/match.c @@ -166,6 +166,10 @@ psyc_map_lookup(const PsycMap * map, size_t size, return NULL; } +extern inline intptr_t +psyc_map_lookup_int (const PsycMapInt *map, size_t size, + const char *key, size_t keylen, PsycBool inherit); + #ifdef CMDTOOL int main(int argc, char **argv) diff --git a/src/packet.c b/src/packet.c index db2ac6c..1a9f870 100644 --- a/src/packet.c +++ b/src/packet.c @@ -21,6 +21,17 @@ #include "lib.h" #include +extern inline size_t +psyc_num_length (size_t n); + +extern inline PsycModifierFlag +psyc_modifier_length_check (PsycModifier *m); + +extern inline void +psyc_modifier_init (PsycModifier *m, PsycOperator oper, + char *name, size_t namelen, + char *value, size_t valuelen, PsycModifierFlag flag); + inline PsycElemFlag psyc_elem_length_check (PsycString *value, const char end) { diff --git a/src/parse.c b/src/parse.c index 8534465..0f89051 100644 --- a/src/parse.c +++ b/src/parse.c @@ -53,6 +53,65 @@ typedef struct { size_t startc; } ParseState; +extern inline void +psyc_parse_state_init (PsycParseState *state, uint8_t flags); + +extern inline void +psyc_parse_buffer_set (PsycParseState *state, const char *buffer, size_t length); + +extern inline void +psyc_parse_list_state_init (PsycParseListState *state); + +extern inline void +psyc_parse_list_buffer_set (PsycParseListState *state, + const char *buffer, size_t length); + +extern inline void +psyc_parse_dict_state_init (PsycParseDictState *state); + +extern inline void +psyc_parse_dict_buffer_set (PsycParseDictState *state, + const char *buffer, size_t length); + +extern inline void +psyc_parse_index_state_init (PsycParseIndexState *state); + +extern inline void +psyc_parse_index_buffer_set (PsycParseIndexState *state, + const char *buffer, size_t length); + +extern inline void +psyc_parse_update_state_init (PsycParseUpdateState *state); + +extern inline void +psyc_parse_update_buffer_set (PsycParseUpdateState *state, + const char *buffer, size_t length); + +extern inline size_t +psyc_parse_content_length (PsycParseState *state); + +extern inline PsycBool +psyc_parse_content_length_found (PsycParseState *state); + +extern inline size_t +psyc_parse_value_length (PsycParseState *state); + +extern inline PsycBool +psyc_parse_value_length_found (PsycParseState *state); + +extern inline size_t +psyc_parse_cursor (PsycParseState *state); + +extern inline size_t +psyc_parse_buffer_length (PsycParseState *state); + +extern inline size_t +psyc_parse_remaining_length (PsycParseState *state); + +extern inline const char * +psyc_parse_remaining_buffer (PsycParseState *state); + + /** * Parse variable name or method name. * @@ -155,7 +214,10 @@ parse_until (ParseState *state, const char end, PsycString *value) * Parse simple or binary variable. * @return PARSE_ERROR or PARSE_SUCCESS */ -static inline ParseRC +#ifdef __INLINE_PSYC_PARSE +extern inline +#endif +ParseRC psyc_parse_modifier (PsycParseState *state, char *oper, PsycString *name, PsycString *value) { @@ -504,7 +566,7 @@ psyc_parse (PsycParseState *state, char *oper, * list-value = %x00-7B / %x7D-FF ; any byte except "|" */ #ifdef __INLINE_PSYC_PARSE -static inline +extern inline #endif PsycParseListRC psyc_parse_list (PsycParseListState *state, PsycString *type, PsycString *elem) @@ -664,6 +726,9 @@ psyc_parse_list (PsycParseListState *state, PsycString *type, PsycString *elem) * dict-key = %x00-7C / %x7E-FF ; any byte except "{" * dict-value = %x00-7A / %x7C-FF ; any byte except "}" */ +#ifdef __INLINE_PSYC_PARSE +extern inline +#endif PsycParseDictRC psyc_parse_dict (PsycParseDictState *state, PsycString *type, PsycString *elem) { @@ -886,7 +951,7 @@ psyc_parse_dict (PsycParseDictState *state, PsycString *type, PsycString *elem) } #ifdef __INLINE_PSYC_PARSE -static inline +extern inline #endif PsycParseIndexRC psyc_parse_index (PsycParseIndexState *state, PsycString *idx) @@ -1014,7 +1079,7 @@ psyc_parse_index (PsycParseIndexState *state, PsycString *idx) } #ifdef __INLINE_PSYC_PARSE -static inline +extern inline #endif PsycParseUpdateRC psyc_parse_update (PsycParseUpdateState *state, char *oper, PsycString *value) @@ -1146,3 +1211,37 @@ psyc_parse_update (PsycParseUpdateState *state, char *oper, PsycString *value) return PSYC_PARSE_INDEX_ERROR; // should not be reached } + +extern inline size_t +psyc_parse_int (const char *value, size_t len, int64_t *n); + +extern inline size_t +psyc_parse_uint (const char *value, size_t len, uint64_t *n); + +extern inline size_t +psyc_parse_list_index (const char *value, size_t len, int64_t *n); + +extern inline PsycBool +psyc_is_oper (char g); + +extern inline char +psyc_is_numeric (char c); + +extern inline char +psyc_is_alpha (char c); + +extern inline char +psyc_is_alpha_numeric (char c); + +extern inline char +psyc_is_kw_char (char c); + +extern inline char +psyc_is_name_char (char c); + +extern inline char +psyc_is_host_char (char c); + +extern inline size_t +psyc_parse_keyword (const char *data, size_t len); + diff --git a/src/render.c b/src/render.c index c77a7ed..6edde8b 100644 --- a/src/render.c +++ b/src/render.c @@ -78,7 +78,7 @@ psyc_render_dict_key (PsycDictKey *elem, char *buffer, size_t buflen) } #ifdef __INLINE_PSYC_RENDER -static inline +extern inline #endif PsycRenderRC psyc_render_list (PsycList *list, char *buffer, size_t buflen) @@ -162,7 +162,7 @@ psyc_render_modifier (PsycModifier *mod, char *buffer) } #ifdef __INLINE_PSYC_RENDER -static inline +extern inline #endif PsycRenderRC psyc_render (PsycPacket *p, char *buffer, size_t buflen) diff --git a/src/text.c b/src/text.c index cd90ae6..9267253 100644 --- a/src/text.c +++ b/src/text.c @@ -25,6 +25,24 @@ const PsycTemplates psyc_templates = { .s = { #include "templates.h" }}; +extern inline void +psyc_text_state_init (PsycTextState *state, + char *tmpl, size_t tmplen, + char *buffer, size_t buflen); + +extern inline void +psyc_text_state_init_custom (PsycTextState *state, + char *tmpl, size_t tmplen, + char *buffer, size_t buflen, + char *ope, size_t opelen, + char *clo, size_t clolen); + +extern inline void +psyc_text_buffer_set (PsycTextState *state, char *buffer, size_t length); + +extern inline size_t +psyc_text_bytes_written (PsycTextState *state); + PsycTextRC psyc_text (PsycTextState *state, PsycTextCB get_value, void *get_value_cls) { diff --git a/src/uniform.c b/src/uniform.c index 67a3621..75c617c 100644 --- a/src/uniform.c +++ b/src/uniform.c @@ -195,3 +195,6 @@ psyc_uniform_parse (PsycUniform *uni, const char *buffer, size_t length) uni->valid = 1; return uni->type; } + +extern inline int +psyc_entity_type (char entity); diff --git a/src/variable.c b/src/variable.c index c34473b..1e72ce5 100644 --- a/src/variable.c +++ b/src/variable.c @@ -104,6 +104,15 @@ const PsycMapInt psyc_methods[] = { }; const size_t psyc_methods_num = PSYC_NUM_ELEM(psyc_methods); +extern inline PsycRoutingVar +psyc_var_routing (const char *name, size_t len); + +extern inline PsycType +psyc_var_type (const char *name, size_t len); + +extern inline PsycBool +psyc_var_is_list (const char *name, size_t len); + /** * Get the method, its family and its flags. */