From cb6c444a19e8650bd073bafe4dd9fb161791e77f Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Wed, 4 Sep 1996 21:50:16 +0000 Subject: [PATCH] added hashed fd to fdi convert --- src/libCom/fdManager.cc | 45 +++++++++--- src/libCom/fdManager.h | 130 +++++++++++++++++++++++---------- src/libCom/fdmgr/fdManager.cpp | 45 +++++++++--- src/libCom/fdmgr/fdManager.h | 130 +++++++++++++++++++++++---------- 4 files changed, 250 insertions(+), 100 deletions(-) diff --git a/src/libCom/fdManager.cc b/src/libCom/fdManager.cc index 952aa94f2..f089cde2e 100644 --- a/src/libCom/fdManager.cc +++ b/src/libCom/fdManager.cc @@ -4,6 +4,9 @@ // // // $Log$ +// Revision 1.1 1996/08/13 22:48:23 jhill +// dfMgr =>fdManager +// // // @@ -44,12 +47,20 @@ inline int selectErrno() // fdManager::fdManager() { - FD_ZERO (&this->read); - FD_ZERO (&this->write); - FD_ZERO (&this->exception); + size_t i; + + for (i=0u; ifdSets)/sizeof(this->fdSets[0u]); i++) { + FD_ZERO(&this->fdSets[i]); + } this->maxFD = 0; - this->processInProg = 0; + this->processInProg = 0u; this->pCBReg = 0; + // + // should throw an exception here + // when most compilers are implementing + // exceptions + // + assert (this->fdTbl.init(0x100)>=0); } // @@ -90,7 +101,7 @@ void fdManager::process (const osiTime &delay) this->processInProg = 1; while ( (pReg=regIter()) ) { - FD_SET(pReg->fd, &pReg->fdSet); + FD_SET(pReg->getFD(), &this->fdSets[pReg->getType()]); } // @@ -111,8 +122,8 @@ void fdManager::process (const osiTime &delay) minDelay = delay; } minDelay.getTV (tv.tv_sec, tv.tv_usec); - status = select (this->maxFD, &this->read, - &this->write, &this->exception, &tv); + status = select (this->maxFD, &this->fdSets[fdrRead], + &this->fdSets[fdrWrite], &this->fdSets[fdrExcp], &tv); staticTimerQueue.process(); if (status==0) { this->processInProg = 0; @@ -135,8 +146,8 @@ void fdManager::process (const osiTime &delay) // regIter.reset(); while ( (pReg=regIter()) ) { - if (FD_ISSET(pReg->fd, &pReg->fdSet)) { - FD_CLR(pReg->fd, &pReg->fdSet); + if (FD_ISSET(pReg->getFD(), &this->fdSets[pReg->getType()])) { + FD_CLR(pReg->getFD(), &this->fdSets[pReg->getType()]); regIter.remove(); this->activeList.add(*pReg); pReg->state = fdrActive; @@ -201,10 +212,22 @@ void fdReg::show(unsigned level) { printf ("fdReg at %x\n", (unsigned) this); if (level>1u) { - printf ("\tfd = %d, fdSet at %x, state = %d, onceOnly = %d\n", - this->fd, (unsigned)&this->fdSet, + printf ("\tstate = %d, onceOnly = %d\n", this->state, this->onceOnly); } + this->fdRegId::show(level); +} + +// +// fdRegId::show() +// +void fdRegId::show(unsigned level) +{ + printf ("fdRegId at %x\n", (unsigned) this); + if (level>1u) { + printf ("\tfd = %d, type = %d\n", + this->fd, this->type); + } } diff --git a/src/libCom/fdManager.h b/src/libCom/fdManager.h index a73b1e010..3e41a6383 100644 --- a/src/libCom/fdManager.h +++ b/src/libCom/fdManager.h @@ -32,6 +32,9 @@ * * History * $Log$ + * Revision 1.1 1996/08/13 22:48:21 jhill + * dfMgr =>fdManager + * * */ @@ -39,6 +42,7 @@ #define fdManagerH_included #include +#include #include #ifdef WIN32 @@ -52,14 +56,62 @@ extern "C" { #include -enum fdRegType {fdrRead, fdrWrite, fdrExcp}; +enum fdRegType {fdrRead, fdrWrite, fdrExcp, fdRegTypeNElem}; enum fdRegState {fdrActive, fdrPending, fdrLimbo}; +class fdRegId +{ +public: + fdRegId (const int fdIn, const fdRegType typeIn) : + fd(fdIn), type(typeIn) {} + + int getFD() + { + return this->fd; + } + + fdRegType getType() + { + return this->type; + } + + int operator == (const fdRegId &idIn) + { + return this->fd == idIn.fd && this->type==idIn.type; + } + + resTableIndex resourceHash(unsigned nBitsId) const + { + unsigned src = (unsigned) this->fd; + resTableIndex hashid; + + hashid = src; + src = src >> nBitsId; + while (src) { + hashid = hashid ^ src; + src = src >> nBitsId; + } + hashid = hashid ^ this->type; + + // + // the result here is always masked to the + // proper size after it is returned to the resource class + // + return hashid; + } + + virtual void show(unsigned level); +private: + const int fd; + const fdRegType type; +}; + // // fdReg // file descriptor registration // -class fdReg : public tsDLNode { +class fdReg : public tsDLNode, public fdRegId, + public tsSLNode { friend class fdManager; public: fdReg (const int fdIn, const fdRegType typ, @@ -89,8 +141,6 @@ private: // virtual void destroy (); - const int fd; - fd_set &fdSet; unsigned char state; // fdRegState goes here unsigned char onceOnly; }; @@ -101,12 +151,15 @@ public: fdManager(); ~fdManager(); void process (const osiTime &delay); + + // + // returns NULL if the fd is unknown + // + fdReg *lookUpFD(const int fd, const fdRegType type); private: tsDLList regList; tsDLList activeList; - fd_set read; - fd_set write; - fd_set exception; + fd_set fdSets[fdRegTypeNElem]; int maxFD; unsigned processInProg; // @@ -117,11 +170,22 @@ private: void installReg (fdReg ®); void removeReg (fdReg ®); - fd_set *pFDSet (fdRegType typIn); + resTable fdTbl; }; extern fdManager fileDescriptorManager; +// +// lookUpFD() +// +inline fdReg *fdManager::lookUpFD(const int fd, const fdRegType type) +{ + if (fd<0) { + return NULL; + } + fdRegId id (fd,type); + return this->fdTbl.lookup(id); +} // // fdManagerMaxInt () @@ -136,37 +200,23 @@ inline int fdManagerMaxInt (int a, int b) } } -// -// fdManager::pFDSet() -// -inline fd_set *fdManager::pFDSet (fdRegType typIn) -{ - fd_set *pSet; - - switch (typIn) { - case fdrRead: - pSet = &this->read; - break; - case fdrWrite: - pSet = &this->write; - break; - case fdrExcp: - pSet = &this->exception; - break; - default: - assert(0); - } - return pSet; -} - // // fdManager::installReg() // inline void fdManager::installReg (fdReg ®) { - this->maxFD = fdManagerMaxInt(this->maxFD, reg.fd+1); + int status; + + this->maxFD = fdManagerMaxInt(this->maxFD, reg.getFD()+1); this->regList.add(reg); reg.state = fdrPending; + status = this->fdTbl.add(reg); + if (status) { + fprintf (stderr, + "**** Warning - duplicate fdReg object\n"); + fprintf (stderr, + "**** will not be seen by fdManager::lookUpFD()\n"); + } } // @@ -174,6 +224,8 @@ inline void fdManager::installReg (fdReg ®) // inline void fdManager::removeReg(fdReg ®) { + fdReg *pItemFound; + // // signal fdManager that the fdReg was deleted // during the call back @@ -181,21 +233,22 @@ inline void fdManager::removeReg(fdReg ®) if (this->pCBReg == ®) { this->pCBReg = 0; } - FD_CLR(reg.fd, ®.fdSet); + FD_CLR(reg.getFD(), &this->fdSets[reg.getType()]); + pItemFound = this->fdTbl.remove(reg); + assert (pItemFound==®); switch (reg.state) { case fdrActive: this->activeList.remove(reg); - reg.state = fdrLimbo; break; case fdrPending: this->regList.remove(reg); - reg.state = fdrLimbo; break; case fdrLimbo: break; default: assert(0); } + reg.state = fdrLimbo; } // @@ -203,11 +256,10 @@ inline void fdManager::removeReg(fdReg ®) // inline fdReg::fdReg (const int fdIn, const fdRegType typIn, const unsigned onceOnlyIn) : - fd(fdIn), fdSet(*fileDescriptorManager.pFDSet(typIn)), - state(fdrLimbo), onceOnly(onceOnlyIn) + fdRegId(fdIn,typIn), state(fdrLimbo), onceOnly(onceOnlyIn) { - assert (this->fd>=0); - if (this->fd>FD_SETSIZE) { + assert (fdIn>=0); + if (fdIn>FD_SETSIZE) { fprintf (stderr, "%s: fd > FD_SETSIZE ignored\n", __FILE__); return; diff --git a/src/libCom/fdmgr/fdManager.cpp b/src/libCom/fdmgr/fdManager.cpp index 952aa94f2..f089cde2e 100644 --- a/src/libCom/fdmgr/fdManager.cpp +++ b/src/libCom/fdmgr/fdManager.cpp @@ -4,6 +4,9 @@ // // // $Log$ +// Revision 1.1 1996/08/13 22:48:23 jhill +// dfMgr =>fdManager +// // // @@ -44,12 +47,20 @@ inline int selectErrno() // fdManager::fdManager() { - FD_ZERO (&this->read); - FD_ZERO (&this->write); - FD_ZERO (&this->exception); + size_t i; + + for (i=0u; ifdSets)/sizeof(this->fdSets[0u]); i++) { + FD_ZERO(&this->fdSets[i]); + } this->maxFD = 0; - this->processInProg = 0; + this->processInProg = 0u; this->pCBReg = 0; + // + // should throw an exception here + // when most compilers are implementing + // exceptions + // + assert (this->fdTbl.init(0x100)>=0); } // @@ -90,7 +101,7 @@ void fdManager::process (const osiTime &delay) this->processInProg = 1; while ( (pReg=regIter()) ) { - FD_SET(pReg->fd, &pReg->fdSet); + FD_SET(pReg->getFD(), &this->fdSets[pReg->getType()]); } // @@ -111,8 +122,8 @@ void fdManager::process (const osiTime &delay) minDelay = delay; } minDelay.getTV (tv.tv_sec, tv.tv_usec); - status = select (this->maxFD, &this->read, - &this->write, &this->exception, &tv); + status = select (this->maxFD, &this->fdSets[fdrRead], + &this->fdSets[fdrWrite], &this->fdSets[fdrExcp], &tv); staticTimerQueue.process(); if (status==0) { this->processInProg = 0; @@ -135,8 +146,8 @@ void fdManager::process (const osiTime &delay) // regIter.reset(); while ( (pReg=regIter()) ) { - if (FD_ISSET(pReg->fd, &pReg->fdSet)) { - FD_CLR(pReg->fd, &pReg->fdSet); + if (FD_ISSET(pReg->getFD(), &this->fdSets[pReg->getType()])) { + FD_CLR(pReg->getFD(), &this->fdSets[pReg->getType()]); regIter.remove(); this->activeList.add(*pReg); pReg->state = fdrActive; @@ -201,10 +212,22 @@ void fdReg::show(unsigned level) { printf ("fdReg at %x\n", (unsigned) this); if (level>1u) { - printf ("\tfd = %d, fdSet at %x, state = %d, onceOnly = %d\n", - this->fd, (unsigned)&this->fdSet, + printf ("\tstate = %d, onceOnly = %d\n", this->state, this->onceOnly); } + this->fdRegId::show(level); +} + +// +// fdRegId::show() +// +void fdRegId::show(unsigned level) +{ + printf ("fdRegId at %x\n", (unsigned) this); + if (level>1u) { + printf ("\tfd = %d, type = %d\n", + this->fd, this->type); + } } diff --git a/src/libCom/fdmgr/fdManager.h b/src/libCom/fdmgr/fdManager.h index a73b1e010..3e41a6383 100644 --- a/src/libCom/fdmgr/fdManager.h +++ b/src/libCom/fdmgr/fdManager.h @@ -32,6 +32,9 @@ * * History * $Log$ + * Revision 1.1 1996/08/13 22:48:21 jhill + * dfMgr =>fdManager + * * */ @@ -39,6 +42,7 @@ #define fdManagerH_included #include +#include #include #ifdef WIN32 @@ -52,14 +56,62 @@ extern "C" { #include -enum fdRegType {fdrRead, fdrWrite, fdrExcp}; +enum fdRegType {fdrRead, fdrWrite, fdrExcp, fdRegTypeNElem}; enum fdRegState {fdrActive, fdrPending, fdrLimbo}; +class fdRegId +{ +public: + fdRegId (const int fdIn, const fdRegType typeIn) : + fd(fdIn), type(typeIn) {} + + int getFD() + { + return this->fd; + } + + fdRegType getType() + { + return this->type; + } + + int operator == (const fdRegId &idIn) + { + return this->fd == idIn.fd && this->type==idIn.type; + } + + resTableIndex resourceHash(unsigned nBitsId) const + { + unsigned src = (unsigned) this->fd; + resTableIndex hashid; + + hashid = src; + src = src >> nBitsId; + while (src) { + hashid = hashid ^ src; + src = src >> nBitsId; + } + hashid = hashid ^ this->type; + + // + // the result here is always masked to the + // proper size after it is returned to the resource class + // + return hashid; + } + + virtual void show(unsigned level); +private: + const int fd; + const fdRegType type; +}; + // // fdReg // file descriptor registration // -class fdReg : public tsDLNode { +class fdReg : public tsDLNode, public fdRegId, + public tsSLNode { friend class fdManager; public: fdReg (const int fdIn, const fdRegType typ, @@ -89,8 +141,6 @@ private: // virtual void destroy (); - const int fd; - fd_set &fdSet; unsigned char state; // fdRegState goes here unsigned char onceOnly; }; @@ -101,12 +151,15 @@ public: fdManager(); ~fdManager(); void process (const osiTime &delay); + + // + // returns NULL if the fd is unknown + // + fdReg *lookUpFD(const int fd, const fdRegType type); private: tsDLList regList; tsDLList activeList; - fd_set read; - fd_set write; - fd_set exception; + fd_set fdSets[fdRegTypeNElem]; int maxFD; unsigned processInProg; // @@ -117,11 +170,22 @@ private: void installReg (fdReg ®); void removeReg (fdReg ®); - fd_set *pFDSet (fdRegType typIn); + resTable fdTbl; }; extern fdManager fileDescriptorManager; +// +// lookUpFD() +// +inline fdReg *fdManager::lookUpFD(const int fd, const fdRegType type) +{ + if (fd<0) { + return NULL; + } + fdRegId id (fd,type); + return this->fdTbl.lookup(id); +} // // fdManagerMaxInt () @@ -136,37 +200,23 @@ inline int fdManagerMaxInt (int a, int b) } } -// -// fdManager::pFDSet() -// -inline fd_set *fdManager::pFDSet (fdRegType typIn) -{ - fd_set *pSet; - - switch (typIn) { - case fdrRead: - pSet = &this->read; - break; - case fdrWrite: - pSet = &this->write; - break; - case fdrExcp: - pSet = &this->exception; - break; - default: - assert(0); - } - return pSet; -} - // // fdManager::installReg() // inline void fdManager::installReg (fdReg ®) { - this->maxFD = fdManagerMaxInt(this->maxFD, reg.fd+1); + int status; + + this->maxFD = fdManagerMaxInt(this->maxFD, reg.getFD()+1); this->regList.add(reg); reg.state = fdrPending; + status = this->fdTbl.add(reg); + if (status) { + fprintf (stderr, + "**** Warning - duplicate fdReg object\n"); + fprintf (stderr, + "**** will not be seen by fdManager::lookUpFD()\n"); + } } // @@ -174,6 +224,8 @@ inline void fdManager::installReg (fdReg ®) // inline void fdManager::removeReg(fdReg ®) { + fdReg *pItemFound; + // // signal fdManager that the fdReg was deleted // during the call back @@ -181,21 +233,22 @@ inline void fdManager::removeReg(fdReg ®) if (this->pCBReg == ®) { this->pCBReg = 0; } - FD_CLR(reg.fd, ®.fdSet); + FD_CLR(reg.getFD(), &this->fdSets[reg.getType()]); + pItemFound = this->fdTbl.remove(reg); + assert (pItemFound==®); switch (reg.state) { case fdrActive: this->activeList.remove(reg); - reg.state = fdrLimbo; break; case fdrPending: this->regList.remove(reg); - reg.state = fdrLimbo; break; case fdrLimbo: break; default: assert(0); } + reg.state = fdrLimbo; } // @@ -203,11 +256,10 @@ inline void fdManager::removeReg(fdReg ®) // inline fdReg::fdReg (const int fdIn, const fdRegType typIn, const unsigned onceOnlyIn) : - fd(fdIn), fdSet(*fileDescriptorManager.pFDSet(typIn)), - state(fdrLimbo), onceOnly(onceOnlyIn) + fdRegId(fdIn,typIn), state(fdrLimbo), onceOnly(onceOnlyIn) { - assert (this->fd>=0); - if (this->fd>FD_SETSIZE) { + assert (fdIn>=0); + if (fdIn>FD_SETSIZE) { fprintf (stderr, "%s: fd > FD_SETSIZE ignored\n", __FILE__); return;