From 96ab2bed3971301a0892439f2d71be961e2f9bed Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Mon, 16 Sep 2019 17:15:37 +0200 Subject: [PATCH] Improve msgSpaceList functions --- src/nzg_create.c | 24 ++++++++++++++++++++---- src/nzg_global.h | 4 ++++ src/nzg_iface.h | 13 ++++++++++++- test/create_delete.c | 12 ++++++++---- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/nzg_create.c b/src/nzg_create.c index 0181106..960eb54 100644 --- a/src/nzg_create.c +++ b/src/nzg_create.c @@ -19,10 +19,22 @@ msgSpace * msgSpaceCreate( msgSpace * result; int shmId; msgSpaceId nzgId; - /* on créee */ + msgSpaceList + static spaceIdNum=-1; + + spaceIdNum++; + /** recuperation de la liste des msgSpace **/ + /* (creation si elle n'existe pas */ + + /** on créee le nouvel element **/ printf("PAGESIZE : %d\n",(int)PAGESIZE); nzgId=msgSp2nzgId(spaceId); - if ((shmId=shm_open(nzgId,O_RDWR,MSGSPACE_DEFAULT_MODE)) < 0){ + shmId=shm_open( + nzgId, + O_RDWR|O_CREAT|O_EXCL, + MSGSPACE_DEFAULT_MODE + ); + if (shmId < 0 ) { perror("shm_open"); return NULL; } @@ -42,6 +54,10 @@ msgSpace * msgSpaceCreate( } +/* + * spaceId : blabla + */ + msgSpace * msgSpaceOpen(msgSpaceId spaceId){ int shmId; char * nzgId=msgSp2nzgId(spaceId); @@ -65,7 +81,7 @@ msgSpaceId msgSp2nzgId(msgSpaceId spaceId){ char * resNzgId; int slen; slen=strlen(spaceId); - resNzgId = (char *)malloc(sizeof(char)*(slen+4)); - sprintf(resNzgId,"/NZG%s",(char *)spaceId); + resNzgId = (char *)malloc(sizeof(char)*(slen+11)); + sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); return ((msgSpaceId)resNzgId); } diff --git a/src/nzg_global.h b/src/nzg_global.h index 158818e..e89e4e5 100644 --- a/src/nzg_global.h +++ b/src/nzg_global.h @@ -1,3 +1,6 @@ +#ifndef _NZG_GLOBAL +#define _NZG_GLOBAL 1 + #include /* POSIX et al */ #include #include @@ -7,3 +10,4 @@ #include /* shm_open */ #define PAGESIZE sysconf(_SC_PAGESIZE) +#endif diff --git a/src/nzg_iface.h b/src/nzg_iface.h index c8fb611..a2f7665 100644 --- a/src/nzg_iface.h +++ b/src/nzg_iface.h @@ -1,3 +1,7 @@ +#ifndef _NZG_IFACE +#define _NZG_IFACE 1 + + #include "nzg_global.h" #define MSGSPACE_DEFAULT_MODE 0600 @@ -12,7 +16,14 @@ typedef struct MsgSpace { } msgSpace; +typedef char * msgSpaceListElemId; +typedef struct MsgSpaceListElem { + void * id; + int ownerPid; + /* struct msgSpaceListElem * next; */ + msgSpaceListElemId next; +} * msgSpaceList, msgSpaceListElem; - +#endif /* */ diff --git a/test/create_delete.c b/test/create_delete.c index 3dd62f3..33051a2 100644 --- a/test/create_delete.c +++ b/test/create_delete.c @@ -3,10 +3,14 @@ #include "nzg_proto.h" int main(void) { - // msgSpace mS=0; - msgSpaceId testId=(msgSpaceId)"test"; - //msgSpace * mSPAC=msgSpaceCreate(testId,0,0,NULL); + // msgSpace mS=0; + + msgSpaceId testId; + msgSpace * mSPAC; + + testId=(msgSpaceId)"test"; + mSPAC=msgSpaceCreate(testId,0,0,NULL); msgSpaceDelete(testId); - + return 0; }