l3.libnazgul/src/free.c
2020-03-03 23:46:32 +01:00

70 lines
1.8 KiB
C

#include "libnazgul.h"
int msgFree(msgSpace * space, void * addr){
printf("[ FREE 0x%08x ]\n",(int)addr);
int poolIndex;
int bufferIndex;
msgPoolData * poolDataTabAddr;
sem_t * poolDataTabSemFd;
msgPoolDataTabSemId ressourceSemId;
sem_t * ressourceSemFd;
/* on acquiert le droit de modifier les infos sur la ressource */
/* on protege le tableau des associations */
poolDataTabSemFd=sem_open(space->poolDataTabSemId,
O_CREAT,SEM_DEFAULT_MODE,1);
if (poolDataTabSemFd == SEM_FAILED){
NZG_ERROR("sem_open : ouverture de la ressource",
space->poolDataTabSemId);
return -1;
}
sem_wait(poolDataTabSemFd);
/* verifier le premier arg du shm_open */
poolDataTabAddr = msgPoolDataTabOpen(space);
// TODO: verouiller semaphore DataInfo
int err;
poolIndex=-1; bufferIndex=-1;
err=msgBufferGetProcAttach(
poolDataTabAddr,
space->poolNb,
&poolIndex,
&bufferIndex,
addr
);
printf("Found : %d\n",err);
printf("Freing pool: %d, buffer: %d\n",poolIndex,bufferIndex);
msgBufferDetachProc(poolDataTabAddr,poolIndex,bufferIndex,addr);
/* unmapper le buffer */
void * realAddr=addr;
int bufferSize=poolDataTabAddr[poolIndex].bufferSize;
int bufferNb=poolDataTabAddr[poolIndex].bufferNb;
realAddr=realAddr-poolIndex*bufferSize;
munmap(realAddr,bufferSize*bufferNb);
msgPoolDataTabClose(space,poolDataTabAddr);
// deverouiller semaphore DataInfo
msgPoolDataTabUnlock(space);
// sem_post(poolDataTabSemFd);
// sem_close(poolDataTabSemFd);
// deverouiller semaphore ressource.
msgPoolSemIdIntern(ressourceSemId,space->id,poolIndex);
ressourceSemFd = sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0);
if (ressourceSemFd == SEM_FAILED){
NZG_ERROR("sem_open",ressourceSemId);
goto ERROR;
}
sem_post(ressourceSemFd);
sem_close(ressourceSemFd);
return 0;
ERROR:
return -1;
}