/* Chrysalide - Outil d'analyse de fichiers binaires
 * objhole.c - utilisation d'un espace inutilisé dans la structure GObject
 *
 * Copyright (C) 2024 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/>.
 */


#include "objhole.h"


#include <assert.h>


#include "objhole-int.h"



/* Initialise la classe des objets à la structure dense. */
static void g_thick_object_class_init(GThickObjectClass *);

/* Initialise une instance d'objet à la structure dense. */
static void g_thick_object_init(GThickObject *);

/* Supprime toutes les références externes. */
static void g_thick_object_dispose(GThickObject *);

/* Procède à la libération totale de la mémoire. */
static void g_thick_object_finalize(GThickObject *);



G_DEFINE_TYPE(GThickObject, g_thick_object, G_TYPE_OBJECT);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des objets à la structure dense.        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_thick_object_class_init(GThickObjectClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */

    object = G_OBJECT_CLASS(klass);

    object->dispose = (GObjectFinalizeFunc/* ! */)g_thick_object_dispose;
    object->finalize = (GObjectFinalizeFunc)g_thick_object_finalize;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : obj = instance à initialiser.                                *
*                                                                             *
*  Description : Initialise une instance d'objet à la structure dense.        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_thick_object_init(GThickObject *obj)
{
    obj->extra = 0;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : obj = instance d'objet GLib à traiter.                       *
*                                                                             *
*  Description : Supprime toutes les références externes.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_thick_object_dispose(GThickObject *obj)
{
    G_OBJECT_CLASS(g_thick_object_parent_class)->dispose(G_OBJECT(obj));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : obj = instance d'objet GLib à traiter.                       *
*                                                                             *
*  Description : Procède à la libération totale de la mémoire.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_thick_object_finalize(GThickObject *obj)
{
    G_OBJECT_CLASS(g_thick_object_parent_class)->finalize(G_OBJECT(obj));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : obj = instance d'objet GLib à consulter.                     *
*                                                                             *
*  Description : Fournit la valeur courante de la zone de stockage d'un objet.*
*                                                                             *
*  Retour      : Valeur de 32 lues et expurgées des bits GLib.                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

guint g_thick_object_get_extra(const GThickObject *obj)
{
    guint result;                           /* Valeur à retourner          */
    guint mask;                             /* Masque à appliquer          */

    result = g_atomic_int_get(&obj->extra);

    assert(GOBJECT_RESERVED_EXTRA_BITS < 30);

    mask = (1 << GOBJECT_RESERVED_EXTRA_BITS) - 1;

    result &= ~mask;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : obj = instance d'objet GLib à consulter.                     *
*                val = valeur de 32 à conserver.                              *
*                                                                             *
*  Description : Définit la valeur courante de la zone de stockage d'un objet.*
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_thick_object_set_extra(GThickObject *obj, guint val)
{
    guint mask;                             /* Masque à appliquer          */

    assert(GOBJECT_RESERVED_EXTRA_BITS < 30);

    mask = (1 << GOBJECT_RESERVED_EXTRA_BITS) - 1;

    assert((val & mask) == 0);

    val &= ~mask;

    g_atomic_int_and(&obj->extra, val | mask);

    g_atomic_int_or(&obj->extra, val);

}