From c1077faa281c5635f85b892e605e23bd6c8fcc3b Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 13 Dec 2012 06:12:56 -0500 Subject: Optimize much of the database code and serialize code. --- include/access.h | 2 +- include/account.h | 4 ++-- include/anope.h | 11 ++++++++++ include/base.h | 2 +- include/bots.h | 2 +- include/extensible.h | 46 +++++++++++++++++++++++++--------------- include/memo.h | 2 +- include/module.h | 1 - include/regchannel.h | 10 ++++----- include/serialize.h | 57 ++++++++++++++++++-------------------------------- include/signals.h | 48 ------------------------------------------ include/socketengine.h | 2 +- include/sockets.h | 33 ++++++++++++++++++++++------- include/xline.h | 2 +- 14 files changed, 98 insertions(+), 124 deletions(-) delete mode 100644 include/signals.h (limited to 'include') diff --git a/include/access.h b/include/access.h index 918dd7be6..f011ed438 100644 --- a/include/access.h +++ b/include/access.h @@ -87,7 +87,7 @@ class CoreExport ChanAccess : public Serializable ChanAccess(AccessProvider *p); virtual ~ChanAccess(); - Serialize::Data Serialize() const anope_override; + void Serialize(Serialize::Data &data) const anope_override; static Serializable* Unserialize(Serializable *obj, Serialize::Data &); /** Check if this access entry matches the given user or account diff --git a/include/account.h b/include/account.h index a45bf8c90..da855480c 100644 --- a/include/account.h +++ b/include/account.h @@ -122,7 +122,7 @@ class CoreExport NickAlias : public Serializable, public Extensible, public Flag NickAlias(const Anope::string &nickname, NickCore *nickcore); ~NickAlias(); - Serialize::Data Serialize() const anope_override; + void Serialize(Serialize::Data &data) const anope_override; static Serializable* Unserialize(Serializable *obj, Serialize::Data &); /** Release a nick @@ -227,7 +227,7 @@ class CoreExport NickCore : public Serializable, public Extensible, public Flags NickCore(const Anope::string &nickdisplay); ~NickCore(); - Serialize::Data Serialize() const anope_override; + void Serialize(Serialize::Data &data) const anope_override; static Serializable* Unserialize(Serializable *obj, Serialize::Data &); /** Changes the display for this account diff --git a/include/anope.h b/include/anope.h index 30ca48e69..c241524ab 100644 --- a/include/anope.h +++ b/include/anope.h @@ -13,6 +13,8 @@ #ifndef ANOPE_H #define ANOPE_H +#include + #include "hashcomp.h" namespace Anope @@ -282,9 +284,12 @@ namespace Anope * Stream insertion operator, must be friend because they cannot be inside the class. */ friend std::ostream &operator<<(std::ostream &os, const string &_str); + friend std::istream &operator>>(std::istream &is, string &_str); }; inline std::ostream &operator<<(std::ostream &os, const string &_str) { return os << _str._string; } + /* This is not standard to make operator>> behave like operator<< in that it will allow extracting a whole line, not just one word */ + inline std::istream &operator>>(std::istream &is, string &_str) { return std::getline(is, _str._string); } inline const string operator+(char chr, const string &str) { string tmp(chr); tmp += str; return tmp; } inline const string operator+(const char *_str, const string &str) { string tmp(_str); tmp += str; return tmp; } @@ -318,6 +323,8 @@ namespace Anope /** The value to return from main() */ extern int ReturnValue; + + extern sig_atomic_t Signal; extern bool Quitting; extern bool Restarting; extern Anope::string QuitReason; @@ -376,6 +383,10 @@ namespace Anope */ extern void Fork(); + /** Does something with the signal in Anope::Signal + */ + extern void HandleSignal(); + /** One of the first functions called, does general initialization such as reading * command line args, loading the configuration, doing the initial fork() if necessary, * initializating language support, loading modules, and loading databases. diff --git a/include/base.h b/include/base.h index 178bb1937..6fb974ec4 100644 --- a/include/base.h +++ b/include/base.h @@ -17,7 +17,7 @@ class CoreExport Base { /* References to this base class */ - std::set references; + std::set *references; public: Base(); virtual ~Base(); diff --git a/include/bots.h b/include/bots.h index 9b613715b..01c752124 100644 --- a/include/bots.h +++ b/include/bots.h @@ -63,7 +63,7 @@ class CoreExport BotInfo : public User, public Flags, public Serializab */ virtual ~BotInfo(); - Serialize::Data Serialize() const; + void Serialize(Serialize::Data &data) const; static Serializable* Unserialize(Serializable *obj, Serialize::Data &); void GenerateUID(); diff --git a/include/extensible.h b/include/extensible.h index bd7c8c76d..c1de409ed 100644 --- a/include/extensible.h +++ b/include/extensible.h @@ -38,22 +38,25 @@ class CoreExport Extensible { private: typedef std::map extensible_map; - extensible_map extension_items; + extensible_map *extension_items; public: - /** Default constructor, does nothing + /** Default constructor */ - Extensible() { } + Extensible() : extension_items(NULL) { } /** Destructor, deletes all of the extensible items in this object * then clears the map */ virtual ~Extensible() { - for (extensible_map::iterator it = extension_items.begin(), it_end = extension_items.end(); it != it_end; ++it) - if (it->second) - it->second->OnDelete(); - extension_items.clear(); + if (extension_items) + { + for (extensible_map::iterator it = extension_items->begin(), it_end = extension_items->end(); it != it_end; ++it) + if (it->second) + it->second->OnDelete(); + delete extension_items; + } } /** Extend an Extensible class. @@ -70,7 +73,9 @@ class CoreExport Extensible void Extend(const Anope::string &key, ExtensibleItem *p) { this->Shrink(key); - this->extension_items[key] = p; + if (!extension_items) + extension_items = new extensible_map(); + (*this->extension_items)[key] = p; } /** Shrink an Extensible class. @@ -83,8 +88,11 @@ class CoreExport Extensible */ bool Shrink(const Anope::string &key) { - extensible_map::iterator it = this->extension_items.find(key); - if (it != this->extension_items.end()) + if (!extension_items) + return false; + + extensible_map::iterator it = this->extension_items->find(key); + if (it != this->extension_items->end()) { if (it->second != NULL) it->second->OnDelete(); @@ -92,7 +100,7 @@ class CoreExport Extensible * returns the number of elements removed, std::map * is single-associative so this should only be 0 or 1 */ - return this->extension_items.erase(key) > 0; + return this->extension_items->erase(key) > 0; } return false; @@ -105,9 +113,12 @@ class CoreExport Extensible */ template T GetExt(const Anope::string &key) const { - extensible_map::const_iterator it = this->extension_items.find(key); - if (it != this->extension_items.end()) - return anope_dynamic_static_cast(it->second); + if (this->extension_items) + { + extensible_map::const_iterator it = this->extension_items->find(key); + if (it != this->extension_items->end()) + return anope_dynamic_static_cast(it->second); + } return NULL; } @@ -119,7 +130,7 @@ class CoreExport Extensible */ bool HasExt(const Anope::string &key) const { - return this->extension_items.count(key) > 0; + return this->extension_items != NULL && this->extension_items->count(key) > 0; } /** Get a list of all extension items names. @@ -129,8 +140,9 @@ class CoreExport Extensible */ void GetExtList(std::deque &list) const { - for (extensible_map::const_iterator it = extension_items.begin(), it_end = extension_items.end(); it != it_end; ++it) - list.push_back(it->first); + if (extension_items) + for (extensible_map::const_iterator it = extension_items->begin(), it_end = extension_items->end(); it != it_end; ++it) + list.push_back(it->first); } }; diff --git a/include/memo.h b/include/memo.h index 9fdbb3cab..9a7acceb9 100644 --- a/include/memo.h +++ b/include/memo.h @@ -31,7 +31,7 @@ class CoreExport Memo : public Flags, public Serializable public: Memo(); - Serialize::Data Serialize() const anope_override; + void Serialize(Serialize::Data &data) const anope_override; static Serializable* Unserialize(Serializable *obj, Serialize::Data &); Anope::string owner; diff --git a/include/module.h b/include/module.h index e6b63bdfb..197899ca8 100644 --- a/include/module.h +++ b/include/module.h @@ -40,7 +40,6 @@ #include "servers.h" #include "service.h" #include "services.h" -#include "signals.h" #include "socketengine.h" #include "sockets.h" #include "threadengine.h" diff --git a/include/regchannel.h b/include/regchannel.h index 9e01963b8..1cefe27dd 100644 --- a/include/regchannel.h +++ b/include/regchannel.h @@ -143,7 +143,7 @@ struct CoreExport BadWord : Serializable BadWordType type; BadWord() : Serializable("BadWord") { } - Serialize::Data Serialize() const anope_override; + void Serialize(Serialize::Data &data) const anope_override; static Serializable* Unserialize(Serializable *obj, Serialize::Data &); }; @@ -172,7 +172,7 @@ class CoreExport AutoKick : public Flags, public Serializable time_t last_used; AutoKick(); - Serialize::Data Serialize() const anope_override; + void Serialize(Serialize::Data &data) const anope_override; static Serializable* Unserialize(Serializable *obj, Serialize::Data &); }; @@ -188,7 +188,7 @@ struct CoreExport ModeLock : Serializable ModeLock(ChannelInfo *ch, bool s, ChannelModeName n, const Anope::string &p, const Anope::string &se = "", time_t c = Anope::CurTime); - Serialize::Data Serialize() const anope_override; + void Serialize(Serialize::Data &data) const anope_override; static Serializable* Unserialize(Serializable *obj, Serialize::Data &); }; @@ -206,7 +206,7 @@ struct CoreExport LogSetting : Serializable time_t created; LogSetting() : Serializable("LogSetting") { } - Serialize::Data Serialize() const anope_override; + void Serialize(Serialize::Data &data) const anope_override; static Serializable* Unserialize(Serializable *obj, Serialize::Data &); }; @@ -263,7 +263,7 @@ class CoreExport ChannelInfo : public Serializable, public Extensible, public Fl ~ChannelInfo(); - Serialize::Data Serialize() const anope_override; + void Serialize(Serialize::Data &data) const anope_override; static Serializable* Unserialize(Serializable *obj, Serialize::Data &); /** Change the founder of the channek diff --git a/include/serialize.h b/include/serialize.h index 8b8497c40..1a16e5b35 100644 --- a/include/serialize.h +++ b/include/serialize.h @@ -20,41 +20,24 @@ namespace Serialize { - enum DataType + class Data { - DT_TEXT, - DT_INT - }; - - class CoreExport stringstream : public std::stringstream - { - private: - Serialize::DataType type; - unsigned _max; - public: - stringstream(); - stringstream(const stringstream &ss); - Anope::string astr() const; - - template std::istream &operator>>(T &val) + enum Type { - std::istringstream is(this->str()); - is >> val; - return *this; - } - std::istream &operator>>(Anope::string &val); + DT_TEXT, + DT_INT + }; - bool operator==(const stringstream &other) const; - bool operator!=(const stringstream &other) const; + virtual ~Data() { } - stringstream &SetType(Serialize::DataType t); - Serialize::DataType GetType() const; - stringstream &SetMax(unsigned m); - unsigned GetMax() const; - }; + virtual std::iostream& operator[](const Anope::string &key) = 0; - typedef std::map Data; + virtual bool IsEqual(Data *other) { throw CoreException("Not supported"); } + + virtual void SetType(const Anope::string &key, Type t) { } + virtual Type GetType(const Anope::string &key) const { return DT_TEXT; } + }; extern void RegisterTypes(); @@ -64,7 +47,7 @@ namespace Serialize } /** A serialziable object. Serializable objects can be serialized into - * a map of stringstreams (Serialize::Data), and then reconstructed or + * abstract data types (Serialize::Data), and then reconstructed or * updated later at any time. */ class CoreExport Serializable : public virtual Base @@ -82,7 +65,7 @@ class CoreExport Serializable : public virtual Base /* Iterator into serializable_items */ std::list::iterator s_iter; /* The last serialized form of this object commited to the database */ - Serialize::Data last_commit; + Serialize::Data *last_commit; /* The last time this object was commited to the database */ time_t last_commit_time; @@ -108,8 +91,8 @@ class CoreExport Serializable : public virtual Base */ void QueueUpdate(); - bool IsCached(); - void UpdateCache(); + bool IsCached(Serialize::Data *); + void UpdateCache(Serialize::Data *); bool IsTSCached(); void UpdateTS(); @@ -117,9 +100,9 @@ class CoreExport Serializable : public virtual Base /** Get the type of serializable object this is * @return The serializable object type */ - Serialize::Type* GetSerializableType() const; + Serialize::Type* GetSerializableType() const { return this->s_type; } - virtual Serialize::Data Serialize() const = 0; + virtual void Serialize(Serialize::Data &data) const = 0; static const std::list &GetItems(); }; @@ -164,7 +147,7 @@ class CoreExport Serialize::Type /** Gets the name for this type * @return The name, eg "NickAlias" */ - const Anope::string &GetName(); + const Anope::string &GetName() { return this->name; } /** Unserialized an object. * @param obj NULL if this object doesn't yet exist. If this isn't NULL, instead @@ -187,7 +170,7 @@ class CoreExport Serialize::Type */ void UpdateTimestamp(); - Module* GetOwner() const; + Module* GetOwner() const { return this->owner; } static Serialize::Type *Find(const Anope::string &name); diff --git a/include/signals.h b/include/signals.h deleted file mode 100644 index 771404f52..000000000 --- a/include/signals.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * - * (C) 2003-2012 Anope Team - * Contact us at team@anope.org - * - * Please read COPYING and README for further details. - * - * Based on the original code of Epona by Lara. - * Based on the original code of Services by Andy Church. - * - */ - -#ifndef SIGNAL_H -#define SIGNAL_H - -#include - -#include "sockets.h" - -/** Represents a signal handler - */ -class Signal : public Pipe -{ - static std::vector SignalHandlers; - static void SignalHandler(int signal); - - struct sigaction action, old; - public: - int signal; - - /** Constructor - * @param s The signal to listen for - */ - Signal(int s); - ~Signal(); - - /** - * Called when the signal is received. - * Note this is not *immediatly* called when the signal is received, - * but it is saved and called at a later time when we are not doing something - * important. This is always called on the main thread, even on systems that - * spawn threads for signals, like Windows. - */ - virtual void OnNotify() anope_override = 0; -}; - -#endif - diff --git a/include/socketengine.h b/include/socketengine.h index 5f4bc5980..696f69c56 100644 --- a/include/socketengine.h +++ b/include/socketengine.h @@ -18,7 +18,7 @@ class CoreExport SocketEngine { - static const int DefaultSize = 8; // Uplink, DNS, Signal handlers, Mode stacker + static const int DefaultSize = 2; // Uplink, mode stacker public: /* Map of sockets */ static std::map Sockets; diff --git a/include/sockets.h b/include/sockets.h index 1ef10d0c8..ae7d6e798 100644 --- a/include/sockets.h +++ b/include/sockets.h @@ -225,15 +225,11 @@ class CoreExport Socket : public Flags */ bool IsIPv6() const; - /** Mark a socket as blocking + /** Mark a socket as (non)blocking + * @param state true to enable blocking, false to disable blocking * @return true if the socket is now blocking */ - bool SetBlocking(); - - /** Mark a socket as non-blocking - * @return true if the socket is now non-blocking - */ - bool SetNonBlocking(); + bool SetBlocking(bool state); /** Bind the socket to an ip and port * @param ip The ip @@ -456,7 +452,7 @@ class CoreExport ClientSocket : public virtual Socket class CoreExport Pipe : public Socket { public: - /** The FD of the write pipe (if this isn't evenfd) + /** The FD of the write pipe * this->sock is the readfd */ int write_pipe; @@ -468,7 +464,28 @@ class CoreExport Pipe : public Socket */ bool ProcessRead() anope_override; + /** Write data to this pipe + * @param data The data to write + * @param sz The amount of data to wirite + */ + void Write(const char *data, size_t sz); + inline void Write(const Anope::string &data) { this->Write(data.c_str(), data.length() + 1); } + + /** Read data from this pipe + * @param data A buffer to read data into + * @param sz The size of the buffer + * @return The amount of data read + */ + int Read(char *data, size_t sz); + + /** Mark the write end of this pipe (non)blocking + * @param state true to enable blocking, false to disable blocking + * @return true if the socket is now blocking + */ + bool SetWriteBlocking(bool state); + /** Called when this pipe needs to be woken up + * Is the same as Write("\0", 1) */ void Notify(); diff --git a/include/xline.h b/include/xline.h index 5d407d4ba..374b75a6a 100644 --- a/include/xline.h +++ b/include/xline.h @@ -42,7 +42,7 @@ class CoreExport XLine : public Serializable bool HasNickOrReal() const; bool IsRegex() const; - Serialize::Data Serialize() const anope_override; + void Serialize(Serialize::Data &data) const anope_override; static Serializable* Unserialize(Serializable *obj, Serialize::Data &data); }; -- cgit