diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/glibext/Makefile.am | 1 | ||||
| -rw-r--r-- | src/glibext/singleton-int.h | 64 | ||||
| -rw-r--r-- | src/glibext/singleton.c | 389 | ||||
| -rw-r--r-- | src/glibext/singleton.h | 97 | 
4 files changed, 551 insertions, 0 deletions
| diff --git a/src/glibext/Makefile.am b/src/glibext/Makefile.am index 07a72d1..6bcf4b8 100644 --- a/src/glibext/Makefile.am +++ b/src/glibext/Makefile.am @@ -32,6 +32,7 @@ libglibext_la_SOURCES =					\  	proto.h								\  	seq.h seq.c							\  	signal.h signal.c					\ +	singleton.h singleton.c				\  	widthtracker.h widthtracker.c  libglibext_la_LIBADD = 					\ diff --git a/src/glibext/singleton-int.h b/src/glibext/singleton-int.h new file mode 100644 index 0000000..ac31a32 --- /dev/null +++ b/src/glibext/singleton-int.h @@ -0,0 +1,64 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * singleton-int.h - définitions internes propres aux réductions du nombre d'instances d'un même type + * + * Copyright (C) 2021 Cyrille Bagard + * + *  This file is part of Chrysalide. + * + *  Chrysalide is free software; you can redistribute it and/or modify + *  it under the terms of the GNU General Public License as published by + *  the Free Software Foundation; either version 3 of the License, or + *  (at your option) any later version. + * + *  Chrysalide is distributed in the hope that it will be useful, + *  but WITHOUT ANY WARRANTY; without even the implied warranty of + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + *  GNU General Public License for more details. + * + *  You should have received a copy of the GNU General Public License + *  along with Chrysalide.  If not, see <http://www.gnu.org/licenses/>. + */ + + +#ifndef _GLIBEXT_SINGLETON_INT_H +#define _GLIBEXT_SINGLETON_INT_H + + +#include "singleton.h" + + + +/* Fournit l'empreinte d'un candidat à une centralisation. */ +typedef guint (* hash_candidate_fc) (const GSingletonCandidate *); + +/* Détermine si deux candidats à l'unicité sont identiques. */ +typedef gboolean (* is_candidate_equal_fc) (const GSingletonCandidate *, const GSingletonCandidate *); + +/* Marque un candidat comme traité ou en cours de traitement. */ +typedef void (* mark_candidate_as_processed_fc) (GSingletonCandidate *, bool); + +/* Indique si un objet marqué comme unique. */ +typedef bool (* is_candidate_processed_fc) (const GSingletonCandidate *, bool); + + +/* Instance d'objet visant à être unique (interface) */ +struct _GSingletonCandidateIface +{ +    GTypeInterface base_iface;              /* A laisser en premier        */ + +    hash_candidate_fc hash;                 /* Prise d'empreinte           */ +    is_candidate_equal_fc is_equal;         /* Comparaison                 */ + +    mark_candidate_as_processed_fc mark;    /* Définition de l'état        */ +    is_candidate_processed_fc is_processed; /* Consultation de l'état      */ + +}; + + +/* Redéfinition */ +typedef GSingletonCandidateIface GSingletonCandidateInterface; + + + +#endif  /* _GLIBEXT_SINGLETON_INT_H */ diff --git a/src/glibext/singleton.c b/src/glibext/singleton.c new file mode 100644 index 0000000..f0ce86f --- /dev/null +++ b/src/glibext/singleton.c @@ -0,0 +1,389 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * singleton.c - réduction du nombre d'instances d'un même type + * + * Copyright (C) 2021 Cyrille Bagard + * + *  This file is part of Chrysalide. + * + *  Chrysalide is free software; you can redistribute it and/or modify + *  it under the terms of the GNU General Public License as published by + *  the Free Software Foundation; either version 3 of the License, or + *  (at your option) any later version. + * + *  Chrysalide is distributed in the hope that it will be useful, + *  but WITHOUT ANY WARRANTY; without even the implied warranty of + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + *  GNU General Public License for more details. + * + *  You should have received a copy of the GNU General Public License + *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>. + */ + + +#include "singleton.h" + + +#include <assert.h> + + +#include "singleton-int.h" + + + +/* ------------------ INTERFACE POUR CANDIDAT A UNE CENTRALISATION ------------------ */ + +/* Procède à l'initialisation de l'interface de rassemblement. */ +static void g_singleton_candidate_default_init(GSingletonCandidateInterface *); + + + +/* ------------------------- COLLECTION D'INSTANCES UNIQUES ------------------------- */ + + +/* Définition d'un compacteur d'instances de types (instance) */ +struct _GSingletonFactory +{ +    GObject parent;                         /* A laisser en premier        */ + +    GHashTable *table;                      /* Suivi des conservations     */ +    GMutex access;                          /* Verrou pour la concurrence  */ + +}; + +/* Définition d'un compacteur d'instances de types (classe) */ +struct _GSingletonFactoryClass +{ +    GObjectClass parent;                    /* A laisser en premier        */ + +}; + + +/* Initialise la classe des compacteurs d'instances de types. */ +static void g_singleton_factory_class_init(GSingletonFactoryClass *); + +/* Initialise une instance de compacteur d'instances de types. */ +static void g_singleton_factory_init(GSingletonFactory *); + +/* Supprime toutes les références externes. */ +static void g_singleton_factory_dispose(GSingletonFactory *); + +/* Procède à la libération totale de la mémoire. */ +static void g_singleton_factory_finalize(GSingletonFactory *); + + + +/* ---------------------------------------------------------------------------------- */ +/*                    INTERFACE POUR CANDIDAT A UNE CENTRALISATION                    */ +/* ---------------------------------------------------------------------------------- */ + + +/* Détermine le type d'une interface pour la lecture de binaire. */ +G_DEFINE_INTERFACE(GSingletonCandidate, g_singleton_candidate, G_TYPE_OBJECT) + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : iface = interface GLib à initialiser.                        * +*                                                                             * +*  Description : Procède à l'initialisation de l'interface de rassemblement.  * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_singleton_candidate_default_init(GSingletonCandidateInterface *iface) +{ + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : candidate = objet dont l'instance se veut unique.            * +*                                                                             * +*  Description : Fournit l'empreinte d'un candidat à une centralisation.      * +*                                                                             * +*  Retour      : Empreinte de l'élément représenté.                           * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +guint g_singleton_candidate_hash(const GSingletonCandidate *candidate) +{ +    guint result;                           /* Valeur à retourner          */ +    GSingletonCandidateIface *iface;        /* Interface utilisée          */ + +    iface = G_SINGLETON_CANDIDATE_GET_IFACE(candidate); + +    result = iface->hash(candidate); + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : candidate = objet dont l'instance se veut unique.            * +*                other     = second élément à analyser.                       * +*                                                                             * +*  Description : Détermine si deux candidats à l'unicité sont identiques.     * +*                                                                             * +*  Retour      : Bilan de la comparaison.                                     * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +gboolean g_singleton_candidate_is_equal(const GSingletonCandidate *candidate, const GSingletonCandidate *other) +{ +    gboolean result;                        /* Bilan à renvoyer            */ +    GSingletonCandidateIface *iface;        /* Interface utilisée          */ + +    iface = G_SINGLETON_CANDIDATE_GET_IFACE(candidate); + +    result = iface->is_equal(candidate, other); + +    return result; + +} + + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : candidate = objet dont l'instance se veut unique.            * +*                soon      = indique un traitement démarré et en cours.       * +*                                                                             * +*  Description : Marque un candidat comme traité ou en cours de traitement.   * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +void g_singleton_candidate_mark_as_processed(GSingletonCandidate *candidate, bool soon) +{ +    GSingletonCandidateIface *iface;        /* Interface utilisée          */ + +    iface = G_SINGLETON_CANDIDATE_GET_IFACE(candidate); + +    iface->mark(candidate, soon); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : candidate = objet dont l'instance se veut unique.            * +*                soon      = indique un traitement démarré et en cours.       * +*                                                                             * +*  Description : Indique si un objet marqué comme unique.                     * +*                                                                             * +*  Retour      : true si l'objet est traité ou en phase de l'être, ou false.  * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +bool g_singleton_candidate_is_processed(const GSingletonCandidate *candidate, bool soon) +{ +    bool result;                            /* Statut à retourner          */ +    GSingletonCandidateIface *iface;        /* Interface utilisée          */ + +    iface = G_SINGLETON_CANDIDATE_GET_IFACE(candidate); + +    result = iface->is_processed(candidate, soon); + +    return result; + + +} + + + +/* ---------------------------------------------------------------------------------- */ +/*                           COLLECTION D'INSTANCES UNIQUES                           */ +/* ---------------------------------------------------------------------------------- */ + + +/* Indique le type défini pour une mémoire de types d'objets. */ +G_DEFINE_TYPE(GSingletonFactory, g_singleton_factory, G_TYPE_OBJECT); + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : klass = classe à initialiser.                                * +*                                                                             * +*  Description : Initialise la classe des compacteurs d'instances de types.   * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_singleton_factory_class_init(GSingletonFactoryClass *klass) +{ +    GObjectClass *object;                   /* Autre version de la classe  */ + +    object = G_OBJECT_CLASS(klass); + +    object->dispose = (GObjectFinalizeFunc/* ! */)g_singleton_factory_dispose; +    object->finalize = (GObjectFinalizeFunc)g_singleton_factory_finalize; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : factory = instance à initialiser.                            * +*                                                                             * +*  Description : Initialise une instance de compacteur d'instances de types.  * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_singleton_factory_init(GSingletonFactory *factory) +{ +    factory->table = g_hash_table_new_full((GHashFunc)g_singleton_candidate_hash, +                                           (GEqualFunc)g_singleton_candidate_is_equal, +                                           g_object_unref, NULL); + +    g_mutex_init(&factory->access); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : factory = instance d'objet GLib à traiter.                   * +*                                                                             * +*  Description : Supprime toutes les références externes.                     * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_singleton_factory_dispose(GSingletonFactory *factory) +{ +    if (factory->table != NULL) +    { +        g_hash_table_unref(factory->table); +        factory->table = NULL; +    } + +    G_OBJECT_CLASS(g_singleton_factory_parent_class)->dispose(G_OBJECT(factory)); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : factory = instance d'objet GLib à traiter.                   * +*                                                                             * +*  Description : Procède à la libération totale de la mémoire.                * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_singleton_factory_finalize(GSingletonFactory *factory) +{ +    g_mutex_clear(&factory->access); + +    G_OBJECT_CLASS(g_singleton_factory_parent_class)->finalize(G_OBJECT(factory)); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : -                                                            * +*                                                                             * +*  Description : Crée un compacteur d'instances de types.                     * +*                                                                             * +*  Retour      : Instance mise en place.                                      * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +GSingletonFactory *g_singleton_factory_new(void) +{ +    GSingletonFactory *result;                    /* Structure à retourner       */ + +    result = g_object_new(G_TYPE_SINGLETON_FACTORY, NULL); + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : factory   = gestionnaire d'instances uniques à consulter.    * +*                candidate = instance à conserver ou oublier.                 * +*                                                                             * +*  Description : Fournit l'instance unique correspondant à un objet.          * +*                                                                             * +*  Retour      : Instance unique à utiliser.                                  * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +GSingletonCandidate *g_singleton_factory_get_instance(GSingletonFactory *factory, GSingletonCandidate *candidate) +{ +    GSingletonCandidate *result;            /* Instance unique à retourner */ +#ifndef NDEBUG +    gboolean status;                         /* Validation d'une opération */ +#endif + +    g_mutex_lock(&factory->access); + +    if (g_hash_table_contains(factory->table, candidate)) +    { +#ifndef NDEBUG +        status = g_hash_table_lookup_extended(factory->table, candidate, (void **)&result, NULL); +        assert(status); +#else +        g_hash_table_lookup_extended(factory->table, candidate, (void **)&result, NULL); +#endif + +    } + +    else +    { +        g_object_ref(G_OBJECT(candidate)); + +#ifndef NDEBUG +        status = g_hash_table_add(factory->table, candidate); +        assert(status); +#else +        g_hash_table_add(factory->table, candidate); +#endif + +        result = candidate; + +    } + +    g_object_ref(G_OBJECT(result)); + +    g_mutex_unlock(&factory->access); + +    return result; + +} diff --git a/src/glibext/singleton.h b/src/glibext/singleton.h new file mode 100644 index 0000000..6de9f41 --- /dev/null +++ b/src/glibext/singleton.h @@ -0,0 +1,97 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * singleton.h - prototypes pour la réduction du nombre d'instances d'un même type + * + * Copyright (C) 2021 Cyrille Bagard + * + *  This file is part of Chrysalide. + * + *  Chrysalide is free software; you can redistribute it and/or modify + *  it under the terms of the GNU General Public License as published by + *  the Free Software Foundation; either version 3 of the License, or + *  (at your option) any later version. + * + *  Chrysalide is distributed in the hope that it will be useful, + *  but WITHOUT ANY WARRANTY; without even the implied warranty of + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + *  GNU General Public License for more details. + * + *  You should have received a copy of the GNU General Public License + *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>. + */ + + +#ifndef _GLIBEXT_SINGLETON_H +#define _GLIBEXT_SINGLETON_H + + +#include <glib-object.h> +#include <stdbool.h> + + + +/* ------------------ INTERFACE POUR CANDIDAT A UNE CENTRALISATION ------------------ */ + + +#define G_TYPE_SINGLETON_CANDIDATE             (g_singleton_candidate_get_type()) +#define G_SINGLETON_CANDIDATE(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_SINGLETON_CANDIDATE, GSingletonCandidate)) +#define G_SINGLETON_CANDIDATE_CLASS(vtable)    (G_TYPE_CHECK_CLASS_CAST((vtable), G_TYPE_SINGLETON_CANDIDATE, GSingletonCandidateIface)) +#define G_IS_SINGLETON_CANDIDATE(obj)          (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_SINGLETON_CANDIDATE)) +#define G_IS_SINGLETON_CANDIDATE_CLASS(vtable) (G_TYPE_CHECK_CLASS_TYPE((vtable), G_TYPE_SINGLETON_CANDIDATE)) +#define G_SINGLETON_CANDIDATE_GET_IFACE(inst)  (G_TYPE_INSTANCE_GET_INTERFACE((inst), G_TYPE_SINGLETON_CANDIDATE, GSingletonCandidateIface)) + + +/* Instance d'objet visant à être unique (coquille vide) */ +typedef struct _GSingletonCandidate GSingletonCandidate; + +/* Instance d'objet visant à être unique (interface) */ +typedef struct _GSingletonCandidateIface GSingletonCandidateIface; + + +/* Détermine le type d'une interface pour la lecture de binaire. */ +GType g_singleton_candidate_get_type(void) G_GNUC_CONST; + +/* Fournit l'empreinte d'un candidat à une centralisation. */ +guint g_singleton_candidate_hash(const GSingletonCandidate *); + +/* Détermine si deux candidats à l'unicité sont identiques. */ +gboolean g_singleton_candidate_is_equal(const GSingletonCandidate *, const GSingletonCandidate *); + +/* Marque un candidat comme traité ou en cours de traitement. */ +void g_singleton_candidate_mark_as_processed(GSingletonCandidate *, bool); + +/* Indique si un objet marqué comme unique. */ +bool g_singleton_candidate_is_processed(const GSingletonCandidate *, bool); + + + +/* ------------------------- COLLECTION D'INSTANCES UNIQUES ------------------------- */ + + +#define G_TYPE_SINGLETON_FACTORY            g_singleton_factory_get_type() +#define G_SINGLETON_FACTORY(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_SINGLETON_FACTORY, GSingletonFactory)) +#define G_IS_SINGLETON_FACTORY(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_SINGLETON_FACTORY)) +#define G_SINGLETON_FACTORY_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_SINGLETON_FACTORY, GSingletonFactoryClass)) +#define G_IS_SINGLETON_FACTORY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_SINGLETON_FACTORY)) +#define G_SINGLETON_FACTORY_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_SINGLETON_FACTORY, GSingletonFactoryClass)) + + +/* Définition d'un compacteur d'instances de types (instance) */ +typedef struct _GSingletonFactory GSingletonFactory; + +/* Définition d'un compacteur d'instances de types (classe) */ +typedef struct _GSingletonFactoryClass GSingletonFactoryClass; + + +/* Indique le type défini pour une mémoire de types d'objets. */ +GType g_singleton_factory_get_type(void); + +/* Crée un compacteur d'instances de types. */ +GSingletonFactory *g_singleton_factory_new(void); + +/* Fournit l'instance unique correspondant à un objet. */ +GSingletonCandidate *g_singleton_factory_get_instance(GSingletonFactory *, GSingletonCandidate *); + + + +#endif  /* _GLIBEXT_SINGLETON_H */ | 
