From a2524602d5dedd78579b92d883f610a9ae5c5287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Fri, 3 May 2024 22:01:58 +0200 Subject: [PATCH 01/15] croco: Use g_try_malloc0 instead of g_try_malloc + memset This is just safer to use and internally use calloc so there are less calls to perform at our level and calloc can be quite optimized to avoid memset at all. --- src/st/croco/cr-additional-sel.c | 4 +--- src/st/croco/cr-cascade.c | 6 ++---- src/st/croco/cr-declaration.c | 3 +-- src/st/croco/cr-doc-handler.c | 7 ++----- src/st/croco/cr-fonts.c | 9 +++------ src/st/croco/cr-input.c | 6 ++---- src/st/croco/cr-num.c | 4 +--- src/st/croco/cr-om-parser.c | 7 ++----- src/st/croco/cr-parser.c | 4 +--- src/st/croco/cr-prop-list.c | 6 ++---- src/st/croco/cr-rgb.c | 4 +--- src/st/croco/cr-selector.c | 3 +-- src/st/croco/cr-simple-sel.c | 3 +-- src/st/croco/cr-statement.c | 32 +++++++++++--------------------- src/st/croco/cr-string.c | 3 +-- src/st/croco/cr-stylesheet.c | 4 +--- src/st/croco/cr-term.c | 3 +-- src/st/croco/cr-tknzr.c | 7 ++----- src/st/croco/cr-token.c | 4 +--- 19 files changed, 37 insertions(+), 82 deletions(-) diff --git a/src/st/croco/cr-additional-sel.c b/src/st/croco/cr-additional-sel.c index 9bd8d6a7d..70c719e39 100644 --- a/src/st/croco/cr-additional-sel.c +++ b/src/st/croco/cr-additional-sel.c @@ -47,15 +47,13 @@ cr_additional_sel_new (void) { CRAdditionalSel *result = NULL; - result = g_try_malloc (sizeof (CRAdditionalSel)); + result = g_try_malloc0 (sizeof (CRAdditionalSel)); if (result == NULL) { cr_utils_trace_debug ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRAdditionalSel)); - return result; } diff --git a/src/st/croco/cr-cascade.c b/src/st/croco/cr-cascade.c index 68f59bb2d..2b207f9df 100644 --- a/src/st/croco/cr-cascade.c +++ b/src/st/croco/cr-cascade.c @@ -66,20 +66,18 @@ cr_cascade_new (CRStyleSheet * a_author_sheet, { CRCascade *result = NULL; - result = g_try_malloc (sizeof (CRCascade)); + result = g_try_malloc0 (sizeof (CRCascade)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRCascade)); - PRIVATE (result) = g_try_malloc (sizeof (CRCascadePriv)); + PRIVATE (result) = g_try_malloc0 (sizeof (CRCascadePriv)); if (!PRIVATE (result)) { cr_utils_trace_info ("Out of memory"); g_free (result); return NULL; } - memset (PRIVATE (result), 0, sizeof (CRCascadePriv)); if (a_author_sheet) { cr_cascade_set_sheet (result, a_author_sheet, ORIGIN_AUTHOR); diff --git a/src/st/croco/cr-declaration.c b/src/st/croco/cr-declaration.c index 6c70128a2..3aba74d0b 100644 --- a/src/st/croco/cr-declaration.c +++ b/src/st/croco/cr-declaration.c @@ -86,12 +86,11 @@ cr_declaration_new (CRStatement * a_statement, || (a_statement->type == AT_PAGE_RULE_STMT)), NULL); - result = g_try_malloc (sizeof (CRDeclaration)); + result = g_try_malloc0 (sizeof (CRDeclaration)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRDeclaration)); result->property = a_property; result->value = a_value; diff --git a/src/st/croco/cr-doc-handler.c b/src/st/croco/cr-doc-handler.c index b0ef13c07..0bdfe36fe 100644 --- a/src/st/croco/cr-doc-handler.c +++ b/src/st/croco/cr-doc-handler.c @@ -71,22 +71,19 @@ cr_doc_handler_new (void) { CRDocHandler *result = NULL; - result = g_try_malloc (sizeof (CRDocHandler)); + result = g_try_malloc0 (sizeof (CRDocHandler)); g_return_val_if_fail (result, NULL); - memset (result, 0, sizeof (CRDocHandler)); result->ref_count++; - result->priv = g_try_malloc (sizeof (CRDocHandlerPriv)); + result->priv = g_try_malloc0 (sizeof (CRDocHandlerPriv)); if (!result->priv) { cr_utils_trace_info ("Out of memory exception"); g_free (result); return NULL; } - cr_doc_handler_set_default_sac_handler (result); - return result; } diff --git a/src/st/croco/cr-fonts.c b/src/st/croco/cr-fonts.c index a64ffc045..1bd204437 100644 --- a/src/st/croco/cr-fonts.c +++ b/src/st/croco/cr-fonts.c @@ -155,14 +155,13 @@ cr_font_family_new (enum CRFontFamilyType a_type, guchar * a_name) { CRFontFamily *result = NULL; - result = g_try_malloc (sizeof (CRFontFamily)); + result = g_try_malloc0 (sizeof (CRFontFamily)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRFontFamily)); result->type = a_type; cr_font_family_set_name (result, a_name); @@ -335,12 +334,11 @@ cr_font_size_new (void) { CRFontSize *result = NULL; - result = g_try_malloc (sizeof (CRFontSize)); + result = g_try_malloc0 (sizeof (CRFontSize)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRFontSize)); return result; } @@ -921,12 +919,11 @@ cr_font_size_adjust_new (void) { CRFontSizeAdjust *result = NULL; - result = g_try_malloc (sizeof (CRFontSizeAdjust)); + result = g_try_malloc0 (sizeof (CRFontSizeAdjust)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRFontSizeAdjust)); return result; } diff --git a/src/st/croco/cr-input.c b/src/st/croco/cr-input.c index 430e75ed8..470780992 100644 --- a/src/st/croco/cr-input.c +++ b/src/st/croco/cr-input.c @@ -90,20 +90,18 @@ cr_input_new_real (void) { CRInput *result = NULL; - result = g_try_malloc (sizeof (CRInput)); + result = g_try_malloc0 (sizeof (CRInput)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRInput)); - PRIVATE (result) = g_try_malloc (sizeof (CRInputPriv)); + PRIVATE (result) = g_try_malloc0 (sizeof (CRInputPriv)); if (!PRIVATE (result)) { cr_utils_trace_info ("Out of memory"); g_free (result); return NULL; } - memset (PRIVATE (result), 0, sizeof (CRInputPriv)); PRIVATE (result)->free_in_buf = TRUE; return result; } diff --git a/src/st/croco/cr-num.c b/src/st/croco/cr-num.c index ba17285b2..b46f7e9b6 100644 --- a/src/st/croco/cr-num.c +++ b/src/st/croco/cr-num.c @@ -45,15 +45,13 @@ cr_num_new (void) { CRNum *result = NULL; - result = g_try_malloc (sizeof (CRNum)); + result = g_try_malloc0 (sizeof (CRNum)); if (result == NULL) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRNum)); - return result; } diff --git a/src/st/croco/cr-om-parser.c b/src/st/croco/cr-om-parser.c index 90f710621..eb870ae99 100644 --- a/src/st/croco/cr-om-parser.c +++ b/src/st/croco/cr-om-parser.c @@ -795,23 +795,20 @@ cr_om_parser_new (CRInput * a_input) CROMParser *result = NULL; enum CRStatus status = CR_OK; - result = g_try_malloc (sizeof (CROMParser)); + result = g_try_malloc0 (sizeof (CROMParser)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CROMParser)); - PRIVATE (result) = g_try_malloc (sizeof (CROMParserPriv)); + PRIVATE (result) = g_try_malloc0 (sizeof (CROMParserPriv)); if (!PRIVATE (result)) { cr_utils_trace_info ("Out of memory"); goto error; } - memset (PRIVATE (result), 0, sizeof (CROMParserPriv)); - PRIVATE (result)->parser = cr_parser_new_from_input (a_input); if (!PRIVATE (result)->parser) { diff --git a/src/st/croco/cr-parser.c b/src/st/croco/cr-parser.c index d4f40cf1d..15c51e4a7 100644 --- a/src/st/croco/cr-parser.c +++ b/src/st/croco/cr-parser.c @@ -418,15 +418,13 @@ cr_parser_error_new (const guchar * a_msg, enum CRStatus a_status) { CRParserError *result = NULL; - result = g_try_malloc (sizeof (CRParserError)); + result = g_try_malloc0 (sizeof (CRParserError)); if (result == NULL) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRParserError)); - cr_parser_error_set_msg (result, a_msg); cr_parser_error_set_status (result, a_status); diff --git a/src/st/croco/cr-prop-list.c b/src/st/croco/cr-prop-list.c index 03c447822..b329c720b 100644 --- a/src/st/croco/cr-prop-list.c +++ b/src/st/croco/cr-prop-list.c @@ -43,19 +43,17 @@ cr_prop_list_allocate (void) { CRPropList *result = NULL; - result = g_try_malloc (sizeof (CRPropList)); + result = g_try_malloc0 (sizeof (CRPropList)); if (!result) { cr_utils_trace_info ("could not allocate CRPropList"); return NULL; } - memset (result, 0, sizeof (CRPropList)); - PRIVATE (result) = g_try_malloc (sizeof (CRPropListPriv)); + PRIVATE (result) = g_try_malloc0 (sizeof (CRPropListPriv)); if (!result) { cr_utils_trace_info ("could not allocate CRPropListPriv"); g_free (result); return NULL; } - memset (PRIVATE (result), 0, sizeof (CRPropListPriv)); return result; } diff --git a/src/st/croco/cr-rgb.c b/src/st/croco/cr-rgb.c index a2b478f7c..0b62f097d 100644 --- a/src/st/croco/cr-rgb.c +++ b/src/st/croco/cr-rgb.c @@ -192,15 +192,13 @@ cr_rgb_new (void) { CRRgb *result = NULL; - result = g_try_malloc (sizeof (CRRgb)); + result = g_try_malloc0 (sizeof (CRRgb)); if (result == NULL) { cr_utils_trace_info ("No more memory"); return NULL; } - memset (result, 0, sizeof (CRRgb)); - return result; } diff --git a/src/st/croco/cr-selector.c b/src/st/croco/cr-selector.c index c9aad4327..7a346f139 100644 --- a/src/st/croco/cr-selector.c +++ b/src/st/croco/cr-selector.c @@ -40,12 +40,11 @@ cr_selector_new (CRSimpleSel * a_simple_sel) { CRSelector *result = NULL; - result = g_try_malloc (sizeof (CRSelector)); + result = g_try_malloc0 (sizeof (CRSelector)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRSelector)); result->simple_sel = a_simple_sel; return result; } diff --git a/src/st/croco/cr-simple-sel.c b/src/st/croco/cr-simple-sel.c index bac862187..90796aaeb 100644 --- a/src/st/croco/cr-simple-sel.c +++ b/src/st/croco/cr-simple-sel.c @@ -37,12 +37,11 @@ cr_simple_sel_new (void) { CRSimpleSel *result = NULL; - result = g_try_malloc (sizeof (CRSimpleSel)); + result = g_try_malloc0 (sizeof (CRSimpleSel)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRSimpleSel)); return result; } diff --git a/src/st/croco/cr-statement.c b/src/st/croco/cr-statement.c index eaeb49f35..0bb2a97f1 100644 --- a/src/st/croco/cr-statement.c +++ b/src/st/croco/cr-statement.c @@ -1124,7 +1124,7 @@ cr_statement_new_ruleset (CRStyleSheet * a_sheet, NULL); } - result = g_try_malloc (sizeof (CRStatement)); + result = g_try_malloc0 (sizeof (CRStatement)); if (!result) { cr_utils_trace_info ("Out of memory"); @@ -1133,7 +1133,7 @@ cr_statement_new_ruleset (CRStyleSheet * a_sheet, memset (result, 0, sizeof (CRStatement)); result->type = RULESET_STMT; - result->kind.ruleset = g_try_malloc (sizeof (CRRuleSet)); + result->kind.ruleset = g_try_malloc0 (sizeof (CRRuleSet)); if (!result->kind.ruleset) { cr_utils_trace_info ("Out of memory"); @@ -1142,7 +1142,6 @@ cr_statement_new_ruleset (CRStyleSheet * a_sheet, return NULL; } - memset (result->kind.ruleset, 0, sizeof (CRRuleSet)); result->kind.ruleset->sel_list = a_sel_list; if (a_sel_list) cr_selector_ref (a_sel_list); @@ -1259,23 +1258,21 @@ cr_statement_new_at_media_rule (CRStyleSheet * a_sheet, if (a_rulesets) g_return_val_if_fail (a_rulesets->type == RULESET_STMT, NULL); - result = g_try_malloc (sizeof (CRStatement)); + result = g_try_malloc0 (sizeof (CRStatement)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRStatement)); result->type = AT_MEDIA_RULE_STMT; - result->kind.media_rule = g_try_malloc (sizeof (CRAtMediaRule)); + result->kind.media_rule = g_try_malloc0 (sizeof (CRAtMediaRule)); if (!result->kind.media_rule) { cr_utils_trace_info ("Out of memory"); g_free (result); return NULL; } - memset (result->kind.media_rule, 0, sizeof (CRAtMediaRule)); result->kind.media_rule->rulesets = a_rulesets; for (cur = a_rulesets; cur; cur = cur->next) { if (cur->type != RULESET_STMT || !cur->kind.ruleset) { @@ -1318,17 +1315,16 @@ cr_statement_new_at_import_rule (CRStyleSheet * a_container_sheet, { CRStatement *result = NULL; - result = g_try_malloc (sizeof (CRStatement)); + result = g_try_malloc0 (sizeof (CRStatement)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRStatement)); result->type = AT_IMPORT_RULE_STMT; - result->kind.import_rule = g_try_malloc (sizeof (CRAtImportRule)); + result->kind.import_rule = g_try_malloc0 (sizeof (CRAtImportRule)); if (!result->kind.import_rule) { cr_utils_trace_info ("Out of memory"); @@ -1336,7 +1332,6 @@ cr_statement_new_at_import_rule (CRStyleSheet * a_container_sheet, return NULL; } - memset (result->kind.import_rule, 0, sizeof (CRAtImportRule)); result->kind.import_rule->url = a_url; result->kind.import_rule->media_list = a_media_list; result->kind.import_rule->sheet = a_imported_sheet; @@ -1441,17 +1436,16 @@ cr_statement_new_at_page_rule (CRStyleSheet * a_sheet, { CRStatement *result = NULL; - result = g_try_malloc (sizeof (CRStatement)); + result = g_try_malloc0 (sizeof (CRStatement)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRStatement)); result->type = AT_PAGE_RULE_STMT; - result->kind.page_rule = g_try_malloc (sizeof (CRAtPageRule)); + result->kind.page_rule = g_try_malloc0 (sizeof (CRAtPageRule)); if (!result->kind.page_rule) { cr_utils_trace_info ("Out of memory"); @@ -1459,7 +1453,6 @@ cr_statement_new_at_page_rule (CRStyleSheet * a_sheet, return NULL; } - memset (result->kind.page_rule, 0, sizeof (CRAtPageRule)); if (a_decl_list) { result->kind.page_rule->decl_list = a_decl_list; cr_declaration_ref (a_decl_list); @@ -1566,24 +1559,22 @@ cr_statement_new_at_charset_rule (CRStyleSheet * a_sheet, g_return_val_if_fail (a_charset, NULL); - result = g_try_malloc (sizeof (CRStatement)); + result = g_try_malloc0 (sizeof (CRStatement)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRStatement)); result->type = AT_CHARSET_RULE_STMT; - result->kind.charset_rule = g_try_malloc (sizeof (CRAtCharsetRule)); + result->kind.charset_rule = g_try_malloc0 (sizeof (CRAtCharsetRule)); if (!result->kind.charset_rule) { cr_utils_trace_info ("Out of memory"); g_free (result); return NULL; } - memset (result->kind.charset_rule, 0, sizeof (CRAtCharsetRule)); result->kind.charset_rule->charset = a_charset; cr_statement_set_parent_sheet (result, a_sheet); @@ -1660,13 +1651,12 @@ cr_statement_new_at_font_face_rule (CRStyleSheet * a_sheet, { CRStatement *result = NULL; - result = g_try_malloc (sizeof (CRStatement)); + result = g_try_malloc0 (sizeof (CRStatement)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRStatement)); result->type = AT_FONT_FACE_RULE_STMT; result->kind.font_face_rule = g_try_malloc diff --git a/src/st/croco/cr-string.c b/src/st/croco/cr-string.c index 6a1667627..dbdacd359 100644 --- a/src/st/croco/cr-string.c +++ b/src/st/croco/cr-string.c @@ -34,12 +34,11 @@ cr_string_new (void) { CRString *result = NULL ; - result = g_try_malloc (sizeof (CRString)) ; + result = g_try_malloc0 (sizeof (CRString)) ; if (!result) { cr_utils_trace_info ("Out of memory") ; return NULL ; } - memset (result, 0, sizeof (CRString)) ; result->stryng = g_string_new (NULL) ; return result ; } diff --git a/src/st/croco/cr-stylesheet.c b/src/st/croco/cr-stylesheet.c index 63e763feb..8c120b2bf 100644 --- a/src/st/croco/cr-stylesheet.c +++ b/src/st/croco/cr-stylesheet.c @@ -38,14 +38,12 @@ cr_stylesheet_new (CRStatement * a_stmts) { CRStyleSheet *result; - result = g_try_malloc (sizeof (CRStyleSheet)); + result = g_try_malloc0 (sizeof (CRStyleSheet)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRStyleSheet)); - if (a_stmts) result->statements = a_stmts; diff --git a/src/st/croco/cr-term.c b/src/st/croco/cr-term.c index b527d954f..412b09ba0 100644 --- a/src/st/croco/cr-term.c +++ b/src/st/croco/cr-term.c @@ -86,12 +86,11 @@ cr_term_new (void) { CRTerm *result = NULL; - result = g_try_malloc (sizeof (CRTerm)); + result = g_try_malloc0 (sizeof (CRTerm)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRTerm)); return result; } diff --git a/src/st/croco/cr-tknzr.c b/src/st/croco/cr-tknzr.c index 54f18f270..da14aad3f 100644 --- a/src/st/croco/cr-tknzr.c +++ b/src/st/croco/cr-tknzr.c @@ -1596,16 +1596,14 @@ cr_tknzr_new (CRInput * a_input) { CRTknzr *result = NULL; - result = g_try_malloc (sizeof (CRTknzr)); + result = g_try_malloc0 (sizeof (CRTknzr)); if (result == NULL) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRTknzr)); - - result->priv = g_try_malloc (sizeof (CRTknzrPriv)); + result->priv = g_try_malloc0 (sizeof (CRTknzrPriv)); if (result->priv == NULL) { cr_utils_trace_info ("Out of memory"); @@ -1617,7 +1615,6 @@ cr_tknzr_new (CRInput * a_input) return NULL; } - memset (result->priv, 0, sizeof (CRTknzrPriv)); if (a_input) cr_tknzr_set_input (result, a_input); return result; diff --git a/src/st/croco/cr-token.c b/src/st/croco/cr-token.c index 91dd632b5..a486d92eb 100644 --- a/src/st/croco/cr-token.c +++ b/src/st/croco/cr-token.c @@ -135,15 +135,13 @@ cr_token_new (void) { CRToken *result = NULL; - result = g_try_malloc (sizeof (CRToken)); + result = g_try_malloc0 (sizeof (CRToken)); if (result == NULL) { cr_utils_trace_info ("Out of memory"); return NULL; } - memset (result, 0, sizeof (CRToken)); - return result; } From 602d218a354d2a27f8a1e0437140294902d43355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Fri, 3 May 2024 21:43:08 +0200 Subject: [PATCH 02/15] croco/cr-doc-handler: Move ref_count to private structure Since we've one, let's hide this private bit --- src/st/croco/cr-doc-handler.c | 14 ++++++++------ src/st/croco/cr-doc-handler.h | 1 - 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/st/croco/cr-doc-handler.c b/src/st/croco/cr-doc-handler.c index 0bdfe36fe..90a4a4f88 100644 --- a/src/st/croco/cr-doc-handler.c +++ b/src/st/croco/cr-doc-handler.c @@ -36,6 +36,8 @@ #define PRIVATE(obj) (obj)->priv struct _CRDocHandlerPriv { + gulong ref_count; + /** *This pointer is to hold an application parsing context. *For example, it used by the Object Model parser to @@ -75,8 +77,6 @@ cr_doc_handler_new (void) g_return_val_if_fail (result, NULL); - result->ref_count++; - result->priv = g_try_malloc0 (sizeof (CRDocHandlerPriv)); if (!result->priv) { cr_utils_trace_info ("Out of memory exception"); @@ -84,6 +84,8 @@ cr_doc_handler_new (void) return NULL; } + PRIVATE (result)->ref_count++; + return result; } @@ -208,7 +210,7 @@ cr_doc_handler_ref (CRDocHandler * a_this) { g_return_if_fail (a_this); - a_this->ref_count++; + PRIVATE (a_this)->ref_count++; } /** @@ -225,11 +227,11 @@ cr_doc_handler_unref (CRDocHandler * a_this) { g_return_val_if_fail (a_this, FALSE); - if (a_this->ref_count > 0) { - a_this->ref_count--; + if (PRIVATE (a_this)->ref_count > 0) { + PRIVATE (a_this)->ref_count--; } - if (a_this->ref_count == 0) { + if (PRIVATE (a_this)->ref_count == 0) { cr_doc_handler_destroy (a_this); return TRUE; } diff --git a/src/st/croco/cr-doc-handler.h b/src/st/croco/cr-doc-handler.h index d12673f31..af0f311a6 100644 --- a/src/st/croco/cr-doc-handler.h +++ b/src/st/croco/cr-doc-handler.h @@ -269,7 +269,6 @@ struct _CRDocHandler void (*unrecoverable_error) (CRDocHandler *a_this) ; gboolean resolve_import ; - gulong ref_count ; } ; CRDocHandler * cr_doc_handler_new (void) ; From e64bac189b604de961f3854b8c3a11f396a711a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 17 Feb 2021 16:01:37 +0100 Subject: [PATCH 03/15] croco/stylesheet: Move ref-count to private implementation This always meant to be a private bit so there's no point to expose it. We use a private structure type for this without doing extra allocations as in other croco structures, since it just unneeded to have an extra pointer just for this, and we can just use a bigger allocation from the start that is hidden to the library users. Also adding such type will allow us to add some more bits we don't want to be directly modifiable from outside. --- src/st/croco/cr-stylesheet.c | 25 ++++++++++++++++++++----- src/st/croco/cr-stylesheet.h | 9 --------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/st/croco/cr-stylesheet.c b/src/st/croco/cr-stylesheet.c index 8c120b2bf..2c26cdff6 100644 --- a/src/st/croco/cr-stylesheet.c +++ b/src/st/croco/cr-stylesheet.c @@ -28,6 +28,17 @@ *The definition of the #CRStyleSheet class */ +typedef struct { + CRStyleSheet stylesheet; + + /** + *the reference count of this instance of #CRStyleSheet. + *It can be manipulated with cr_stylesheet_ref() and + *cr_stylesheet_unref() + */ + gulong ref_count; +} CRStyleSheetReal; + /** *Constructor of the #CRStyleSheet class. *@param the initial list of css statements. @@ -38,7 +49,7 @@ cr_stylesheet_new (CRStatement * a_stmts) { CRStyleSheet *result; - result = g_try_malloc0 (sizeof (CRStyleSheet)); + result = g_try_malloc0 (sizeof (CRStyleSheetReal)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; @@ -137,20 +148,24 @@ cr_stylesheet_statement_get_from_list (CRStyleSheet * a_this, int itemnr) void cr_stylesheet_ref (CRStyleSheet * a_this) { + CRStyleSheetReal *real = (CRStyleSheetReal *) a_this; + g_return_if_fail (a_this); - a_this->ref_count++; + real->ref_count++; } gboolean cr_stylesheet_unref (CRStyleSheet * a_this) { + CRStyleSheetReal *real = (CRStyleSheetReal *) a_this; + g_return_val_if_fail (a_this, FALSE); - if (a_this->ref_count) - a_this->ref_count--; + if (real->ref_count) + real->ref_count--; - if (!a_this->ref_count) { + if (!real->ref_count) { cr_stylesheet_destroy (a_this); return TRUE; } diff --git a/src/st/croco/cr-stylesheet.h b/src/st/croco/cr-stylesheet.h index 2d6b4fa0f..81aefa040 100644 --- a/src/st/croco/cr-stylesheet.h +++ b/src/st/croco/cr-stylesheet.h @@ -34,7 +34,6 @@ G_BEGIN_DECLS *The declaration of the #CRStyleSheet class. */ - enum CRStyleOrigin { /*Please don't change the order of @@ -72,14 +71,6 @@ struct _CRStyleSheet *Can be used by applications. */ gpointer app_data ; - - /** - *the reference count of this instance - *Please, don't never ever modify it - *directly. Use cr_stylesheet_ref() - *and cr_stylesheet_unref() instead. - */ - gulong ref_count ; } ; CRStyleSheet * cr_stylesheet_new (CRStatement *a_stmts) ; From bd6572f8eb6b9543f69e36ae710e98bd110f2719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Fri, 3 May 2024 21:46:28 +0200 Subject: [PATCH 04/15] croco: Simplify handling of private data avoiding multiple allocations We can just allocate a bigger space and use the private area of the structs instead of using multiple allocations and extra pointers. --- src/st/croco/cr-cascade.c | 35 +++------ src/st/croco/cr-cascade.h | 4 +- src/st/croco/cr-doc-handler.c | 40 ++++------ src/st/croco/cr-doc-handler.h | 5 -- src/st/croco/cr-input.c | 108 ++++++++++----------------- src/st/croco/cr-input.h | 3 +- src/st/croco/cr-om-parser.c | 41 +++-------- src/st/croco/cr-om-parser.h | 3 +- src/st/croco/cr-parser.c | 98 ++++++++++--------------- src/st/croco/cr-parser.h | 3 +- src/st/croco/cr-prop-list.c | 45 +++++------- src/st/croco/cr-prop-list.h | 3 +- src/st/croco/cr-statement.c | 10 +-- src/st/croco/cr-tknzr.c | 133 +++++++++++----------------------- src/st/croco/cr-tknzr.h | 4 +- 15 files changed, 189 insertions(+), 346 deletions(-) diff --git a/src/st/croco/cr-cascade.c b/src/st/croco/cr-cascade.c index 2b207f9df..8ce320c6b 100644 --- a/src/st/croco/cr-cascade.c +++ b/src/st/croco/cr-cascade.c @@ -29,9 +29,11 @@ #include #include "cr-cascade.h" -#define PRIVATE(a_this) ((a_this)->priv) +#define PRIVATE(a_this) ((CRCascadeReal *) a_this) + +typedef struct _CRCascadeReal { + CRCascade parent; -struct _CRCascadePriv { /** *the 3 style sheets of the cascade: *author, user, and useragent sheet. @@ -41,7 +43,7 @@ struct _CRCascadePriv { */ CRStyleSheet *sheets[3]; guint ref_count; -}; +} CRCascadeReal; /** * cr_cascade_new: @@ -66,19 +68,12 @@ cr_cascade_new (CRStyleSheet * a_author_sheet, { CRCascade *result = NULL; - result = g_try_malloc0 (sizeof (CRCascade)); + result = g_try_malloc0 (sizeof (CRCascadeReal)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - PRIVATE (result) = g_try_malloc0 (sizeof (CRCascadePriv)); - if (!PRIVATE (result)) { - cr_utils_trace_info ("Out of memory"); - g_free (result); - return NULL; - } - if (a_author_sheet) { cr_cascade_set_sheet (result, a_author_sheet, ORIGIN_AUTHOR); } @@ -194,20 +189,14 @@ cr_cascade_destroy (CRCascade * a_this) { g_return_if_fail (a_this); - if (PRIVATE (a_this)) { - gulong i = 0; - - for (i = 0; i < NB_ORIGINS; i++) { - if (PRIVATE (a_this)->sheets[i]) { - if (cr_stylesheet_unref - (PRIVATE (a_this)->sheets[i]) - == TRUE) { - PRIVATE (a_this)->sheets[i] = NULL; - } + for (int i = 0; i < NB_ORIGINS; i++) { + if (PRIVATE (a_this)->sheets[i]) { + if (cr_stylesheet_unref + (PRIVATE (a_this)->sheets[i]) + == TRUE) { + PRIVATE (a_this)->sheets[i] = NULL; } } - g_free (PRIVATE (a_this)); - PRIVATE (a_this) = NULL; } g_free (a_this); } diff --git a/src/st/croco/cr-cascade.h b/src/st/croco/cr-cascade.h index 3119ae85f..96fbf2347 100644 --- a/src/st/croco/cr-cascade.h +++ b/src/st/croco/cr-cascade.h @@ -38,8 +38,6 @@ G_BEGIN_DECLS -typedef struct _CRCascadePriv CRCascadePriv ; - /** *An abstraction of the "Cascade" defined *in the css2 spec, chapter 6.4. @@ -48,7 +46,7 @@ typedef struct _CRCascade CRCascade ; struct _CRCascade { - CRCascadePriv *priv ; + gpointer dummy; }; diff --git a/src/st/croco/cr-doc-handler.c b/src/st/croco/cr-doc-handler.c index 90a4a4f88..bd3554676 100644 --- a/src/st/croco/cr-doc-handler.c +++ b/src/st/croco/cr-doc-handler.c @@ -33,9 +33,11 @@ *to custom values. */ -#define PRIVATE(obj) (obj)->priv +#define PRIVATE(obj) ((CRDocHandlerReal *) obj) + +typedef struct _CRDocHandlerReal { + CRDocHandler parent; -struct _CRDocHandlerPriv { gulong ref_count; /** @@ -58,7 +60,7 @@ struct _CRDocHandlerPriv { *the current document. */ CRParser *parser ; -}; +} CRDocHandlerReal; /** * cr_doc_handler_new: @@ -73,16 +75,11 @@ cr_doc_handler_new (void) { CRDocHandler *result = NULL; - result = g_try_malloc0 (sizeof (CRDocHandler)); + result = g_try_malloc0 (sizeof (CRDocHandlerReal)); g_return_val_if_fail (result, NULL); - result->priv = g_try_malloc0 (sizeof (CRDocHandlerPriv)); - if (!result->priv) { - cr_utils_trace_info ("Out of memory exception"); - g_free (result); - return NULL; - } + cr_doc_handler_set_default_sac_handler (result); PRIVATE (result)->ref_count++; @@ -102,9 +99,9 @@ cr_doc_handler_new (void) enum CRStatus cr_doc_handler_get_ctxt (CRDocHandler const * a_this, gpointer * a_ctxt) { - g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); - *a_ctxt = a_this->priv->context; + *a_ctxt = PRIVATE (a_this)->context; return CR_OK; } @@ -121,8 +118,8 @@ cr_doc_handler_get_ctxt (CRDocHandler const * a_this, gpointer * a_ctxt) enum CRStatus cr_doc_handler_set_ctxt (CRDocHandler * a_this, gpointer a_ctxt) { - g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR); - a_this->priv->context = a_ctxt; + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); + PRIVATE (a_this)->context = a_ctxt; return CR_OK; } @@ -139,9 +136,9 @@ cr_doc_handler_set_ctxt (CRDocHandler * a_this, gpointer a_ctxt) enum CRStatus cr_doc_handler_get_result (CRDocHandler const * a_this, gpointer * a_result) { - g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); - *a_result = a_this->priv->result; + *a_result = PRIVATE (a_this)->result; return CR_OK; } @@ -159,8 +156,8 @@ cr_doc_handler_get_result (CRDocHandler const * a_this, gpointer * a_result) enum CRStatus cr_doc_handler_set_result (CRDocHandler * a_this, gpointer a_result) { - g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR); - a_this->priv->result = a_result; + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); + PRIVATE (a_this)->result = a_result; return CR_OK; } @@ -250,10 +247,6 @@ cr_doc_handler_destroy (CRDocHandler * a_this) { g_return_if_fail (a_this); - if (a_this->priv) { - g_free (a_this->priv); - a_this->priv = NULL; - } g_free (a_this); } @@ -268,8 +261,7 @@ void cr_doc_handler_associate_a_parser (CRDocHandler *a_this, gpointer a_parser) { - g_return_if_fail (a_this && PRIVATE (a_this) - && a_parser) ; + g_return_if_fail (a_this && a_parser) ; PRIVATE (a_this)->parser = a_parser ; } diff --git a/src/st/croco/cr-doc-handler.h b/src/st/croco/cr-doc-handler.h index af0f311a6..e9639190d 100644 --- a/src/st/croco/cr-doc-handler.h +++ b/src/st/croco/cr-doc-handler.h @@ -39,9 +39,6 @@ G_BEGIN_DECLS typedef struct _CRDocHandler CRDocHandler ; -struct _CRDocHandlerPriv ; -typedef struct _CRDocHandlerPriv CRDocHandlerPriv ; - /** *The SAC document handler. @@ -52,8 +49,6 @@ typedef struct _CRDocHandlerPriv CRDocHandlerPriv ; */ struct _CRDocHandler { - CRDocHandlerPriv *priv ; - /** *This pointer is to be used by the application for *it custom needs. It is there to extend the doc handler. diff --git a/src/st/croco/cr-input.c b/src/st/croco/cr-input.c index 470780992..a998e1281 100644 --- a/src/st/croco/cr-input.c +++ b/src/st/croco/cr-input.c @@ -38,9 +38,11 @@ /** *The private attributes of - *the #CRInputPriv class. + *the #RInputReal class. */ -struct _CRInputPriv { +typedef struct _CRInputReal { + CRInput parent; + /* *The input buffer */ @@ -74,9 +76,9 @@ struct _CRInputPriv { */ guint ref_count; gboolean free_in_buf; -}; +} CRInputReal; -#define PRIVATE(object) (object)->priv +#define PRIVATE(object) ((CRInputReal *) object) /*************************** *private constants @@ -90,18 +92,12 @@ cr_input_new_real (void) { CRInput *result = NULL; - result = g_try_malloc0 (sizeof (CRInput)); + result = g_try_malloc0 (sizeof (CRInputReal)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - PRIVATE (result) = g_try_malloc0 (sizeof (CRInputPriv)); - if (!PRIVATE (result)) { - cr_utils_trace_info ("Out of memory"); - g_free (result); - return NULL; - } PRIVATE (result)->free_in_buf = TRUE; return result; } @@ -283,15 +279,8 @@ cr_input_destroy (CRInput * a_this) if (a_this == NULL) return; - if (PRIVATE (a_this)) { - if (PRIVATE (a_this)->in_buf && PRIVATE (a_this)->free_in_buf) { - g_free (PRIVATE (a_this)->in_buf); - PRIVATE (a_this)->in_buf = NULL; - } - - g_free (PRIVATE (a_this)); - PRIVATE (a_this) = NULL; - } + if (PRIVATE (a_this)->free_in_buf) + g_clear_pointer (&PRIVATE (a_this)->in_buf, g_free); g_free (a_this); } @@ -306,7 +295,7 @@ cr_input_destroy (CRInput * a_this) void cr_input_ref (CRInput * a_this) { - g_return_if_fail (a_this && PRIVATE (a_this)); + g_return_if_fail (a_this); PRIVATE (a_this)->ref_count++; } @@ -324,7 +313,7 @@ cr_input_ref (CRInput * a_this) gboolean cr_input_unref (CRInput * a_this) { - g_return_val_if_fail (a_this && PRIVATE (a_this), FALSE); + g_return_val_if_fail (a_this, FALSE); if (PRIVATE (a_this)->ref_count) { PRIVATE (a_this)->ref_count--; @@ -354,8 +343,7 @@ cr_input_unref (CRInput * a_this) enum CRStatus cr_input_end_of_input (CRInput const * a_this, gboolean * a_end_of_input) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_end_of_input, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_end_of_input, CR_BAD_PARAM_ERROR); *a_end_of_input = (PRIVATE (a_this)->next_byte_index >= PRIVATE (a_this)->in_buf_size) ? TRUE : FALSE; @@ -373,7 +361,7 @@ cr_input_end_of_input (CRInput const * a_this, gboolean * a_end_of_input) glong cr_input_get_nb_bytes_left (CRInput const * a_this) { - g_return_val_if_fail (a_this && PRIVATE (a_this), -1); + g_return_val_if_fail (a_this, -1); g_return_val_if_fail (PRIVATE (a_this)->nb_bytes <= PRIVATE (a_this)->in_buf_size, -1); g_return_val_if_fail (PRIVATE (a_this)->next_byte_index @@ -404,8 +392,7 @@ cr_input_read_byte (CRInput * a_this, guchar * a_byte) { gulong nb_bytes_left = 0; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_byte, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_byte, CR_BAD_PARAM_ERROR); g_return_val_if_fail (PRIVATE (a_this)->next_byte_index <= PRIVATE (a_this)->nb_bytes, CR_BAD_PARAM_ERROR); @@ -449,8 +436,7 @@ cr_input_read_char (CRInput * a_this, guint32 * a_char) gulong consumed = 0, nb_bytes_left = 0; - g_return_val_if_fail (a_this && PRIVATE (a_this) && a_char, - CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_char, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->end_of_input == TRUE) return CR_END_OF_INPUT_ERROR; @@ -500,7 +486,7 @@ cr_input_read_char (CRInput * a_this, guint32 * a_char) enum CRStatus cr_input_set_line_num (CRInput * a_this, glong a_line_num) { - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); PRIVATE (a_this)->line = a_line_num; @@ -519,8 +505,7 @@ cr_input_set_line_num (CRInput * a_this, glong a_line_num) enum CRStatus cr_input_get_line_num (CRInput const * a_this, glong * a_line_num) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_line_num, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_line_num, CR_BAD_PARAM_ERROR); *a_line_num = PRIVATE (a_this)->line; @@ -539,7 +524,7 @@ cr_input_get_line_num (CRInput const * a_this, glong * a_line_num) enum CRStatus cr_input_set_column_num (CRInput * a_this, glong a_col) { - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); PRIVATE (a_this)->col = a_col; @@ -558,8 +543,7 @@ cr_input_set_column_num (CRInput * a_this, glong a_col) enum CRStatus cr_input_get_column_num (CRInput const * a_this, glong * a_col) { - g_return_val_if_fail (a_this && PRIVATE (a_this) && a_col, - CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_col, CR_BAD_PARAM_ERROR); *a_col = PRIVATE (a_this)->col; @@ -578,7 +562,7 @@ cr_input_get_column_num (CRInput const * a_this, glong * a_col) enum CRStatus cr_input_increment_line_num (CRInput * a_this, glong a_increment) { - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); PRIVATE (a_this)->line += a_increment; @@ -597,7 +581,7 @@ cr_input_increment_line_num (CRInput * a_this, glong a_increment) enum CRStatus cr_input_increment_col_num (CRInput * a_this, glong a_increment) { - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); PRIVATE (a_this)->col += a_increment; @@ -622,7 +606,7 @@ cr_input_consume_char (CRInput * a_this, guint32 a_char) guint32 c; enum CRStatus status; - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); if ((status = cr_input_peek_char (a_this, &c)) != CR_OK) { return status; @@ -663,11 +647,9 @@ cr_input_consume_chars (CRInput * a_this, guint32 a_char, gulong * a_nb_char) enum CRStatus status = CR_OK; gulong nb_consumed = 0; - g_return_val_if_fail (a_this && PRIVATE (a_this) && a_nb_char, - CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_nb_char, CR_BAD_PARAM_ERROR); - g_return_val_if_fail (a_char != 0 || a_nb_char != NULL, - CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_char != 0 || a_nb_char != NULL, CR_BAD_PARAM_ERROR); for (nb_consumed = 0; ((status == CR_OK) && (*a_nb_char > 0 @@ -705,8 +687,7 @@ cr_input_consume_white_spaces (CRInput * a_this, gulong * a_nb_chars) guint32 cur_char = 0, nb_consumed = 0; - g_return_val_if_fail (a_this && PRIVATE (a_this) && a_nb_chars, - CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_nb_chars, CR_BAD_PARAM_ERROR); for (nb_consumed = 0; ((*a_nb_chars > 0) && (nb_consumed < *a_nb_chars)); @@ -756,7 +737,7 @@ cr_input_peek_char (CRInput const * a_this, guint32 * a_char) gulong consumed = 0, nb_bytes_left = 0; - g_return_val_if_fail (a_this && PRIVATE (a_this) + g_return_val_if_fail (a_this && a_char, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->next_byte_index >= @@ -804,7 +785,7 @@ cr_input_peek_byte (CRInput const * a_this, enum CRSeekPos a_origin, { gulong abs_offset = 0; - g_return_val_if_fail (a_this && PRIVATE (a_this) + g_return_val_if_fail (a_this && a_byte, CR_BAD_PARAM_ERROR); switch (a_origin) { @@ -856,7 +837,7 @@ cr_input_peek_byte2 (CRInput const * a_this, gulong a_offset, gboolean * a_eof) guchar result = 0; enum CRStatus status = CR_ERROR; - g_return_val_if_fail (a_this && PRIVATE (a_this), 0); + g_return_val_if_fail (a_this, 0); if (a_eof) *a_eof = FALSE; @@ -884,7 +865,7 @@ cr_input_peek_byte2 (CRInput const * a_this, gulong a_offset, gboolean * a_eof) guchar * cr_input_get_byte_addr (CRInput * a_this, gulong a_offset) { - g_return_val_if_fail (a_this && PRIVATE (a_this), NULL); + g_return_val_if_fail (a_this, NULL); if (a_offset >= PRIVATE (a_this)->nb_bytes) { return NULL; @@ -905,8 +886,7 @@ cr_input_get_byte_addr (CRInput * a_this, gulong a_offset) enum CRStatus cr_input_get_cur_byte_addr (CRInput * a_this, guchar ** a_offset) { - g_return_val_if_fail (a_this && PRIVATE (a_this) && a_offset, - CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_offset, CR_BAD_PARAM_ERROR); if (!PRIVATE (a_this)->next_byte_index) { return CR_START_OF_INPUT_ERROR; @@ -940,7 +920,7 @@ cr_input_seek_index (CRInput * a_this, enum CRSeekPos a_origin, gint a_pos) glong abs_offset = 0; - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); switch (a_origin) { @@ -992,8 +972,7 @@ cr_input_seek_index (CRInput * a_this, enum CRSeekPos a_origin, gint a_pos) enum CRStatus cr_input_get_cur_pos (CRInput const * a_this, CRInputPos * a_pos) { - g_return_val_if_fail (a_this && PRIVATE (a_this) && a_pos, - CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_pos, CR_BAD_PARAM_ERROR); a_pos->next_byte_index = PRIVATE (a_this)->next_byte_index; a_pos->line = PRIVATE (a_this)->line; @@ -1021,10 +1000,7 @@ enum CRStatus cr_input_get_parsing_location (CRInput const *a_this, CRParsingLocation *a_loc) { - g_return_val_if_fail (a_this - && PRIVATE (a_this) - && a_loc, - CR_BAD_PARAM_ERROR) ; + g_return_val_if_fail (a_this && a_loc, CR_BAD_PARAM_ERROR) ; a_loc->line = PRIVATE (a_this)->line ; a_loc->column = PRIVATE (a_this)->col ; @@ -1052,8 +1028,7 @@ cr_input_get_parsing_location (CRInput const *a_this, enum CRStatus cr_input_get_cur_index (CRInput const * a_this, glong * a_index) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_index, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_index, CR_BAD_PARAM_ERROR); *a_index = PRIVATE (a_this)->next_byte_index; @@ -1074,7 +1049,7 @@ cr_input_get_cur_index (CRInput const * a_this, glong * a_index) enum CRStatus cr_input_set_cur_index (CRInput * a_this, glong a_index) { - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); PRIVATE (a_this)->next_byte_index = a_index; @@ -1093,7 +1068,7 @@ cr_input_set_cur_index (CRInput * a_this, glong a_index) enum CRStatus cr_input_set_end_of_file (CRInput * a_this, gboolean a_eof) { - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); PRIVATE (a_this)->end_of_input = a_eof; @@ -1113,8 +1088,7 @@ cr_input_set_end_of_file (CRInput * a_this, gboolean a_eof) enum CRStatus cr_input_get_end_of_file (CRInput const * a_this, gboolean * a_eof) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_eof, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_eof, CR_BAD_PARAM_ERROR); *a_eof = PRIVATE (a_this)->end_of_input; @@ -1134,7 +1108,7 @@ cr_input_get_end_of_file (CRInput const * a_this, gboolean * a_eof) enum CRStatus cr_input_set_end_of_line (CRInput * a_this, gboolean a_eol) { - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); PRIVATE (a_this)->end_of_line = a_eol; @@ -1155,8 +1129,7 @@ cr_input_set_end_of_line (CRInput * a_this, gboolean a_eol) enum CRStatus cr_input_get_end_of_line (CRInput const * a_this, gboolean * a_eol) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_eol, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_eol, CR_BAD_PARAM_ERROR); *a_eol = PRIVATE (a_this)->end_of_line; @@ -1176,8 +1149,7 @@ cr_input_get_end_of_line (CRInput const * a_this, gboolean * a_eol) enum CRStatus cr_input_set_cur_pos (CRInput * a_this, CRInputPos const * a_pos) { - g_return_val_if_fail (a_this && PRIVATE (a_this) && a_pos, - CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_pos, CR_BAD_PARAM_ERROR); cr_input_set_column_num (a_this, a_pos->col); cr_input_set_line_num (a_this, a_pos->line); diff --git a/src/st/croco/cr-input.h b/src/st/croco/cr-input.h index 9eb402a87..857486536 100644 --- a/src/st/croco/cr-input.h +++ b/src/st/croco/cr-input.h @@ -38,7 +38,6 @@ G_BEGIN_DECLS */ typedef struct _CRInput CRInput ; -typedef struct _CRInputPriv CRInputPriv ; /** *The #CRInput class provides the abstraction of @@ -46,7 +45,7 @@ typedef struct _CRInputPriv CRInputPriv ; */ struct _CRInput { - CRInputPriv *priv ; + gpointer dummy; } ; typedef struct _CRInputPos CRInputPos ; diff --git a/src/st/croco/cr-om-parser.c b/src/st/croco/cr-om-parser.c index eb870ae99..f9a3cba08 100644 --- a/src/st/croco/cr-om-parser.c +++ b/src/st/croco/cr-om-parser.c @@ -33,11 +33,13 @@ *in cr-parser.h and cr-doc-handler.h */ -struct _CROMParserPriv { - CRParser *parser; -}; +typedef struct _CROMParserReal { + CROMParser parent; -#define PRIVATE(a_this) ((a_this)->priv) + CRParser *parser; +} CROMParserReal; + +#define PRIVATE(a_this) ((CROMParserReal *) a_this) /* *Forward declaration of a type defined later @@ -145,8 +147,7 @@ cr_om_parser_init_default_sac_handler (CROMParser * a_this) gboolean created_handler = FALSE; enum CRStatus status = CR_OK; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->parser, + g_return_val_if_fail (a_this && PRIVATE (a_this)->parser, CR_BAD_PARAM_ERROR); status = cr_parser_get_sac_handler (PRIVATE (a_this)->parser, @@ -795,20 +796,12 @@ cr_om_parser_new (CRInput * a_input) CROMParser *result = NULL; enum CRStatus status = CR_OK; - result = g_try_malloc0 (sizeof (CROMParser)); + result = g_try_malloc0 (sizeof (CROMParserReal)); if (!result) { cr_utils_trace_info ("Out of memory"); return NULL; } - - PRIVATE (result) = g_try_malloc0 (sizeof (CROMParserPriv)); - - if (!PRIVATE (result)) { - cr_utils_trace_info ("Out of memory"); - goto error; - } - PRIVATE (result)->parser = cr_parser_new_from_input (a_input); if (!PRIVATE (result)->parser) { @@ -1120,20 +1113,8 @@ cr_om_parser_simply_parse_paths_to_cascade (const guchar * a_author_path, void cr_om_parser_destroy (CROMParser * a_this) { - g_return_if_fail (a_this && PRIVATE (a_this)); + g_return_if_fail (a_this); - if (PRIVATE (a_this)->parser) { - cr_parser_destroy (PRIVATE (a_this)->parser); - PRIVATE (a_this)->parser = NULL; - } - - if (PRIVATE (a_this)) { - g_free (PRIVATE (a_this)); - PRIVATE (a_this) = NULL; - } - - if (a_this) { - g_free (a_this); - a_this = NULL; - } + g_clear_pointer (&PRIVATE (a_this)->parser, cr_parser_destroy); + g_free (a_this); } diff --git a/src/st/croco/cr-om-parser.h b/src/st/croco/cr-om-parser.h index 13d35b1cd..4fa359516 100644 --- a/src/st/croco/cr-om-parser.h +++ b/src/st/croco/cr-om-parser.h @@ -42,7 +42,6 @@ G_BEGIN_DECLS typedef struct _CROMParser CROMParser ; -typedef struct _CROMParserPriv CROMParserPriv ; /** *The Object model parser. @@ -52,7 +51,7 @@ typedef struct _CROMParserPriv CROMParserPriv ; */ struct _CROMParser { - CROMParserPriv *priv ; + gpointer dummy; } ; CROMParser * cr_om_parser_new (CRInput *a_input) ; diff --git a/src/st/croco/cr-parser.c b/src/st/croco/cr-parser.c index 15c51e4a7..d410ed2d2 100644 --- a/src/st/croco/cr-parser.c +++ b/src/st/croco/cr-parser.c @@ -105,7 +105,7 @@ enum CRParserState { *The private attributes of *#CRParser. */ -struct _CRParserPriv { +typedef struct _CRParserReal { /** *The tokenizer */ @@ -130,9 +130,9 @@ struct _CRParserPriv { gboolean resolve_import; gboolean is_case_sensitive; gboolean use_core_grammar; -}; +} CRParserReal; -#define PRIVATE(obj) ((obj)->priv) +#define PRIVATE(obj) ((CRParserReal *) obj) #define CHARS_TAB_SIZE 12 @@ -523,8 +523,7 @@ cr_parser_push_error (CRParser * a_this, CRParserError *error = NULL; CRInputPos pos; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_msg, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_msg, CR_BAD_PARAM_ERROR); error = cr_parser_error_new (a_msg, a_status); @@ -566,7 +565,7 @@ cr_parser_dump_err_stack (CRParser * a_this, gboolean a_clear_errs) { GList *cur = NULL; - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->err_stack == NULL) return CR_OK; @@ -592,7 +591,7 @@ cr_parser_clear_errors (CRParser * a_this) { GList *cur = NULL; - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); for (cur = PRIVATE (a_this)->err_stack; cur; cur = cur->next) { if (cur->data) { @@ -624,7 +623,7 @@ cr_parser_try_to_skip_spaces_and_comments (CRParser * a_this) enum CRStatus status = CR_ERROR; CRToken *token = NULL; - g_return_val_if_fail (a_this && PRIVATE (a_this) + g_return_val_if_fail (a_this && PRIVATE (a_this)->tknzr, CR_BAD_PARAM_ERROR); do { if (token) { @@ -681,7 +680,7 @@ cr_parser_parse_stylesheet_core (CRParser * a_this) CRInputPos init_pos; enum CRStatus status = CR_ERROR; - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -763,7 +762,7 @@ cr_parser_parse_atrule_core (CRParser * a_this) CRInputPos init_pos; enum CRStatus status = CR_ERROR; - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -838,7 +837,7 @@ cr_parser_parse_ruleset_core (CRParser * a_this) CRInputPos init_pos; enum CRStatus status = CR_ERROR; - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); status = cr_parser_parse_selector_core (a_this); @@ -927,7 +926,7 @@ cr_parser_parse_selector_core (CRParser * a_this) CRInputPos init_pos; enum CRStatus status = CR_ERROR; - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -968,7 +967,7 @@ cr_parser_parse_block_core (CRParser * a_this, CRInputPos init_pos; enum CRStatus status = CR_ERROR; - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); if (n_calls > RECURSIVE_CALLERS_LIMIT) return CR_ERROR; @@ -1041,7 +1040,7 @@ cr_parser_parse_declaration_core (CRParser * a_this) enum CRStatus status = CR_ERROR; CRString *prop = NULL; - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -1097,7 +1096,7 @@ cr_parser_parse_value_core (CRParser * a_this) enum CRStatus status = CR_ERROR; glong ref = 0; - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); continue_parsing: @@ -1479,8 +1478,7 @@ cr_parser_parse_property (CRParser * a_this, enum CRStatus status = CR_OK; CRInputPos init_pos; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->tknzr + g_return_val_if_fail (a_this && PRIVATE (a_this)->tknzr && a_property, CR_BAD_PARAM_ERROR); @@ -1945,10 +1943,7 @@ cr_parser_parse_simple_sels (CRParser * a_this, CRSimpleSel *sel = NULL; guint32 cur_char = 0; - g_return_val_if_fail (a_this - && PRIVATE (a_this) - && a_sel, - CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_sel, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -2144,7 +2139,7 @@ cr_parser_parse_function (CRParser * a_this, CRToken *token = NULL; CRTerm *expr = NULL; - g_return_val_if_fail (a_this && PRIVATE (a_this) + g_return_val_if_fail (a_this && a_func_name, CR_BAD_PARAM_ERROR); @@ -2226,7 +2221,7 @@ cr_parser_parse_uri (CRParser * a_this, CRString ** a_str) enum CRStatus status = CR_PARSING_ERROR; - g_return_val_if_fail (a_this && PRIVATE (a_this) + g_return_val_if_fail (a_this && PRIVATE (a_this)->tknzr, CR_BAD_PARAM_ERROR); status = cr_tknzr_parse_token (PRIVATE (a_this)->tknzr, @@ -2256,7 +2251,7 @@ cr_parser_parse_string (CRParser * a_this, CRString ** a_str) { enum CRStatus status = CR_OK; - g_return_val_if_fail (a_this && PRIVATE (a_this) + g_return_val_if_fail (a_this && PRIVATE (a_this)->tknzr && a_str, CR_BAD_PARAM_ERROR); @@ -2284,7 +2279,7 @@ cr_parser_parse_ident (CRParser * a_this, CRString ** a_str) { enum CRStatus status = CR_OK; - g_return_val_if_fail (a_this && PRIVATE (a_this) + g_return_val_if_fail (a_this && PRIVATE (a_this)->tknzr && a_str, CR_BAD_PARAM_ERROR); @@ -2319,7 +2314,7 @@ cr_parser_parse_stylesheet (CRParser * a_this) CRToken *token = NULL; CRString *charset = NULL; - g_return_val_if_fail (a_this && PRIVATE (a_this) + g_return_val_if_fail (a_this && PRIVATE (a_this)->tknzr, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -2764,9 +2759,7 @@ cr_parser_new (CRTknzr * a_tknzr) CRParser *result = NULL; enum CRStatus status = CR_OK; - result = g_malloc0 (sizeof (CRParser)); - - PRIVATE (result) = g_malloc0 (sizeof (CRParserPriv)); + result = g_malloc0 (sizeof (CRParserReal)); if (a_tknzr) { status = cr_parser_set_tknzr (result, a_tknzr); @@ -2921,7 +2914,7 @@ cr_parser_set_default_sac_handler (CRParser * a_this) CRDocHandler *default_sac_handler = NULL; enum CRStatus status = CR_ERROR; - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); default_sac_handler = cr_doc_handler_new (); @@ -2948,7 +2941,7 @@ enum CRStatus cr_parser_set_use_core_grammar (CRParser * a_this, gboolean a_use_core_grammar) { - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); PRIVATE (a_this)->use_core_grammar = a_use_core_grammar; @@ -2966,7 +2959,7 @@ enum CRStatus cr_parser_get_use_core_grammar (CRParser const * a_this, gboolean * a_use_core_grammar) { - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); *a_use_core_grammar = PRIVATE (a_this)->use_core_grammar; @@ -2991,7 +2984,7 @@ cr_parser_parse_file (CRParser * a_this, enum CRStatus status = CR_ERROR; CRTknzr *tknzr = NULL; - g_return_val_if_fail (a_this && PRIVATE (a_this) + g_return_val_if_fail (a_this && a_file_uri, CR_BAD_PARAM_ERROR); tknzr = cr_tknzr_new_from_uri (a_file_uri, a_enc); @@ -3028,8 +3021,7 @@ cr_parser_parse_expr (CRParser * a_this, CRTerm ** a_expr) guchar next_byte = 0; gulong nb_terms = 0; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_expr, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_expr, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -3132,7 +3124,7 @@ cr_parser_parse_prio (CRParser * a_this, CRString ** a_prio) CRInputPos init_pos; CRToken *token = NULL; - g_return_val_if_fail (a_this && PRIVATE (a_this) + g_return_val_if_fail (a_this && a_prio && *a_prio == NULL, CR_BAD_PARAM_ERROR); @@ -3187,7 +3179,7 @@ cr_parser_parse_declaration (CRParser * a_this, CRTerm *expr = NULL; CRString *prio = NULL; - g_return_val_if_fail (a_this && PRIVATE (a_this) + g_return_val_if_fail (a_this && a_property && a_expr && a_important, CR_BAD_PARAM_ERROR); @@ -3277,7 +3269,7 @@ cr_parser_parse_statement_core (CRParser * a_this) CRInputPos init_pos; enum CRStatus status = CR_ERROR; - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -3723,9 +3715,7 @@ cr_parser_parse_media (CRParser * a_this) GList *media_list = NULL; CRParsingLocation location = {0} ; - g_return_val_if_fail (a_this - && PRIVATE (a_this), - CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -4385,7 +4375,7 @@ cr_parser_parse (CRParser * a_this) { enum CRStatus status = CR_ERROR; - g_return_val_if_fail (a_this && PRIVATE (a_this) + g_return_val_if_fail (a_this && PRIVATE (a_this)->tknzr, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->use_core_grammar == FALSE) { @@ -4407,7 +4397,7 @@ cr_parser_parse (CRParser * a_this) enum CRStatus cr_parser_set_tknzr (CRParser * a_this, CRTknzr * a_tknzr) { - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->tknzr) { cr_tknzr_unref (PRIVATE (a_this)->tknzr); @@ -4434,8 +4424,7 @@ cr_parser_set_tknzr (CRParser * a_this, CRTknzr * a_tknzr) enum CRStatus cr_parser_get_tknzr (CRParser * a_this, CRTknzr ** a_tknzr) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_tknzr, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_tknzr, CR_BAD_PARAM_ERROR); *a_tknzr = PRIVATE (a_this)->tknzr; return CR_OK; @@ -4455,9 +4444,7 @@ enum CRStatus cr_parser_get_parsing_location (CRParser const *a_this, CRParsingLocation *a_loc) { - g_return_val_if_fail (a_this - && PRIVATE (a_this) - && a_loc, CR_BAD_PARAM_ERROR) ; + g_return_val_if_fail (a_this && a_loc, CR_BAD_PARAM_ERROR); return cr_tknzr_get_parsing_location (PRIVATE (a_this)->tknzr, a_loc) ; @@ -4482,8 +4469,7 @@ cr_parser_parse_buf (CRParser * a_this, enum CRStatus status = CR_ERROR; CRTknzr *tknzr = NULL; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_buf, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_buf, CR_BAD_PARAM_ERROR); tknzr = cr_tknzr_new_from_buf ((guchar*)a_buf, a_len, a_enc, FALSE); @@ -4508,7 +4494,7 @@ cr_parser_parse_buf (CRParser * a_this, void cr_parser_destroy (CRParser * a_this) { - g_return_if_fail (a_this && PRIVATE (a_this)); + g_return_if_fail (a_this); if (PRIVATE (a_this)->tknzr) { if (cr_tknzr_unref (PRIVATE (a_this)->tknzr) == TRUE) @@ -4525,13 +4511,5 @@ cr_parser_destroy (CRParser * a_this) PRIVATE (a_this)->err_stack = NULL; } - if (PRIVATE (a_this)) { - g_free (PRIVATE (a_this)); - PRIVATE (a_this) = NULL; - } - - if (a_this) { - g_free (a_this); - a_this = NULL; /*useless. Just for the sake of coherence */ - } + g_free (a_this); } diff --git a/src/st/croco/cr-parser.h b/src/st/croco/cr-parser.h index 6dce9439e..68e52e2b6 100644 --- a/src/st/croco/cr-parser.h +++ b/src/st/croco/cr-parser.h @@ -38,7 +38,6 @@ G_BEGIN_DECLS *of the #CRParser class. */ typedef struct _CRParser CRParser ; -typedef struct _CRParserPriv CRParserPriv ; /** @@ -49,7 +48,7 @@ typedef struct _CRParserPriv CRParserPriv ; *the provided methods. */ struct _CRParser { - CRParserPriv *priv ; + gpointer dummy; } ; diff --git a/src/st/croco/cr-prop-list.c b/src/st/croco/cr-prop-list.c index b329c720b..32084b99a 100644 --- a/src/st/croco/cr-prop-list.c +++ b/src/st/croco/cr-prop-list.c @@ -22,14 +22,16 @@ #include #include "cr-prop-list.h" -#define PRIVATE(a_obj) (a_obj)->priv +#define PRIVATE(a_obj) ((CRPropListReal *) a_obj) + +typedef struct _CRPropListReal { + CRPropList parent; -struct _CRPropListPriv { CRString *prop; CRDeclaration *decl; CRPropList *next; CRPropList *prev; -}; +} CRPropListReal; static CRPropList *cr_prop_list_allocate (void); @@ -43,17 +45,11 @@ cr_prop_list_allocate (void) { CRPropList *result = NULL; - result = g_try_malloc0 (sizeof (CRPropList)); + result = g_try_malloc0 (sizeof (CRPropListReal)); if (!result) { cr_utils_trace_info ("could not allocate CRPropList"); return NULL; } - PRIVATE (result) = g_try_malloc0 (sizeof (CRPropListPriv)); - if (!result) { - cr_utils_trace_info ("could not allocate CRPropListPriv"); - g_free (result); - return NULL; - } return result; } @@ -165,8 +161,7 @@ cr_prop_list_prepend2 (CRPropList * a_this, CRPropList *list = NULL, *result = NULL; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_prop_name && a_decl, NULL); + g_return_val_if_fail (a_this && a_prop_name && a_decl, NULL); list = cr_prop_list_allocate (); g_return_val_if_fail (list, NULL); @@ -186,8 +181,7 @@ cr_prop_list_prepend2 (CRPropList * a_this, enum CRStatus cr_prop_list_set_prop (CRPropList * a_this, CRString * a_prop) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_prop, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_prop, CR_BAD_PARAM_ERROR); PRIVATE (a_this)->prop = a_prop; return CR_OK; @@ -207,8 +201,7 @@ cr_prop_list_set_prop (CRPropList * a_this, CRString * a_prop) enum CRStatus cr_prop_list_get_prop (CRPropList const * a_this, CRString ** a_prop) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_prop, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_prop, CR_BAD_PARAM_ERROR); *a_prop = PRIVATE (a_this)->prop; return CR_OK; @@ -224,8 +217,7 @@ cr_prop_list_get_prop (CRPropList const * a_this, CRString ** a_prop) enum CRStatus cr_prop_list_set_decl (CRPropList * a_this, CRDeclaration * a_decl) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_decl, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_decl, CR_BAD_PARAM_ERROR); PRIVATE (a_this)->decl = a_decl; return CR_OK; @@ -241,8 +233,7 @@ cr_prop_list_set_decl (CRPropList * a_this, CRDeclaration * a_decl) enum CRStatus cr_prop_list_get_decl (CRPropList const * a_this, CRDeclaration ** a_decl) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_decl, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_decl, CR_BAD_PARAM_ERROR); *a_decl = PRIVATE (a_this)->decl; return CR_OK; @@ -305,7 +296,7 @@ cr_prop_list_lookup_prop (CRPropList * a_this, CRPropList * cr_prop_list_get_next (CRPropList * a_this) { - g_return_val_if_fail (a_this && PRIVATE (a_this), NULL); + g_return_val_if_fail (a_this, NULL); return PRIVATE (a_this)->next; } @@ -322,7 +313,7 @@ cr_prop_list_get_next (CRPropList * a_this) CRPropList * cr_prop_list_get_prev (CRPropList * a_this) { - g_return_val_if_fail (a_this && PRIVATE (a_this), NULL); + g_return_val_if_fail (a_this, NULL); return PRIVATE (a_this)->prev; } @@ -342,7 +333,7 @@ cr_prop_list_unlink (CRPropList * a_this, CRPropList * a_pair) CRPropList *prev = NULL, *next = NULL; - g_return_val_if_fail (a_this && PRIVATE (a_this) && a_pair, NULL); + g_return_val_if_fail (a_this && a_pair, NULL); /*some sanity checks */ if (PRIVATE (a_pair)->next) { @@ -380,10 +371,10 @@ cr_prop_list_destroy (CRPropList * a_this) CRPropList *tail = NULL, *cur = NULL; - g_return_if_fail (a_this && PRIVATE (a_this)); + g_return_if_fail (a_this); for (tail = a_this; - tail && PRIVATE (tail) && PRIVATE (tail)->next; + tail && PRIVATE (tail)->next; tail = cr_prop_list_get_next (tail)) ; g_return_if_fail (tail); @@ -391,11 +382,9 @@ cr_prop_list_destroy (CRPropList * a_this) while (cur) { tail = PRIVATE (cur)->prev; - if (tail && PRIVATE (tail)) + if (tail) PRIVATE (tail)->next = NULL; PRIVATE (cur)->prev = NULL; - g_free (PRIVATE (cur)); - PRIVATE (cur) = NULL; g_free (cur); cur = tail; } diff --git a/src/st/croco/cr-prop-list.h b/src/st/croco/cr-prop-list.h index 797ba43ea..8b9840e47 100644 --- a/src/st/croco/cr-prop-list.h +++ b/src/st/croco/cr-prop-list.h @@ -29,11 +29,10 @@ G_BEGIN_DECLS typedef struct _CRPropList CRPropList ; -typedef struct _CRPropListPriv CRPropListPriv ; struct _CRPropList { - CRPropListPriv * priv; + gpointer dummy; } ; CRPropList * cr_prop_list_append (CRPropList *a_this, diff --git a/src/st/croco/cr-statement.c b/src/st/croco/cr-statement.c index 0bb2a97f1..fe62c3030 100644 --- a/src/st/croco/cr-statement.c +++ b/src/st/croco/cr-statement.c @@ -235,7 +235,7 @@ parse_at_media_start_media_cb (CRDocHandler * a_this, CRStatement *at_media = NULL; GList *media_list = NULL; - g_return_if_fail (a_this && a_this->priv); + g_return_if_fail (a_this); if (a_media_list) { /*duplicate media list */ @@ -287,7 +287,7 @@ parse_at_media_start_selector_cb (CRDocHandler * a_this, CRStatement **at_media_ptr = NULL; CRStatement *ruleset = NULL; - g_return_if_fail (a_this && a_this->priv && a_sellist); + g_return_if_fail (a_this && a_sellist); at_media_ptr = &at_media; status = cr_doc_handler_get_ctxt (a_this, (gpointer *) at_media_ptr); @@ -367,7 +367,7 @@ parse_at_media_end_media_cb (CRDocHandler * a_this, CRStatement *at_media = NULL; CRStatement **at_media_ptr = NULL; - g_return_if_fail (a_this && a_this->priv); + g_return_if_fail (a_this); at_media_ptr = &at_media; status = cr_doc_handler_get_ctxt (a_this, @@ -382,7 +382,7 @@ parse_ruleset_start_selector_cb (CRDocHandler * a_this, { CRStatement *ruleset = NULL; - g_return_if_fail (a_this && a_this->priv && a_sellist); + g_return_if_fail (a_this && a_sellist); ruleset = cr_statement_new_ruleset (NULL, a_sellist, NULL, NULL); g_return_if_fail (ruleset); @@ -422,7 +422,7 @@ parse_ruleset_property_cb (CRDocHandler * a_this, CRDeclaration *decl = NULL; CRString *stringue = NULL; - g_return_if_fail (a_this && a_this->priv && a_name); + g_return_if_fail (a_this && a_name); stringue = cr_string_dup (a_name); g_return_if_fail (stringue); diff --git a/src/st/croco/cr-tknzr.c b/src/st/croco/cr-tknzr.c index da14aad3f..a2219cde6 100644 --- a/src/st/croco/cr-tknzr.c +++ b/src/st/croco/cr-tknzr.c @@ -31,7 +31,9 @@ #include "cr-tknzr.h" #include "cr-doc-handler.h" -struct _CRTknzrPriv { +typedef struct _CRTknzrReal { + CRTknzr parent; + /**The parser input stream of bytes*/ CRInput *input; @@ -58,9 +60,9 @@ struct _CRTknzrPriv { *and cr_tknzr_unref(). */ glong ref_count; -}; +} CRTknzrReal; -#define PRIVATE(obj) ((obj)->priv) +#define PRIVATE(obj) ((CRTknzrReal *) obj) /** *return TRUE if the character is a number ([0-9]), FALSE otherwise @@ -267,8 +269,7 @@ cr_tknzr_parse_w (CRTknzr * a_this, CRInputPos init_pos; enum CRStatus status = CR_OK; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input + g_return_val_if_fail (a_this && PRIVATE (a_this)->input && a_start && a_end, CR_BAD_PARAM_ERROR); @@ -341,7 +342,7 @@ cr_tknzr_parse_nl (CRTknzr * a_this, guchar next_chars[2] = { 0 }; enum CRStatus status = CR_PARSING_ERROR; - g_return_val_if_fail (a_this && PRIVATE (a_this) + g_return_val_if_fail (a_this && a_start && a_end, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -395,8 +396,7 @@ cr_tknzr_try_to_skip_spaces (CRTknzr * a_this) enum CRStatus status = CR_ERROR; guint32 cur_char = 0; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); status = cr_input_peek_char (PRIVATE (a_this)->input, &cur_char); @@ -435,8 +435,7 @@ cr_tknzr_parse_comment (CRTknzr * a_this, CRString *comment = NULL; CRParsingLocation loc = {0} ; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input, + g_return_val_if_fail (a_this && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -538,8 +537,7 @@ cr_tknzr_parse_unicode_escape (CRTknzr * a_this, *tmp_char_ptr2 = NULL; enum CRStatus status = CR_OK; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_unicode, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_unicode, CR_BAD_PARAM_ERROR); /*first, let's backup the current position pointer */ RECORD_INITIAL_POS (a_this, &init_pos); @@ -607,8 +605,7 @@ cr_tknzr_parse_escape (CRTknzr * a_this, guint32 * a_esc_code, CRInputPos init_pos; guchar next_chars[2]; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && a_esc_code, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && a_esc_code, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -674,8 +671,7 @@ cr_tknzr_parse_string (CRTknzr * a_this, CRString ** a_str) enum CRStatus status = CR_OK; CRString *str = NULL; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input + g_return_val_if_fail (a_this && PRIVATE (a_this)->input && a_str, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -801,8 +797,7 @@ cr_tknzr_parse_nmstart (CRTknzr * a_this, guint32 cur_char = 0, next_char = 0; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input + g_return_val_if_fail (a_this && PRIVATE (a_this)->input && a_char, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -867,7 +862,7 @@ cr_tknzr_parse_nmchar (CRTknzr * a_this, guint32 * a_char, enum CRStatus status = CR_OK; CRInputPos init_pos; - g_return_val_if_fail (a_this && PRIVATE (a_this) && a_char, + g_return_val_if_fail (a_this && a_char, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -934,8 +929,7 @@ cr_tknzr_parse_ident (CRTknzr * a_this, CRString ** a_str) enum CRStatus status = CR_OK; gboolean location_is_set = FALSE ; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input + g_return_val_if_fail (a_this && PRIVATE (a_this)->input && a_str, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -1024,9 +1018,7 @@ cr_tknzr_parse_name (CRTknzr * a_this, glong i = 0; CRParsingLocation loc = {0} ; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input - && a_str, + g_return_val_if_fail (a_this && PRIVATE (a_this)->input && a_str, CR_BAD_PARAM_ERROR) ; RECORD_INITIAL_POS (a_this, &init_pos); @@ -1076,8 +1068,7 @@ cr_tknzr_parse_hash (CRTknzr * a_this, CRString ** a_str) gboolean str_needs_free = FALSE; CRParsingLocation loc = {0} ; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input, + g_return_val_if_fail (a_this && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -1130,8 +1121,7 @@ cr_tknzr_parse_uri (CRTknzr * a_this, CRParsingLocation location = {0} ; g_return_val_if_fail (a_this - && PRIVATE (a_this) - && PRIVATE (a_this)->input + && PRIVATE (a_this)->input && a_str, CR_BAD_PARAM_ERROR); @@ -1254,7 +1244,7 @@ cr_tknzr_parse_rgb (CRTknzr * a_this, CRRgb ** a_rgb) gboolean is_percentage = FALSE; CRParsingLocation location = {0} ; - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -1389,8 +1379,7 @@ cr_tknzr_parse_atkeyword (CRTknzr * a_this, gboolean str_needs_free = FALSE; enum CRStatus status = CR_OK; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input + g_return_val_if_fail (a_this && PRIVATE (a_this)->input && a_str, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -1429,8 +1418,7 @@ cr_tknzr_parse_important (CRTknzr * a_this, CRInputPos init_pos; enum CRStatus status = CR_OK; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input, + g_return_val_if_fail (a_this && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -1497,8 +1485,7 @@ cr_tknzr_parse_num (CRTknzr * a_this, CRParsingLocation location = {0} ; int sign = 1; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input, + g_return_val_if_fail (a_this && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); RECORD_INITIAL_POS (a_this, &init_pos); @@ -1596,25 +1583,13 @@ cr_tknzr_new (CRInput * a_input) { CRTknzr *result = NULL; - result = g_try_malloc0 (sizeof (CRTknzr)); + result = g_try_malloc0 (sizeof (CRTknzrReal)); if (result == NULL) { cr_utils_trace_info ("Out of memory"); return NULL; } - result->priv = g_try_malloc0 (sizeof (CRTknzrPriv)); - - if (result->priv == NULL) { - cr_utils_trace_info ("Out of memory"); - - if (result) { - g_free (result); - result = NULL; - } - - return NULL; - } if (a_input) cr_tknzr_set_input (result, a_input); return result; @@ -1656,7 +1631,7 @@ cr_tknzr_new_from_uri (const guchar * a_file_uri, void cr_tknzr_ref (CRTknzr * a_this) { - g_return_if_fail (a_this && PRIVATE (a_this)); + g_return_if_fail (a_this); PRIVATE (a_this)->ref_count++; } @@ -1664,7 +1639,7 @@ cr_tknzr_ref (CRTknzr * a_this) gboolean cr_tknzr_unref (CRTknzr * a_this) { - g_return_val_if_fail (a_this && PRIVATE (a_this), FALSE); + g_return_val_if_fail (a_this, FALSE); if (PRIVATE (a_this)->ref_count > 0) { PRIVATE (a_this)->ref_count--; @@ -1681,7 +1656,7 @@ cr_tknzr_unref (CRTknzr * a_this) enum CRStatus cr_tknzr_set_input (CRTknzr * a_this, CRInput * a_input) { - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->input) { cr_input_unref (PRIVATE (a_this)->input); @@ -1697,7 +1672,7 @@ cr_tknzr_set_input (CRTknzr * a_this, CRInput * a_input) enum CRStatus cr_tknzr_get_input (CRTknzr * a_this, CRInput ** a_input) { - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); *a_input = PRIVATE (a_this)->input; @@ -1720,7 +1695,7 @@ cr_tknzr_get_input (CRTknzr * a_this, CRInput ** a_input) enum CRStatus cr_tknzr_read_byte (CRTknzr * a_this, guchar * a_byte) { - g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); return cr_input_read_byte (PRIVATE (a_this)->input, a_byte); @@ -1736,8 +1711,7 @@ cr_tknzr_read_byte (CRTknzr * a_this, guchar * a_byte) enum CRStatus cr_tknzr_read_char (CRTknzr * a_this, guint32 * a_char) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input + g_return_val_if_fail (a_this && PRIVATE (a_this)->input && a_char, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->token_cache) { @@ -1761,8 +1735,7 @@ cr_tknzr_read_char (CRTknzr * a_this, guint32 * a_char) enum CRStatus cr_tknzr_peek_char (CRTknzr * a_this, guint32 * a_char) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input + g_return_val_if_fail (a_this && PRIVATE (a_this)->input && a_char, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->token_cache) { @@ -1787,8 +1760,7 @@ cr_tknzr_peek_char (CRTknzr * a_this, guint32 * a_char) enum CRStatus cr_tknzr_peek_byte (CRTknzr * a_this, gulong a_offset, guchar * a_byte) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input && a_byte, + g_return_val_if_fail (a_this && PRIVATE (a_this)->input && a_byte, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->token_cache) { @@ -1815,8 +1787,7 @@ cr_tknzr_peek_byte (CRTknzr * a_this, gulong a_offset, guchar * a_byte) guchar cr_tknzr_peek_byte2 (CRTknzr * a_this, gulong a_offset, gboolean * a_eof) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input, 0); + g_return_val_if_fail (a_this && PRIVATE (a_this)->input, 0); return cr_input_peek_byte2 (PRIVATE (a_this)->input, a_offset, a_eof); } @@ -1830,8 +1801,7 @@ cr_tknzr_peek_byte2 (CRTknzr * a_this, gulong a_offset, gboolean * a_eof) glong cr_tknzr_get_nb_bytes_left (CRTknzr * a_this) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->token_cache) { cr_input_set_cur_pos (PRIVATE (a_this)->input, @@ -1846,8 +1816,7 @@ cr_tknzr_get_nb_bytes_left (CRTknzr * a_this) enum CRStatus cr_tknzr_get_cur_pos (CRTknzr * a_this, CRInputPos * a_pos) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input + g_return_val_if_fail (a_this && PRIVATE (a_this)->input && a_pos, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->token_cache) { @@ -1864,10 +1833,7 @@ enum CRStatus cr_tknzr_get_parsing_location (CRTknzr *a_this, CRParsingLocation *a_loc) { - g_return_val_if_fail (a_this - && PRIVATE (a_this) - && a_loc, - CR_BAD_PARAM_ERROR) ; + g_return_val_if_fail (a_this && a_loc, CR_BAD_PARAM_ERROR) ; return cr_input_get_parsing_location (PRIVATE (a_this)->input, a_loc) ; @@ -1876,8 +1842,7 @@ cr_tknzr_get_parsing_location (CRTknzr *a_this, enum CRStatus cr_tknzr_get_cur_byte_addr (CRTknzr * a_this, guchar ** a_addr) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->token_cache) { cr_input_set_cur_pos (PRIVATE (a_this)->input, &PRIVATE (a_this)->prev_pos); @@ -1891,8 +1856,7 @@ cr_tknzr_get_cur_byte_addr (CRTknzr * a_this, guchar ** a_addr) enum CRStatus cr_tknzr_seek_index (CRTknzr * a_this, enum CRSeekPos a_origin, gint a_pos) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->token_cache) { cr_input_set_cur_pos (PRIVATE (a_this)->input, @@ -1909,8 +1873,7 @@ cr_tknzr_consume_chars (CRTknzr * a_this, guint32 a_char, glong * a_nb_char) { gulong consumed = *(gulong *) a_nb_char; enum CRStatus status; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->token_cache) { cr_input_set_cur_pos (PRIVATE (a_this)->input, @@ -1928,8 +1891,7 @@ cr_tknzr_consume_chars (CRTknzr * a_this, guint32 a_char, glong * a_nb_char) enum CRStatus cr_tknzr_set_cur_pos (CRTknzr * a_this, CRInputPos * a_pos) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); + g_return_val_if_fail (a_this && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->token_cache) { cr_token_destroy (PRIVATE (a_this)->token_cache); @@ -1942,8 +1904,7 @@ cr_tknzr_set_cur_pos (CRTknzr * a_this, CRInputPos * a_pos) enum CRStatus cr_tknzr_unget_token (CRTknzr * a_this, CRToken * a_token) { - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->token_cache == NULL, + g_return_val_if_fail (a_this && PRIVATE (a_this)->token_cache == NULL, CR_BAD_PARAM_ERROR); PRIVATE (a_this)->token_cache = a_token; @@ -1976,9 +1937,9 @@ cr_tknzr_get_next_token (CRTknzr * a_this, CRToken ** a_tk) CRRgb *rgb = NULL; CRParsingLocation location = {0} ; - g_return_val_if_fail (a_this && PRIVATE (a_this) + g_return_val_if_fail (a_this && a_tk && *a_tk == NULL - && PRIVATE (a_this)->input, + && PRIVATE (a_this)->input, CR_BAD_PARAM_ERROR); if (PRIVATE (a_this)->token_cache) { @@ -2630,8 +2591,7 @@ cr_tknzr_parse_token (CRTknzr * a_this, enum CRTokenType a_type, enum CRStatus status = CR_OK; CRToken *token = NULL; - g_return_val_if_fail (a_this && PRIVATE (a_this) - && PRIVATE (a_this)->input + g_return_val_if_fail (a_this && PRIVATE (a_this)->input && a_res, CR_BAD_PARAM_ERROR); status = cr_tknzr_get_next_token (a_this, &token); @@ -2738,7 +2698,7 @@ cr_tknzr_destroy (CRTknzr * a_this) { g_return_if_fail (a_this); - if (PRIVATE (a_this) && PRIVATE (a_this)->input) { + if (PRIVATE (a_this)->input) { if (cr_input_unref (PRIVATE (a_this)->input) == TRUE) { PRIVATE (a_this)->input = NULL; @@ -2750,10 +2710,5 @@ cr_tknzr_destroy (CRTknzr * a_this) PRIVATE (a_this)->token_cache = NULL; } - if (PRIVATE (a_this)) { - g_free (PRIVATE (a_this)); - PRIVATE (a_this) = NULL; - } - g_free (a_this); } diff --git a/src/st/croco/cr-tknzr.h b/src/st/croco/cr-tknzr.h index 13985b30e..34c28e217 100644 --- a/src/st/croco/cr-tknzr.h +++ b/src/st/croco/cr-tknzr.h @@ -38,7 +38,6 @@ G_BEGIN_DECLS typedef struct _CRTknzr CRTknzr ; -typedef struct _CRTknzrPriv CRTknzrPriv ; /** *The tokenizer is the class that knows @@ -48,8 +47,7 @@ typedef struct _CRTknzrPriv CRTknzrPriv ; */ struct _CRTknzr { - /*the private data of the tokenizer.*/ - CRTknzrPriv *priv ; + gpointer dummy; } ; CRTknzr * cr_tknzr_new (CRInput *a_input) ; From a4fbb5f0faa6ddf4bd9241bf6428f47c2de693e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 17 Feb 2021 14:39:43 +0100 Subject: [PATCH 05/15] croco/stylesheet: Return the ref'ed stylesheet on ref function Make cr_stylesheet_ref to return ref'ed instance so that we can use this on the caller function to make clear what we're passing. This follows the expected design in GLib world. --- src/st/croco/cr-cascade.c | 3 +-- src/st/croco/cr-stylesheet.c | 6 ++++-- src/st/croco/cr-stylesheet.h | 2 +- src/st/st-theme.c | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/st/croco/cr-cascade.c b/src/st/croco/cr-cascade.c index 8ce320c6b..acfe8e3e5 100644 --- a/src/st/croco/cr-cascade.c +++ b/src/st/croco/cr-cascade.c @@ -135,8 +135,7 @@ cr_cascade_set_sheet (CRCascade * a_this, if (PRIVATE (a_this)->sheets[a_origin]) cr_stylesheet_unref (PRIVATE (a_this)->sheets[a_origin]); - PRIVATE (a_this)->sheets[a_origin] = a_sheet; - cr_stylesheet_ref (a_sheet); + PRIVATE (a_this)->sheets[a_origin] = cr_stylesheet_ref (a_sheet); a_sheet->origin = a_origin; return CR_OK; } diff --git a/src/st/croco/cr-stylesheet.c b/src/st/croco/cr-stylesheet.c index 2c26cdff6..bdbb4adf8 100644 --- a/src/st/croco/cr-stylesheet.c +++ b/src/st/croco/cr-stylesheet.c @@ -145,14 +145,16 @@ cr_stylesheet_statement_get_from_list (CRStyleSheet * a_this, int itemnr) return cr_statement_get_from_list (a_this->statements, itemnr); } -void +CRStyleSheet * cr_stylesheet_ref (CRStyleSheet * a_this) { CRStyleSheetReal *real = (CRStyleSheetReal *) a_this; - g_return_if_fail (a_this); + g_return_val_if_fail (a_this, NULL); real->ref_count++; + + return a_this; } gboolean diff --git a/src/st/croco/cr-stylesheet.h b/src/st/croco/cr-stylesheet.h index 81aefa040..75347f3ab 100644 --- a/src/st/croco/cr-stylesheet.h +++ b/src/st/croco/cr-stylesheet.h @@ -82,7 +82,7 @@ gint cr_stylesheet_nr_rules (CRStyleSheet const *a_this) ; CRStatement * cr_stylesheet_statement_get_from_list (CRStyleSheet *a_this, int itemnr) ; -void cr_stylesheet_ref (CRStyleSheet *a_this) ; +CRStyleSheet * cr_stylesheet_ref (CRStyleSheet *a_this) ; gboolean cr_stylesheet_unref (CRStyleSheet *a_this) ; diff --git a/src/st/st-theme.c b/src/st/st-theme.c index 20d42fd33..f5a9c54d4 100644 --- a/src/st/st-theme.c +++ b/src/st/st-theme.c @@ -274,8 +274,8 @@ st_theme_load_stylesheet (StTheme *theme, stylesheet->app_data = GUINT_TO_POINTER (TRUE); insert_stylesheet (theme, file, stylesheet); - cr_stylesheet_ref (stylesheet); - theme->custom_stylesheets = g_slist_prepend (theme->custom_stylesheets, stylesheet); + theme->custom_stylesheets = g_slist_prepend (theme->custom_stylesheets, + cr_stylesheet_ref (stylesheet)); g_signal_emit (theme, signals[STYLESHEETS_CHANGED], 0); return TRUE; From b832c55bb08fdb8730ef59a7c98a71734f57fe0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 17 Feb 2021 14:22:13 +0100 Subject: [PATCH 06/15] croco/stylesheet: Move app_data to private area and add DestroyNotify support Stylesheet data used to be a simple pointer modifiable by any application and never touched by libcroco itself. However it's useful to make the library control its lifecycle together with the stylesheet itself when a destroy notify function is defined. So, move the app_data pointer to the private area so that we prevent it to be modified from applications and add a GDestroyNotify function as well. As per this define a getter and setter that allows to manage it safely. --- src/st/croco/cr-stylesheet.c | 48 ++++++++++++++++++++++++++++++++++++ src/st/croco/cr-stylesheet.h | 9 +++---- src/st/st-theme.c | 9 ++++--- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/st/croco/cr-stylesheet.c b/src/st/croco/cr-stylesheet.c index bdbb4adf8..9fc9d4221 100644 --- a/src/st/croco/cr-stylesheet.c +++ b/src/st/croco/cr-stylesheet.c @@ -37,6 +37,16 @@ typedef struct { *cr_stylesheet_unref() */ gulong ref_count; + + /** + *custom application data pointer + *Can be used by applications. + *libcroco itself will handle its destruction + *if app_data_destroy_func is set via + *cr_stylesheet_set_app_data(). + */ + gpointer app_data; + GDestroyNotify app_data_destroy_func; } CRStyleSheetReal; /** @@ -175,6 +185,15 @@ cr_stylesheet_unref (CRStyleSheet * a_this) return FALSE; } +static void +cleanup_app_data (CRStyleSheetReal * real) +{ + if (real->app_data_destroy_func) { + g_clear_pointer (&real->app_data, real->app_data_destroy_func); + real->app_data_destroy_func = NULL; + } +} + /** *Destructor of the #CRStyleSheet class. *@param a_this the current instance of the #CRStyleSheet class. @@ -182,11 +201,40 @@ cr_stylesheet_unref (CRStyleSheet * a_this) void cr_stylesheet_destroy (CRStyleSheet * a_this) { + CRStyleSheetReal *real = (CRStyleSheetReal *) a_this; + g_return_if_fail (a_this); if (a_this->statements) { cr_statement_destroy (a_this->statements); a_this->statements = NULL; } + + cleanup_app_data (real); g_free (a_this); } + +void +cr_stylesheet_set_app_data (CRStyleSheet * a_this, + gpointer app_data, + GDestroyNotify app_data_destroy_func) +{ + CRStyleSheetReal *real = (CRStyleSheetReal *) a_this; + + g_return_if_fail (a_this); + + cleanup_app_data (real); + + real->app_data = app_data; + real->app_data_destroy_func = app_data_destroy_func; +} + +gpointer +cr_stylesheet_get_app_data (CRStyleSheet *a_this) +{ + CRStyleSheetReal *real = (CRStyleSheetReal *) a_this; + + g_return_val_if_fail (a_this, NULL); + + return real->app_data; +} diff --git a/src/st/croco/cr-stylesheet.h b/src/st/croco/cr-stylesheet.h index 75347f3ab..701846bed 100644 --- a/src/st/croco/cr-stylesheet.h +++ b/src/st/croco/cr-stylesheet.h @@ -65,12 +65,6 @@ struct _CRStyleSheet /**custom data used by libcroco*/ gpointer croco_data ; - - /** - *custom application data pointer - *Can be used by applications. - */ - gpointer app_data ; } ; CRStyleSheet * cr_stylesheet_new (CRStatement *a_stmts) ; @@ -88,6 +82,9 @@ gboolean cr_stylesheet_unref (CRStyleSheet *a_this) ; void cr_stylesheet_destroy (CRStyleSheet *a_this) ; +void cr_stylesheet_set_app_data (CRStyleSheet *a_this, gpointer app_data, GDestroyNotify app_data_destroy_func); +gpointer cr_stylesheet_get_app_data (CRStyleSheet *a_this); + G_END_DECLS #endif /*__CR_STYLESHEET_H__*/ diff --git a/src/st/st-theme.c b/src/st/st-theme.c index f5a9c54d4..567b91319 100644 --- a/src/st/st-theme.c +++ b/src/st/st-theme.c @@ -207,7 +207,7 @@ parse_stylesheet (GFile *file, } /* Extension stylesheet */ - stylesheet->app_data = GUINT_TO_POINTER (FALSE); + cr_stylesheet_set_app_data (stylesheet, GUINT_TO_POINTER (FALSE), NULL); return stylesheet; } @@ -271,7 +271,7 @@ st_theme_load_stylesheet (StTheme *theme, if (!stylesheet) return FALSE; - stylesheet->app_data = GUINT_TO_POINTER (TRUE); + cr_stylesheet_set_app_data (stylesheet, GUINT_TO_POINTER (TRUE), NULL); insert_stylesheet (theme, file, stylesheet); theme->custom_stylesheets = g_slist_prepend (theme->custom_stylesheets, @@ -979,8 +979,9 @@ add_matched_properties (StTheme *a_this, static inline int get_origin (const CRDeclaration * decl) { - enum CRStyleOrigin origin = decl->parent_statement->parent_sheet->origin; - gboolean is_extension_sheet = GPOINTER_TO_UINT (decl->parent_statement->parent_sheet->app_data); + CRStyleSheet *stylesheet = decl->parent_statement->parent_sheet; + enum CRStyleOrigin origin = stylesheet->origin; + gboolean is_extension_sheet = GPOINTER_TO_UINT (cr_stylesheet_get_app_data (stylesheet)); if (decl->important) origin += ORIGIN_OFFSET_IMPORTANT; From e4cd1ed0c01188de68e20880b296a442ed66fad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 17 Feb 2021 15:30:20 +0100 Subject: [PATCH 07/15] croco: Coherently initialize objects ref counting to 1 Croco used to initialize most objects with a 0 ref-counting even though it will behave exactly the same as an object with just one reference at destruction time. Exception was CRDocHandler which already followed this schema. This is incoherent to what GLib does, given that at destruction time an object with 1 reference will be still destroyed ad unref phase, and that all the objects provide also a destroy function to ignore ref-counting we can safely just ensure we always init objects with valid ref-counting. This required some minor code adaptation inside croco itself as we need to ensure that we don't leak when an object is created and passed around if referenced again. --- src/st/croco/cr-cascade.c | 2 ++ src/st/croco/cr-declaration.c | 10 +++++----- src/st/croco/cr-input.c | 1 + src/st/croco/cr-om-parser.c | 11 +++++------ src/st/croco/cr-parser.c | 36 +++++++++-------------------------- src/st/croco/cr-selector.c | 3 ++- src/st/croco/cr-statement.c | 3 ++- src/st/croco/cr-stylesheet.c | 4 ++++ src/st/croco/cr-term.c | 1 + src/st/croco/cr-tknzr.c | 4 ++++ 10 files changed, 35 insertions(+), 40 deletions(-) diff --git a/src/st/croco/cr-cascade.c b/src/st/croco/cr-cascade.c index acfe8e3e5..781e4e540 100644 --- a/src/st/croco/cr-cascade.c +++ b/src/st/croco/cr-cascade.c @@ -74,6 +74,8 @@ cr_cascade_new (CRStyleSheet * a_author_sheet, return NULL; } + PRIVATE (result)->ref_count = 1; + if (a_author_sheet) { cr_cascade_set_sheet (result, a_author_sheet, ORIGIN_AUTHOR); } diff --git a/src/st/croco/cr-declaration.c b/src/st/croco/cr-declaration.c index 3aba74d0b..2f5c124f7 100644 --- a/src/st/croco/cr-declaration.c +++ b/src/st/croco/cr-declaration.c @@ -93,6 +93,7 @@ cr_declaration_new (CRStatement * a_statement, } result->property = a_property; result->value = a_value; + result->ref_count = 1; if (a_value) { cr_term_ref (a_value); @@ -144,7 +145,7 @@ cr_declaration_parse_from_buf (CRStatement * a_statement, result = cr_declaration_new (a_statement, property, value); if (result) { property = NULL; - value = NULL; + g_clear_pointer (&value, cr_term_unref); result->important = important; } @@ -215,7 +216,7 @@ cr_declaration_parse_list_from_buf (const guchar * a_str, result = cr_declaration_new (NULL, property, value); if (result) { property = NULL; - value = NULL; + g_clear_pointer (&value, cr_term_unref); result->important = important; } /*now, go parse the other declarations */ @@ -247,10 +248,9 @@ cr_declaration_parse_list_from_buf (const guchar * a_str, cur_decl = cr_declaration_new (NULL, property, value); if (cur_decl) { cur_decl->important = important; - result = cr_declaration_append (result, cur_decl); + result = cr_declaration_append (result, g_steal_pointer (&cur_decl)); + g_clear_pointer (&value, cr_term_unref); property = NULL; - value = NULL; - cur_decl = NULL; } else { break; } diff --git a/src/st/croco/cr-input.c b/src/st/croco/cr-input.c index a998e1281..17af9d2ec 100644 --- a/src/st/croco/cr-input.c +++ b/src/st/croco/cr-input.c @@ -98,6 +98,7 @@ cr_input_new_real (void) return NULL; } + PRIVATE (result)->ref_count = 1; PRIVATE (result)->free_in_buf = TRUE; return result; } diff --git a/src/st/croco/cr-om-parser.c b/src/st/croco/cr-om-parser.c index f9a3cba08..bc3111e9a 100644 --- a/src/st/croco/cr-om-parser.c +++ b/src/st/croco/cr-om-parser.c @@ -1052,13 +1052,12 @@ cr_om_parser_parse_paths_to_cascade (CROMParser * a_this, } } result = cr_cascade_new (sheets[0], sheets[1], sheets[2]); - if (!result) { - for (i = 0; i < 3; i++) { - cr_stylesheet_unref (sheets[i]); - sheets[i] = 0; - } + for (i = 0; i < 3; i++) + g_clear_pointer (&sheets[i], cr_stylesheet_unref); + + if (!result) return CR_ERROR; - } + *a_result = result; return CR_OK; } diff --git a/src/st/croco/cr-parser.c b/src/st/croco/cr-parser.c index d410ed2d2..820687543 100644 --- a/src/st/croco/cr-parser.c +++ b/src/st/croco/cr-parser.c @@ -1613,9 +1613,7 @@ cr_parser_parse_term (CRParser * a_this, CRTerm ** a_term) } cr_parsing_location_copy (&result->location, &location) ; - *a_term = cr_term_append_term (*a_term, result); - - result = NULL; + *a_term = cr_term_append_term (*a_term, g_steal_pointer (&result)); cr_parser_try_to_skip_spaces_and_comments (a_this); @@ -2799,11 +2797,11 @@ cr_parser_new_from_buf (guchar * a_buf, g_return_val_if_fail (input, NULL); result = cr_parser_new_from_input (input); - if (!result) { - cr_input_destroy (input); - input = NULL; + cr_input_unref (input); + + if (!result) return NULL; - } + return result; } @@ -2825,6 +2823,7 @@ cr_parser_new_from_input (CRInput * a_input) } result = cr_parser_new (tokenizer); + g_clear_pointer (&tokenizer, cr_tknzr_unref); g_return_val_if_fail (result, NULL); return result; @@ -2850,6 +2849,7 @@ cr_parser_new_from_file (const guchar * a_file_uri, enum CREncoding a_enc) } result = cr_parser_new (tokenizer); + cr_tknzr_unref (tokenizer); g_return_val_if_fail (result, NULL); return result; } @@ -2992,6 +2992,7 @@ cr_parser_parse_file (CRParser * a_this, g_return_val_if_fail (tknzr != NULL, CR_ERROR); status = cr_parser_set_tknzr (a_this, tknzr); + cr_tknzr_unref (tknzr); g_return_val_if_fail (status == CR_OK, CR_ERROR); status = cr_parser_parse (a_this); @@ -3357,14 +3358,6 @@ cr_parser_parse_ruleset (CRParser * a_this) if (PRIVATE (a_this)->sac_handler && PRIVATE (a_this)->sac_handler->start_selector) { - /* - *the selector is ref counted so that the parser's user - *can choose to keep it. - */ - if (selector) { - cr_selector_ref (selector); - } - PRIVATE (a_this)->sac_handler->start_selector (PRIVATE (a_this)->sac_handler, selector); start_selector = TRUE; @@ -3377,9 +3370,6 @@ cr_parser_parse_ruleset (CRParser * a_this) status = cr_parser_parse_declaration (a_this, &property, &expr, &is_important); - if (expr) { - cr_term_ref (expr); - } if (status == CR_OK && PRIVATE (a_this)->sac_handler && PRIVATE (a_this)->sac_handler->property) { @@ -3432,9 +3422,6 @@ cr_parser_parse_ruleset (CRParser * a_this) status = cr_parser_parse_declaration (a_this, &property, &expr, &is_important); - if (expr) { - cr_term_ref (expr); - } if (status == CR_OK && PRIVATE (a_this)->sac_handler && PRIVATE (a_this)->sac_handler->property) { @@ -3968,9 +3955,6 @@ cr_parser_parse_page (CRParser * a_this) */ if (PRIVATE (a_this)->sac_handler && PRIVATE (a_this)->sac_handler->property) { - if (css_expression) - cr_term_ref (css_expression); - PRIVATE (a_this)->sac_handler->property (PRIVATE (a_this)->sac_handler, property, css_expression, important); @@ -4022,7 +4006,6 @@ cr_parser_parse_page (CRParser * a_this) */ if (PRIVATE (a_this)->sac_handler && PRIVATE (a_this)->sac_handler->property) { - cr_term_ref (css_expression); PRIVATE (a_this)->sac_handler->property (PRIVATE (a_this)->sac_handler, property, css_expression, important); @@ -4269,7 +4252,6 @@ cr_parser_parse_font_face (CRParser * a_this) /* *here, call the relevant SAC handler. */ - cr_term_ref (css_expression); if (PRIVATE (a_this)->sac_handler && PRIVATE (a_this)->sac_handler->property) { PRIVATE (a_this)->sac_handler->property @@ -4304,7 +4286,6 @@ cr_parser_parse_font_face (CRParser * a_this) /* *here, call the relevant SAC handler. */ - cr_term_ref (css_expression); if (PRIVATE (a_this)->sac_handler->property) { PRIVATE (a_this)->sac_handler->property (PRIVATE (a_this)->sac_handler, @@ -4476,6 +4457,7 @@ cr_parser_parse_buf (CRParser * a_this, g_return_val_if_fail (tknzr != NULL, CR_ERROR); status = cr_parser_set_tknzr (a_this, tknzr); + cr_tknzr_unref (tknzr); g_return_val_if_fail (status == CR_OK, CR_ERROR); status = cr_parser_parse (a_this); diff --git a/src/st/croco/cr-selector.c b/src/st/croco/cr-selector.c index 7a346f139..6bc94a3a9 100644 --- a/src/st/croco/cr-selector.c +++ b/src/st/croco/cr-selector.c @@ -46,6 +46,7 @@ cr_selector_new (CRSimpleSel * a_simple_sel) return NULL; } result->simple_sel = a_simple_sel; + result->ref_count = 1; return result; } @@ -133,7 +134,7 @@ cr_selector_append_simple_sel (CRSelector * a_this, selector = cr_selector_new (a_simple_sel); g_return_val_if_fail (selector, NULL); - return cr_selector_append (a_this, selector); + return cr_selector_append (a_this, g_steal_pointer (&selector)); } guchar * diff --git a/src/st/croco/cr-statement.c b/src/st/croco/cr-statement.c index fe62c3030..081ae67a6 100644 --- a/src/st/croco/cr-statement.c +++ b/src/st/croco/cr-statement.c @@ -204,7 +204,8 @@ parse_page_property_cb (CRDocHandler * a_this, g_return_if_fail (decl); decl->important = a_important; stmt->kind.page_rule->decl_list = - cr_declaration_append (stmt->kind.page_rule->decl_list, decl); + cr_declaration_append (stmt->kind.page_rule->decl_list, + g_steal_pointer (&decl)); g_return_if_fail (stmt->kind.page_rule->decl_list); } diff --git a/src/st/croco/cr-stylesheet.c b/src/st/croco/cr-stylesheet.c index 9fc9d4221..d825dd8c7 100644 --- a/src/st/croco/cr-stylesheet.c +++ b/src/st/croco/cr-stylesheet.c @@ -58,6 +58,7 @@ CRStyleSheet * cr_stylesheet_new (CRStatement * a_stmts) { CRStyleSheet *result; + CRStyleSheetReal *real; result = g_try_malloc0 (sizeof (CRStyleSheetReal)); if (!result) { @@ -65,6 +66,9 @@ cr_stylesheet_new (CRStatement * a_stmts) return NULL; } + real = (CRStyleSheetReal *) result; + real->ref_count = 1; + if (a_stmts) result->statements = a_stmts; diff --git a/src/st/croco/cr-term.c b/src/st/croco/cr-term.c index 412b09ba0..2f3c98592 100644 --- a/src/st/croco/cr-term.c +++ b/src/st/croco/cr-term.c @@ -91,6 +91,7 @@ cr_term_new (void) cr_utils_trace_info ("Out of memory"); return NULL; } + result->ref_count = 1; return result; } diff --git a/src/st/croco/cr-tknzr.c b/src/st/croco/cr-tknzr.c index a2219cde6..bbe1e0181 100644 --- a/src/st/croco/cr-tknzr.c +++ b/src/st/croco/cr-tknzr.c @@ -1590,6 +1590,8 @@ cr_tknzr_new (CRInput * a_input) return NULL; } + PRIVATE (result)->ref_count = 1; + if (a_input) cr_tknzr_set_input (result, a_input); return result; @@ -1609,6 +1611,7 @@ cr_tknzr_new_from_buf (guchar * a_buf, gulong a_len, g_return_val_if_fail (input != NULL, NULL); result = cr_tknzr_new (input); + g_clear_pointer (&input, cr_input_unref); return result; } @@ -1624,6 +1627,7 @@ cr_tknzr_new_from_uri (const guchar * a_file_uri, g_return_val_if_fail (input != NULL, NULL); result = cr_tknzr_new (input); + g_clear_pointer (&input, cr_input_unref); return result; } From 33ae810af247be3436851da3fa5eb48f7aabfffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 17 Feb 2021 15:44:56 +0100 Subject: [PATCH 08/15] croco: Ensure ref counting is a valid value when unreffing As per previous commit we always initialize the objects with a positive ref count, so we can stop handling 0-ref-count as 1-ref-count when unreffing and always require this to be a positive value. --- src/st/croco/cr-cascade.c | 5 +++-- src/st/croco/cr-declaration.c | 5 ++--- src/st/croco/cr-doc-handler.c | 5 ++--- src/st/croco/cr-input.c | 5 ++--- src/st/croco/cr-selector.c | 5 ++--- src/st/croco/cr-stylesheet.c | 6 ++---- src/st/croco/cr-term.c | 5 ++--- src/st/croco/cr-tknzr.c | 5 ++--- 8 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/st/croco/cr-cascade.c b/src/st/croco/cr-cascade.c index 781e4e540..f3963cb82 100644 --- a/src/st/croco/cr-cascade.c +++ b/src/st/croco/cr-cascade.c @@ -172,8 +172,9 @@ cr_cascade_unref (CRCascade * a_this) { g_return_if_fail (a_this && PRIVATE (a_this)); - if (PRIVATE (a_this)->ref_count) - PRIVATE (a_this)->ref_count--; + g_assert (PRIVATE (a_this)->ref_count > 0); + PRIVATE (a_this)->ref_count--; + if (!PRIVATE (a_this)->ref_count) { cr_cascade_destroy (a_this); } diff --git a/src/st/croco/cr-declaration.c b/src/st/croco/cr-declaration.c index 2f5c124f7..0c3baef44 100644 --- a/src/st/croco/cr-declaration.c +++ b/src/st/croco/cr-declaration.c @@ -744,9 +744,8 @@ cr_declaration_unref (CRDeclaration * a_this) { g_return_val_if_fail (a_this, FALSE); - if (a_this->ref_count) { - a_this->ref_count--; - } + g_assert (a_this->ref_count > 0); + a_this->ref_count--; if (a_this->ref_count == 0) { cr_declaration_destroy (a_this); diff --git a/src/st/croco/cr-doc-handler.c b/src/st/croco/cr-doc-handler.c index bd3554676..af59e0c83 100644 --- a/src/st/croco/cr-doc-handler.c +++ b/src/st/croco/cr-doc-handler.c @@ -224,9 +224,8 @@ cr_doc_handler_unref (CRDocHandler * a_this) { g_return_val_if_fail (a_this, FALSE); - if (PRIVATE (a_this)->ref_count > 0) { - PRIVATE (a_this)->ref_count--; - } + g_assert (PRIVATE (a_this)->ref_count > 0); + PRIVATE (a_this)->ref_count--; if (PRIVATE (a_this)->ref_count == 0) { cr_doc_handler_destroy (a_this); diff --git a/src/st/croco/cr-input.c b/src/st/croco/cr-input.c index 17af9d2ec..d8b8996a8 100644 --- a/src/st/croco/cr-input.c +++ b/src/st/croco/cr-input.c @@ -316,9 +316,8 @@ cr_input_unref (CRInput * a_this) { g_return_val_if_fail (a_this, FALSE); - if (PRIVATE (a_this)->ref_count) { - PRIVATE (a_this)->ref_count--; - } + g_assert (PRIVATE(a_this)->ref_count > 0); + PRIVATE(a_this)->ref_count--; if (PRIVATE (a_this)->ref_count == 0) { cr_input_destroy (a_this); diff --git a/src/st/croco/cr-selector.c b/src/st/croco/cr-selector.c index 6bc94a3a9..ed067c269 100644 --- a/src/st/croco/cr-selector.c +++ b/src/st/croco/cr-selector.c @@ -235,9 +235,8 @@ cr_selector_unref (CRSelector * a_this) { g_return_val_if_fail (a_this, FALSE); - if (a_this->ref_count) { - a_this->ref_count--; - } + g_assert (a_this->ref_count > 0); + a_this->ref_count--; if (a_this->ref_count == 0) { cr_selector_destroy (a_this); diff --git a/src/st/croco/cr-stylesheet.c b/src/st/croco/cr-stylesheet.c index d825dd8c7..384e7d6aa 100644 --- a/src/st/croco/cr-stylesheet.c +++ b/src/st/croco/cr-stylesheet.c @@ -176,10 +176,8 @@ cr_stylesheet_unref (CRStyleSheet * a_this) { CRStyleSheetReal *real = (CRStyleSheetReal *) a_this; - g_return_val_if_fail (a_this, FALSE); - - if (real->ref_count) - real->ref_count--; + g_assert (real->ref_count > 0); + real->ref_count--; if (!real->ref_count) { cr_stylesheet_destroy (a_this); diff --git a/src/st/croco/cr-term.c b/src/st/croco/cr-term.c index 2f3c98592..1bc71187b 100644 --- a/src/st/croco/cr-term.c +++ b/src/st/croco/cr-term.c @@ -750,9 +750,8 @@ cr_term_unref (CRTerm * a_this) { g_return_val_if_fail (a_this, FALSE); - if (a_this->ref_count) { - a_this->ref_count--; - } + g_assert (a_this->ref_count > 0); + a_this->ref_count--; if (a_this->ref_count == 0) { cr_term_destroy (a_this); diff --git a/src/st/croco/cr-tknzr.c b/src/st/croco/cr-tknzr.c index bbe1e0181..6c1cbfddd 100644 --- a/src/st/croco/cr-tknzr.c +++ b/src/st/croco/cr-tknzr.c @@ -1645,9 +1645,8 @@ cr_tknzr_unref (CRTknzr * a_this) { g_return_val_if_fail (a_this, FALSE); - if (PRIVATE (a_this)->ref_count > 0) { - PRIVATE (a_this)->ref_count--; - } + g_assert (PRIVATE (a_this)->ref_count > 0); + PRIVATE (a_this)->ref_count--; if (PRIVATE (a_this)->ref_count == 0) { cr_tknzr_destroy (a_this); From 185b1296621d0fe29815498d96b310628fbbfb80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Sat, 4 May 2024 02:18:35 +0200 Subject: [PATCH 09/15] croco: Use grefcount to manage refcounting GLib provides already all the primitives we need without having to handle all this manually ourself, so let's use them. --- src/st/croco/cr-cascade.c | 11 ++++------- src/st/croco/cr-declaration.c | 9 +++------ src/st/croco/cr-declaration.h | 2 +- src/st/croco/cr-doc-handler.c | 11 ++++------- src/st/croco/cr-input.c | 11 ++++------- src/st/croco/cr-selector.c | 9 +++------ src/st/croco/cr-selector.h | 2 +- src/st/croco/cr-stylesheet.c | 11 ++++------- src/st/croco/cr-term.c | 9 +++------ src/st/croco/cr-term.h | 2 +- src/st/croco/cr-tknzr.c | 11 ++++------- 11 files changed, 32 insertions(+), 56 deletions(-) diff --git a/src/st/croco/cr-cascade.c b/src/st/croco/cr-cascade.c index f3963cb82..d6d9874eb 100644 --- a/src/st/croco/cr-cascade.c +++ b/src/st/croco/cr-cascade.c @@ -42,7 +42,7 @@ typedef struct _CRCascadeReal { *of sheets[ORIGIN_UA] ; */ CRStyleSheet *sheets[3]; - guint ref_count; + grefcount ref_count; } CRCascadeReal; /** @@ -74,7 +74,7 @@ cr_cascade_new (CRStyleSheet * a_author_sheet, return NULL; } - PRIVATE (result)->ref_count = 1; + g_ref_count_init (&PRIVATE (result)->ref_count); if (a_author_sheet) { cr_cascade_set_sheet (result, a_author_sheet, ORIGIN_AUTHOR); @@ -154,7 +154,7 @@ cr_cascade_ref (CRCascade * a_this) { g_return_if_fail (a_this && PRIVATE (a_this)); - PRIVATE (a_this)->ref_count++; + g_ref_count_inc (&PRIVATE (a_this)->ref_count); } /** @@ -172,10 +172,7 @@ cr_cascade_unref (CRCascade * a_this) { g_return_if_fail (a_this && PRIVATE (a_this)); - g_assert (PRIVATE (a_this)->ref_count > 0); - PRIVATE (a_this)->ref_count--; - - if (!PRIVATE (a_this)->ref_count) { + if (g_ref_count_dec (&PRIVATE (a_this)->ref_count)) { cr_cascade_destroy (a_this); } } diff --git a/src/st/croco/cr-declaration.c b/src/st/croco/cr-declaration.c index 0c3baef44..c72113cc5 100644 --- a/src/st/croco/cr-declaration.c +++ b/src/st/croco/cr-declaration.c @@ -93,7 +93,7 @@ cr_declaration_new (CRStatement * a_statement, } result->property = a_property; result->value = a_value; - result->ref_count = 1; + g_ref_count_init (&result->ref_count); if (a_value) { cr_term_ref (a_value); @@ -726,7 +726,7 @@ cr_declaration_ref (CRDeclaration * a_this) { g_return_if_fail (a_this); - a_this->ref_count++; + g_ref_count_inc (&a_this->ref_count); } /** @@ -744,10 +744,7 @@ cr_declaration_unref (CRDeclaration * a_this) { g_return_val_if_fail (a_this, FALSE); - g_assert (a_this->ref_count > 0); - a_this->ref_count--; - - if (a_this->ref_count == 0) { + if (g_ref_count_dec (&a_this->ref_count)) { cr_declaration_destroy (a_this); return TRUE; } diff --git a/src/st/croco/cr-declaration.h b/src/st/croco/cr-declaration.h index eee8be321..88c571966 100644 --- a/src/st/croco/cr-declaration.h +++ b/src/st/croco/cr-declaration.h @@ -64,7 +64,7 @@ struct _CRDeclaration /*does the declaration have the important keyword ?*/ gboolean important ; - glong ref_count ; + grefcount ref_count ; CRParsingLocation location ; /*reserved for future usage*/ diff --git a/src/st/croco/cr-doc-handler.c b/src/st/croco/cr-doc-handler.c index af59e0c83..c55bb3b86 100644 --- a/src/st/croco/cr-doc-handler.c +++ b/src/st/croco/cr-doc-handler.c @@ -38,7 +38,7 @@ typedef struct _CRDocHandlerReal { CRDocHandler parent; - gulong ref_count; + grefcount ref_count; /** *This pointer is to hold an application parsing context. @@ -81,7 +81,7 @@ cr_doc_handler_new (void) cr_doc_handler_set_default_sac_handler (result); - PRIVATE (result)->ref_count++; + g_ref_count_init (&PRIVATE (result)->ref_count); return result; } @@ -207,7 +207,7 @@ cr_doc_handler_ref (CRDocHandler * a_this) { g_return_if_fail (a_this); - PRIVATE (a_this)->ref_count++; + g_ref_count_inc (&PRIVATE (a_this)->ref_count); } /** @@ -224,10 +224,7 @@ cr_doc_handler_unref (CRDocHandler * a_this) { g_return_val_if_fail (a_this, FALSE); - g_assert (PRIVATE (a_this)->ref_count > 0); - PRIVATE (a_this)->ref_count--; - - if (PRIVATE (a_this)->ref_count == 0) { + if (g_ref_count_dec (&PRIVATE (a_this)->ref_count)) { cr_doc_handler_destroy (a_this); return TRUE; } diff --git a/src/st/croco/cr-input.c b/src/st/croco/cr-input.c index d8b8996a8..23efe5004 100644 --- a/src/st/croco/cr-input.c +++ b/src/st/croco/cr-input.c @@ -74,7 +74,7 @@ typedef struct _CRInputReal { *the reference count of this *instance. */ - guint ref_count; + grefcount ref_count; gboolean free_in_buf; } CRInputReal; @@ -98,7 +98,7 @@ cr_input_new_real (void) return NULL; } - PRIVATE (result)->ref_count = 1; + g_ref_count_init (&PRIVATE (result)->ref_count); PRIVATE (result)->free_in_buf = TRUE; return result; } @@ -298,7 +298,7 @@ cr_input_ref (CRInput * a_this) { g_return_if_fail (a_this); - PRIVATE (a_this)->ref_count++; + g_ref_count_inc (&PRIVATE (a_this)->ref_count); } /** @@ -316,10 +316,7 @@ cr_input_unref (CRInput * a_this) { g_return_val_if_fail (a_this, FALSE); - g_assert (PRIVATE(a_this)->ref_count > 0); - PRIVATE(a_this)->ref_count--; - - if (PRIVATE (a_this)->ref_count == 0) { + if (g_ref_count_dec (&PRIVATE (a_this)->ref_count)) { cr_input_destroy (a_this); return TRUE; } diff --git a/src/st/croco/cr-selector.c b/src/st/croco/cr-selector.c index ed067c269..5ca71e508 100644 --- a/src/st/croco/cr-selector.c +++ b/src/st/croco/cr-selector.c @@ -46,7 +46,7 @@ cr_selector_new (CRSimpleSel * a_simple_sel) return NULL; } result->simple_sel = a_simple_sel; - result->ref_count = 1; + g_ref_count_init (&result->ref_count); return result; } @@ -214,7 +214,7 @@ cr_selector_ref (CRSelector * a_this) { g_return_if_fail (a_this); - a_this->ref_count++; + g_ref_count_inc (&a_this->ref_count); } /** @@ -235,10 +235,7 @@ cr_selector_unref (CRSelector * a_this) { g_return_val_if_fail (a_this, FALSE); - g_assert (a_this->ref_count > 0); - a_this->ref_count--; - - if (a_this->ref_count == 0) { + if (g_ref_count_dec (&a_this->ref_count)) { cr_selector_destroy (a_this); return TRUE; } diff --git a/src/st/croco/cr-selector.h b/src/st/croco/cr-selector.h index dd6a7f786..bed096df9 100644 --- a/src/st/croco/cr-selector.h +++ b/src/st/croco/cr-selector.h @@ -65,7 +65,7 @@ struct _CRSelector CRSelector *next ; CRSelector *prev ; CRParsingLocation location ; - glong ref_count ; + grefcount ref_count; }; CRSelector* cr_selector_new (CRSimpleSel *a_sel_expr) ; diff --git a/src/st/croco/cr-stylesheet.c b/src/st/croco/cr-stylesheet.c index 384e7d6aa..449aae282 100644 --- a/src/st/croco/cr-stylesheet.c +++ b/src/st/croco/cr-stylesheet.c @@ -36,7 +36,7 @@ typedef struct { *It can be manipulated with cr_stylesheet_ref() and *cr_stylesheet_unref() */ - gulong ref_count; + grefcount ref_count; /** *custom application data pointer @@ -67,7 +67,7 @@ cr_stylesheet_new (CRStatement * a_stmts) } real = (CRStyleSheetReal *) result; - real->ref_count = 1; + g_ref_count_init (&real->ref_count); if (a_stmts) result->statements = a_stmts; @@ -166,7 +166,7 @@ cr_stylesheet_ref (CRStyleSheet * a_this) g_return_val_if_fail (a_this, NULL); - real->ref_count++; + g_ref_count_inc (&real->ref_count); return a_this; } @@ -176,10 +176,7 @@ cr_stylesheet_unref (CRStyleSheet * a_this) { CRStyleSheetReal *real = (CRStyleSheetReal *) a_this; - g_assert (real->ref_count > 0); - real->ref_count--; - - if (!real->ref_count) { + if (g_ref_count_dec (&real->ref_count)) { cr_stylesheet_destroy (a_this); return TRUE; } diff --git a/src/st/croco/cr-term.c b/src/st/croco/cr-term.c index 1bc71187b..47db7fa76 100644 --- a/src/st/croco/cr-term.c +++ b/src/st/croco/cr-term.c @@ -91,7 +91,7 @@ cr_term_new (void) cr_utils_trace_info ("Out of memory"); return NULL; } - result->ref_count = 1; + g_ref_count_init (&result->ref_count); return result; } @@ -735,7 +735,7 @@ cr_term_ref (CRTerm * a_this) { g_return_if_fail (a_this); - a_this->ref_count++; + g_ref_count_inc (&a_this->ref_count); } /** @@ -750,10 +750,7 @@ cr_term_unref (CRTerm * a_this) { g_return_val_if_fail (a_this, FALSE); - g_assert (a_this->ref_count > 0); - a_this->ref_count--; - - if (a_this->ref_count == 0) { + if (g_ref_count_dec (&a_this->ref_count)) { cr_term_destroy (a_this); return TRUE; } diff --git a/src/st/croco/cr-term.h b/src/st/croco/cr-term.h index 0f22dda75..c1176f6aa 100644 --- a/src/st/croco/cr-term.h +++ b/src/st/croco/cr-term.h @@ -128,7 +128,7 @@ struct _CRTerm */ gpointer app_data ; - glong ref_count ; + grefcount ref_count ; /** *A pointer to the next term, diff --git a/src/st/croco/cr-tknzr.c b/src/st/croco/cr-tknzr.c index 6c1cbfddd..879d81e5a 100644 --- a/src/st/croco/cr-tknzr.c +++ b/src/st/croco/cr-tknzr.c @@ -59,7 +59,7 @@ typedef struct _CRTknzrReal { *of #CRTknzr. Is manipulated by cr_tknzr_ref() *and cr_tknzr_unref(). */ - glong ref_count; + grefcount ref_count; } CRTknzrReal; #define PRIVATE(obj) ((CRTknzrReal *) obj) @@ -1590,7 +1590,7 @@ cr_tknzr_new (CRInput * a_input) return NULL; } - PRIVATE (result)->ref_count = 1; + g_ref_count_init (&PRIVATE (result)->ref_count); if (a_input) cr_tknzr_set_input (result, a_input); @@ -1637,7 +1637,7 @@ cr_tknzr_ref (CRTknzr * a_this) { g_return_if_fail (a_this); - PRIVATE (a_this)->ref_count++; + g_ref_count_inc (&PRIVATE (a_this)->ref_count); } gboolean @@ -1645,10 +1645,7 @@ cr_tknzr_unref (CRTknzr * a_this) { g_return_val_if_fail (a_this, FALSE); - g_assert (PRIVATE (a_this)->ref_count > 0); - PRIVATE (a_this)->ref_count--; - - if (PRIVATE (a_this)->ref_count == 0) { + if (g_ref_count_dec (&PRIVATE (a_this)->ref_count)) { cr_tknzr_destroy (a_this); return TRUE; } From 6ad1c0a8ad3668d52b82393a2432060b79da4b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 17 Feb 2021 15:06:30 +0100 Subject: [PATCH 10/15] croco/stylesheet: Define the autoptr cleanup function for CRStyleSheet --- src/st/croco/cr-stylesheet.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/st/croco/cr-stylesheet.h b/src/st/croco/cr-stylesheet.h index 701846bed..ad5850647 100644 --- a/src/st/croco/cr-stylesheet.h +++ b/src/st/croco/cr-stylesheet.h @@ -85,6 +85,8 @@ void cr_stylesheet_destroy (CRStyleSheet *a_this) ; void cr_stylesheet_set_app_data (CRStyleSheet *a_this, gpointer app_data, GDestroyNotify app_data_destroy_func); gpointer cr_stylesheet_get_app_data (CRStyleSheet *a_this); +G_DEFINE_AUTOPTR_CLEANUP_FUNC (CRStyleSheet, cr_stylesheet_unref); + G_END_DECLS #endif /*__CR_STYLESHEET_H__*/ From be214e41ae60f8f0e849134e8e03314b527b118b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 17 Feb 2021 17:15:19 +0100 Subject: [PATCH 11/15] st/theme: Adapt stylesheets refcounting to croco changes Given that now croco initializes referenced objects we need to unref them when they are not required anymore. We've just an entry point to create stylesheets so we can just unref its return value all the times, and in case we keep a reference when the stylesheet is saved. --- src/st/st-theme.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/st/st-theme.c b/src/st/st-theme.c index 567b91319..48775c728 100644 --- a/src/st/st-theme.c +++ b/src/st/st-theme.c @@ -265,7 +265,7 @@ st_theme_load_stylesheet (StTheme *theme, GFile *file, GError **error) { - CRStyleSheet *stylesheet; + g_autoptr(CRStyleSheet) stylesheet = NULL; stylesheet = parse_stylesheet (file, error); if (!stylesheet) @@ -275,7 +275,7 @@ st_theme_load_stylesheet (StTheme *theme, insert_stylesheet (theme, file, stylesheet); theme->custom_stylesheets = g_slist_prepend (theme->custom_stylesheets, - cr_stylesheet_ref (stylesheet)); + g_steal_pointer (&stylesheet)); g_signal_emit (theme, signals[STYLESHEETS_CHANGED], 0); return TRUE; @@ -345,9 +345,9 @@ static void st_theme_constructed (GObject *object) { StTheme *theme = ST_THEME (object); - CRStyleSheet *application_stylesheet; - CRStyleSheet *theme_stylesheet; - CRStyleSheet *default_stylesheet; + g_autoptr(CRStyleSheet) application_stylesheet = NULL; + g_autoptr(CRStyleSheet) theme_stylesheet = NULL; + g_autoptr(CRStyleSheet) default_stylesheet = NULL; G_OBJECT_CLASS (st_theme_parent_class)->constructed (object); @@ -903,7 +903,7 @@ add_matched_properties (StTheme *a_this, if (import_rule->sheet) { insert_stylesheet (a_this, file, import_rule->sheet); - /* refcount of stylesheets starts off at zero, so we don't need to unref! */ + cr_stylesheet_unref (import_rule->sheet); } else { From 5be217db3928fc024281cb91fe83caa25e27bd6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Tue, 7 May 2019 01:28:40 +0200 Subject: [PATCH 12/15] st/theme: Use CRStyleSheet app_data instead of hash map Use the CRStyleSheet field to save stylesheet details instead of using an extra hash table. This way we can access to the stylesheet file faster without having to look it up. Related-to: https://gitlab.gnome.org/GNOME/gnome-shell/issues/1265 --- src/st/st-theme.c | 91 ++++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 36 deletions(-) diff --git a/src/st/st-theme.c b/src/st/st-theme.c index 48775c728..3b8d43c35 100644 --- a/src/st/st-theme.c +++ b/src/st/st-theme.c @@ -67,11 +67,23 @@ struct _StTheme GSList *custom_stylesheets; GHashTable *stylesheets_by_file; - GHashTable *files_by_stylesheet; CRCascade *cascade; }; +typedef struct _StyleSheetData +{ + GFile *file; + gboolean extension_stylesheet; +} StyleSheetData; + +static void +stylesheet_data_free (StyleSheetData *stylesheet_data) +{ + g_clear_object (&stylesheet_data->file); + g_free (stylesheet_data); +} + enum { PROP_0, @@ -110,9 +122,10 @@ file_equal0 (GFile *file1, static void st_theme_init (StTheme *theme) { - theme->stylesheets_by_file = g_hash_table_new_full (g_file_hash, (GEqualFunc) g_file_equal, - (GDestroyNotify)g_object_unref, (GDestroyNotify)cr_stylesheet_unref); - theme->files_by_stylesheet = g_hash_table_new (g_direct_hash, g_direct_equal); + theme->stylesheets_by_file = + g_hash_table_new_full (g_file_hash, (GEqualFunc) g_file_equal, + (GDestroyNotify) g_object_unref, + (GDestroyNotify) cr_stylesheet_unref); } static void @@ -206,9 +219,6 @@ parse_stylesheet (GFile *file, return NULL; } - /* Extension stylesheet */ - cr_stylesheet_set_app_data (stylesheet, GUINT_TO_POINTER (FALSE), NULL); - return stylesheet; } @@ -235,19 +245,28 @@ parse_stylesheet_nofail (GFile *file) return result; } -static void +static gboolean insert_stylesheet (StTheme *theme, GFile *file, CRStyleSheet *stylesheet) { + StyleSheetData *stylesheet_data; + if (stylesheet == NULL) - return; + return FALSE; - g_object_ref (file); - cr_stylesheet_ref (stylesheet); + if (g_hash_table_contains (theme->stylesheets_by_file, file)) + return FALSE; - g_hash_table_insert (theme->stylesheets_by_file, file, stylesheet); - g_hash_table_insert (theme->files_by_stylesheet, stylesheet, file); + stylesheet_data = g_new0 (StyleSheetData, 1); + stylesheet_data->file = g_object_ref (file); + + cr_stylesheet_set_app_data (stylesheet, stylesheet_data, + (GDestroyNotify) stylesheet_data_free); + + g_hash_table_insert (theme->stylesheets_by_file, + g_object_ref (file), cr_stylesheet_ref (stylesheet)); + return TRUE; } /** @@ -266,14 +285,15 @@ st_theme_load_stylesheet (StTheme *theme, GError **error) { g_autoptr(CRStyleSheet) stylesheet = NULL; + StyleSheetData *stylesheet_data; stylesheet = parse_stylesheet (file, error); - if (!stylesheet) + if (!insert_stylesheet (theme, file, stylesheet)) return FALSE; - cr_stylesheet_set_app_data (stylesheet, GUINT_TO_POINTER (TRUE), NULL); + stylesheet_data = cr_stylesheet_get_app_data (stylesheet); + stylesheet_data->extension_stylesheet = TRUE; - insert_stylesheet (theme, file, stylesheet); theme->custom_stylesheets = g_slist_prepend (theme->custom_stylesheets, g_steal_pointer (&stylesheet)); g_signal_emit (theme, signals[STYLESHEETS_CHANGED], 0); @@ -302,16 +322,14 @@ st_theme_unload_stylesheet (StTheme *theme, if (!g_slist_find (theme->custom_stylesheets, stylesheet)) return; + g_hash_table_remove (theme->stylesheets_by_file, file); theme->custom_stylesheets = g_slist_remove (theme->custom_stylesheets, stylesheet); - g_signal_emit (theme, signals[STYLESHEETS_CHANGED], 0); - - /* We need to remove the entry from the hashtable after emitting the signal - * since we might still access the files_by_stylesheet hashtable in - * _st_theme_resolve_url() during the signal emission. + /* We need to unref the stylesheet after emitting the signal since we might + * still access the stylesheet in _st_theme_resolve_url() during the signal + * emission. */ - g_hash_table_remove (theme->stylesheets_by_file, file); - g_hash_table_remove (theme->files_by_stylesheet, stylesheet); + g_signal_emit (theme, signals[STYLESHEETS_CHANGED], 0); cr_stylesheet_unref (stylesheet); } @@ -333,9 +351,10 @@ st_theme_get_custom_stylesheets (StTheme *theme) for (iter = theme->custom_stylesheets; iter; iter = iter->next) { CRStyleSheet *stylesheet = iter->data; - GFile *file = g_hash_table_lookup (theme->files_by_stylesheet, stylesheet); + StyleSheetData *stylesheet_data = cr_stylesheet_get_app_data (stylesheet); - result = g_slist_prepend (result, g_object_ref (file)); + if (stylesheet_data && stylesheet_data->file) + result = g_slist_prepend (result, g_object_ref (stylesheet_data->file)); } return result; @@ -377,7 +396,6 @@ st_theme_finalize (GObject * object) theme->custom_stylesheets = NULL; g_hash_table_destroy (theme->stylesheets_by_file); - g_hash_table_destroy (theme->files_by_stylesheet); g_clear_object (&theme->application_stylesheet); g_clear_object (&theme->theme_stylesheet); @@ -894,18 +912,18 @@ add_matched_properties (StTheme *a_this, if (import_rule->url->stryng && import_rule->url->stryng->str) { + g_autoptr(CRStyleSheet) sheet = NULL; + file = _st_theme_resolve_url (a_this, a_nodesheet, import_rule->url->stryng->str); - import_rule->sheet = parse_stylesheet (file, NULL); + sheet = parse_stylesheet (file, NULL); + + if (insert_stylesheet (a_this, file, sheet)) + import_rule->sheet = sheet; } - if (import_rule->sheet) - { - insert_stylesheet (a_this, file, import_rule->sheet); - cr_stylesheet_unref (import_rule->sheet); - } - else + if (!import_rule->sheet) { /* Set a marker to avoid repeatedly trying to parse a non-existent or * broken stylesheet @@ -981,12 +999,12 @@ get_origin (const CRDeclaration * decl) { CRStyleSheet *stylesheet = decl->parent_statement->parent_sheet; enum CRStyleOrigin origin = stylesheet->origin; - gboolean is_extension_sheet = GPOINTER_TO_UINT (cr_stylesheet_get_app_data (stylesheet)); + StyleSheetData *sheet_data = cr_stylesheet_get_app_data (stylesheet); if (decl->important) origin += ORIGIN_OFFSET_IMPORTANT; - if (is_extension_sheet) + if (sheet_data && sheet_data->extension_stylesheet) origin += ORIGIN_OFFSET_EXTENSION; return origin; @@ -1065,8 +1083,9 @@ _st_theme_resolve_url (StTheme *theme, else if (base_stylesheet != NULL) { GFile *base_file = NULL, *parent; + StyleSheetData *stylesheet_data = cr_stylesheet_get_app_data (base_stylesheet); - base_file = g_hash_table_lookup (theme->files_by_stylesheet, base_stylesheet); + base_file = stylesheet_data->file; /* This is an internal function, if we get here with a bad @base_stylesheet we have a problem. */ From d5c3d285baf3ba1ee2f1160ecd5c99141b24cf78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Tue, 7 May 2019 08:59:11 +0200 Subject: [PATCH 13/15] st/theme: Remove custom stylesheets list Since we already mark the stylesheet loaded by extensions in the data, we don't need to use another list to go trough these as we can just iterate over the hash table with a minimum overhead, as this will normally contain just one default stylesheet and all the extension stylesheets anyways. --- src/st/st-theme.c | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/st/st-theme.c b/src/st/st-theme.c index 3b8d43c35..ea4a8425f 100644 --- a/src/st/st-theme.c +++ b/src/st/st-theme.c @@ -64,7 +64,6 @@ struct _StTheme GFile *application_stylesheet; GFile *default_stylesheet; GFile *theme_stylesheet; - GSList *custom_stylesheets; GHashTable *stylesheets_by_file; @@ -294,8 +293,6 @@ st_theme_load_stylesheet (StTheme *theme, stylesheet_data = cr_stylesheet_get_app_data (stylesheet); stylesheet_data->extension_stylesheet = TRUE; - theme->custom_stylesheets = g_slist_prepend (theme->custom_stylesheets, - g_steal_pointer (&stylesheet)); g_signal_emit (theme, signals[STYLESHEETS_CHANGED], 0); return TRUE; @@ -314,21 +311,22 @@ st_theme_unload_stylesheet (StTheme *theme, GFile *file) { CRStyleSheet *stylesheet; + StyleSheetData *stylesheet_data; stylesheet = g_hash_table_lookup (theme->stylesheets_by_file, file); if (!stylesheet) return; - if (!g_slist_find (theme->custom_stylesheets, stylesheet)) + stylesheet_data = cr_stylesheet_get_app_data (stylesheet); + if (!stylesheet_data || !stylesheet_data->extension_stylesheet) return; - g_hash_table_remove (theme->stylesheets_by_file, file); - theme->custom_stylesheets = g_slist_remove (theme->custom_stylesheets, stylesheet); - - /* We need to unref the stylesheet after emitting the signal since we might + /* We need to keep a reference while emitting the signal since we might * still access the stylesheet in _st_theme_resolve_url() during the signal * emission. */ + cr_stylesheet_ref (stylesheet); + g_hash_table_remove (theme->stylesheets_by_file, file); g_signal_emit (theme, signals[STYLESHEETS_CHANGED], 0); cr_stylesheet_unref (stylesheet); } @@ -346,14 +344,17 @@ GSList* st_theme_get_custom_stylesheets (StTheme *theme) { GSList *result = NULL; - GSList *iter; + GHashTableIter iter; + gpointer value; - for (iter = theme->custom_stylesheets; iter; iter = iter->next) + g_hash_table_iter_init (&iter, theme->stylesheets_by_file); + + while (g_hash_table_iter_next (&iter, NULL, &value)) { - CRStyleSheet *stylesheet = iter->data; + CRStyleSheet *stylesheet = value; StyleSheetData *stylesheet_data = cr_stylesheet_get_app_data (stylesheet); - if (stylesheet_data && stylesheet_data->file) + if (stylesheet_data && stylesheet_data->extension_stylesheet && stylesheet_data->file) result = g_slist_prepend (result, g_object_ref (stylesheet_data->file)); } @@ -391,10 +392,6 @@ st_theme_finalize (GObject * object) { StTheme *theme = ST_THEME (object); - g_slist_foreach (theme->custom_stylesheets, (GFunc) cr_stylesheet_unref, NULL); - g_slist_free (theme->custom_stylesheets); - theme->custom_stylesheets = NULL; - g_hash_table_destroy (theme->stylesheets_by_file); g_clear_object (&theme->application_stylesheet); @@ -1039,7 +1036,7 @@ _st_theme_get_matched_properties (StTheme *theme, enum CRStyleOrigin origin = 0; CRStyleSheet *sheet = NULL; GPtrArray *props = g_ptr_array_new (); - GSList *iter; + GHashTableIter iter; g_return_val_if_fail (ST_IS_THEME (theme), NULL); g_return_val_if_fail (ST_IS_THEME_NODE (node), NULL); @@ -1053,8 +1050,14 @@ _st_theme_get_matched_properties (StTheme *theme, add_matched_properties (theme, sheet, node, props); } - for (iter = theme->custom_stylesheets; iter; iter = iter->next) - add_matched_properties (theme, iter->data, node, props); + g_hash_table_iter_init (&iter, theme->stylesheets_by_file); + while (g_hash_table_iter_next (&iter, NULL, (gpointer) &sheet)) + { + StyleSheetData *sheet_data = cr_stylesheet_get_app_data (sheet); + + if (sheet_data && sheet_data->extension_stylesheet) + add_matched_properties (theme, sheet, node, props); + } /* We count on a stable sort here so that later declarations come * after earlier declarations */ From 4961a0b27796d9cc3ef04e41cde21a56f71a3c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Tue, 7 May 2019 01:33:36 +0200 Subject: [PATCH 14/15] st/theme: Use utility functions to finalize objects --- src/st/st-theme.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/st/st-theme.c b/src/st/st-theme.c index ea4a8425f..cf0341226 100644 --- a/src/st/st-theme.c +++ b/src/st/st-theme.c @@ -392,17 +392,13 @@ st_theme_finalize (GObject * object) { StTheme *theme = ST_THEME (object); - g_hash_table_destroy (theme->stylesheets_by_file); + g_clear_pointer (&theme->stylesheets_by_file, g_hash_table_destroy); g_clear_object (&theme->application_stylesheet); g_clear_object (&theme->theme_stylesheet); g_clear_object (&theme->default_stylesheet); - if (theme->cascade) - { - cr_cascade_unref (theme->cascade); - theme->cascade = NULL; - } + g_clear_pointer (&theme->cascade, cr_cascade_unref); G_OBJECT_CLASS (st_theme_parent_class)->finalize (object); } From 789539c6749e752f9f4f798eee84c93d9467d1b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Tue, 7 May 2019 01:40:13 +0200 Subject: [PATCH 15/15] st/theme: Use glib auto free/ptr features Use g_autofree and g_autoptr for managing memory in a smarter way. --- src/st/st-theme.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/st/st-theme.c b/src/st/st-theme.c index cf0341226..48b5e87bf 100644 --- a/src/st/st-theme.c +++ b/src/st/st-theme.c @@ -194,7 +194,7 @@ parse_stylesheet (GFile *file, { enum CRStatus status; CRStyleSheet *stylesheet; - char *contents; + g_autofree char *contents = NULL; gsize length; if (file == NULL) @@ -207,14 +207,11 @@ parse_stylesheet (GFile *file, length, CR_UTF_8, &stylesheet); - g_free (contents); - if (status != CR_OK) { - char *uri = g_file_get_uri (file); + g_autofree char *uri = g_file_get_uri (file); g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Error parsing stylesheet '%s'; errcode:%d", uri, status); - g_free (uri); return NULL; } @@ -901,7 +898,7 @@ add_matched_properties (StTheme *a_this, if (import_rule->sheet == NULL) { - GFile *file = NULL; + g_autoptr (GFile) file = NULL; if (import_rule->url->stryng && import_rule->url->stryng->str) { @@ -923,9 +920,6 @@ add_matched_properties (StTheme *a_this, */ import_rule->sheet = (CRStyleSheet *) - 1; } - - if (file) - g_object_unref (file); } if (import_rule->sheet != (CRStyleSheet *) - 1) @@ -1071,20 +1065,18 @@ _st_theme_resolve_url (StTheme *theme, CRStyleSheet *base_stylesheet, const char *url) { - char *scheme; + g_autofree char *scheme = NULL; GFile *resource; if ((scheme = g_uri_parse_scheme (url))) { - g_free (scheme); resource = g_file_new_for_uri (url); } else if (base_stylesheet != NULL) { - GFile *base_file = NULL, *parent; StyleSheetData *stylesheet_data = cr_stylesheet_get_app_data (base_stylesheet); - - base_file = stylesheet_data->file; + GFile *base_file = stylesheet_data->file; + g_autoptr (GFile) parent = NULL; /* This is an internal function, if we get here with a bad @base_stylesheet we have a problem. */ @@ -1092,8 +1084,6 @@ _st_theme_resolve_url (StTheme *theme, parent = g_file_get_parent (base_file); resource = g_file_resolve_relative_path (parent, url); - - g_object_unref (parent); } else {