Improve code (reedit)

This commit is contained in:
Glenn Y. Rolland 2019-09-17 08:58:16 +02:00
parent 9ef6f53162
commit 42a6fc274f
3 changed files with 66 additions and 20 deletions

View file

@ -8,6 +8,7 @@
#include <fcntl.h> /* pour O_RDWR */ #include <fcntl.h> /* pour O_RDWR */
#include <sys/types.h> #include <sys/types.h>
#include <sys/mman.h> /* shm_open */ #include <sys/mman.h> /* shm_open */
#include <errno.h>
#define PAGESIZE sysconf(_SC_PAGESIZE) #define PAGESIZE sysconf(_SC_PAGESIZE)
#endif #endif

View file

@ -6,27 +6,42 @@
#define MSGSPACE_DEFAULT_MODE 0600 #define MSGSPACE_DEFAULT_MODE 0600
typedef char * msgSpaceId; typedef char * msgSpaceId;
typedef char * msgSpaceListId;
typedef char * msgSpaceListElemId; typedef char * msgSpaceListElemId;
typedef char * msgSpacePoolDataId; typedef char * msgSpacePoolDataId;
typedef char * msgSpacePoolId; typedef char * msgSpacePoolId;
typedef char * msgSpaceQueueDataId; typedef char * msgSpaceQueueDataId;
typedef char * msgSpaceQueueId; typedef char * msgSpaceQueueId;
/* pid[] */
/* liste des processus demandeurs */
typedef struct MsgPool { typedef struct MsgPool {
msgSpacePoolId id;
int bufferNb;
int bufferSize;
int allocDispBuffer;
int allocOverload;
} msgPool; } msgPool;
/* TODO: queueId */
typedef struct MsgSpace { typedef struct MsgSpace {
msgSpaceId id;
int poolNb;
int queueNb;
int pid;
msgSpacePoolDataId poolDataId;
} msgSpace; } msgSpace;
/* struct msgSpaceListElem * next; */
typedef struct MsgSpaceListElem { typedef struct MsgSpaceListElem {
void * id; void * id;
int ownerPid; int ownerPid;
/* struct msgSpaceListElem * next; */ msgSpaceListElemId next;
msgSpaceListElemId next;
} * msgSpaceList, msgSpaceListElem; } * msgSpaceList, msgSpaceListElem;

View file

@ -1,6 +1,5 @@
#include "libnazgul.h" #include "libnazgul.h"
#include <sys/types.h>
#include <sys/mman.h>
/* /*
* spaceId : identifiant externe de l'espace de msg * spaceId : identifiant externe de l'espace de msg
@ -14,41 +13,71 @@ msgSpace * msgSpaceCreate(
int queueNb, int queueNb,
int poolNb, int poolNb,
msgPool * queueNbCar ){ msgPool * queueNbCar ){
msgSpace * result;
int shmId;
msgSpaceId nzgId; msgSpaceId nzgId;
msgSpaceList mSList; msgSpaceList mSList;
int mSFd; // shm file descriptor
int i;
static int mSIdNum=-1; static int mSIdNum=-1;
msgSpace * mSAddr;
mSIdNum++; mSIdNum++;
mSAddr=NULL;
/** recuperation de la liste des msgSpace **/ /** recuperation de la liste des msgSpace **/
/* (creation si elle n'existe pas */ /* (creation si elle n'existe pas */
/** on créee le nouvel element **/ /** on créee le nouvel element **/
printf("PAGESIZE : %d\n",(int)PAGESIZE); printf("PAGESIZE : %d\n",(int)PAGESIZE);
nzgId=msgSp2nzgId(spaceId); nzgId=msgSp2nzgId(spaceId);
shmId=shm_open( mSFd=shm_open(
nzgId, nzgId,
O_RDWR|O_CREAT|O_EXCL, O_RDWR|O_CREAT|O_EXCL|O_TRUNC,
MSGSPACE_DEFAULT_MODE MSGSPACE_DEFAULT_MODE
); );
if (shmId < 0 ) { if (mSFd == -1 ) {
perror("shm_open"); fprintf( stderr, "msgSpace %s creation failed: %s\n",
(char*)nzgId,
strerror( errno ) );
return NULL; return NULL;
} }
//on redimentionne l'element
if (ftruncate(mSFd, sizeof(PAGESIZE)) == -1){
fprintf( stderr, "msgSpace resizing failed: %s\n",
strerror( errno ) );
return NULL;
}
/* Map the memory object */
mSAddr = mmap( 0, sizeof( *mSAddr ),
PROT_READ | PROT_WRITE,
MAP_SHARED, mSFd, 0 );
if( mSAddr == MAP_FAILED ) {
fprintf( stderr, "mmap failed: %s\n",
strerror( errno ) );
return NULL;
}
/* on ajoute spaceId a la liste des msgSpace connus */ printf( "Map addr is 0x%08x\n", (int)mSAddr );
/* on crée queueNb files de messages */ /* on ferme le descripteur du fichier */
close(mSFd);
/* on créer poolNb pool de buffers */ /* TODO: on ajoute spaceId a la liste des msgSpace connus */
/* on attache tout ce beau monde au spaceId */ /* TODO: on crée queueNb files de messages */
/* on créer poolNb pool de buffers */
/* et on "attache" tout ce beau monde au spaceId */
for (i=0;i<poolNb;i++){
// creation d'une pool
/* on renvoie un pointeur sur le bon spaceId */ // mSAddr->
result=NULL;
return result; }
/* on renvoie un pointeur sur le bon spaceId */
return mSAddr;
} }
@ -80,6 +109,7 @@ msgSpaceId msgSp2nzgId(msgSpaceId spaceId){
int slen; int slen;
slen=strlen(spaceId); slen=strlen(spaceId);
resNzgId = (char *)malloc(sizeof(char)*(slen+11)); resNzgId = (char *)malloc(sizeof(char)*(slen+11));
sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); /* sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); */
sprintf(resNzgId,"/nzgSpace%s",(char *)spaceId);
return ((msgSpaceId)resNzgId); return ((msgSpaceId)resNzgId);
} }