From 74f1f696efa553fbb1118ffb856a321ceaee7315 Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Tue, 17 Sep 2019 14:02:24 +0200 Subject: [PATCH] Add nzg_pool.c & fix problems related to ID type --- src/nzg_iface.h | 15 +++++++------- src/nzg_pool.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ src/nzg_proto.h | 26 +++++++++--------------- src/nzg_spaces.c | 34 ++++++++++++++++++------------- 4 files changed, 91 insertions(+), 37 deletions(-) create mode 100644 src/nzg_pool.c diff --git a/src/nzg_iface.h b/src/nzg_iface.h index b74393b..aeba9c3 100644 --- a/src/nzg_iface.h +++ b/src/nzg_iface.h @@ -4,16 +4,17 @@ #include "nzg_global.h" #define MSGSPACE_DEFAULT_MODE 0600 +#define MSGSPACE_ID_LEN 32 -typedef char * msgSpaceId; +typedef char msgSpaceId[MSGSPACE_ID_LEN]; -typedef char * msgSpaceListId; -typedef char * msgSpaceListElemId; +typedef char msgSpaceListId[4*MSGSPACE_ID_LEN]; +typedef char msgSpaceListElemId[4*MSGSPACE_ID_LEN]; -typedef char * msgSpacePoolDataId; -typedef char * msgSpacePoolId; -typedef char * msgSpaceQueueDataId; -typedef char * msgSpaceQueueId; +typedef char msgSpacePoolDataId[4*MSGSPACE_ID_LEN]; +typedef char msgSpacePoolId[4*MSGSPACE_ID_LEN]; +typedef char msgSpaceQueueDataId[4*MSGSPACE_ID_LEN]; +typedef char msgSpaceQueueId[4*MSGSPACE_ID_LEN]; /* pid[] */ /* liste des processus demandeurs */ diff --git a/src/nzg_pool.c b/src/nzg_pool.c new file mode 100644 index 0000000..c807c3f --- /dev/null +++ b/src/nzg_pool.c @@ -0,0 +1,53 @@ +# include "libnazgul.h" + +/* pid[] + liste process demandeurs */ + +int msgPoolCreate( + msgSpacePoolId poolId, + int buffNb, + int buffSize + ) { + + int poolFd; + static int poolNb; + msgSpacePoolId id; + + if (msgSpacePoolId2nzgPoolId(id,poolId,poolNb) == -1){ + fprintf( stderr, "msgPoolId creation failed for id %s\n", + (char*)poolId ); + return -1; + } + + poolFd=shm_open(id,O_RDWR|O_CREAT|O_EXCL|O_TRUNC,MSGSPACE_DEFAULT_MODE); + if (poolFd == -1 ) { + fprintf( stderr, "msgPool creation failed: %s\n", + strerror( errno ) ); + return -1; + } + + // on met le pool a la taille voulue pour qu'il + // puisse contenir les buffs + if (ftruncate(poolFd, sizeof((buffSize)*buffNb)) == -1){ + fprintf( stderr, "msgPool resizing failed: %s\n", + strerror( errno ) ); + return -1; + } + + close(poolFd); + + return 0; +} + +int msgSpacePoolId2nzgPoolId(msgSpacePoolId dest,msgSpacePoolId src, int num){ + if (strlen(src)>MSGSPACE_ID_LEN){ + return -1; + } + sprintf(dest,"/nzgSpacesPool%s%d",(char *)src,num); + return 0; +} + +//TODO: msgPoolAllocate +// //donne l'adr en mappant in the memory + //poolAddr = mmap(NULL, sizeof(*msgPool), PROT_NONE, MAP_SHARED, poolFd, 0); + diff --git a/src/nzg_proto.h b/src/nzg_proto.h index 2353ac9..3ab05eb 100644 --- a/src/nzg_proto.h +++ b/src/nzg_proto.h @@ -1,23 +1,17 @@ #ifndef _NZG_PROTO #define _NZG_PROTO 1 +/* nzg_list.c */ char **msgSpaceIdList(void); - -void * msgAllocate( - msgSpace * space, //espace de message concerné - int pool, //numéro de pool ou réaliser l'allocation - int taille, //si l'argument précédent est ANYPOOL, taille requise - int option // 0 ou NONBLOCK - ); - - -/* nzg_create.c */ -msgSpace *msgSpaceCreate( - msgSpaceId spaceId, int queueNb, int poolNb, msgPool *queueNbCar - ); - +/* nzg_pool.c */ +int msgPoolCreate(msgSpacePoolId poolId, int buffNb, int buffSize); +int msgSpacePoolId2nzgPoolId(msgSpacePoolId dest, msgSpacePoolId src, int num); +/* nzg_spaces.c */ +msgSpace *msgSpaceCreate(msgSpaceId spaceId, int queueNb, int poolNb, msgPool *queueNbCar); msgSpace *msgSpaceOpen(msgSpaceId spaceId); -void msgSpaceDelete(msgSpaceId spaceId); -msgSpaceId msgSp2nzgId(msgSpaceId spaceId); +int msgSpaceDelete(msgSpaceId spaceId); +int msgSp2nzgId(msgSpaceId dest, const msgSpaceId src); +/* nzg_state.c */ + #endif diff --git a/src/nzg_spaces.c b/src/nzg_spaces.c index a1ca57c..7aa57c9 100644 --- a/src/nzg_spaces.c +++ b/src/nzg_spaces.c @@ -14,7 +14,7 @@ msgSpace * msgSpaceCreate( int poolNb, msgPool * queueNbCar ){ msgSpaceId nzgId; - msgSpaceList mSList; + //msgSpaceList mSList; int mSFd; // shm file descriptor int i; static int mSIdNum=-1; @@ -27,7 +27,7 @@ msgSpace * msgSpaceCreate( /** on créee le nouvel element **/ printf("PAGESIZE : %d\n",(int)PAGESIZE); - nzgId=msgSp2nzgId(spaceId); + msgSp2nzgId(spaceId,nzgId); mSFd=shm_open( nzgId, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, @@ -87,7 +87,8 @@ msgSpace * msgSpaceCreate( msgSpace * msgSpaceOpen(msgSpaceId spaceId){ int shmId; - char * nzgId=msgSp2nzgId(spaceId); + msgSpaceId nzgId; + if (msgSp2nzgId(nzgId,spaceId)); if ((shmId=shm_open(nzgId,O_RDWR,MSGSPACE_DEFAULT_MODE)) < 0){ perror("shm_open"); return NULL; @@ -95,21 +96,26 @@ msgSpace * msgSpaceOpen(msgSpaceId spaceId){ return NULL; } -void msgSpaceDelete(msgSpaceId spaceId){ +int msgSpaceDelete(msgSpaceId spaceId){ //int shmId; - msgSpaceId nzgId=msgSp2nzgId(spaceId); - int er; - if ((er=shm_unlink(nzgId))<0){ + msgSpaceId nzgId; + if (msgSp2nzgId(nzgId,spaceId) == -1){ + //TODO: message d'erreur + return -1; + } + + if (shm_unlink(nzgId)<0){ perror("shm_unlink"); + return -1; }; + return 0; } -msgSpaceId msgSp2nzgId(msgSpaceId spaceId){ - char * resNzgId; - int slen; - slen=strlen(spaceId); - resNzgId = (char *)malloc(sizeof(char)*(slen+11)); +int msgSp2nzgId(msgSpaceId dest,const msgSpaceId src ){ + if (strlen(src)>MSGSPACE_ID_LEN){ + return -1; + } /* sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); */ - sprintf(resNzgId,"/nzgSpace%s",(char *)spaceId); - return ((msgSpaceId)resNzgId); + sprintf(dest,"/nzgSpace%s",(char *)src); + return 0; }