diff options
Diffstat (limited to 'plugins/pychrysalide')
-rw-r--r-- | plugins/pychrysalide/Makefile.am | 4 | ||||
-rw-r--r-- | plugins/pychrysalide/core.c | 6 | ||||
-rw-r--r-- | plugins/pychrysalide/glibext/Makefile.am | 45 | ||||
-rw-r--r-- | plugins/pychrysalide/glibext/module.c | 9 | ||||
-rw-r--r-- | plugins/pychrysalide/glibext/work.c | 351 | ||||
-rw-r--r-- | plugins/pychrysalide/glibext/work.h | 45 | ||||
-rw-r--r-- | plugins/pychrysalide/glibext/workqueue.c | 586 | ||||
-rw-r--r-- | plugins/pychrysalide/glibext/workqueue.h | 45 |
8 files changed, 1066 insertions, 25 deletions
diff --git a/plugins/pychrysalide/Makefile.am b/plugins/pychrysalide/Makefile.am index 888de8a..a6abf7f 100644 --- a/plugins/pychrysalide/Makefile.am +++ b/plugins/pychrysalide/Makefile.am @@ -49,7 +49,6 @@ AM_CFLAGS = $(LIBPYTHON_INTERPRETER_CFLAGS) $(LIBPYGOBJECT_CFLAGS) $(TOOLKIT_CFL # core/libpychrysacore.la \ # debug/libpychrysadebug.la \ # format/libpychrysaformat.la \ -# glibext/libpychrysaglibext.la \ # $(GTKEXT_LIBADD) \ # $(GUI_LIBADD) \ # mangling/libpychrysamangling.la \ @@ -59,6 +58,7 @@ pychrysalide_la_LIBADD = \ analysis/libpychrysaanalysis4.la \ arch/libpychrysaarch4.la \ core/libpychrysacore.la \ + glibext/libpychrysaglibext.la \ plugins/libpychrysaplugins.la # -ldl: dladdr(), dlerror() @@ -76,4 +76,4 @@ dev_HEADERS = $(pychrysalide_la_SOURCES:%c=) #SUBDIRS = analysis arch common core debug format glibext $(GTKEXT_SUBDIR) $(GUI_SUBDIR) mangling plugins -SUBDIRS = analysis arch core plugins +SUBDIRS = analysis arch core glibext plugins diff --git a/plugins/pychrysalide/core.c b/plugins/pychrysalide/core.c index 35c0e35..4d744c7 100644 --- a/plugins/pychrysalide/core.c +++ b/plugins/pychrysalide/core.c @@ -60,11 +60,11 @@ #include "struct.h" #include "analysis/module.h" #include "arch/module.h" +#include "glibext/module.h" /* #include "common/module.h" */ /* #include "core/module.h" */ /* #include "debug/module.h" */ /* #include "format/module.h" */ -/* #include "glibext/module.h" */ /* #ifdef INCLUDE_GTK_SUPPORT */ /* # include "gtkext/module.h" */ /* # include "gui/module.h" */ @@ -645,12 +645,12 @@ PyMODINIT_FUNC PyInit_pychrysalide(void) if (status) status = add_analysis_module(result); if (status) status = add_arch_module(result); + if (status) status = add_glibext_module(result); /* if (status) status = add_common_module(result); if (status) status = add_core_module(result); if (status) status = add_debug_module(result); if (status) status = add_format_module(result); - if (status) status = add_glibext_module(result); #ifdef INCLUDE_GTK_SUPPORT if (status) status = add_gtkext_module(result); if (status) status = add_gui_module(result); @@ -668,12 +668,12 @@ PyMODINIT_FUNC PyInit_pychrysalide(void) if (status) status = populate_analysis_module(); if (status) status = populate_arch_module(); + if (status) status = populate_glibext_module(); /* if (status) status = populate_common_module(); if (status) status = populate_core_module(); if (status) status = populate_debug_module(); if (status) status = populate_format_module(); - if (status) status = populate_glibext_module(); #ifdef INCLUDE_GTK_SUPPORT if (status) status = populate_gtkext_module(); if (status) status = populate_gui_module(); diff --git a/plugins/pychrysalide/glibext/Makefile.am b/plugins/pychrysalide/glibext/Makefile.am index 2ed2aa5..e9c3756 100644 --- a/plugins/pychrysalide/glibext/Makefile.am +++ b/plugins/pychrysalide/glibext/Makefile.am @@ -1,29 +1,34 @@ noinst_LTLIBRARIES = libpychrysaglibext.la +# libpychrysaglibext_la_SOURCES = \ +# constants.h constants.c \ +# binarycursor.h binarycursor.c \ +# binportion.h binportion.c \ +# buffercache.h buffercache.c \ +# bufferline.h bufferline.c \ +# comparison.h comparison.c \ +# configuration.h configuration.c \ +# linecursor.h linecursor.c \ +# linegen.h linegen.c \ +# module.h module.c \ +# singleton.h singleton.c + +# if BUILD_GTK_SUPPORT + +# libpychrysaglibext_la_SOURCES += \ +# bufferview.h bufferview.c \ +# loadedpanel.h loadedpanel.c \ +# named.h named.c + +# endif + libpychrysaglibext_la_SOURCES = \ - constants.h constants.c \ - binarycursor.h binarycursor.c \ - binportion.h binportion.c \ - buffercache.h buffercache.c \ - bufferline.h bufferline.c \ - comparison.h comparison.c \ - configuration.h configuration.c \ - linecursor.h linecursor.c \ - linegen.h linegen.c \ module.h module.c \ - singleton.h singleton.c - -if BUILD_GTK_SUPPORT - -libpychrysaglibext_la_SOURCES += \ - bufferview.h bufferview.c \ - loadedpanel.h loadedpanel.c \ - named.h named.c - -endif + work.h work.c \ + workqueue.h workqueue.c -libpychrysaglibext_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_INTERPRETER_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \ +libpychrysaglibext_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBPYTHON_INTERPRETER_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \ -I$(top_srcdir)/src -DNO_IMPORT_PYGOBJECT diff --git a/plugins/pychrysalide/glibext/module.c b/plugins/pychrysalide/glibext/module.c index 3e4307a..23e7a7d 100644 --- a/plugins/pychrysalide/glibext/module.c +++ b/plugins/pychrysalide/glibext/module.c @@ -28,6 +28,7 @@ #include <assert.h> +/* #include "binarycursor.h" #include "binportion.h" #include "buffercache.h" @@ -40,6 +41,9 @@ #include "loadedpanel.h" #include "named.h" #include "singleton.h" +*/ +#include "work.h" +#include "workqueue.h" #include "../helpers.h" @@ -106,6 +110,10 @@ bool populate_glibext_module(void) result = true; + if (result) result = ensure_python_generic_work_is_registered(); + if (result) result = ensure_python_work_queue_is_registered(); + + /* if (result) result = ensure_python_singleton_candidate_is_registered(); if (result) result = ensure_python_binary_cursor_is_registered(); @@ -126,6 +134,7 @@ bool populate_glibext_module(void) if (result) result = ensure_python_named_widget_is_registered(); #endif if (result) result = ensure_python_singleton_factory_is_registered(); + */ assert(result); diff --git a/plugins/pychrysalide/glibext/work.c b/plugins/pychrysalide/glibext/work.c new file mode 100644 index 0000000..6a15984 --- /dev/null +++ b/plugins/pychrysalide/glibext/work.c @@ -0,0 +1,351 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * work.c - prototypes pour l'équivalent Python du fichier "glibext/work.c" + * + * 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "work.h" + + +#include <assert.h> +#include <pygobject.h> + + +#include <glibext/work-int.h> + + +#include "../access.h" +#include "../helpers.h" + + + +/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ + + +/* Initialise la classe des travaux programmés. */ +static void py_generic_work_init_gclass(GGenericWorkClass *, gpointer); + +CREATE_DYN_ABSTRACT_CONSTRUCTOR(generic_work, G_TYPE_GENERIC_WORK, py_generic_work_init_gclass); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_generic_work_init(PyObject *, PyObject *, PyObject *); + +/* Mène l'opération programmée. */ +static void py_generic_work_run_wrapper(GGenericWork *); + + + +/* ------------------------- CONNEXION AVEC L'API DE PYTHON ------------------------- */ + + +/* Mène l'opération programmée. */ +static PyObject *py_generic_work_process(PyObject *, PyObject *); + + + +/* ---------------------------------------------------------------------------------- */ +/* GLUE POUR CREATION DEPUIS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : class = classe à initialiser. * +* unused = données non utilisées ici. * +* * +* Description : Initialise la classe des travaux programmés. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_generic_work_init_gclass(GGenericWorkClass *class, gpointer unused) +{ + class->run = py_generic_work_run_wrapper; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet à initialiser (théoriquement). * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Initialise une instance sur la base du dérivé de GObject. * +* * +* Retour : 0. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_generic_work_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + int ret; /* Bilan de lecture des args. */ + +#define GENERIC_WORK_DOC \ + "GenericWork defines a basic work aimed to get processed in a" \ + " thread set up by a pychrysalide.glibext.WorkQueue object.\n" \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " GenericWork()" \ + "\n" \ + "The following method has to be defined for new classes:\n" \ + "* pychrysalide.glibext.GenericWork._run();\n" \ + "\n" \ + "The following signal may be emitted:\n" \ + "* *work-completed* : when the work has been processed." + + /* Initialisation d'un objet GLib */ + + ret = forward_pygobjet_init(self); + if (ret == -1) return -1; + + return 0; + +} + + +/****************************************************************************** +* * +* Paramètres : work = travail à effectuer. * +* * +* Description : Mène l'opération programmée. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_generic_work_run_wrapper(GGenericWork *work) +{ + PyGILState_STATE gstate; /* Sauvegarde d'environnement */ + PyObject *pyobj; /* Objet Python concerné */ + PyObject *pyret; /* Bilan de consultation */ + +#define GENERIC_WORK_RUN_WRAPPER PYTHON_WRAPPER_DEF \ +( \ + _run, "$self", \ + METH_NOARGS, \ + "Abstract method used to process the work.\n" \ + "\n" \ + "No result is expected from this function." \ +) + + gstate = PyGILState_Ensure(); + + pyobj = pygobject_new(G_OBJECT(work)); + + if (has_python_method(pyobj, "_run")) + { + pyret = run_python_method(pyobj, "_run", NULL); + + Py_XDECREF(pyret); + + } + + Py_DECREF(pyobj); + + PyGILState_Release(gstate); + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* CONNEXION AVEC L'API DE PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : self = élément d'appel à consulter. * +* args = arguments fournis pour l'opération. * +* * +* Description : Mène l'opération programmée. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_generic_work_process(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + GGenericWork *work; /* Version native */ + +#define GENERIC_WORK_PROCESS_METHOD PYTHON_METHOD_DEF \ +( \ + process, "$self", \ + METH_NOARGS, py_generic_work, \ + "Process the work.\n" \ + "\n" \ + "The function does not return anything.\n" \ + "\n" \ + "The *work-completed* signal is emitted when the" \ + " function terminates." \ +) + + work = G_GENERIC_WORK(pygobject_get(self)); + + g_generic_work_process(work); + + result = Py_None; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_generic_work_type(void) +{ + static PyMethodDef py_generic_work_methods[] = { + GENERIC_WORK_RUN_WRAPPER, + GENERIC_WORK_PROCESS_METHOD, + { NULL } + }; + + static PyGetSetDef py_generic_work_getseters[] = { + { NULL } + }; + + static PyTypeObject py_generic_work_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.glibext.GenericWork", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = GENERIC_WORK_DOC, + + .tp_methods = py_generic_work_methods, + .tp_getset = py_generic_work_getseters, + + .tp_init = py_generic_work_init, + .tp_new = py_generic_work_new, + + }; + + return &py_generic_work_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.glibext.GenericWork'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_generic_work_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'GenericWork' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_generic_work_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.glibext"); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_GENERIC_WORK, type)) + return false; + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en travail générique. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_generic_work(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_generic_work_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to generic work"); + break; + + case 1: + *((GGenericWork **)dst) = G_GENERIC_WORK(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/glibext/work.h b/plugins/pychrysalide/glibext/work.h new file mode 100644 index 0000000..de9a936 --- /dev/null +++ b/plugins/pychrysalide/glibext/work.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * work.h - prototypes pour l'équivalent Python du fichier "glibext/work.h" + * + * 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _PLUGINS_PYCHRYSALIDE_GLIBEXT_WORK_H +#define _PLUGINS_PYCHRYSALIDE_GLIBEXT_WORK_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_generic_work_type(void); + +/* Prend en charge l'objet 'pychrysalide.glibext.GenericWork'. */ +bool ensure_python_generic_work_is_registered(void); + +/* Tente de convertir en travail générique. */ +int convert_to_generic_work(PyObject *, void *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_GLIBEXT_WORK_H */ diff --git a/plugins/pychrysalide/glibext/workqueue.c b/plugins/pychrysalide/glibext/workqueue.c new file mode 100644 index 0000000..d8126be --- /dev/null +++ b/plugins/pychrysalide/glibext/workqueue.c @@ -0,0 +1,586 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * workqueue.c - prototypes pour l'équivalent Python du fichier "glibext/workqueue.c" + * + * 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "workqueue.h" + + +#include <assert.h> +#include <pygobject.h> + + +#include <glibext/workqueue-int.h> + + +#include "work.h" +#include "../access.h" +#include "../helpers.h" + + + +/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ + + +CREATE_DYN_CONSTRUCTOR(work_queue, G_TYPE_WORK_QUEUE); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_work_queue_init(PyObject *, PyObject *, PyObject *); + + + +/* ------------------------- CONNEXION AVEC L'API DE PYTHON ------------------------- */ + + +/* Constitue un nouveau groupe de travail. */ +static PyObject *py_work_queue_define_group(PyObject *, PyObject *); + +/* Dissout un groupe de travail existant. */ +static PyObject *py_work_queue_delete_group(PyObject *, PyObject *); + +/* Place une nouvelle tâche en attente. */ +static PyObject *py_work_queue_schedule(PyObject *, PyObject *); + +/* Détermine si un groupe est vide de toute programmation. */ +static PyObject *py_work_queue_is_empty(PyObject *, PyObject *); + +/* Attend que toutes les tâches d'un groupe soient traitées. */ +static PyObject *py_work_queue_wait_for_completion(PyObject *, PyObject *); + +/* Force un réveil d'une attente en cours pour la confirmer. */ +static PyObject *py_work_queue_wake_up_waiters(PyObject *, PyObject *); + + + +/* ---------------------------------------------------------------------------------- */ +/* GLUE POUR CREATION DEPUIS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : self = objet à initialiser (théoriquement). * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Initialise une instance sur la base du dérivé de GObject. * +* * +* Retour : 0. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_work_queue_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + int ret; /* Bilan de lecture des args. */ + +#define WORK_QUEUE_DOC \ + "WorkQueue defines a basic work aimed to get processed in a" \ + " thread setup by a pychrysalide.glibext.WorkQueue instance.\n" \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " WorkQueue()" + + /* Initialisation d'un objet GLib */ + + ret = forward_pygobjet_init(self); + if (ret == -1) return -1; + + return 0; + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* CONNEXION AVEC L'API DE PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : self = élément d'appel à consulter. * +* args = arguments fournis pour l'opération. * +* * +* Description : Constitue un nouveau groupe de travail. * +* * +* Retour : Nouvel identifiant unique d'un nouveau groupe de travail. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_work_queue_define_group(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + unsigned int count; /* Nombre de threads à activer */ + int ret; /* Bilan de lecture des args. */ + GWorkQueue *queue; /* Version native */ + wgroup_id_t id; /* Identifiant de groupe créé */ + +#define WORK_QUEUE_DEFINE_GROUP_METHOD PYTHON_METHOD_DEF \ +( \ + define_group, "$self, /, count=0", \ + METH_VARARGS, py_work_queue, \ + "Create a new work group for a work queue.\n" \ + "\n" \ + "The *count* argument states the number of threads which will" \ + " be activated for the processing. If the provided value is" \ + " zero, then a default value based on the current hardware is" \ + " selected.\n" \ + "\n" \ + "The result is a work group identifier." \ +) + + count = 0; + + ret = PyArg_ParseTuple(args, "|I", &count); + if (!ret) return NULL; + + queue = G_WORK_QUEUE(pygobject_get(self)); + + id = g_work_queue_define_group(queue, count); + + result = PyLong_FromUnsignedLong(id); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = élément d'appel à consulter. * +* args = arguments fournis pour l'opération. * +* * +* Description : Dissout un groupe de travail existant. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_work_queue_delete_group(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + unsigned long long id; /* Identifiant de groupe ciblé */ + int ret; /* Bilan de lecture des args. */ + GWorkQueue *queue; /* Version native */ + +#define WORK_QUEUE_DELETE_GROUP_METHOD PYTHON_METHOD_DEF \ +( \ + delete_group, "$self, id", \ + METH_VARARGS, py_work_queue, \ + "Destroy a work group previously registered inside a queue.\n" \ + "\n" \ + "The *id* argument refers to a work group identifier. It" \ + " should have been provided by a previous call to" \ + " pychrysalide.glibext.WorkQueue.g_work_queue_define_group()." \ +) + + ret = PyArg_ParseTuple(args, "K", &id); + if (!ret) return NULL; + + queue = G_WORK_QUEUE(pygobject_get(self)); + + g_work_queue_delete_group(queue, id); + + result = Py_None; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = élément d'appel à consulter. * +* args = arguments fournis pour l'opération. * +* * +* Description : Place une nouvelle tâche en attente. * +* * +* Retour : Bilan, qui correspond à l'existence du groupe ciblé. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_work_queue_schedule(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + GGenericWork *work; /* Tâche à programmer */ + unsigned long long id; /* Identifiant de groupe ciblé */ + int ret; /* Bilan de lecture des args. */ + GWorkQueue *queue; /* Version native */ + bool status; /* Bilan de programmation */ + +#define WORK_QUEUE_SCHEDULE_METHOD PYTHON_METHOD_DEF \ +( \ + schedule, "$self, work, id", \ + METH_VARARGS, py_work_queue, \ + "Schedule a work into the queue processing.\n" \ + "\n" \ + "The *work* argument has to point to a" \ + " pychrysalide.glibext.GenericWork instance, and the *id*" \ + " argument refers to a work group identifier which should have" \ + " been provided by a previous call to" \ + " pychrysalide.glibext.WorkQueue.g_work_queue_define_group().\n"\ + "\n" \ + "The result is a boolean value: *True* in case of succes (does" \ + " the provided identifier exist?), *False* otherwise." \ +) + + ret = PyArg_ParseTuple(args, "O&K", convert_to_generic_work, &work, &id); + if (!ret) return NULL; + + queue = G_WORK_QUEUE(pygobject_get(self)); + + status = g_work_queue_schedule(queue, work, id); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = élément d'appel à consulter. * +* args = arguments fournis pour l'opération. * +* * +* Description : Détermine si un groupe est vide de toute programmation. * +* * +* Retour : Etat du groupe de travail. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_work_queue_is_empty(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + unsigned long long id; /* Identifiant de groupe ciblé */ + int ret; /* Bilan de lecture des args. */ + GWorkQueue *queue; /* Version native */ + bool status; /* Bilan de consultation */ + +#define WORK_QUEUE_IS_EMPTY_METHOD PYTHON_METHOD_DEF \ +( \ + is_empty, "$self, id", \ + METH_VARARGS, py_work_queue, \ + "Signal if there is some remaining work to get processed by" \ + " the queue or not.\n" \ + "\n" \ + "The *id* argument refers to a work group identifier. It" \ + " should have been provided by a previous call to" \ + " pychrysalide.glibext.WorkQueue.g_work_queue_define_group().\n"\ + "\n" \ + "The result is a boolean value: *True* if the group does not" \ + " contain any works or does not exist, *False* otherwise." \ +) + + ret = PyArg_ParseTuple(args, "K", &id); + if (!ret) return NULL; + + queue = G_WORK_QUEUE(pygobject_get(self)); + + status = g_work_queue_is_empty(queue, id); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = élément d'appel à consulter. * +* args = arguments fournis pour l'opération. * +* * +* Description : Attend que toutes les tâches d'un groupe soient traitées. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_work_queue_wait_for_completion(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + unsigned long long rel; /* Durée d'attente */ + unsigned long long id; /* Identifiant de groupe ciblé */ + int ret; /* Bilan de lecture des args. */ + GWorkQueue *queue; /* Version native */ + bool status; /* Bilan de l'attente menée */ + + /** + * Python appelle cette fonction, lorsqu'elle est sollicitée dans des scripts, + * via la fonction PyObject_Vectorcall(), qui obtient la structure de suivi + * du thread courant avec _PyThreadState_GET(). + * + * Un commentaire associé à cette dernière fonction précise : + * + * The caller must hold the GIL. + * + * Comme le verrou GIL est posé par Python au moment de l'exécution de + * py_work_queue_wait_for_completion(), aucune attente bloquante ne peut + * alors être engagée ici : elle bloquerait la tâche _run() d'un travail + * réalisé en Python(), comme son déférencement via unref_ojbect() qui + * implique une exécution de la fonction pyg_toggle_notify() appelant + * PyGILState_Ensure() dans python3-gi. + * + * Aussi une attente bornée dans le temps est la seule option possible + * ici. + */ + +#define WORK_QUEUE_WAIT_FOR_COMPLETION_METHOD PYTHON_METHOD_DEF \ +( \ + wait_for_completion, "$self, id, /, rel=100000", \ + METH_VARARGS, py_work_queue, \ + "Wait until all the works from a given group have been" \ + " processed.\n" \ + "\n" \ + "The *id* argument refers to a work group identifier. It" \ + " should have been provided by a previous call to" \ + " pychrysalide.glibext.WorkQueue.g_work_queue_define_group()." \ + " The *rel* range provide a relative time to wait (100ms by" \ + " default).\n" \ + "\n" \ + "The returned value is a boolean status: *False* on a timeout," \ + " *True* otherwise." \ + "\n" \ + "_Important note:_\n" \ + "As this function is called by Python with its Global" \ + " Interpreter Lock (GIL) held, an endless wait is impossible" \ + " because it would keep the GIL unreleased whereas others" \ + " threads (including those which may end the wait) may need to" \ + " acquire it.\n" \ + "\n" \ + "So an unoptimized timed wait is performed here instead, and" \ + " the returned status has to be checked by the caller in order" \ + " to schedule another wait if needed." \ +) + + rel = 100 * G_TIME_SPAN_MILLISECOND; + + ret = PyArg_ParseTuple(args, "K|K", &id, &rel); + if (!ret) return NULL; + + queue = G_WORK_QUEUE(pygobject_get(self)); + + status = g_work_queue_wait_timed_for_completion(queue, id, rel); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = élément d'appel à consulter. * +* args = arguments fournis pour l'opération. * +* * +* Description : Force un réveil d'une attente en cours pour la confirmer. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_work_queue_wake_up_waiters(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + unsigned long long id; /* Identifiant de groupe ciblé */ + int ret; /* Bilan de lecture des args. */ + GWorkQueue *queue; /* Version native */ + +#define WORK_QUEUE_WAKE_UP_WAITERS_METHOD PYTHON_METHOD_DEF \ +( \ + wake_up_waiters, "$self, id", \ + METH_VARARGS, py_work_queue, \ + "Force a wake up of all threads waiting for processing a given" \ + " queue work group.\n" \ + "\n" \ + "The *id* argument refers to a work group identifier. It" \ + " should have been provided by a previous call to" \ + " pychrysalide.glibext.WorkQueue.g_work_queue_define_group()." \ +) + + ret = PyArg_ParseTuple(args, "K", &id); + if (!ret) return NULL; + + queue = G_WORK_QUEUE(pygobject_get(self)); + + g_work_queue_wake_up_waiters(queue, id); + + result = Py_None; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_work_queue_type(void) +{ + static PyMethodDef py_work_queue_methods[] = { + WORK_QUEUE_DEFINE_GROUP_METHOD, + WORK_QUEUE_DELETE_GROUP_METHOD, + WORK_QUEUE_SCHEDULE_METHOD, + WORK_QUEUE_IS_EMPTY_METHOD, + WORK_QUEUE_WAIT_FOR_COMPLETION_METHOD, + WORK_QUEUE_WAKE_UP_WAITERS_METHOD, + { NULL } + }; + + static PyGetSetDef py_work_queue_getseters[] = { + { NULL } + }; + + static PyTypeObject py_work_queue_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.glibext.WorkQueue", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = WORK_QUEUE_DOC, + + .tp_methods = py_work_queue_methods, + .tp_getset = py_work_queue_getseters, + + .tp_init = py_work_queue_init, + .tp_new = py_work_queue_new, + + }; + + return &py_work_queue_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.glibext.WorkQueue'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_work_queue_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'WorkQueue' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_work_queue_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.glibext"); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_WORK_QUEUE, type)) + return false; + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en gestionnaire de travaux différés. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_work_queue(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_work_queue_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to work queue"); + break; + + case 1: + *((GWorkQueue **)dst) = G_WORK_QUEUE(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/glibext/workqueue.h b/plugins/pychrysalide/glibext/workqueue.h new file mode 100644 index 0000000..3fe426b --- /dev/null +++ b/plugins/pychrysalide/glibext/workqueue.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * workqueue.h - prototypes pour l'équivalent Python du fichier "glibext/workqueue.h" + * + * 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _PLUGINS_PYCHRYSALIDE_GLIBEXT_WORKQUEUE_H +#define _PLUGINS_PYCHRYSALIDE_GLIBEXT_WORKQUEUE_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_work_queue_type(void); + +/* Prend en charge l'objet 'pychrysalide.glibext.WorkQueue'. */ +bool ensure_python_work_queue_is_registered(void); + +/* Tente de convertir en gestionnaire de travaux différés. */ +int convert_to_work_queue(PyObject *, void *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_GLIBEXT_WORKQUEUE_H */ |