Add nzg_pool.c & fix problems related to ID type

This commit is contained in:
Glenn Y. Rolland 2019-09-17 14:02:24 +02:00
parent 42a6fc274f
commit 74f1f696ef
4 changed files with 91 additions and 37 deletions

View file

@ -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 */

53
src/nzg_pool.c Normal file
View file

@ -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);

View file

@ -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

View file

@ -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;
}