got TcpServer and Thread to inherit Mmged

This commit is contained in:
2018-07-01 02:11:33 +00:00
parent 1d12ec3e3f
commit 27039d9693
13 changed files with 118 additions and 98 deletions

View File

@ -42,7 +42,7 @@ QSE_BEGIN_NAMESPACE(QSE)
class QSE_EXPORT Uncopyable
{
public:
Uncopyable () {}
Uncopyable () QSE_CPP_NOEXCEPT {}
//virtual ~Uncopyable () {}
private:

View File

@ -49,23 +49,23 @@ QSE_BEGIN_NAMESPACE(QSE)
class QSE_EXPORT HeapMmgr: public Mmgr, public Mmged
{
public:
HeapMmgr (qse_size_t heap_size);
HeapMmgr (qse_size_t heap_size) QSE_CPP_NOEXCEPT;
/// The constructor function accepts an memory manager \a mmgr that
/// is used to create a heap of the size \a heap_size.
HeapMmgr (Mmgr* mmgr, qse_size_t heap_size);
HeapMmgr (Mmgr* mmgr, qse_size_t heap_size) QSE_CPP_NOEXCEPT;
/// The destructor function frees the heap. Memory areas returned by
/// allocate(), reallocate(), allocMem(), reallocMem() are invalidated
/// all at once.
~HeapMmgr ();
~HeapMmgr () QSE_CPP_NOEXCEPT;
void* allocMem (qse_size_t n);
void* reallocMem (void* ptr, qse_size_t n);
void freeMem (void* ptr);
void* allocMem (qse_size_t n) QSE_CPP_NOEXCEPT;
void* reallocMem (void* ptr, qse_size_t n) QSE_CPP_NOEXCEPT;
void freeMem (void* ptr) QSE_CPP_NOEXCEPT;
// the library does not provide a stock instance of this class
//static HeapMmgr* getInstance ();
//static HeapMmgr* getInstance () QSE_CPP_NOEXCEPT;
protected:
qse_xma_t* xma;

View File

@ -62,7 +62,7 @@ public:
/// The Mmgr() function builds a memory manager composed of bridge
/// functions connecting itself with it.
///
Mmgr ()
Mmgr () QSE_CPP_NOEXCEPT
{
// NOTE:
// the #qse_mmgr_t interface is not affected by raise_exception
@ -77,7 +77,7 @@ public:
///
/// The ~Mmgr() function finalizes a memory manager.
///
virtual ~Mmgr () {}
virtual ~Mmgr () QSE_CPP_NOEXCEPT {}
///
/// The allocate() function calls allocMem() for memory
@ -125,7 +125,7 @@ public:
///
virtual void* allocMem (
qse_size_t n ///< size of memory chunk to allocate in bytes
) = 0;
) QSE_CPP_NOEXCEPT = 0;
///
/// The reallocMem() function resizes a chunk of memory previously
@ -136,7 +136,7 @@ public:
virtual void* reallocMem (
void* ptr, ///< pointer to memory chunk to resize
qse_size_t n ///< new size in bytes
) = 0;
) QSE_CPP_NOEXCEPT = 0;
///
/// The freeMem() function frees a chunk of memory allocated with
@ -144,27 +144,27 @@ public:
///
virtual void freeMem (
void* ptr ///< pointer to memory chunk to free
) = 0;
) QSE_CPP_NOEXCEPT = 0;
protected:
///
/// bridge function from the #qse_mmgr_t type the allocMem() function.
///
static void* alloc_mem (mmgr_t* mmgr, qse_size_t n);
static void* alloc_mem (mmgr_t* mmgr, qse_size_t n) QSE_CPP_NOEXCEPT;
///
/// bridge function from the #qse_mmgr_t type the reallocMem() function.
///
static void* realloc_mem (mmgr_t* mmgr, void* ptr, qse_size_t n);
static void* realloc_mem (mmgr_t* mmgr, void* ptr, qse_size_t n) QSE_CPP_NOEXCEPT;
///
/// bridge function from the #qse_mmgr_t type the freeMem() function.
///
static void free_mem (mmgr_t* mmgr, void* ptr);
static void free_mem (mmgr_t* mmgr, void* ptr) QSE_CPP_NOEXCEPT;
public:
static Mmgr* getDFL ();
static void setDFL (Mmgr* mmgr);
static Mmgr* getDFL () QSE_CPP_NOEXCEPT;
static void setDFL (Mmgr* mmgr) QSE_CPP_NOEXCEPT;
protected:
static Mmgr* dfl_mmgr;

View File

@ -40,15 +40,15 @@ QSE_BEGIN_NAMESPACE(QSE)
class QSE_EXPORT StdMmgr: public Mmgr
{
public:
StdMmgr (): Mmgr () {}
StdMmgr () QSE_CPP_NOEXCEPT: Mmgr () {}
void* allocMem (qse_size_t n);
void* reallocMem (void* ptr, qse_size_t n);
void freeMem (void* ptr);
void* allocMem (qse_size_t n) QSE_CPP_NOEXCEPT;
void* reallocMem (void* ptr, qse_size_t n) QSE_CPP_NOEXCEPT;
void freeMem (void* ptr) QSE_CPP_NOEXCEPT;
/// The getInstance() function returns the stock instance of the StdMmgr
/// class.
static StdMmgr* getInstance ();
static StdMmgr* getInstance () QSE_CPP_NOEXCEPT;
};
/////////////////////////////////

View File

@ -44,7 +44,7 @@ QSE_BEGIN_NAMESPACE(QSE)
class TcpServer: public Uncopyable, public Mmged, public Types
{
public:
TcpServer () QSE_CPP_NOEXCEPT;
TcpServer (Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT;
virtual ~TcpServer () QSE_CPP_NOEXCEPT;
virtual int start (const qse_char_t* addrs) QSE_CPP_NOEXCEPT;
@ -179,10 +179,10 @@ template <typename F>
class TcpServerF: public TcpServer
{
public:
TcpServerF () QSE_CPP_NOEXCEPT {}
TcpServerF (const F& f) QSE_CPP_NOEXCEPT: __lfunc(f) {}
TcpServerF (Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: TcpServer(mmgr) {}
TcpServerF (const F& f, Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: TcpServer(mmgr), __lfunc(f) {}
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
TcpServerF (F&& f) QSE_CPP_NOEXCEPT: __lfunc(QSE_CPP_RVREF(f)) {}
TcpServerF (F&& f, Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: TcpServer(mmgr), __lfunc(QSE_CPP_RVREF(f)) {}
#endif
protected:
@ -204,15 +204,15 @@ template <typename RT, typename... ARGS>
class TcpServerL<RT(ARGS...)>: public TcpServer
{
public:
TcpServerL () QSE_CPP_NOEXCEPT: __lfunc(nullptr) {}
TcpServerL (Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: Mmgr(mmgr), __lfunc(nullptr) {}
template <typename T>
TcpServerL (T&& f) QSE_CPP_NOEXCEPT: __lfunc(nullptr)
TcpServerL (T&& f, Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: Mmgr(mmgr), __lfunc(nullptr)
{
try
{
// TODO: are there any ways to achieve this without memory allocation?
this->__lfunc = new TCallable<T> (QSE_CPP_RVREF(f));
this->__lfunc = new(this->getMmgr()) TCallable<T> (QSE_CPP_RVREF(f));
}
catch (...)
{
@ -224,7 +224,11 @@ public:
~TcpServerL () QSE_CPP_NOEXCEPT
{
if (this->__lfunc) delete this->__lfunc;
if (this->__lfunc)
{
//delete this->__lfunc;
this->getMmgr()->dispose (this->__lfunc);
}
}
template <typename T>
@ -235,14 +239,19 @@ public:
try
{
// TODO: are there any ways to achieve this without memory allocation?
lf = new TCallable<T> (QSE_CPP_RVREF(f));
//lf = new TCallable<T> (QSE_CPP_RVREF(f));
lf = new(this->getMmgr()) TCallable<T> (QSE_CPP_RVREF(f));
}
catch (...)
{
return -1;
}
if (this->__lfunc) delete this->__lfunc;
if (this->__lfunc)
{
//delete this->__lfunc;
this->getMmgr()->dispose (this->__lfunc);
}
this->__lfunc = lf;
return 0;
}

View File

@ -29,13 +29,14 @@
#include <qse/si/thr.h>
#include <qse/Uncopyable.hpp>
#include <qse/cmn/Mmged.hpp>
QSE_BEGIN_NAMESPACE(QSE)
class Thread: protected qse_thr_t, public Uncopyable
class Thread: public Uncopyable, public Mmged
{
public:
// native thread hadnle type
// native thread handle type
typedef qse_thr_hnd_t Handle;
enum State
@ -52,17 +53,17 @@ public:
SIGNALS_BLOCKED = QSE_THR_SIGNALS_BLOCKED
};
static Handle getCurrentHandle () QSE_CPP_NOEXCEPT { return qse_get_thr_hnd (); }
static Handle getCurrentHandle () QSE_CPP_NOEXCEPT { return qse_get_thr_hnd(); }
Thread () QSE_CPP_NOEXCEPT;
Thread (Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT;
virtual ~Thread () QSE_CPP_NOEXCEPT;
Handle getHandle () const QSE_CPP_NOEXCEPT { return this->__handle; }
int getState () const QSE_CPP_NOEXCEPT { return this->__state; }
int getReturnCode () const QSE_CPP_NOEXCEPT { return this->__return_code; }
Handle getHandle () const QSE_CPP_NOEXCEPT { return this->thr.__handle; }
int getState () const QSE_CPP_NOEXCEPT { return this->thr.__state; }
int getReturnCode () const QSE_CPP_NOEXCEPT { return this->thr.__return_code; }
qse_size_t getStackSize () const QSE_CPP_NOEXCEPT { return this->__stacksize; }
void setStackSize (qse_size_t num) QSE_CPP_NOEXCEPT { qse_thr_setstacksize(this, num); }
qse_size_t getStackSize () const QSE_CPP_NOEXCEPT { return this->thr.__stacksize; }
void setStackSize (qse_size_t num) QSE_CPP_NOEXCEPT { qse_thr_setstacksize(&this->thr, num); }
// execute the main method defined in this class in a thread.
virtual int start (int flags = 0) QSE_CPP_NOEXCEPT;
@ -78,16 +79,17 @@ public:
// change the context pointer value
void setContext (void* ctx) QSE_CPP_NOEXCEPT { this->__exctx = ctx; }
int join () QSE_CPP_NOEXCEPT { return qse_thr_join(this); }
int detach () QSE_CPP_NOEXCEPT { return qse_thr_detach(this); }
int join () QSE_CPP_NOEXCEPT { return qse_thr_join(&this->thr); }
int detach () QSE_CPP_NOEXCEPT { return qse_thr_detach(&this->thr); }
int kill (int sig) QSE_CPP_NOEXCEPT { return qse_thr_kill(this, sig); }
int blockSignal (int sig) QSE_CPP_NOEXCEPT { return qse_thr_blocksig(this, sig); }
int unblockSignal (int sig) QSE_CPP_NOEXCEPT { return qse_thr_unblocksig(this, sig); }
int blockAllSignals () QSE_CPP_NOEXCEPT { return qse_thr_blockallsigs (this); }
int unblockAllSignals () QSE_CPP_NOEXCEPT { return qse_thr_unblockallsigs (this); }
int kill (int sig) QSE_CPP_NOEXCEPT { return qse_thr_kill(&this->thr, sig); }
int blockSignal (int sig) QSE_CPP_NOEXCEPT { return qse_thr_blocksig(&this->thr, sig); }
int unblockSignal (int sig) QSE_CPP_NOEXCEPT { return qse_thr_unblocksig(&this->thr, sig); }
int blockAllSignals () QSE_CPP_NOEXCEPT { return qse_thr_blockallsigs(&this->thr); }
int unblockAllSignals () QSE_CPP_NOEXCEPT { return qse_thr_unblockallsigs(&this->thr); }
protected:
qse_thr_t thr;
void* __exctx;
static Handle INVALID_HANDLE;
};
@ -95,6 +97,7 @@ protected:
class ThreadR: public Thread
{
public:
ThreadR (Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: Thread(mmgr) {}
typedef int (*ThreadRoutine) (Thread* thr);
// execute the given function in a thread.
@ -109,10 +112,10 @@ template <typename F>
class ThreadF: public Thread
{
public:
ThreadF () QSE_CPP_NOEXCEPT {}
ThreadF (const F& f) QSE_CPP_NOEXCEPT: __lfunc(f) {}
ThreadF (Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: Thread(mmgr) {}
ThreadF (const F& f, Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: Thread(mmgr), __lfunc(f) {}
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
ThreadF (F&& f) QSE_CPP_NOEXCEPT: __lfunc(QSE_CPP_RVREF(f)) {}
ThreadF (F&& f, Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: Thread(mmgr), __lfunc(QSE_CPP_RVREF(f)) {}
#endif
static int call_func (qse_thr_t* thr, void* ctx)
@ -123,7 +126,7 @@ public:
int start (int flags = 0) QSE_CPP_NOEXCEPT
{
return qse_thr_start (this, (qse_thr_rtn_t)ThreadF<F>::call_func, this, flags);
return qse_thr_start (&this->thr, (qse_thr_rtn_t)ThreadF<F>::call_func, this, flags);
}
protected:
@ -132,7 +135,6 @@ protected:
#if defined(QSE_LANG_CPP11)
#if 0
// i don't want to use std::function.
class ThreadF: public Thread
@ -148,7 +150,7 @@ public:
int start (X&& f, int flags) QSE_CPP_NOEXCEPT
{
this->__lfunc = QSE_CPP_RVREF(f);
return qse_thr_start (this, (qse_thr_rtn_t)ThreadF::call_func, this, flags);
return qse_thr_start(&this->thr, (qse_thr_rtn_t)ThreadF::call_func, this, flags);
}
protected:
@ -164,10 +166,10 @@ template <typename RT, typename... ARGS>
class ThreadL<RT(ARGS...)>: public Thread
{
public:
ThreadL () QSE_CPP_NOEXCEPT: __lfunc(nullptr) {}
ThreadL (Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: Thread(mmgr), __lfunc(nullptr) {}
~ThreadL () QSE_CPP_NOEXCEPT
{
if (this->__lfunc) delete this->__lfunc;
if (this->__lfunc) this->getMmgr()->dispose(this->__lfunc); //delete this->__lfunc;
}
static int call_func (qse_thr_t* thr, void* ctx)
@ -181,18 +183,18 @@ public:
//int start (T f, int flags) QSE_CPP_NOEXCEPT
{
if (this->__state == QSE_THR_RUNNING) return -1;
if (this->__lfunc) delete this->__lfunc;
if (this->__lfunc) this->getMmgr()->dispose (this->__lfunc); //delete this->__lfunc;
try
{
// TODO: are there any ways to achieve this without memory allocation?
this->__lfunc = new TCallable<T> (QSE_CPP_RVREF(f));
this->__lfunc = new(this->getMmgr()) TCallable<T> (QSE_CPP_RVREF(f));
}
catch (...)
{
this->__lfunc = nullptr;
return -1;
}
return qse_thr_start (this, (qse_thr_rtn_t)ThreadL::call_func, this, flags);
return qse_thr_start(&this->thr, (qse_thr_rtn_t)ThreadL::call_func, this, flags);
}
protected: