/************************************************************************* * UrBackup - Client/Server backup system * Copyright (C) 2011 Martin Raiber * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . **************************************************************************/ #include "bufmgr.h" #include "../Interface/Server.h" CBufMgr::CBufMgr(unsigned int nbuf, unsigned int bsize) { for(unsigned int i=0;icreateMutex(); cond=Server->createCondition(); } CBufMgr::~CBufMgr(void) { for(size_t i=0;iLog("Warning: Deleting used Buffer!", LL_DEBUG); } delete[] buffers[i].buffer; } Server->destroy(mutex); Server->destroy(cond); } char* CBufMgr::getBuffer(void) { IScopedLock lock(mutex); while(true) { for(size_t i=0;iLog("Buffers full... -1", LL_INFO); cond->wait(&lock); } } std::vector CBufMgr::getBuffers(unsigned int n) { std::vector ret; if(n==0) return ret; IScopedLock lock(mutex); while(true) { for(size_t i=0;i=n) return ret; } } Server->Log("Buffers full... -2", LL_INFO); cond->wait(&lock); } } void CBufMgr::releaseBuffer(char* buf) { IScopedLock lock(mutex); for(size_t i=0;inotify_one(); return; } } Server->Log("Warning: Buffer to free not found!", LL_WARNING); } unsigned int CBufMgr::nfreeBufffer(void) { IScopedLock lock(mutex); return freebufs; } CBufMgr2::CBufMgr2(unsigned int nbuf, unsigned int bsize) { bufptr=new char[nbuf*bsize]; for(unsigned int i=0;icreateMutex(); cond=Server->createCondition(); } CBufMgr2::~CBufMgr2(void) { delete [] bufptr; Server->destroy(mutex); Server->destroy(cond); } char* CBufMgr2::getBuffer(void) { IScopedLock lock(mutex); while(free_bufs.empty()) cond->wait(&lock); char *ret=free_bufs.top(); free_bufs.pop(); return ret; } std::vector CBufMgr2::getBuffers(unsigned int n) { std::vector ret; IScopedLock lock(mutex); while(ret.size()wait(&lock); ret.push_back(free_bufs.top()); free_bufs.pop(); } return ret; } void CBufMgr2::releaseBuffer(char* buf) { IScopedLock lock(mutex); free_bufs.push(buf); cond->notify_one(); } unsigned int CBufMgr2::nfreeBufffer(void) { IScopedLock lock(mutex); return (_u32)free_bufs.size(); } CFileBufMgr::CFileBufMgr(unsigned int nbuf, bool pMemory) { nbufs=nbuf; memory=pMemory; mutex=Server->createMutex(); cond=Server->createCondition(); } CFileBufMgr::~CFileBufMgr(void) { Server->destroy(mutex); Server->destroy(cond); } IFile* CFileBufMgr::getBuffer(void) { IScopedLock lock(mutex); while(nbufs==0) cond->wait(&lock); if(!memory) { IFile *r=NULL; while(r==NULL) { r=Server->openTemporaryFile(); if(r==NULL) Server->wait(10000); } return r; } else return Server->openMemoryFile(); } std::vector CFileBufMgr::getBuffers(unsigned int n) { std::vector ret; IScopedLock lock(mutex); while(ret.size()wait(&lock); if(!memory) ret.push_back(Server->openTemporaryFile()); else ret.push_back(Server->openMemoryFile()); } return ret; } void CFileBufMgr::releaseBuffer(IFile *buf) { std::string fn=buf->getFilename(); Server->destroy(buf); Server->deleteFile(fn); }