From b0ea8848a53b30f1c126c60e257a515b5ed83f0e Mon Sep 17 00:00:00 2001 From: Ambal Date: Mon, 26 Oct 2009 01:05:08 +0200 Subject: [PATCH] [8736] operator new[] based version of strdup() function to get bonuses from Intel Memory allocator mainly in DB code Signed-off-by: Ambal --- src/game/Chat.cpp | 8 ++++---- src/game/World.cpp | 4 ++-- src/mangosd/RASocket.cpp | 2 +- src/shared/Common.h | 10 ++++++++++ src/shared/Database/SqlOperations.cpp | 12 ++++++------ src/shared/Database/SqlOperations.h | 10 +++++----- src/shared/revision_nr.h | 2 +- 7 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/game/Chat.cpp b/src/game/Chat.cpp index 17f586ad4..47786c4d2 100644 --- a/src/game/Chat.cpp +++ b/src/game/Chat.cpp @@ -768,7 +768,7 @@ void ChatHandler::SendSysMessage(const char *str) WorldPacket data; // need copy to prevent corruption by strtok call in LineFromMessage original string - char* buf = strdup(str); + char* buf = mangos_strdup(str); char* pos = buf; while(char* line = LineFromMessage(pos)) @@ -777,7 +777,7 @@ void ChatHandler::SendSysMessage(const char *str) m_session->SendPacket(&data); } - free(buf); + delete [] buf; } void ChatHandler::SendGlobalSysMessage(const char *str) @@ -786,7 +786,7 @@ void ChatHandler::SendGlobalSysMessage(const char *str) WorldPacket data; // need copy to prevent corruption by strtok call in LineFromMessage original string - char* buf = strdup(str); + char* buf = mangos_strdup(str); char* pos = buf; while(char* line = LineFromMessage(pos)) @@ -795,7 +795,7 @@ void ChatHandler::SendGlobalSysMessage(const char *str) sWorld.SendGlobalMessage(&data); } - free(buf); + delete [] buf; } void ChatHandler::SendSysMessage(int32 entry) diff --git a/src/game/World.cpp b/src/game/World.cpp index a2931c63b..6685a3d92 100644 --- a/src/game/World.cpp +++ b/src/game/World.cpp @@ -1769,7 +1769,7 @@ void World::SendGlobalText(const char* text, WorldSession *self) WorldPacket data; // need copy to prevent corruption by strtok call in LineFromMessage original string - char* buf = strdup(text); + char* buf = mangos_strdup(text); char* pos = buf; while(char* line = ChatHandler::LineFromMessage(pos)) @@ -1778,7 +1778,7 @@ void World::SendGlobalText(const char* text, WorldSession *self) SendGlobalMessage(&data, self); } - free(buf); + delete [] buf; } /// Send a packet to all players (or players selected team) in the zone (except self if mentioned) diff --git a/src/mangosd/RASocket.cpp b/src/mangosd/RASocket.cpp index 565226947..91baac0a6 100644 --- a/src/mangosd/RASocket.cpp +++ b/src/mangosd/RASocket.cpp @@ -242,7 +242,7 @@ void RASocket::zprint( const char * szText ) #ifdef RA_CRYPT - char *megabuffer=strdup(szText); + char *megabuffer = mangos_strdup(szText); unsigned int sz=strlen(megabuffer); Encrypt(megabuffer,sz); send(r,megabuffer,sz,0); diff --git a/src/shared/Common.h b/src/shared/Common.h index 1b46bf8c5..2cecc12e8 100644 --- a/src/shared/Common.h +++ b/src/shared/Common.h @@ -184,6 +184,16 @@ enum LocaleConstant extern char const* localeNames[MAX_LOCALE]; LocaleConstant GetLocaleByName(const std::string& name); +//operator new[] based version of strdup() function! Release memory by using operator delete[] ! +inline char * mangos_strdup(const char * source) +{ + const size_t length = strlen(source); + char * dest = new char[length + 1]; + //set terminating end-of-string symbol + dest[length] = 0; + strcpy(dest, source); + return dest; +} // we always use stdlibc++ std::max/std::min, undefine some not C++ standard defines (Win API and some pother platforms) #ifdef max diff --git a/src/shared/Database/SqlOperations.cpp b/src/shared/Database/SqlOperations.cpp index f707dd1f2..8e5c45d33 100644 --- a/src/shared/Database/SqlOperations.cpp +++ b/src/shared/Database/SqlOperations.cpp @@ -41,17 +41,17 @@ void SqlTransaction::Execute(Database *db) if(!db->DirectExecute(sql)) { - free((void*)const_cast(sql)); + delete [] ((void*)const_cast(sql)); db->DirectExecute("ROLLBACK"); while(!m_queue.empty()) { - free((void*)const_cast(m_queue.front())); + delete [] ((void*)const_cast(m_queue.front())); m_queue.pop(); } return; } - free((void*)const_cast(sql)); + delete [] ((void*)const_cast(sql)); } db->DirectExecute("COMMIT"); } @@ -107,7 +107,7 @@ bool SqlQueryHolder::SetQuery(size_t index, const char *sql) } /// not executed yet, just stored (it's not called a holder for nothing) - m_queries[index] = SqlResultPair(strdup(sql), (QueryResult*)NULL); + m_queries[index] = SqlResultPair(mangos_strdup(sql), (QueryResult*)NULL); return true; } @@ -141,7 +141,7 @@ QueryResult* SqlQueryHolder::GetResult(size_t index) /// the query strings are freed on the first GetResult or in the destructor if(m_queries[index].first != NULL) { - free((void*)(const_cast(m_queries[index].first))); + delete [] ((void*)(const_cast(m_queries[index].first))); m_queries[index].first = NULL; } /// when you get a result aways remember to delete it! @@ -166,7 +166,7 @@ SqlQueryHolder::~SqlQueryHolder() /// results used already (getresult called) are expected to be deleted if(m_queries[i].first != NULL) { - free((void*)(const_cast(m_queries[i].first))); + delete [] ((void*)(const_cast(m_queries[i].first))); if(m_queries[i].second) delete m_queries[i].second; } diff --git a/src/shared/Database/SqlOperations.h b/src/shared/Database/SqlOperations.h index 29fe51fc6..c74360ec1 100644 --- a/src/shared/Database/SqlOperations.h +++ b/src/shared/Database/SqlOperations.h @@ -46,8 +46,8 @@ class SqlStatement : public SqlOperation private: const char *m_sql; public: - SqlStatement(const char *sql) : m_sql(strdup(sql)){} - ~SqlStatement() { void* tofree = const_cast(m_sql); free(tofree); } + SqlStatement(const char *sql) : m_sql(mangos_strdup(sql)){} + ~SqlStatement() { void* tofree = const_cast(m_sql); delete [] tofree; } void Execute(Database *db); }; @@ -57,7 +57,7 @@ class SqlTransaction : public SqlOperation std::queue m_queue; public: SqlTransaction() {} - void DelayExecute(const char *sql) { m_queue.push(strdup(sql)); } + void DelayExecute(const char *sql) { m_queue.push(mangos_strdup(sql)); } void Execute(Database *db); }; @@ -84,8 +84,8 @@ class SqlQuery : public SqlOperation SqlResultQueue * m_queue; public: SqlQuery(const char *sql, MaNGOS::IQueryCallback * callback, SqlResultQueue * queue) - : m_sql(strdup(sql)), m_callback(callback), m_queue(queue) {} - ~SqlQuery() { void* tofree = const_cast(m_sql); free(tofree); } + : m_sql(mangos_strdup(sql)), m_callback(callback), m_queue(queue) {} + ~SqlQuery() { void* tofree = const_cast(m_sql); delete [] tofree; } void Execute(Database *db); }; diff --git a/src/shared/revision_nr.h b/src/shared/revision_nr.h index 1dc76f1c5..7ab52c7f1 100644 --- a/src/shared/revision_nr.h +++ b/src/shared/revision_nr.h @@ -1,4 +1,4 @@ #ifndef __REVISION_NR_H__ #define __REVISION_NR_H__ - #define REVISION_NR "8735" + #define REVISION_NR "8736" #endif // __REVISION_NR_H__