got TcpServer and Thread to inherit Mmged

This commit is contained in:
hyung-hwan 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 class QSE_EXPORT Uncopyable
{ {
public: public:
Uncopyable () {} Uncopyable () QSE_CPP_NOEXCEPT {}
//virtual ~Uncopyable () {} //virtual ~Uncopyable () {}
private: private:

View File

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

View File

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

View File

@ -40,15 +40,15 @@ QSE_BEGIN_NAMESPACE(QSE)
class QSE_EXPORT StdMmgr: public Mmgr class QSE_EXPORT StdMmgr: public Mmgr
{ {
public: public:
StdMmgr (): Mmgr () {} StdMmgr () QSE_CPP_NOEXCEPT: Mmgr () {}
void* allocMem (qse_size_t n); void* allocMem (qse_size_t n) QSE_CPP_NOEXCEPT;
void* reallocMem (void* ptr, qse_size_t n); void* reallocMem (void* ptr, qse_size_t n) QSE_CPP_NOEXCEPT;
void freeMem (void* ptr); void freeMem (void* ptr) QSE_CPP_NOEXCEPT;
/// The getInstance() function returns the stock instance of the StdMmgr /// The getInstance() function returns the stock instance of the StdMmgr
/// class. /// 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 class TcpServer: public Uncopyable, public Mmged, public Types
{ {
public: public:
TcpServer () QSE_CPP_NOEXCEPT; TcpServer (Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT;
virtual ~TcpServer () QSE_CPP_NOEXCEPT; virtual ~TcpServer () QSE_CPP_NOEXCEPT;
virtual int start (const qse_char_t* addrs) 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 class TcpServerF: public TcpServer
{ {
public: public:
TcpServerF () QSE_CPP_NOEXCEPT {} TcpServerF (Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: TcpServer(mmgr) {}
TcpServerF (const F& f) QSE_CPP_NOEXCEPT: __lfunc(f) {} TcpServerF (const F& f, Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: TcpServer(mmgr), __lfunc(f) {}
#if defined(QSE_CPP_ENABLE_CPP11_MOVE) #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 #endif
protected: protected:
@ -204,15 +204,15 @@ template <typename RT, typename... ARGS>
class TcpServerL<RT(ARGS...)>: public TcpServer class TcpServerL<RT(ARGS...)>: public TcpServer
{ {
public: public:
TcpServerL () QSE_CPP_NOEXCEPT: __lfunc(nullptr) {} TcpServerL (Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: Mmgr(mmgr), __lfunc(nullptr) {}
template <typename T> 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 try
{ {
// TODO: are there any ways to achieve this without memory allocation? // 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 (...) catch (...)
{ {
@ -224,7 +224,11 @@ public:
~TcpServerL () QSE_CPP_NOEXCEPT ~TcpServerL () QSE_CPP_NOEXCEPT
{ {
if (this->__lfunc) delete this->__lfunc; if (this->__lfunc)
{
//delete this->__lfunc;
this->getMmgr()->dispose (this->__lfunc);
}
} }
template <typename T> template <typename T>
@ -235,14 +239,19 @@ public:
try try
{ {
// TODO: are there any ways to achieve this without memory allocation? // 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 (...) catch (...)
{ {
return -1; return -1;
} }
if (this->__lfunc) delete this->__lfunc; if (this->__lfunc)
{
//delete this->__lfunc;
this->getMmgr()->dispose (this->__lfunc);
}
this->__lfunc = lf; this->__lfunc = lf;
return 0; return 0;
} }

View File

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

View File

@ -36,22 +36,22 @@ struct xma_xtn_t
HeapMmgr* heap; HeapMmgr* heap;
}; };
HeapMmgr::HeapMmgr (qse_size_t heap_size): HeapMmgr::HeapMmgr (qse_size_t heap_size) QSE_CPP_NOEXCEPT:
Mmgr(), Mmged(QSE_NULL), xma(QSE_NULL), heap_size (heap_size) Mmgr(), Mmged(QSE_NULL), xma(QSE_NULL), heap_size (heap_size)
{ {
} }
HeapMmgr::HeapMmgr (Mmgr* mmgr, qse_size_t heap_size): HeapMmgr::HeapMmgr (Mmgr* mmgr, qse_size_t heap_size) QSE_CPP_NOEXCEPT:
Mmgr(), Mmged(mmgr), xma(QSE_NULL), heap_size (heap_size) Mmgr(), Mmged(mmgr), xma(QSE_NULL), heap_size (heap_size)
{ {
} }
HeapMmgr::~HeapMmgr () HeapMmgr::~HeapMmgr () QSE_CPP_NOEXCEPT
{ {
if (this->xma) qse_xma_close (this->xma); if (this->xma) qse_xma_close (this->xma);
} }
void* HeapMmgr::allocMem (qse_size_t n) void* HeapMmgr::allocMem (qse_size_t n) QSE_CPP_NOEXCEPT
{ {
if (!this->xma) if (!this->xma)
{ {
@ -67,7 +67,7 @@ void* HeapMmgr::allocMem (qse_size_t n)
return xptr; return xptr;
} }
void* HeapMmgr::reallocMem (void* ptr, qse_size_t n) void* HeapMmgr::reallocMem (void* ptr, qse_size_t n) QSE_CPP_NOEXCEPT
{ {
if (!this->xma) if (!this->xma)
{ {
@ -83,7 +83,7 @@ void* HeapMmgr::reallocMem (void* ptr, qse_size_t n)
return xptr; return xptr;
} }
void HeapMmgr::freeMem (void* ptr) void HeapMmgr::freeMem (void* ptr) QSE_CPP_NOEXCEPT
{ {
if (this->xma) qse_xma_free (this->xma, ptr); if (this->xma) qse_xma_free (this->xma, ptr);
} }

View File

@ -32,17 +32,17 @@
QSE_BEGIN_NAMESPACE(QSE) QSE_BEGIN_NAMESPACE(QSE)
///////////////////////////////// /////////////////////////////////
void* Mmgr::alloc_mem (mmgr_t* mmgr, qse_size_t n) void* Mmgr::alloc_mem (mmgr_t* mmgr, qse_size_t n) QSE_CPP_NOEXCEPT
{ {
return ((Mmgr*)mmgr->ctx)->allocMem (n); return ((Mmgr*)mmgr->ctx)->allocMem (n);
} }
void* Mmgr::realloc_mem (mmgr_t* mmgr, void* ptr, qse_size_t n) void* Mmgr::realloc_mem (mmgr_t* mmgr, void* ptr, qse_size_t n) QSE_CPP_NOEXCEPT
{ {
return ((Mmgr*)mmgr->ctx)->reallocMem (ptr, n); return ((Mmgr*)mmgr->ctx)->reallocMem (ptr, n);
} }
void Mmgr::free_mem (mmgr_t* mmgr, void* ptr) void Mmgr::free_mem (mmgr_t* mmgr, void* ptr) QSE_CPP_NOEXCEPT
{ {
((Mmgr*)mmgr->ctx)->freeMem (ptr); ((Mmgr*)mmgr->ctx)->freeMem (ptr);
} }
@ -56,12 +56,12 @@ void* Mmgr::callocate (qse_size_t n, bool raise_exception)
Mmgr* Mmgr::dfl_mmgr = StdMmgr::getInstance(); Mmgr* Mmgr::dfl_mmgr = StdMmgr::getInstance();
Mmgr* Mmgr::getDFL () Mmgr* Mmgr::getDFL () QSE_CPP_NOEXCEPT
{ {
return Mmgr::dfl_mmgr; return Mmgr::dfl_mmgr;
} }
void Mmgr::setDFL (Mmgr* mmgr) void Mmgr::setDFL (Mmgr* mmgr) QSE_CPP_NOEXCEPT
{ {
Mmgr::dfl_mmgr = mmgr; Mmgr::dfl_mmgr = mmgr;
} }

View File

@ -32,22 +32,22 @@ QSE_BEGIN_NAMESPACE(QSE)
///////////////////////////////// /////////////////////////////////
void* StdMmgr::allocMem (qse_size_t n) void* StdMmgr::allocMem (qse_size_t n) QSE_CPP_NOEXCEPT
{ {
return ::malloc (n); return ::malloc (n);
} }
void* StdMmgr::reallocMem (void* ptr, qse_size_t n) void* StdMmgr::reallocMem (void* ptr, qse_size_t n) QSE_CPP_NOEXCEPT
{ {
return ::realloc (ptr, n); return ::realloc (ptr, n);
} }
void StdMmgr::freeMem (void* ptr) void StdMmgr::freeMem (void* ptr) QSE_CPP_NOEXCEPT
{ {
::free (ptr); ::free (ptr);
} }
StdMmgr* StdMmgr::getInstance () StdMmgr* StdMmgr::getInstance () QSE_CPP_NOEXCEPT
{ {
static StdMmgr DFL; static StdMmgr DFL;
return &DFL; return &DFL;

View File

@ -86,7 +86,8 @@ int TcpServer::Client::stop () QSE_CPP_NOEXCEPT
return 0; return 0;
} }
TcpServer::TcpServer () QSE_CPP_NOEXCEPT: TcpServer::TcpServer (Mmgr* mmgr) QSE_CPP_NOEXCEPT:
Mmged(mmgr),
errcode(E_ENOERR), errcode(E_ENOERR),
stop_requested(false), stop_requested(false),
server_serving(false), server_serving(false),
@ -116,7 +117,7 @@ void TcpServer::free_all_listeners () QSE_CPP_NOEXCEPT
qse_mux_delete (this->listener_list.mux, &evt); qse_mux_delete (this->listener_list.mux, &evt);
lp->close (); lp->close ();
delete lp; this->getMmgr()->dispose(lp); //delete lp;
} }
if (this->listener_list.mux_pipe[0] >= 0) if (this->listener_list.mux_pipe[0] >= 0)
@ -175,9 +176,11 @@ void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QS
if (server->max_connections > 0 && server->max_connections <= server->client_list.getSize()) if (server->max_connections > 0 && server->max_connections <= server->client_list.getSize())
{ {
// too many connections. accept the connection and close it. // too many connections. accept the connection and close it.
Socket s; Socket s;
SocketAddress sa; SocketAddress sa;
if (lsck->accept(&s, &sa, Socket::T_CLOEXEC) >= 0) s.close(); if (lsck->accept(&s, &sa, Socket::T_CLOEXEC) >= 0) s.close();
// TODO: logging.
return; return;
} }
@ -186,13 +189,15 @@ void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QS
// allocating the client object before accept is // allocating the client object before accept is
// a bit awkward. but socket.accept() can be passed // a bit awkward. but socket.accept() can be passed
// the socket field inside the client object. // the socket field inside the client object.
try { client = new Client (lsck); } try { client = new(server->getMmgr()) Client (lsck); }
catch (...) catch (...)
{ {
// memory alloc failed. accept the connection and close it. // memory alloc failed. accept the connection and close it.
Socket s; Socket s;
SocketAddress sa; SocketAddress sa;
if (lsck->accept(&s, &sa, Socket::T_CLOEXEC) >= 0) s.close(); if (lsck->accept(&s, &sa, Socket::T_CLOEXEC) >= 0) s.close();
// TODO: logging.
return; return;
} }
@ -215,7 +220,7 @@ void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QS
if (client->start(0) <= -1) if (client->start(0) <= -1)
#endif #endif
{ {
delete client; server->getMmgr()->dispose (client); //delete client;
return; return;
} }
@ -223,7 +228,7 @@ void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QS
catch (...) catch (...)
{ {
// TODO: logging. // TODO: logging.
delete client; server->getMmgr()->dispose (client); //delete client;
return; return;
} }
@ -296,7 +301,7 @@ int TcpServer::setup_listeners (const qse_char_t* addrs) QSE_CPP_NOEXCEPT
try try
{ {
lsck = new Listener(this); lsck = new(this->getMmgr()) Listener(this);
} }
catch (...) catch (...)
{ {
@ -393,12 +398,12 @@ int TcpServer::start (const qse_char_t* addrs) QSE_CPP_NOEXCEPT
} }
this->delete_all_clients (); this->delete_all_clients ();
if (client != QSE_NULL) delete client; if (client != QSE_NULL) this->getMmgr()->dispose (client); // delete client;
} }
catch (...) catch (...)
{ {
this->delete_all_clients (); this->delete_all_clients ();
if (client != QSE_NULL) delete client; if (client != QSE_NULL) this->getMmgr()->dispose (client); //delete client;
this->setErrorCode (E_EEXCEPT); this->setErrorCode (E_EEXCEPT);
this->server_serving = false; this->server_serving = false;
@ -446,7 +451,7 @@ void TcpServer::delete_dead_clients () QSE_CPP_NOEXCEPT
p->join (); p->join ();
#endif #endif
delete p; this->getMmgr()->dispose (p); //delete p;
np2 = np; np = np->getNextNode(); np2 = np; np = np->getNextNode();
this->client_list.remove (np2); this->client_list.remove (np2);
continue; continue;
@ -478,7 +483,7 @@ void TcpServer::delete_all_clients () QSE_CPP_NOEXCEPT
#else #else
p->join (); p->join ();
#endif #endif
delete p; this->getMmgr()->dispose (p); //delete p;
np2 = np; np = np->getNextNode(); np2 = np; np = np->getNextNode();
this->client_list.remove (np2); this->client_list.remove (np2);
} }

View File

@ -32,21 +32,20 @@ QSE_BEGIN_NAMESPACE(QSE)
Thread::Handle Thread::INVALID_HANDLE = QSE_THR_HND_INVALID; Thread::Handle Thread::INVALID_HANDLE = QSE_THR_HND_INVALID;
Thread::Thread() QSE_CPP_NOEXCEPT : __exctx(QSE_NULL) Thread::Thread(Mmgr* mmgr) QSE_CPP_NOEXCEPT: Mmged(mmgr), __exctx(QSE_NULL)
{ {
//qse_thr_init (this, this->getMmgr()); qse_thr_init (&this->thr, this->getMmgr());
qse_thr_init (this, QSE_NULL);
} }
Thread::~Thread () QSE_CPP_NOEXCEPT Thread::~Thread () QSE_CPP_NOEXCEPT
{ {
QSE_ASSERT (this->__state != QSE_THR_RUNNING); QSE_ASSERT (this->thr.__state != QSE_THR_RUNNING);
// it is subclasses' responsibility to stop the thread gracefully. // it is subclasses' responsibility to stop the thread gracefully.
// so stop is not called here. // so stop is not called here.
// this->stop (); // this->stop ();
/*if (this->__joinable)*/ this->join (); /*if (this->thr.__joinable)*/ this->join ();
qse_thr_fini (this); qse_thr_fini (&this->thr);
} }
@ -58,7 +57,7 @@ static int thr_func_call_main (qse_thr_t* thr, void* ctx)
int Thread::start (int flags) QSE_CPP_NOEXCEPT int Thread::start (int flags) QSE_CPP_NOEXCEPT
{ {
return qse_thr_start(this, thr_func_call_main, this, flags); return qse_thr_start(&this->thr, thr_func_call_main, this, flags);
} }
int Thread::stop () QSE_CPP_NOEXCEPT int Thread::stop () QSE_CPP_NOEXCEPT
@ -67,7 +66,7 @@ int Thread::stop () QSE_CPP_NOEXCEPT
// make sure that subclasses override "stop" and call it // make sure that subclasses override "stop" and call it
// properly so that the thread can be terminated gracefully. // properly so that the thread can be terminated gracefully.
// "stop" here just aborts the running thread. // "stop" here just aborts the running thread.
return qse_thr_stop(this); return qse_thr_stop(&this->thr);
} }
@ -82,7 +81,7 @@ int ThreadR::thr_func_call_rtn (qse_thr_t* thr, void* ctx)
int ThreadR::start (ThreadRoutine rtn, int flags) QSE_CPP_NOEXCEPT int ThreadR::start (ThreadRoutine rtn, int flags) QSE_CPP_NOEXCEPT
{ {
if (this->__state == QSE_THR_RUNNING) return -1; if (this->thr.__state == QSE_THR_RUNNING) return -1;
// this != (qse_thr_t*)this may not be equal if this class // this != (qse_thr_t*)this may not be equal if this class
// has some internal added data fields. e.g. it contains // has some internal added data fields. e.g. it contains
@ -92,7 +91,7 @@ int ThreadR::start (ThreadRoutine rtn, int flags) QSE_CPP_NOEXCEPT
// qse_thr_start (this, (qse_thr_rtn_t)rtn, QSE_NULL, flags); // qse_thr_start (this, (qse_thr_rtn_t)rtn, QSE_NULL, flags);
// so i pass a void pointer 'this' as the third argument. // so i pass a void pointer 'this' as the third argument.
this->__tmprtn = rtn; this->__tmprtn = rtn;
return qse_thr_start(this, thr_func_call_rtn, this, flags); return qse_thr_start(&this->thr, thr_func_call_rtn, this, flags);
} }
QSE_END_NAMESPACE(QSE) QSE_END_NAMESPACE(QSE)

View File

@ -3,7 +3,7 @@
#include <qse/si/sio.h> #include <qse/si/sio.h>
#include <qse/si/os.h> #include <qse/si/os.h>
#include <qse/cmn/mem.h> #include <qse/cmn/mem.h>
#include <qse/cmn/HeapMmgr.hpp>
#include <locale.h> #include <locale.h>
#if defined(_WIN32) #if defined(_WIN32)
@ -51,6 +51,8 @@ static QSE::TcpServerF<ClientHandler>* g_server;
static int test1 (void) static int test1 (void)
{ {
QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 30000);
#if defined(QSE_LANG_CPP11) #if defined(QSE_LANG_CPP11)
QSE::TcpServerL<int(QSE::Socket*,QSE::SocketAddress*)> server ( QSE::TcpServerL<int(QSE::Socket*,QSE::SocketAddress*)> server (
@ -79,7 +81,7 @@ static int test1 (void)
); );
#else #else
QSE::TcpServerF<ClientHandler> server; QSE::TcpServerF<ClientHandler> server (&heap_mmgr);
#endif #endif
server.setThreadStackSize (256000); server.setThreadStackSize (256000);

View File

@ -2,6 +2,7 @@
#include <qse/si/mtx.h> #include <qse/si/mtx.h>
#include <qse/si/sio.h> #include <qse/si/sio.h>
#include <qse/cmn/mem.h> #include <qse/cmn/mem.h>
#include <qse/cmn/HeapMmgr.hpp>
#include <locale.h> #include <locale.h>
#if defined(_WIN32) #if defined(_WIN32)
@ -15,6 +16,8 @@
static int g_stopreq = 0; static int g_stopreq = 0;
static qse_mtx_t* g_prmtx = QSE_NULL; static qse_mtx_t* g_prmtx = QSE_NULL;
QSE::HeapMmgr g_heap_mmgr (QSE::Mmgr::getDFL(), 30000);
class MyThread: public QSE::Thread class MyThread: public QSE::Thread
{ {
public: public:
@ -126,7 +129,7 @@ static int test1 (void)
return -1; return -1;
} }
QSE::ThreadR thr2; QSE::ThreadR thr2 (&g_heap_mmgr);
thr2.setStackSize (64000); thr2.setStackSize (64000);
thr2.setContext (&localstopreq); thr2.setContext (&localstopreq);
#if defined(QSE_LANG_CPP11) #if defined(QSE_LANG_CPP11)