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 <sys/types.h>
#include <sys/mman.h> /* shm_open */
#include <errno.h>
#define PAGESIZE sysconf(_SC_PAGESIZE)
#endif

View file

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

View file

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