10 #ifndef BIDIRMMAPPIPE_H
11 #define BIDIRMMAPPIPE_H
19 #define BEGIN_NAMESPACE_ROOFIT namespace RooFit {
20 #define END_NAMESPACE_ROOFIT }
22 BEGIN_NAMESPACE_ROOFIT
25 namespace BidirMMapPipe_impl {
27 class BidirMMapPipeException;
56 static unsigned s_physpgsz;
57 static unsigned s_pagesize;
59 static MMapVariety s_mmapworks;
62 typedef BidirMMapPipeException Exception;
68 std::list<void*> m_freelist;
74 static unsigned getPageSize();
77 static void* dommap(
unsigned len);
79 static void domunmap(
void* p,
unsigned len);
81 PageChunk(
const PageChunk&) {}
83 PageChunk& operator=(
const PageChunk&) {
return *
this; }
86 static unsigned pagesize() {
return s_pagesize; }
88 static unsigned physPgSz() {
return s_physpgsz; }
90 static MMapVariety mmapVariety() {
return s_mmapworks; }
93 PageChunk(PagePool* parent,
unsigned length,
unsigned nPgPerGroup);
99 bool contains(
const Pages& p)
const;
105 void push(
const Pages& p);
110 return reinterpret_cast<unsigned char*
>(m_end) -
111 reinterpret_cast<unsigned char*>(m_begin);
114 unsigned nPagesPerGroup()
const {
return m_nPgPerGrp; }
117 bool empty()
const {
return !m_nUsedGrp; }
120 bool full()
const {
return m_freelist.empty(); }
140 unsigned char m_npages;
144 Pages() : m_pimpl(0) { }
154 Pages(
const Pages& other);
161 Pages& operator=(
const Pages& other);
164 static unsigned pagesize();
167 unsigned npages()
const {
return m_pimpl->m_npages; }
170 Page* page(
unsigned pgno)
const;
173 Page* operator[](
unsigned pgno)
const {
return page(pgno); }
176 unsigned pageno(Page* p)
const;
179 unsigned operator[](Page* p)
const {
return pageno(p); }
182 void swap(Pages& other)
184 impl* tmp = other.m_pimpl;
185 other.m_pimpl = m_pimpl;
191 friend class BidirMMapPipe_impl::PageChunk;
197 Pages(PageChunk* parent, Page* pages,
unsigned npg);
379 class BidirMMapPipe {
383 typedef std::size_t size_type;
385 typedef BidirMMapPipe_impl::BidirMMapPipeException Exception;
392 badbit = rderrbit | wrerrbit,
420 BidirMMapPipe(
bool useExceptions =
true,
bool useSocketpair =
false);
432 static int debugflag() {
return s_debugflag; }
438 static void setDebugflag(
int flag) { s_debugflag = flag; }
449 size_type read(
void* addr, size_type sz);
462 size_type write(
const void* addr, size_type sz);
482 size_type bytesReadableNonBlocking();
488 size_type bytesWritableNonBlocking();
503 pid_t pidOtherEnd()
const
504 {
return isChild() ? m_parentPid : m_childPid; }
513 Error = ReadError | WriteError,
516 EndOfFile = ReadEndOfFile | WriteEndOfFile,
519 Invalid = ReadInvalid | WriteInvalid
529 PollEntry(BidirMMapPipe* _pipe) :
530 pipe(_pipe), events(None), revents(None) { }
532 PollEntry(BidirMMapPipe* _pipe,
int _events) :
533 pipe(_pipe), events(_events), revents(None) { }
536 typedef std::vector<PollEntry> PollVector;
676 static int poll(PollVector& pipes,
int timeout);
682 bool isParent()
const {
return m_childPid; }
688 bool isChild()
const {
return !m_childPid; }
694 bool usesSocketpair()
const {
return m_inpipe == m_outpipe; }
700 bool usesPipepair()
const {
return m_inpipe != m_outpipe; }
706 int rdstate()
const {
return m_flags; }
712 bool eof()
const {
return m_flags & eofbit; }
718 bool fail()
const {
return m_flags & failbit; }
724 bool bad()
const {
return m_flags & badbit; }
730 bool good()
const {
return !(m_flags & (eofbit | failbit | badbit)); }
736 bool closed()
const {
return m_flags & failbit; }
744 operator bool()
const {
return !fail() && !bad(); }
750 bool operator!()
const {
return fail() || bad(); }
755 #define STREAMOP(TYPE) \
756 BidirMMapPipe& operator<<(const TYPE& val) \
757 { write(&val, sizeof(TYPE)); return *this; } \
758 BidirMMapPipe& operator>>(TYPE& val) \
759 { read(&val, sizeof(TYPE)); return *this; }
766 STREAMOP(
unsigned char);
767 STREAMOP(
unsigned short);
768 STREAMOP(
unsigned int);
769 STREAMOP(
unsigned long);
770 STREAMOP(
unsigned long long);
780 BidirMMapPipe& operator<<(
const char* str);
791 BidirMMapPipe& operator>>(
char* (&str));
798 BidirMMapPipe& operator<<(
const std::string& str);
805 BidirMMapPipe& operator>>(std::string& str);
815 template<
class T> BidirMMapPipe& operator<<(
const T* tptr)
816 { write(&tptr,
sizeof(tptr));
return *
this; }
826 template<
class T> BidirMMapPipe& operator>>(T* &tptr)
827 { read(&tptr,
sizeof(tptr));
return *
this; }
839 BidirMMapPipe& operator<<(BidirMMapPipe& (*manip)(BidirMMapPipe&))
840 {
return manip(*
this); }
852 BidirMMapPipe& operator>>(BidirMMapPipe& (*manip)(BidirMMapPipe&))
853 {
return manip(*
this); }
856 static BidirMMapPipe& flush(BidirMMapPipe& pipe) { pipe.flush();
return pipe; }
858 static BidirMMapPipe& purge(BidirMMapPipe& pipe) { pipe.purge();
return pipe; }
862 BidirMMapPipe(
const BidirMMapPipe&);
864 BidirMMapPipe& operator=(
const BidirMMapPipe&) {
return *
this; }
867 friend class BidirMMapPipe_impl::Page;
869 typedef BidirMMapPipe_impl::Page Page;
878 PagesPerEnd = TotPages / 2,
882 FlushThresh = (3 * PagesPerEnd) / 4
886 static pthread_mutex_t s_openpipesmutex;
888 static std::list<BidirMMapPipe*> s_openpipes;
890 static BidirMMapPipe_impl::PagePool* s_pagepool;
892 static unsigned s_pagepoolrefcnt;
894 static int s_debugflag;
897 static BidirMMapPipe_impl::PagePool& pagepool();
900 BidirMMapPipe_impl::Pages m_pages;
911 static void teardownall(
void);
914 static unsigned lenPageList(
const Page* list);
924 void feedPageLists(Page* plist);
927 void markPageDirty(Page* p);
930 static size_type xferraw(
int fd,
void* addr, size_type len,
931 ssize_t (*xferfn)(
int,
void*, std::size_t));
933 static size_type xferraw(
int fd,
void* addr,
const size_type len,
934 ssize_t (*xferfn)(
int,
const void*, std::size_t))
936 return xferraw(fd, addr, len,
937 reinterpret_cast<ssize_t (*)(
938 int,
void*, std::size_t)
>(xferfn));
951 void sendpages(Page* plist);
963 unsigned recvpages();
972 unsigned recvpages_nonblock();
980 int doClose(
bool force,
bool holdlock =
false);
982 void doFlush(
bool forcePartialPages =
true);
988 #undef BEGIN_NAMESPACE_ROOFIT
989 #undef END_NAMESPACE_ROOFIT
991 #endif // BIDIRMMAPPIPE_H