]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/users.cpp
Clean up static allocations
[user/henk/code/inspircd.git] / src / users.cpp
index 889e5acdc5c0f0a8e8ac4e6f77dda5fcc9868f1c..729213e426f6754c42ddf2597a76d7385822e6e7 100644 (file)
@@ -11,8 +11,6 @@
  * ---------------------------------------------------
  */
 
-/* $Core */
-
 #include "inspircd.h"
 #include <stdarg.h>
 #include "socketengine.h"
 #include "bancache.h"
 #include "commands/cmd_whowas.h"
 
-/* XXX: Used for speeding up WriteCommon operations */
-unsigned long uniq_id = 1;
-
-static unsigned long* already_sent = NULL;
-
-LocalIntExt User::NICKForced("NICKForced", NULL);
-LocalStringExt User::OperQuit("OperQuit", NULL);
-
-void InitializeAlreadySent(SocketEngine* SE)
+typedef unsigned int uniq_id_t;
+class sent
 {
-       already_sent = new unsigned long[SE->GetMaxFds()];
-       memset(already_sent, 0, SE->GetMaxFds() * sizeof(unsigned long));
-}
+       uniq_id_t uniq_id;
+       uniq_id_t* array;
+       void init()
+       {
+               if (!array)
+                       array = new uniq_id_t[ServerInstance->SE->GetMaxFds()];
+               memset(array, 0, ServerInstance->SE->GetMaxFds() * sizeof(uniq_id_t));
+               uniq_id++;
+       }
+ public:
+       sent() : uniq_id(static_cast<uniq_id_t>(-1)), array(NULL) {}
+       inline uniq_id_t operator++()
+       {
+               if (++uniq_id == 0)
+                       init();
+               return uniq_id;
+       }
+       inline uniq_id_t& operator[](int i)
+       {
+               return array[i];
+       }
+       ~sent()
+       {
+               delete array;
+       }
+};
+
+static sent already_sent;
 
 std::string User::ProcessNoticeMasks(const char *sm)
 {
@@ -915,9 +931,9 @@ bool User::ForceNickChange(const char* newnick)
 
        this->InvalidateCache();
 
-       NICKForced.set(this, 1);
+       ServerInstance->NICKForced.set(this, 1);
        FIRST_MOD_RESULT(OnUserPreNick, MOD_RESULT, (this, newnick));
-       NICKForced.set(this, 0);
+       ServerInstance->NICKForced.set(this, 0);
 
        if (MOD_RESULT == MOD_RES_DENY)
        {
@@ -931,9 +947,9 @@ bool User::ForceNickChange(const char* newnick)
        {
                std::vector<std::string> parameters;
                parameters.push_back(newnick);
-               NICKForced.set(this, 1);
+               ServerInstance->NICKForced.set(this, 1);
                bool result = (ServerInstance->Parser->CallHandler("NICK", parameters, this) == CMD_SUCCESS);
-               NICKForced.set(this, 0);
+               ServerInstance->NICKForced.set(this, 0);
                return result;
        }
 
@@ -1204,7 +1220,7 @@ void User::WriteCommon(const char* text, ...)
        char textbuffer[MAXBUF];
        va_list argsPtr;
 
-       if (this->registered != REG_ALL || !IS_LOCAL(this) || quitting)
+       if (this->registered != REG_ALL || quitting)
                return;
 
        int len = snprintf(textbuffer,MAXBUF,":%s ",this->GetFullHost().c_str());
@@ -1221,7 +1237,7 @@ void User::WriteCommonExcept(const char* text, ...)
        char textbuffer[MAXBUF];
        va_list argsPtr;
 
-       if (this->registered != REG_ALL || !IS_LOCAL(this) || quitting)
+       if (this->registered != REG_ALL || quitting)
                return;
 
        int len = snprintf(textbuffer,MAXBUF,":%s ",this->GetFullHost().c_str());
@@ -1235,12 +1251,10 @@ void User::WriteCommonExcept(const char* text, ...)
 
 void User::WriteCommonRaw(const std::string &line, bool include_self)
 {
-       if (this->registered != REG_ALL || !IS_LOCAL(this) || quitting)
+       if (this->registered != REG_ALL || quitting)
                return;
 
-       if (!already_sent)
-               InitializeAlreadySent(ServerInstance->SE);
-       uniq_id++;
+       uniq_id_t uniq_id = ++already_sent;
 
        UserChanList include_c(chans);
        std::map<User*,bool> exceptions;
@@ -1283,10 +1297,7 @@ void User::WriteCommonQuit(const std::string &normal_text, const std::string &op
        if (this->registered != REG_ALL)
                return;
 
-       uniq_id++;
-
-       if (!already_sent)
-               InitializeAlreadySent(ServerInstance->SE);
+       uniq_id_t uniq_id = ++already_sent;
 
        snprintf(tb1,MAXBUF,":%s QUIT :%s",this->GetFullHost().c_str(),normal_text.c_str());
        snprintf(tb2,MAXBUF,":%s QUIT :%s",this->GetFullHost().c_str(),oper_text.c_str());
@@ -1405,10 +1416,8 @@ void User::DoHostCycle(const std::string &quitline)
        if (!ServerInstance->Config->CycleHosts)
                return;
 
-       uniq_id++;
-
-       if (!already_sent)
-               InitializeAlreadySent(ServerInstance->SE);
+       uniq_id_t silent_id = ++already_sent;
+       uniq_id_t seen_id = ++already_sent;
 
        UserChanList include_c(chans);
        std::map<User*,bool> exceptions;
@@ -1420,9 +1429,15 @@ void User::DoHostCycle(const std::string &quitline)
                User* u = i->first;
                if (IS_LOCAL(u) && !u->quitting)
                {
-                       already_sent[u->fd] = uniq_id;
                        if (i->second)
+                       {
+                               already_sent[u->fd] = seen_id;
                                u->Write(quitline);
+                       }
+                       else
+                       {
+                               already_sent[u->fd] = silent_id;
+                       }
                }
        }
        for (UCListIter v = include_c.begin(); v != include_c.end(); ++v)
@@ -1443,11 +1458,13 @@ void User::DoHostCycle(const std::string &quitline)
                        User* u = i->first;
                        if (u == this || !IS_LOCAL(u))
                                continue;
+                       if (already_sent[u->fd] == silent_id)
+                               continue;
 
-                       if (already_sent[i->first->fd] != uniq_id)
+                       if (already_sent[u->fd] != seen_id)
                        {
                                u->Write(quitline);
-                               already_sent[i->first->fd] = uniq_id;
+                               already_sent[i->first->fd] = seen_id;
                        }
                        u->Write(joinline);
                        if (modeline.length() > 0)