l3.libnazgul/src/nzg_spaces.c

116 lines
2.5 KiB
C
Raw Normal View History

#include "libnazgul.h"
2019-09-17 06:58:16 +00:00
2004-02-16 21:25:11 +00:00
/*
* spaceId : identifiant externe de l'espace de msg
* queueNb : nombre de files de messages
* poolNb : nombre de pool de buffers
* queueNbCar : tableau de caracteristiques des different pool
*/
msgSpace * msgSpaceCreate(
msgSpaceId spaceId,
int queueNb,
int poolNb,
msgPool * queueNbCar ){
msgSpaceId nzgId;
2019-09-16 15:17:43 +00:00
msgSpaceList mSList;
2019-09-17 06:58:16 +00:00
int mSFd; // shm file descriptor
int i;
2019-09-16 15:17:43 +00:00
static int mSIdNum=-1;
2019-09-17 06:58:16 +00:00
msgSpace * mSAddr;
2019-09-16 15:15:37 +00:00
2019-09-16 15:17:43 +00:00
mSIdNum++;
2019-09-17 06:58:16 +00:00
mSAddr=NULL;
2019-09-16 15:15:37 +00:00
/** recuperation de la liste des msgSpace **/
/* (creation si elle n'existe pas */
/** on cr<63>ee le nouvel element **/
2020-03-03 22:42:59 +00:00
printf("PAGESIZE : %d\n",(int)PAGESIZE);
nzgId=msgSp2nzgId(spaceId);
2019-09-17 06:58:16 +00:00
mSFd=shm_open(
2019-09-16 15:15:37 +00:00
nzgId,
2019-09-17 06:58:16 +00:00
O_RDWR|O_CREAT|O_EXCL|O_TRUNC,
2019-09-16 15:15:37 +00:00
MSGSPACE_DEFAULT_MODE
);
2019-09-17 06:58:16 +00:00
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 ) );
2020-03-03 22:42:59 +00:00
return NULL;
}
2019-09-17 06:58:16 +00:00
/* 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;
}
2004-02-16 21:25:11 +00:00
2019-09-17 06:58:16 +00:00
printf( "Map addr is 0x%08x\n", (int)mSAddr );
2004-02-16 21:25:11 +00:00
2019-09-17 06:58:16 +00:00
/* on ferme le descripteur du fichier */
close(mSFd);
2019-09-17 06:58:16 +00:00
/* TODO: on ajoute spaceId a la liste des msgSpace connus */
2019-09-17 06:58:16 +00:00
/* TODO: on cr<63>e queueNb files de messages */
/* on cr<63>er poolNb pool de buffers */
/* et on "attache" tout ce beau monde au spaceId */
for (i=0;i<poolNb;i++){
// creation d'une pool
2019-09-17 06:58:16 +00:00
// mSAddr->
}
2004-02-16 21:25:11 +00:00
2019-09-17 06:58:16 +00:00
/* on renvoie un pointeur sur le bon spaceId */
return mSAddr;
2004-02-16 21:25:11 +00:00
}
2019-09-16 15:15:37 +00:00
/*
* spaceId : blabla
*/
2020-03-03 22:42:59 +00:00
msgSpace * msgSpaceOpen(msgSpaceId spaceId){
int shmId;
2019-09-16 14:54:52 +00:00
char * nzgId=msgSp2nzgId(spaceId);
2020-03-03 22:42:59 +00:00
if ((shmId=shm_open(nzgId,O_RDWR,MSGSPACE_DEFAULT_MODE)) < 0){
perror("shm_open");
return NULL;
}
2019-09-16 14:43:39 +00:00
return NULL;
}
void msgSpaceDelete(msgSpaceId spaceId){
2020-03-03 22:41:24 +00:00
//int shmId;
2019-09-16 14:54:52 +00:00
msgSpaceId nzgId=msgSp2nzgId(spaceId);
int er;
if ((er=shm_unlink(nzgId))<0){
perror("shm_unlink");
};
2019-09-16 14:43:39 +00:00
}
2019-09-16 14:54:52 +00:00
msgSpaceId msgSp2nzgId(msgSpaceId spaceId){
char * resNzgId;
int slen;
slen=strlen(spaceId);
2019-09-16 15:15:37 +00:00
resNzgId = (char *)malloc(sizeof(char)*(slen+11));
2019-09-17 06:58:16 +00:00
/* sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); */
sprintf(resNzgId,"/nzgSpace%s",(char *)spaceId);
2019-09-16 14:54:52 +00:00
return ((msgSpaceId)resNzgId);
}