l3.libnazgul/src/nzg_allocate.c

164 lines
4.3 KiB
C
Raw Normal View History

2004-02-21 12:59:43 +00:00
#include "libnazgul.h"
#include "nzg_ids.h"
/* TODO: remplacer le bool de msgPoolData par un identifiant
de semaphore. Le semaphore contient poolNb valeurs (et indique
donc le nombre de ressources disponnibles).
*/
#define ANYPOOL -1
#define SPECIFICPOOL 0
void * msgAllocate(msgSpace *space,
int pool,
int taille,
int option
){
2004-02-21 16:12:22 +00:00
void * resultAddr=NULL;
2004-02-21 12:59:43 +00:00
int i, mSPoolDataTabFd;
2004-02-21 16:12:22 +00:00
msgPoolId resultPoolId;
2004-02-21 12:59:43 +00:00
/* tableau des valeurs des semPoolCoef/pool pour identifier le pool
* qui sera lib<EFBFBD>r<EFBFBD> le plus rapidement */
float semPoolCoef[space->poolNb];
int idxPoolOptimum;
2004-02-21 14:46:24 +00:00
bool gotRessourceSem;
sem_t * poolDataTabSemFd=NULL;
msgPoolDataTabSemId ressourceSemId;
2004-02-21 16:12:22 +00:00
sem_t * ressourceSemFd=NULL;
int * ressourceSemVal=NULL;
2004-02-21 12:59:43 +00:00
float minPoolCoef;
2004-02-21 16:12:22 +00:00
int selectedIdx;
2004-02-21 14:46:24 +00:00
2004-02-21 16:12:22 +00:00
selectedIdx=-1;
2004-02-21 12:59:43 +00:00
/* TODO: verifier le premier arg du shm_open */
mSPoolDataTabFd=shm_open(space->poolDataTabId,O_RDWR,MSGSPACE_DEFAULT_MODE);
2004-02-21 12:59:43 +00:00
if (mSPoolDataTabFd == -1 ) {
fprintf( stderr, "Allocate %s failed: %s\n",
(char*)space->poolDataTabId,
2004-02-21 12:59:43 +00:00
strerror( errno ) );
return NULL;
}
msgPoolData * mSPoolDataTab;
mSPoolDataTab = mmap( 0, (space->poolNb) * sizeof( msgPoolData ),
PROT_READ | PROT_WRITE,
MAP_SHARED, mSPoolDataTabFd, 0 );
if( mSPoolDataTab == MAP_FAILED) {
fprintf( stderr, "mmap failed: %s\n",
strerror( errno ) );
return NULL;
}
2004-02-21 14:46:24 +00:00
gotRessourceSem=false;
2004-02-21 12:59:43 +00:00
/* initialisation des coefs */
for (i=0;i<(space->poolNb);i++){
semPoolCoef[i]=-1;
}
int nbLockedSem=0;
if ( pool == ANYPOOL){
// choisir le pool au hasard (ou presque)
for(i=0; i<(space->poolNb); i++) {
if(mSPoolDataTab[i].bufferSize >= taille) {
/* choisir le numero du semaphore
en fonction du nombre de lock poses / nombre de buffer */
2004-02-21 16:12:22 +00:00
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;
}
2004-02-21 12:59:43 +00:00
/* on remplit le tableau avec les valeurs des semaphores */
2004-02-21 16:12:22 +00:00
if (sem_getvalue(ressourceSemFd, ressourceSemVal) < 0){
#warning "G<>rer mieu les erreurs..."
perror("sem_getvalue"); return NULL;
}
2004-02-21 14:46:24 +00:00
if ((*ressourceSemVal) < 0){
2004-02-21 16:12:22 +00:00
/* il y a ressourceSemVal processus qui attendent d<>ja... */
semPoolCoef[nbLockedSem] =
(float) (- (*ressourceSemVal) / mSPoolDataTab[i].bufferNb);
2004-02-21 12:59:43 +00:00
nbLockedSem++;
}
2004-02-21 14:46:24 +00:00
if(sem_trywait(ressourceSemFd)) {
2004-02-21 12:59:43 +00:00
/* choisir la 1ere pool de taille plus grande
* libre si possible */
2004-02-21 14:46:24 +00:00
gotRessourceSem=true;
2004-02-21 16:12:22 +00:00
selectedIdx=i;
2004-02-21 12:59:43 +00:00
break;
}
2004-02-21 16:12:22 +00:00
if( sem_close(ressourceSemFd) <0){
#warning "G<>rer mieu les erreurs..."
perror("sem_getvalue"); return NULL;
}
2004-02-21 12:59:43 +00:00
} // if buffSize > taille
} // for
2004-02-21 14:46:24 +00:00
if (!gotRessourceSem) {
2004-02-21 12:59:43 +00:00
minPoolCoef= semPoolCoef[0];
idxPoolOptimum = 0;
/* on cherche le pool avec le moins de lock poses / nbre de buffer
* le num<EFBFBD>ro du pool est stock<EFBFBD> dans idxPoolOptimum */
for(i=0; i<nbLockedSem; i++) {
if ((semPoolCoef[i] != -1)
&& (semPoolCoef[i] < minPoolCoef)) {
minPoolCoef = semPoolCoef[i];
idxPoolOptimum = i;
}
}
if (minPoolCoef == -1){
/* il n'y a aucune pool dont la taille satisfait la demande */
return NULL;
} else {
2004-02-21 16:12:22 +00:00
selectedIdx=idxPoolOptimum;
2004-02-21 12:59:43 +00:00
}
}
}else {
2004-02-21 16:12:22 +00:00
selectedIdx=pool;
2004-02-21 12:59:43 +00:00
}
2004-02-21 16:12:22 +00:00
if (!gotRessourceSem){
strcpy(resultPoolId,mSPoolDataTab[selectedIdx].id);
msgPoolSemIdIntern(ressourceSemId,space->id,selectedIdx);
ressourceSemFd=sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0);
2004-02-21 16:49:52 +00:00
if(ressourceSemFd==SEM_FAILED){
perror("sem_open");
return NULL;
}
2004-02-21 16:12:22 +00:00
}
if (sem_wait(ressourceSemFd) < 0){
perror("sem_wait");
sem_close(ressourceSemFd);
return NULL;
}
2004-02-21 16:49:52 +00:00
/* on a acqui un semaphore pour la ressouce */
/* on acquiert le droit de modifier les infos sur la ressource */
poolDataTabSemFd=sem_open(space->poolDataTabSemId,O_CREAT,SEM_DEFAULT_MODE,1);
2004-02-21 16:12:22 +00:00
2004-02-21 14:46:24 +00:00
/* on modifie maintenant les donn<6E>es */
2004-02-22 09:52:22 +00:00
/* - on r<>cupere l'index du premier buffer libre */
//TODO:int bufferFreeIndex = msgBufferGetFreeIndex(space,selectedIdx);
/*TODO: mapper le buffer libre dans l'esp addr du proc */
//TODO:
/* - on s'enregistre aupres de ce buffer */
//TODO: msgBufferAttachProc(space,selectedIdx,resultAddr);
2004-02-21 16:12:22 +00:00
sem_close(poolDataTabSemFd);
2004-02-21 16:49:52 +00:00
2004-02-21 12:59:43 +00:00
/* TODO: unmapper le msgPoolDataTab */
return resultAddr;
}