From 634e65599b668dd1a5938bcfbab3c8ec718c6315 Mon Sep 17 00:00:00 2001 From: glenux Date: Sun, 22 Feb 2004 11:50:29 +0000 Subject: [PATCH] * renommages --- src/nzg_allocate.c | 29 ++++++++------ src/nzg_buffer.c | 14 ++----- src/nzg_bufferInit.c | 13 +++---- src/nzg_proto.h | 2 +- src/{nzg_spaces.c => nzg_spaceCreate.c} | 52 +------------------------ src/nzg_spaceDelete.c | 35 +++++++++++++++++ src/nzg_spaceOpen.c | 25 ++++++++++++ 7 files changed, 88 insertions(+), 82 deletions(-) rename src/{nzg_spaces.c => nzg_spaceCreate.c} (77%) create mode 100644 src/nzg_spaceDelete.c create mode 100644 src/nzg_spaceOpen.c diff --git a/src/nzg_allocate.c b/src/nzg_allocate.c index 00e7f09..1ad73a5 100644 --- a/src/nzg_allocate.c +++ b/src/nzg_allocate.c @@ -29,11 +29,14 @@ void * msgAllocate(msgSpace *space, int * ressourceSemVal=NULL; float minPoolCoef; int selectedPoolIndex; + int bufferFreeIndex; selectedPoolIndex=-1; /* TODO: verifier le premier arg du shm_open */ - mSPoolDataTabFd=shm_open(space->poolDataTabId,O_RDWR,MSGSPACE_DEFAULT_MODE); + mSPoolDataTabFd=shm_open(space->poolDataTabId, + O_RDWR, + MSGSPACE_DEFAULT_MODE); if (mSPoolDataTabFd == -1 ) { fprintf( stderr, "Allocate %s failed: %s\n", (char*)space->poolDataTabId, @@ -41,13 +44,13 @@ void * msgAllocate(msgSpace *space, return NULL; } - msgPoolData * mSPoolDataTab; + msgPoolData * mSPoolDataTabAddr; - mSPoolDataTab = mmap( 0, (space->poolNb) * sizeof( msgPoolData ), + mSPoolDataTabAddr = mmap( 0, (space->poolNb) * sizeof( msgPoolData ), PROT_READ | PROT_WRITE, MAP_SHARED, mSPoolDataTabFd, 0 ); - if( mSPoolDataTab == MAP_FAILED) { + if( mSPoolDataTabAddr == MAP_FAILED) { fprintf( stderr, "mmap failed: %s\n", strerror( errno ) ); return NULL; @@ -64,26 +67,24 @@ void * msgAllocate(msgSpace *space, if ( pool == ANYPOOL){ // choisir le pool au hasard (ou presque) for(i=0; i<(space->poolNb); i++) { - if(mSPoolDataTab[i].bufferSize >= taille) { + if(mSPoolDataTabAddr[i].bufferSize >= taille) { /* choisir le numero du semaphore en fonction du nombre de lock poses / nombre de buffer */ msgPoolSemIdIntern(ressourceSemId,space->id,i); ressourceSemFd = sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0); if (ressourceSemFd == SEM_FAILED){ -#warning "Gérer mieu les erreurs..." perror("sem_open"); return NULL; } /* on remplit le tableau avec les valeurs des semaphores */ if (sem_getvalue(ressourceSemFd, ressourceSemVal) < 0){ -#warning "Gérer mieu les erreurs..." perror("sem_getvalue"); return NULL; } if ((*ressourceSemVal) < 0){ /* il y a ressourceSemVal processus qui attendent déja... */ semPoolCoef[nbLockedSem] = - (float) (- (*ressourceSemVal) / mSPoolDataTab[i].bufferNb); + (float) (- (*ressourceSemVal) / mSPoolDataTabAddr[i].bufferNb); nbLockedSem++; } if(sem_trywait(ressourceSemFd)) { @@ -94,7 +95,6 @@ void * msgAllocate(msgSpace *space, break; } if( sem_close(ressourceSemFd) <0){ -#warning "Gérer mieu les erreurs..." perror("sem_getvalue"); return NULL; } } // if buffSize > taille @@ -125,7 +125,7 @@ void * msgAllocate(msgSpace *space, } if (!gotRessourceSem){ - strcpy(resultPoolId,mSPoolDataTab[selectedPoolIndex].id); + strcpy(resultPoolId,mSPoolDataTabAddr[selectedPoolIndex].id); msgPoolSemIdIntern(ressourceSemId,space->id,selectedPoolIndex); ressourceSemFd=sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0); if(ressourceSemFd==SEM_FAILED){ @@ -146,8 +146,13 @@ void * msgAllocate(msgSpace *space, /* on modifie maintenant les données */ /* - on récupere l'index du premier buffer libre */ - //TODO:int bufferFreeIndex = msgBufferGetFreeIndex(space,selectedPoolIndex); - + bufferFreeIndex = msgBufferGetFreeIndex(mSPoolDataTabAddr,selectedPoolIndex); + if (bufferFreeIndex < 0){ + sem_close(poolDataTabSemFd); + // aucun buffer libre ? + return NULL; + } + printf("Buffer %d libre a attacher !\n",bufferFreeIndex); /*TODO: mapper le buffer libre dans l'esp addr du proc */ //TODO: /* - on s'enregistre aupres de ce buffer */ diff --git a/src/nzg_buffer.c b/src/nzg_buffer.c index 6851883..57a51e6 100644 --- a/src/nzg_buffer.c +++ b/src/nzg_buffer.c @@ -1,10 +1,4 @@ -#include "libnazgul.h" -#include "nzg_ids.h" - -int msgBufferGetFreeIndex(msgSpaceId spaceId,int poolIndex){ - return -1; -} - -int msgBufferAttachProc(msgSpaceId spaceId,int poolIndex,void * addr){ - return -1; -} +/* + int msgBufferDetachProc(msgSpace * space, int poolIndex){ + } + */ diff --git a/src/nzg_bufferInit.c b/src/nzg_bufferInit.c index 17f1661..38fb8d8 100644 --- a/src/nzg_bufferInit.c +++ b/src/nzg_bufferInit.c @@ -5,7 +5,6 @@ int msgBufferInit(msgPoolData * poolDataTabAddr,int poolIndex){ int bufferInfoTabFd; msgBufferInfo * bufferInfoTabAddr; int bufferInfoNb; -int bufferFreeIndex; //récuperer l'ID du BufferInfoTab; strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId); @@ -21,16 +20,14 @@ int bufferFreeIndex; bufferInfoTabAddr=mmap(NULL,bufferInfoNb*sizeof(msgBufferInfo), PROT_READ,MAP_SHARED,bufferInfoTabFd,(off_t)0); - int i=0; - while ((ipoolDataTabId) < 0){ - perror("shm_unlink"); - return -1; - } - printf("ok\n"); - - if (shm_unlink(nzgId)<0){ - perror("shm_unlink"); - return -1; - }; - return 0; -} diff --git a/src/nzg_spaceDelete.c b/src/nzg_spaceDelete.c new file mode 100644 index 0000000..186819d --- /dev/null +++ b/src/nzg_spaceDelete.c @@ -0,0 +1,35 @@ +#include "libnazgul.h" +#include "nzg_ids.h" + +/* prototypes des fonctions annexes à ne pas exporter */ +int msgSpaceIdIntern(msgSpaceId dest,const msgSpaceId src ); +int msgPoolDataIdIntern(msgSpaceId dest,const msgSpaceId src ); + +int msgSpaceDelete(msgSpaceId spaceId){ + fprintf(stderr,"Deleting msgSpace with id : %s\n",spaceId); + //int shmId; + msgSpaceId nzgId; + msgSpace * space; + if (msgSpaceIdIntern(nzgId,spaceId) == -1){ + //TODO: message d'erreur + return -1; + } + + space = msgSpaceOpen(spaceId); + /* TODO: supprimer chaque pool */ + + printf("openned successfully !\n"); + printf("Unlinking DataTab... "); + if (shm_unlink(space->poolDataTabId) < 0){ + perror("shm_unlink"); + return -1; + } + printf("ok\n"); + + if (shm_unlink(nzgId)<0){ + perror("shm_unlink"); + return -1; + }; + return 0; +} + diff --git a/src/nzg_spaceOpen.c b/src/nzg_spaceOpen.c new file mode 100644 index 0000000..c29bb56 --- /dev/null +++ b/src/nzg_spaceOpen.c @@ -0,0 +1,25 @@ +#include "libnazgul.h" +#include "nzg_ids.h" + +/* prototypes des fonctions annexes à ne pas exporter */ +int msgSpaceIdIntern(msgSpaceId dest,const msgSpaceId src ); +int msgPoolDataIdIntern(msgSpaceId dest,const msgSpaceId src ); + + +msgSpace * msgSpaceOpen(msgSpaceId spaceId){ + int shmFd; + msgSpaceId nzgId; + msgSpace * mSAddr; + if (msgSpaceIdIntern(nzgId,spaceId) < 0){ + return NULL; + } + if ((shmFd=shm_open(nzgId,O_RDWR,MSGSPACE_DEFAULT_MODE)) < 0){ + perror("shm_open"); + return NULL; + } + mSAddr=mmap(NULL,sizeof(msgSpace),PROT_READ|PROT_WRITE,MAP_SHARED,shmFd,(off_t)0); + + return mSAddr; +} + +