/*----------------------------------------------------------------------------*/ /* libshmem.h */ /*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/ /* This file is part of LibShMem. */ /* */ /* LibShMem is free software: you can redistribute it and/or modify it */ /* under the terms of the GNU Lesser General Public License as published */ /* by the Free Software Foundation, either version 3 of the License, or */ /* (at your option) any later version. */ /* */ /* LibShMem 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 Lesser General Public License for more details. */ /* */ /* You should have received a copy of the GNU Lesser General Public */ /* License along with LibShMem. If not, see */ /* . */ /*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/ /* Includes */ /*----------------------------------------------------------------------------*/ #include #include #include #include #include #include #include #include #include #include /*----------------------------------------------------------------------------*/ /* Definitions */ /*----------------------------------------------------------------------------*/ #define LGD_MODULE_NAME "sm" /* Compteur d'ouverture de la librairie */ unsigned int SM_Open_Counter = 0; /* Flux de sortie des messages d'erreur générés par la librairie */ FILE * SM_stderr; extern char * strdup (const char *); #define max(A,B) ((A < B) ? B : A) /* Limite supérieure d'adressage */ #define MEM_LIMIT 1000000000 //#define SMD_CMD_VALUE_SUM (NDT_Command)65 #define NDD_CMD_VALUE_SUM (NDT_Command)NDD_CMD_USER_TRAVERSE /* Taille d'un segment = 100 Ko par défaut */ #define K 1024 #define NB_BLOCS 100 #define SEGMENT_DEFAULT_SIZE NB_BLOCS * K char Info_Msg [256]; /* Etats possibles pour un heap */ #define SMD_STATE_VALID 0 #define SMD_STATE_UNVALIDATED 1 #define SMD_STATE_CORRUPTED 2 /* Référence sur le heap système */ SMT_Heap * System_Heap; /* Liste des heaps ouverts par le processus courant */ NDT_Root * Opened_Heap_List; /* Taille par défaut des chunks alloués dans le heap système : Cette taille est fixée à la taille maximale des chunks pouvant y être alloué (de type NDT_Node, NDT_Root, SMT_MHH ou SMT_DSH) */ #define DEFAULT_CHUNK_SIZE max(max(sizeof(NDT_Root), sizeof(NDT_Node)), max(sizeof(SMT_MHH), sizeof(SMT_DSH))) /* Nombre de chunk libre minimum avant extension du heap système par un nouveau segment de données (fixé à 3 car l'ajout d'un nouveau segment nécessite l'allocation de 2 chunks (un noeud et une entête de segment). */ #define FREE_CHUNK_LIMIT 3 int SM_Instance; char * SM_Context; /* Contexte et instance d'utilisation de la librairie */ #define DEFAULT_INSTANCE 1000 #define INSTANCE_ENV_VAR "INSTANCE" #define DEFAULT_CONTEXT "CTX" #define CONTEXT_ENV_VAR "CONTEXT" /* Variable globale permettant d'indiquer que l'on est en train d'étendre le heap système par ajout d'un nouveau segment. */ unsigned int Adding_Segment = FALSE; SMT_Chunk * Tmp_Chunk; SMT_MHH * Tmp_MHH; /* Définition des opérations de verouillage et de déverrouillage NB : la valeur 1 d'un sémaphore correspond à l'état non verrouillé */ /* Pose d'un verrou en lecture : 2 opérations : ( -1 puis +2 ) */ struct sembuf SM_SemOp_SSL [2] = { {0, -1, SEM_UNDO}, {0, 2, SEM_UNDO} }; /* Libération d'un verrou en lecture : 2 opérations ( -2 puis +1 ) */ struct sembuf SM_SemOp_RSL [2] = { {0, -2, SEM_UNDO|IPC_NOWAIT}, {0, 1, SEM_UNDO|IPC_NOWAIT} }; /* Pose d'un verrou en écriture : 2 opérations ( -1 puis 0 ) */ struct sembuf SM_SemOp_SEL [2] = { {0, -1, SEM_UNDO}, {0, 0, SEM_UNDO} }; /* Libération d'un verrou en écriture : 2 opérations ( 0 puis +1 ) */ struct sembuf SM_SemOp_REL [2] = { {0, 0, SEM_UNDO|IPC_NOWAIT}, {0, 1, SEM_UNDO|IPC_NOWAIT} }; /* Transformation d'un verrou en écriture en un verrou en lecture : 2 opérations ( 0 puis +2 ) */ struct sembuf SM_SemOp_TSL [2] = { {0, 0, SEM_UNDO|IPC_NOWAIT}, {0, 2, SEM_UNDO|IPC_NOWAIT} }; typedef union semun { int val; struct semid_ds * buf; unsigned short int * array; } semun; /*------------------------------------------------------------------------------*/ /* */ /*------------------------------------------------------------------------------*/ #define SM_LIBSHMEM_OPEN_CHECK() if( !SM_Base) { LG_LOG_ERROR_0( "LibShMem library is not open"); return( SMS_ERRAPI); } #define SM_HEAP_NAME_CHECK(ptr) if( !(ptr)) { LG_LOG_ERROR_0( "Heap name is undefined"); return( SMS_ERRAPI); } #define SM_HEAP_CHECK( ptr) if( !(ptr)) { LG_LOG_ERROR_0( "Heap is undefined"); return( SMS_ERRAPI); } /*------------------------------------------------------------------------------*/ /* Allocation de mémoire dans la base */ /*------------------------------------------------------------------------------*/ NDT_Status SM_Base_Alloc( void **, size_t, void *); /*------------------------------------------------------------------------------*/ /* Désallocation de mémoire dans la base */ /*------------------------------------------------------------------------------*/ NDT_Status SM_Base_Free( void *, void *); /*------------------------------------------------------------------------------*/ /* Allocation de mémoire dans le heap système */ /*------------------------------------------------------------------------------*/ NDT_Status SM_System_Alloc( void **, size_t, void *); /*------------------------------------------------------------------------------*/ /* Désallocation de mémoire dans le heap système */ /*------------------------------------------------------------------------------*/ NDT_Status SM_System_Free( void *, void *); /*------------------------------------------------------------------------------*/ /* Initialisation de la base */ /*------------------------------------------------------------------------------*/ SMT_Status SM_Base_Init (void); /*------------------------------------------------------------------------------*/ /* Terminaison de la base */ /*------------------------------------------------------------------------------*/ SMT_Status SM_Base_End (void); /*------------------------------------------------------------------------------*/ /* Ouverture de la base */ /*------------------------------------------------------------------------------*/ SMT_Status SM_Base_Open (void); /*------------------------------------------------------------------------------*/ /* Fermeture de la base */ /*------------------------------------------------------------------------------*/ SMT_Status SM_Base_Close (void); /*------------------------------------------------------------------------------*/ /* Pose d'un verrou sur la base : */ /*------------------------------------------------------------------------------*/ SMT_Status SM_Base_Lock (SMT_Flags); /*------------------------------------------------------------------------------*/ /* Libération d'un verrou sur la base : */ /*------------------------------------------------------------------------------*/ SMT_Status SM_Base_Unlock (SMT_Flags); /*------------------------------------------------------------------------------*/ /* Fonction manager de la liste des heaps ouverts */ /*------------------------------------------------------------------------------*/ NDT_Status SM_Opened_Heap_List_Manager ( NDT_Root *, NDT_Index_Id, NDT_Node *, NDT_Command, va_list *); /*------------------------------------------------------------------------------*/ /* Fonction manager du MHR (Memory Heap Root) */ /*------------------------------------------------------------------------------*/ NDT_Status MHR_Manager( NDT_Root *, NDT_Index_Id, NDT_Node *, NDT_Command, va_list *); /*------------------------------------------------------------------------------*/ /* Destruction d'un MHH (Memory Heap Header) */ /*------------------------------------------------------------------------------*/ SMT_Status SM_MHH_End (SMT_MHH *); /*------------------------------------------------------------------------------*/ /* Fonction manager pour un DSR (Data Segment Root) */ /*------------------------------------------------------------------------------*/ NDT_Status SM_DSR_Manager( NDT_Root *, NDT_Index_Id, NDT_Node *, NDT_Command, va_list *); /*------------------------------------------------------------------------------*/ /* Ouverture d'un segment de données */ /*------------------------------------------------------------------------------*/ SMT_Status SM_DataSegment_Open (SMT_DSH *); /*------------------------------------------------------------------------------*/ /* Fermeture d'un segment de données */ /*------------------------------------------------------------------------------*/ SMT_Status SM_DataSegment_Close (SMT_DSH *); /*------------------------------------------------------------------------------*/ /* Compression d'un segment de données */ /*------------------------------------------------------------------------------*/ size_t SM_DataSegment_Compress (SMT_DSH *, NDT_Root *); /*------------------------------------------------------------------------------*/ /* Fonction manager pour un ACR (Allocated Chunk Root) */ /*------------------------------------------------------------------------------*/ NDT_Status SM_ACR_Manager( NDT_Root *, NDT_Index_Id, NDT_Node *, NDT_Command, va_list *); /*------------------------------------------------------------------------------*/ /* Fonction manager pour un FCR (Free Chunk Root) */ /*------------------------------------------------------------------------------*/ NDT_Status SM_FCR_Manager( NDT_Root *, NDT_Index_Id, NDT_Node *, NDT_Command, va_list *); /*------------------------------------------------------------------------------*/ /* Pose d'un verrou sur un heap : */ /*------------------------------------------------------------------------------*/ SMT_Status SM_Heap_Lock_Set (SMT_MHH *, SMT_Flags); /*------------------------------------------------------------------------------*/ /* Changement d'un verrou sur un heap : */ /*------------------------------------------------------------------------------*/ SMT_Status SM_Heap_Lock_Change (SMT_MHH *, SMT_Flags); /*------------------------------------------------------------------------------*/ /* Libération d'un verrou sur un heap : */ /*------------------------------------------------------------------------------*/ SMT_Status SM_Heap_Lock_Release (SMT_MHH *, SMT_Flags); /*------------------------------------------------------------------------------*/ /* Opération sur un sémaphore */ /*------------------------------------------------------------------------------*/ SMT_Status SM_Semaphore_Operate (int, struct sembuf *, unsigned int); /*------------------------------------------------------------------------------*/ /* Récupère sous forme explicite l'état d'un verrou */ /*------------------------------------------------------------------------------*/ char * SM_Lock_Status_Get (const char *, void *); /*------------------------------------------------------------------------------*/ /* Add context prefix to heap name */ /*------------------------------------------------------------------------------*/ /* (O) Prefixed: Prefixed heap name */ /* (I) Unprefixed: Unprefixed heap name */ /*------------------------------------------------------------------------------*/ SMT_Status SM_Name_Prefix( char *, const char *); /*------------------------------------------------------------------------------*/