diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2024-03-04 08:11:19 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2024-03-04 08:11:19 (GMT) |
commit | fad3328dbabccc33e984f2f9a39411e959dc03e1 (patch) | |
tree | 810175d04bacc667bc19032e929ffbe84436d136 /src/analysis/scan/exprs | |
parent | 7f25bd904e483ca90548c7001839a102090eb290 (diff) |
Share instances to avoid extra calls to g_object_[un]ref().
Diffstat (limited to 'src/analysis/scan/exprs')
-rw-r--r-- | src/analysis/scan/exprs/handler-int.h | 12 | ||||
-rw-r--r-- | src/analysis/scan/exprs/handler.c | 87 | ||||
-rw-r--r-- | src/analysis/scan/exprs/handler.h | 3 | ||||
-rw-r--r-- | src/analysis/scan/exprs/setcounter-int.h | 11 | ||||
-rw-r--r-- | src/analysis/scan/exprs/setcounter.c | 122 | ||||
-rw-r--r-- | src/analysis/scan/exprs/setcounter.h | 8 |
6 files changed, 218 insertions, 25 deletions
diff --git a/src/analysis/scan/exprs/handler-int.h b/src/analysis/scan/exprs/handler-int.h index a38dd03..e051b30 100644 --- a/src/analysis/scan/exprs/handler-int.h +++ b/src/analysis/scan/exprs/handler-int.h @@ -37,8 +37,13 @@ struct _GScanPatternHandler { GScanExpression parent; /* A laisser en premier */ - GSearchPattern **patterns; /* Motifs associés */ + union + { + const GSearchPattern **patterns; /* Motifs associés */ + GSearchPattern **ref_patterns; /* Motifs associés */ + }; size_t count; /* Nombre de ces motifs */ + bool shared; /* Définition de propriété */ ScanHandlerType type; /* Manipulation attendue */ @@ -53,7 +58,10 @@ struct _GScanPatternHandlerClass /* Met en place une manipulation de correspondances établies. */ -bool g_scan_pattern_handler_create(GScanPatternHandler *, GSearchPattern ** const, size_t, ScanHandlerType); +bool g_scan_pattern_handler_create_shared(GScanPatternHandler *, const GSearchPattern ** const, size_t, ScanHandlerType); + +/* Met en place une manipulation de correspondances établies. */ +bool g_scan_pattern_handler_create_and_ref(GScanPatternHandler *, GSearchPattern ** const, size_t, ScanHandlerType); diff --git a/src/analysis/scan/exprs/handler.c b/src/analysis/scan/exprs/handler.c index d40d00f..2706dae 100644 --- a/src/analysis/scan/exprs/handler.c +++ b/src/analysis/scan/exprs/handler.c @@ -124,6 +124,7 @@ static void g_scan_pattern_handler_init(GScanPatternHandler *handler) { handler->patterns = NULL; handler->count = 0; + handler->shared = true; handler->type = SHT_RAW; @@ -146,8 +147,9 @@ static void g_scan_pattern_handler_dispose(GScanPatternHandler *handler) { size_t i; /* Boucle de parcours */ - for (i = 0; i < handler->count; i++) - g_clear_object(&handler->patterns[i]); + if (!handler->shared) + for (i = 0; i < handler->count; i++) + g_clear_object(&handler->ref_patterns[i]); G_OBJECT_CLASS(g_scan_pattern_handler_parent_class)->dispose(G_OBJECT(handler)); @@ -190,13 +192,79 @@ static void g_scan_pattern_handler_finalize(GScanPatternHandler *handler) * * ******************************************************************************/ +GScanExpression *g_scan_pattern_handler_new_shared(const GSearchPattern ** const patterns, size_t count, ScanHandlerType type) +{ + GScanExpression *result; /* Structure à retourner */ + + result = g_object_new(G_TYPE_SCAN_PATTERN_HANDLER, NULL); + + if (!g_scan_pattern_handler_create_shared(G_SCAN_PATTERN_HANDLER(result), patterns, count, type)) + g_clear_object(&result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : handler = instance à initialiser pleinement. * +* patterns = motifs à impliquer. * +* count = quantité de ces motifs. * +* type = type de manipulation attendue. * +* * +* Description : Met en place une manipulation de correspondances établies. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_scan_pattern_handler_create_shared(GScanPatternHandler *handler, const GSearchPattern ** const patterns, size_t count, ScanHandlerType type) +{ + bool result; /* Bilan à retourner */ + + result = g_scan_expression_create(G_SCAN_EXPRESSION(handler), SRS_WAIT_FOR_SCAN); + if (!result) goto exit; + + handler->patterns = malloc(count * sizeof(GSearchPattern *)); + handler->count = count; + + memcpy(handler->patterns, patterns, count * sizeof(GSearchPattern *)); + + handler->shared = true; + + handler->type = type; + + exit: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : patterns = motifs à impliquer. * +* count = quantité de ces motifs. * +* type = type de manipulation attendue. * +* * +* Description : Met en place une manipulation de correspondances établies. * +* * +* Retour : Expression mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + GScanExpression *g_scan_pattern_handler_new(GSearchPattern ** const patterns, size_t count, ScanHandlerType type) { GScanExpression *result; /* Structure à retourner */ result = g_object_new(G_TYPE_SCAN_PATTERN_HANDLER, NULL); - if (!g_scan_pattern_handler_create(G_SCAN_PATTERN_HANDLER(result), patterns, count, type)) + if (!g_scan_pattern_handler_create_and_ref(G_SCAN_PATTERN_HANDLER(result), patterns, count, type)) g_clear_object(&result); return result; @@ -219,7 +287,7 @@ GScanExpression *g_scan_pattern_handler_new(GSearchPattern ** const patterns, si * * ******************************************************************************/ -bool g_scan_pattern_handler_create(GScanPatternHandler *handler, GSearchPattern ** const patterns, size_t count, ScanHandlerType type) +bool g_scan_pattern_handler_create_and_ref(GScanPatternHandler *handler, GSearchPattern ** const patterns, size_t count, ScanHandlerType type) { bool result; /* Bilan à retourner */ size_t i; /* Boucle de parcours */ @@ -230,11 +298,12 @@ bool g_scan_pattern_handler_create(GScanPatternHandler *handler, GSearchPattern handler->patterns = malloc(count * sizeof(GSearchPattern *)); handler->count = count; + memcpy(handler->patterns, patterns, count * sizeof(GSearchPattern *)); + for (i = 0; i < count; i++) - { - handler->patterns[i] = patterns[i]; g_object_ref(G_OBJECT(patterns[i])); - } + + handler->shared = false; handler->type = type; @@ -464,7 +533,7 @@ static bool g_scan_pattern_handler_get_item(const GScanPatternHandler *expr, siz bool result; /* Bilan à retourner */ size_t i; /* Boucle de parcours */ size_t count; /* Quantité de correspondances */ - GScanBytesMatches *matches; /* Correspondances d'un motif */ + GScanMatches *matches; /* Correspondances d'un motif */ const match_area_t *area; /* Zone de correspondance */ GBinContent *content; /* Contenu binaire à relire */ vmpa2t pos; /* Tête de lecture */ @@ -495,7 +564,7 @@ static bool g_scan_pattern_handler_get_item(const GScanPatternHandler *expr, siz matches = g_scan_context_get_full_matches(ctx, expr->patterns[i]); if (matches == NULL) goto done; - area = g_scan_bytes_matches_get(matches, index); + area = g_scan_bytes_matches_get(G_SCAN_BYTES_MATCHES(matches), index); if (area == NULL) goto done_with_matches; /* Traitement adapté de la requête */ diff --git a/src/analysis/scan/exprs/handler.h b/src/analysis/scan/exprs/handler.h index 96e9301..a1ddf98 100644 --- a/src/analysis/scan/exprs/handler.h +++ b/src/analysis/scan/exprs/handler.h @@ -60,6 +60,9 @@ typedef enum _ScanHandlerType GType g_scan_pattern_handler_get_type(void); /* Met en place une manipulation de correspondances établies. */ +GScanExpression *g_scan_pattern_handler_new_shared(const GSearchPattern ** const, size_t, ScanHandlerType); + +/* Met en place une manipulation de correspondances établies. */ GScanExpression *g_scan_pattern_handler_new(GSearchPattern ** const, size_t, ScanHandlerType); /* Indique le type de manipulation de correspondances spécifié. */ diff --git a/src/analysis/scan/exprs/setcounter-int.h b/src/analysis/scan/exprs/setcounter-int.h index fbed209..c9e3da5 100644 --- a/src/analysis/scan/exprs/setcounter-int.h +++ b/src/analysis/scan/exprs/setcounter-int.h @@ -37,8 +37,13 @@ struct _GScanSetMatchCounter { GScanExpression parent; /* A laisser en premier */ - GSearchPattern **patterns; /* Motifs associés */ + union + { + const GSearchPattern **patterns; /* Motifs associés */ + GSearchPattern **ref_patterns; /* Motifs associés */ + }; size_t count; /* Nombre de ces motifs */ + bool shared; /* Définition de propriété */ ScanSetCounterType type; /* Type de décompte */ size_t number; /* Eventuel volume associé */ @@ -53,9 +58,11 @@ struct _GScanSetMatchCounterClass }; +/* Met en place un décompte de motifs avec correspondances. */ +bool g_scan_set_match_counter_create_shared(GScanSetMatchCounter *, const GSearchPattern ** const, size_t); /* Met en place un décompte de motifs avec correspondances. */ -bool g_scan_set_match_counter_create(GScanSetMatchCounter *, GSearchPattern ** const, size_t); +bool g_scan_set_match_counter_create_and_ref(GScanSetMatchCounter *, GSearchPattern ** const, size_t); diff --git a/src/analysis/scan/exprs/setcounter.c b/src/analysis/scan/exprs/setcounter.c index da37746..bed315e 100644 --- a/src/analysis/scan/exprs/setcounter.c +++ b/src/analysis/scan/exprs/setcounter.c @@ -24,6 +24,10 @@ #include "setcounter.h" +#include <assert.h> +#include <string.h> + + #include "setcounter-int.h" #include "literal.h" @@ -109,6 +113,7 @@ static void g_scan_set_match_counter_init(GScanSetMatchCounter *counter) { counter->patterns = NULL; counter->count = 0; + counter->shared = true; counter->type = SSCT_NONE; counter->number = 0; @@ -132,8 +137,9 @@ static void g_scan_set_match_counter_dispose(GScanSetMatchCounter *counter) { size_t i; /* Boucle de parcours */ - for (i = 0; i < counter->count; i++) - g_clear_object(&counter->patterns[i]); + if (!counter->shared) + for (i = 0; i < counter->count; i++) + g_clear_object(&counter->ref_patterns[i]); G_OBJECT_CLASS(g_scan_set_match_counter_parent_class)->dispose(G_OBJECT(counter)); @@ -175,13 +181,75 @@ static void g_scan_set_match_counter_finalize(GScanSetMatchCounter *counter) * * ******************************************************************************/ +GScanExpression *g_scan_set_match_counter_new_shared(const GSearchPattern ** const patterns, size_t count) +{ + GScanExpression *result; /* Structure à retourner */ + + result = g_object_new(G_TYPE_SCAN_SET_MATCH_COUNTER, NULL); + + if (!g_scan_set_match_counter_create_shared(G_SCAN_SET_MATCH_COUNTER(result), patterns, count)) + g_clear_object(&result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : counter = instance à initialiser pleinement. * +* patterns = motifs à impliquer. * +* count = quantité de ces motifs. * +* * +* Description : Met en place un décompte de motifs avec correspondances. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_scan_set_match_counter_create_shared(GScanSetMatchCounter *counter, const GSearchPattern ** const patterns, size_t count) +{ + bool result; /* Bilan à retourner */ + + result = g_scan_expression_create(G_SCAN_EXPRESSION(counter), SRS_WAIT_FOR_SCAN); + if (!result) goto exit; + + counter->patterns = malloc(count * sizeof(GSearchPattern *)); + counter->count = count; + + memcpy(counter->patterns, patterns, count * sizeof(GSearchPattern *)); + + counter->shared = true; + + exit: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : patterns = motifs à impliquer. * +* count = quantité de ces motifs. * +* * +* Description : Constitue un décompte de motifs avec correspondances. * +* * +* Retour : Expression mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + GScanExpression *g_scan_set_match_counter_new(GSearchPattern ** const patterns, size_t count) { GScanExpression *result; /* Structure à retourner */ result = g_object_new(G_TYPE_SCAN_SET_MATCH_COUNTER, NULL); - if (!g_scan_set_match_counter_create(G_SCAN_SET_MATCH_COUNTER(result), patterns, count)) + if (!g_scan_set_match_counter_create_and_ref(G_SCAN_SET_MATCH_COUNTER(result), patterns, count)) g_clear_object(&result); return result; @@ -203,7 +271,7 @@ GScanExpression *g_scan_set_match_counter_new(GSearchPattern ** const patterns, * * ******************************************************************************/ -bool g_scan_set_match_counter_create(GScanSetMatchCounter *counter, GSearchPattern ** const patterns, size_t count) +bool g_scan_set_match_counter_create_and_ref(GScanSetMatchCounter *counter, GSearchPattern ** const patterns, size_t count) { bool result; /* Bilan à retourner */ size_t i; /* Boucle de parcours */ @@ -214,11 +282,12 @@ bool g_scan_set_match_counter_create(GScanSetMatchCounter *counter, GSearchPatte counter->patterns = malloc(count * sizeof(GSearchPattern *)); counter->count = count; + memcpy(counter->patterns, patterns, count * sizeof(GSearchPattern *)); + for (i = 0; i < count; i++) - { - counter->patterns[i] = patterns[i]; g_object_ref(G_OBJECT(patterns[i])); - } + + counter->shared = false; exit: @@ -241,21 +310,52 @@ bool g_scan_set_match_counter_create(GScanSetMatchCounter *counter, GSearchPatte * * ******************************************************************************/ -void g_scan_set_match_counter_add_extra_patterns(GScanSetMatchCounter *counter, GSearchPattern ** const patterns, size_t count) +void g_scan_set_match_counter_add_extra_shared_patterns(GScanSetMatchCounter *counter, const GSearchPattern ** const patterns, size_t count) +{ + size_t first; /* Premier emplacement libre */ + + assert(counter->shared); + + first = counter->count; + + counter->count += count; + counter->patterns = realloc(counter->patterns, counter->count * sizeof(GSearchPattern *)); + + memcpy(counter->patterns + first, patterns, count * sizeof(GSearchPattern *)); + +} + + +/****************************************************************************** +* * +* Paramètres : counter = décompte à compléter. * +* patterns = motifs à impliquer. * +* count = quantité de ces motifs. * +* * +* Description : Ajoute de nouveaux motifs à un ensemble à décompter. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void g_scan_set_match_counter_add_and_ref_extra_patterns(GScanSetMatchCounter *counter, GSearchPattern ** const patterns, size_t count) { size_t first; /* Premier emplacement libre */ size_t i; /* Boucle de parcours */ + assert(!counter->shared); + first = counter->count; counter->count += count; counter->patterns = realloc(counter->patterns, counter->count * sizeof(GSearchPattern *)); + memcpy(counter->patterns + first, patterns, count * sizeof(GSearchPattern *)); + for (i = 0; i < count; i++) - { - counter->patterns[first + i] = patterns[i]; g_object_ref(G_OBJECT(patterns[i])); - } } diff --git a/src/analysis/scan/exprs/setcounter.h b/src/analysis/scan/exprs/setcounter.h index 59762f9..28c92b4 100644 --- a/src/analysis/scan/exprs/setcounter.h +++ b/src/analysis/scan/exprs/setcounter.h @@ -52,10 +52,16 @@ typedef struct _GScanSetMatchCounterClass GScanSetMatchCounterClass; GType g_scan_set_match_counter_get_type(void); /* Met en place un décompte de correspondances obtenues. */ +GScanExpression *g_scan_set_match_counter_new_shared(const GSearchPattern ** const, size_t); + +/* Met en place un décompte de correspondances obtenues. */ GScanExpression *g_scan_set_match_counter_new(GSearchPattern ** const, size_t); /* Ajoute de nouveaux motifs à un ensemble à décompter. */ -void g_scan_set_match_counter_add_extra_patterns(GScanSetMatchCounter *, GSearchPattern ** const, size_t); +void g_scan_set_match_counter_add_extra_shared_patterns(GScanSetMatchCounter *, const GSearchPattern ** const, size_t); + +/* Ajoute de nouveaux motifs à un ensemble à décompter. */ +void g_scan_set_match_counter_add_and_ref_extra_patterns(GScanSetMatchCounter *, GSearchPattern ** const, size_t); /* Formes de volume de correspondances */ typedef enum _ScanSetCounterType |