l3.libnazgul/src/bufferGetFreeIndex.c

40 lines
1 KiB
C
Raw Normal View History

2019-09-17 12:45:48 +00:00
#include "libnazgul.h"
int msgBufferGetFreeIndex(msgPoolData * poolDataTabAddr,int poolIndex){
msgBufferInfoTabId bufferInfoTabId;
int bufferInfoTabFd;
msgBufferInfo * bufferInfoTabAddr;
int bufferInfoNb;
int bufferFreeIndex;
2004-02-23 11:18:35 +00:00
int i;
2019-09-17 12:45:48 +00:00
//r<>cuperer l'ID du BufferInfoTab;
strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId);
bufferInfoTabFd=shm_open(bufferInfoTabId,O_RDWR,SHM_DEFAULT_MODE);
if (bufferInfoTabFd<0){
2004-02-22 15:29:36 +00:00
NZG_ERROR("shm_open",bufferInfoTabId);
2019-09-17 12:45:48 +00:00
return -1;
}
/** on regarde dans le tableau d'infos de buffer **/
bufferInfoNb=poolDataTabAddr[poolIndex].bufferNb;
bufferInfoTabAddr=mmap(NULL,bufferInfoNb*sizeof(msgBufferInfo),
PROT_READ,MAP_SHARED,bufferInfoTabFd,(off_t)0);
2004-02-23 11:18:35 +00:00
i=0;
2019-09-17 12:45:48 +00:00
while ((i<bufferInfoNb)
&& (bufferInfoTabAddr[i].ownerPid != (pid_t)-1)){ i++; }
if (i == bufferInfoNb){ return -1; }
bufferFreeIndex=i;
if (munmap(bufferInfoTabAddr,bufferInfoNb*sizeof(msgBufferInfo))< 0)
2004-02-22 15:29:36 +00:00
{
NZG_ERROR("munmap",bufferInfoTabId);
return -1;
}
2019-09-17 12:45:48 +00:00
return bufferFreeIndex;
}