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
|
|
|
|
|
){
|
|
|
|
|
|
|
|
|
|
void * resultAddr;
|
|
|
|
|
int i, mSPoolDataTabFd;
|
2004-02-21 13:17:47 +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;
|
|
|
|
|
bool gotSem;
|
|
|
|
|
sem_t * semFd;
|
|
|
|
|
int * sval;
|
|
|
|
|
float minPoolCoef;
|
|
|
|
|
|
|
|
|
|
/* TODO: verifier le premier arg du shm_open */
|
|
|
|
|
mSPoolDataTabFd=shm_open(space->poolDataId,O_RDWR,MSGSPACE_DEFAULT_MODE);
|
|
|
|
|
if (mSPoolDataTabFd == -1 ) {
|
|
|
|
|
fprintf( stderr, "Allocate %s failed: %s\n",
|
|
|
|
|
(char*)space->poolDataId,
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gotSem=false;
|
|
|
|
|
int gotIdx=-1;
|
|
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
semFd = sem_open(mSPoolDataTab[i].id,0);
|
|
|
|
|
/* on remplit le tableau avec les valeurs des semaphores */
|
|
|
|
|
sem_getvalue(semFd, sval);
|
|
|
|
|
if ((*sval) < 0){
|
|
|
|
|
semPoolCoef[nbLockedSem] = (float) (- (*sval) / mSPoolDataTab[i].bufferNb);
|
|
|
|
|
nbLockedSem++;
|
|
|
|
|
}
|
|
|
|
|
if(sem_trywait(semFd)) {
|
|
|
|
|
/* choisir la 1ere pool de taille plus grande
|
|
|
|
|
* libre si possible */
|
|
|
|
|
gotSem=true;
|
|
|
|
|
gotIdx=i;
|
|
|
|
|
strcpy(resultPoolID,mSPoolDataTab[gotIdx].id);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} // if buffSize > taille
|
|
|
|
|
} // for
|
|
|
|
|
|
|
|
|
|
if (!gotSem) {
|
|
|
|
|
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 {
|
|
|
|
|
if (sem_wait(semFd) < 0){
|
|
|
|
|
perror("sem_wait");
|
|
|
|
|
}
|
|
|
|
|
strncpy(resultPoolID,
|
|
|
|
|
mSPoolDataTab[idxPoolOptimum].id,
|
|
|
|
|
MSGSPACE_ID_LEN*4
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else {
|
2004-02-21 13:17:47 +00:00
|
|
|
|
|
2004-02-21 12:59:43 +00:00
|
|
|
|
semFd=sem_open(mSPoolDataTab[i].id,0);
|
|
|
|
|
if (sem_wait(semFd) < 0){
|
|
|
|
|
perror("sem_wait");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strncpy(resultPoolID,
|
|
|
|
|
mSPoolDataTab[pool].id,
|
|
|
|
|
MSGSPACE_ID_LEN*4
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* trouver un buffer libre, ou dormir */
|
|
|
|
|
|
|
|
|
|
/* TODO: mapper le buffer dans l'esp addr du proc */
|
|
|
|
|
|
|
|
|
|
/* TODO: unmapper le msgPoolDataTab */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return resultAddr;
|
|
|
|
|
}
|