l3.libnazgul/src/nzg_allocate.c

170 lines
4.5 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).
*/
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-22 09:59:56 +00:00
int selectedPoolIndex;
2004-02-22 11:50:29 +00:00
int bufferFreeIndex;
2004-02-21 14:46:24 +00:00
2004-02-22 09:59:56 +00:00
selectedPoolIndex=-1;
2004-02-21 16:12:22 +00:00
2004-02-21 12:59:43 +00:00
/* TODO: verifier le premier arg du shm_open */
2004-02-22 11:50:29 +00:00
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;
}
2004-02-22 11:50:29 +00:00
msgPoolData * mSPoolDataTabAddr;
2004-02-21 12:59:43 +00:00
2004-02-22 11:50:29 +00:00
mSPoolDataTabAddr = mmap( 0, (space->poolNb) * sizeof( msgPoolData ),
2004-02-21 12:59:43 +00:00
PROT_READ | PROT_WRITE,
MAP_SHARED, mSPoolDataTabFd, 0 );
2004-02-22 11:50:29 +00:00
if( mSPoolDataTabAddr == MAP_FAILED) {
2004-02-21 12:59:43 +00:00
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++) {
2004-02-22 11:50:29 +00:00
if(mSPoolDataTabAddr[i].bufferSize >= taille) {
2004-02-21 12:59:43 +00:00
/* 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){
2004-02-22 15:29:36 +00:00
NZG_ERROR("sem_open",ressourceSemId);
return NULL;
2004-02-21 16:12:22 +00:00
}
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){
2004-02-22 15:29:36 +00:00
NZG_ERROR("sem_getvalue",ressourceSemId);
return NULL;
2004-02-21 16:12:22 +00:00
}
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] =
2004-02-22 11:50:29 +00:00
(float) (- (*ressourceSemVal) / mSPoolDataTabAddr[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-22 09:59:56 +00:00
selectedPoolIndex=i;
2004-02-21 12:59:43 +00:00
break;
}
2004-02-21 16:12:22 +00:00
if( sem_close(ressourceSemFd) <0){
2004-02-22 15:29:36 +00:00
NZG_ERROR("sem_getvalue",ressourceSemId);
return NULL;
2004-02-21 16:12:22 +00:00
}
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-22 09:59:56 +00:00
selectedPoolIndex=idxPoolOptimum;
2004-02-21 12:59:43 +00:00
}
}
}else {
2004-02-22 09:59:56 +00:00
selectedPoolIndex=pool;
2004-02-21 12:59:43 +00:00
}
2004-02-21 16:12:22 +00:00
if (!gotRessourceSem){
2004-02-22 11:50:29 +00:00
strcpy(resultPoolId,mSPoolDataTabAddr[selectedPoolIndex].id);
2004-02-22 09:59:56 +00:00
msgPoolSemIdIntern(ressourceSemId,space->id,selectedPoolIndex);
2004-02-21 16:12:22 +00:00
ressourceSemFd=sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0);
2004-02-21 16:49:52 +00:00
if(ressourceSemFd==SEM_FAILED){
2004-02-22 15:29:36 +00:00
NZG_ERROR("sem_open",ressourceSemId);
2004-02-21 16:49:52 +00:00
return NULL;
}
2004-02-21 16:12:22 +00:00
}
if (sem_wait(ressourceSemFd) < 0){
2004-02-22 15:29:36 +00:00
NZG_ERROR("sem_wait",ressourceSemId);
2004-02-21 16:12:22 +00:00
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 */
2004-02-22 11:50:29 +00:00
bufferFreeIndex = msgBufferGetFreeIndex(mSPoolDataTabAddr,selectedPoolIndex);
if (bufferFreeIndex < 0){
sem_close(poolDataTabSemFd);
// aucun buffer libre ?
return NULL;
}
printf("Buffer %d libre a attacher !\n",bufferFreeIndex);
2004-02-22 09:52:22 +00:00
/*TODO: mapper le buffer libre dans l'esp addr du proc */
//TODO:
/* - on s'enregistre aupres de ce buffer */
2004-02-22 09:59:56 +00:00
//TODO: msgBufferAttachProc(space,selectedPoolIndex,resultAddr);
2004-02-22 09:52:22 +00:00
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;
}