diff --git a/python/obitools3/dms/capi/obidms.pxd b/python/obitools3/dms/capi/obidms.pxd index 1a50da5..f62a61a 100644 --- a/python/obitools3/dms/capi/obidms.pxd +++ b/python/obitools3/dms/capi/obidms.pxd @@ -17,6 +17,7 @@ cdef extern from "obidms.h" nogil: int obi_close_dms(OBIDMS_p dms) char* obi_dms_get_dms_path(OBIDMS_p dms) char* obi_dms_get_full_path(OBIDMS_p dms, const_char_p path_name) - + void obi_close_atexit() + obiversion_t obi_import_column(const char* dms_path_1, const char* dms_path_2, const char* column_name, obiversion_t version_number) int obi_import_view(const char* dms_path_1, const char* dms_path_2, const char* view_name_1, const char* view_name_2) diff --git a/python/obitools3/dms/dms.pyx b/python/obitools3/dms/dms.pyx index dc2ccaa..4e3de64 100644 --- a/python/obitools3/dms/dms.pyx +++ b/python/obitools3/dms/dms.pyx @@ -1,14 +1,15 @@ #cython: language_level=3 -from libc.stdlib cimport free +from libc.stdlib cimport free, atexit from cpython.list cimport PyList_Size from .capi.obidms cimport obi_open_dms, \ obi_create_dms, \ obi_close_dms, \ obi_dms_exists, \ - obi_dms_get_full_path + obi_dms_get_full_path, \ + obi_close_atexit from .capi.obitypes cimport const_char_p @@ -30,6 +31,10 @@ cdef class DMS(OBIWrapper): cdef inline OBIDMS_p pointer(self): return (self._pointer) + @staticmethod + def obi_atexit(): + atexit(obi_close_atexit) + @staticmethod def open_or_new(object dms_name) : cdef OBIDMS_p pointer diff --git a/src/obidms.c b/src/obidms.c index 8db9592..9228820 100644 --- a/src/obidms.c +++ b/src/obidms.c @@ -34,6 +34,10 @@ #define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?) +// Initialize global list of opened DMS +OBIDMS_p global_opened_dms_list[MAX_NB_OPENED_DMS+1] = { 0 }; + + /************************************************************************** * * D E C L A R A T I O N O F T H E P R I V A T E F U N C T I O N S @@ -98,6 +102,31 @@ static char* build_infos_file_name(const char* dms_name); int create_dms_infos_file(int dms_file_descriptor, const char* dms_name); +/** + * Internal function adding a DMS in the global list of DMS opened by a program. + * + * @param dms A pointer on the DMS to add. + * + * @retval 0 if the operation was successfully completed. + * @retval -1 if an error occurred. + * + * @since October 2017 + * @author Celine Mercier (celine.mercier@metabarcoding.org) + */ +static int list_dms(OBIDMS_p dms); + + +/** + * Internal function removing a DMS from the global list of DMS opened by a program. + * + * @param dms A pointer on the DMS to remove. + * + * @since October 2017 + * @author Celine Mercier (celine.mercier@metabarcoding.org) + */ +static void unlist_dms(OBIDMS_p dms); + + /************************************************************************ * * D E F I N I T I O N O F T H E P R I V A T E F U N C T I O N S @@ -202,11 +231,39 @@ int create_dms_infos_file(int dms_file_descriptor, const char* dms_name) return -1; } - return 0; } +static int list_dms(OBIDMS_p dms) +{ + int i = 0; + while (global_opened_dms_list[i] != NULL) + i++; + if (i == (MAX_NB_OPENED_DMS-1)) + { + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + obidebug(1, "\nError opening a DMS: maximum number of DMS opened by a program reached"); + return -1; + } + global_opened_dms_list[i] = dms; + return 0; +} + + +static void unlist_dms(OBIDMS_p dms) +{ + int i = 0; + while (global_opened_dms_list[i] != dms) + i++; + while (global_opened_dms_list[i] != NULL) + { + global_opened_dms_list[i] = global_opened_dms_list[i+1]; + i++; + } +} + + /********************************************************************** * * D E F I N I T I O N O F T H E P U B L I C F U N C T I O N S @@ -538,6 +595,9 @@ OBIDMS_p obi_open_dms(const char* dms_path) dms->opened_indexers = (Opened_indexers_list_p) malloc(sizeof(Opened_indexers_list_t)); (dms->opened_indexers)->nb_opened_indexers = 0; + // Add in the global list of opened DMS + list_dms(dms); + return dms; } @@ -617,6 +677,10 @@ int obi_close_dms(OBIDMS_p dms) free(dms); return -1; } + + // Remove DMS from global list of opened DMS + unlist_dms(dms); + free(dms); } @@ -1085,5 +1149,14 @@ int obi_import_view(const char* dms_path_1, const char* dms_path_2, const char* } +void obi_close_atexit() +{ + int i=0; + while (global_opened_dms_list[i] != NULL) + { + obi_close_dms(global_opened_dms_list[i]); + i++; + } +} diff --git a/src/obidms.h b/src/obidms.h index 9055c07..0e44213 100644 --- a/src/obidms.h +++ b/src/obidms.h @@ -36,6 +36,8 @@ */ #define MAX_NB_OPENED_COLUMNS (1000) /**< The maximum number of columns open at the same time. */ +#define MAX_NB_OPENED_DMS (1000) /**< The maximum number of DMS open at the same time for a given program. + */ #define MAX_NB_OPENED_INDEXERS (1000) /**< The maximum number of indexers open at the same time. */ #define MAX_PATH_LEN (1024) /**< Maximum length for the character string defining a @@ -121,6 +123,12 @@ typedef struct OBIDMS { } OBIDMS_t, *OBIDMS_p; +/** + * @brief Global Array of DMS pointers listing all the DMS opened by a program. + */ +extern OBIDMS_p global_opened_dms_list[MAX_NB_OPENED_DMS+1]; + + /** * @brief Checks if an OBIDMS exists. * @@ -403,4 +411,14 @@ obiversion_t obi_import_column(const char* dms_path_1, const char* dms_path_2, c int obi_import_view(const char* dms_path_1, const char* dms_path_2, const char* view_name_1, const char* view_name_2); +/** + * @brief Closes all DMS in the global list of opened DMS. + * To be executed 'at exit' of programs. + * + * @since October 2017 + * @author Celine Mercier (celine.mercier@metabarcoding.org) + */ +void obi_close_atexit(); + + #endif /* OBIDMS_H_ */