summaryrefslogtreecommitdiff
path: root/plugins/Variables/src/tokenregister.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2014-01-03 15:57:52 +0000
committerGeorge Hazan <george.hazan@gmail.com>2014-01-03 15:57:52 +0000
commit5e0c012d677635e13cbfcf6730f2deafdc38c025 (patch)
treec6cd34a70ae0402f69faf5d65e974014b5e63fda /plugins/Variables/src/tokenregister.cpp
parentcd4e43a5af4dcf660969bc5abada0d97cfd0cd03 (diff)
- memory leak fixes
- code cleaning git-svn-id: http://svn.miranda-ng.org/main/trunk@7479 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/Variables/src/tokenregister.cpp')
-rw-r--r--plugins/Variables/src/tokenregister.cpp271
1 files changed, 109 insertions, 162 deletions
diff --git a/plugins/Variables/src/tokenregister.cpp b/plugins/Variables/src/tokenregister.cpp
index ad0d25635d..f4865e84e5 100644
--- a/plugins/Variables/src/tokenregister.cpp
+++ b/plugins/Variables/src/tokenregister.cpp
@@ -1,37 +1,39 @@
/*
- Variables Plugin for Miranda-IM (www.miranda-im.org)
- Copyright 2003-2006 P. Boon
+ Variables Plugin for Miranda-IM (www.miranda-im.org)
+ Copyright 2003-2006 P. Boon
- This program is mir_free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
+ This program is mir_free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-*/
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
#include "variables.h"
-typedef struct {
+struct TokenRegisterEntry
+{
TOKENREGISTEREX tr;
DWORD nameHash;
-}
- TokenRegisterEntry;
+};
-struct
+static int CompareTokens(const TokenRegisterEntry* p1, const TokenRegisterEntry* p2)
{
- TokenRegisterEntry** items;
- int count, limit, increment;
- FSortFunc sortFunc;
+ if (p1->nameHash == p2->nameHash)
+ return 0;
+
+ return (p1->nameHash < p2->nameHash) ? -1 : 1;
}
-static tokens;
+
+static LIST<TokenRegisterEntry> tokens(100, CompareTokens);
static CRITICAL_SECTION csRegister;
@@ -44,20 +46,16 @@ static DWORD NameHashFunction(TCHAR *tszStr)
static TokenRegisterEntry* FindTokenRegisterByName(TCHAR *name)
{
- int idx;
TokenRegisterEntry temp;
- temp.nameHash = NameHashFunction( name );
- if (List_GetIndex(( SortedList* )&tokens, &temp, &idx ))
- return tokens.items[ idx ];
-
- return NULL;
+ temp.nameHash = NameHashFunction(name);
+ return tokens.find(&temp);
}
int registerIntToken(TCHAR *szToken, TCHAR *(*parseFunction)(ARGUMENTSINFO *ai), int extraFlags, char* szHelpText)
{
TOKENREGISTEREX tr = { 0 };
tr.cbSize = sizeof(tr);
- tr.flags = TRF_FREEMEM|TRF_TCHAR|TRF_PARSEFUNC|extraFlags;
+ tr.flags = TRF_FREEMEM | TRF_TCHAR | TRF_PARSEFUNC | extraFlags;
//tr.memType = TR_MEM_VARIABLES;
tr.memType = TR_MEM_MIRANDA;
tr.szHelpText = szHelpText;
@@ -69,34 +67,32 @@ int registerIntToken(TCHAR *szToken, TCHAR *(*parseFunction)(ARGUMENTSINFO *ai),
int deRegisterToken(TCHAR *token)
{
- TokenRegisterEntry *tre;
-
if (token == NULL)
return -1;
- EnterCriticalSection(&csRegister);
- tre = FindTokenRegisterByName( token );
- if (tre == NULL) {
- LeaveCriticalSection(&csRegister);
- return -1;
- }
+ TokenRegisterEntry *tre;
+ {
+ mir_cslock lck(csRegister);
+ tre = FindTokenRegisterByName(token);
+ if (tre == NULL)
+ return -1;
- List_RemovePtr(( SortedList* )&tokens, tre );
- LeaveCriticalSection(&csRegister);
+ List_RemovePtr((SortedList*)&tokens, tre);
+ }
- if (!( tre->tr.flags & TRF_PARSEFUNC ) && tre->tr.szService != NULL)
- mir_free( tre->tr.szService );
+ if (!(tre->tr.flags & TRF_PARSEFUNC) && tre->tr.szService != NULL)
+ mir_free(tre->tr.szService);
if (tre->tr.tszTokenString != NULL)
- mir_free( tre->tr.tszTokenString );
+ mir_free(tre->tr.tszTokenString);
if (tre->tr.szHelpText != NULL)
- mir_free( tre->tr.szHelpText );
+ mir_free(tre->tr.szHelpText);
- if ((tre->tr.flags & TRF_CLEANUP ) && !( tre->tr.flags & TRF_CLEANUPFUNC ) && tre->tr.szCleanupService != NULL)
- mir_free( tre->tr.szCleanupService );
+ if ((tre->tr.flags & TRF_CLEANUP) && !(tre->tr.flags & TRF_CLEANUPFUNC) && tre->tr.szCleanupService != NULL)
+ mir_free(tre->tr.szCleanupService);
- mir_free( tre );
+ mir_free(tre);
return 0;
}
@@ -104,77 +100,62 @@ INT_PTR registerToken(WPARAM wParam, LPARAM lParam)
{
DWORD hash;
int idx;
- TokenRegisterEntry *tre;
- TOKENREGISTEREX *newVr = ( TOKENREGISTEREX* )lParam;
- if (newVr == NULL || newVr->szTokenString == NULL || newVr->cbSize <= 0 )
+ TOKENREGISTEREX *newVr = (TOKENREGISTEREX*)lParam;
+ if (newVr == NULL || newVr->szTokenString == NULL || newVr->cbSize <= 0)
return -1;
- if (newVr->flags & TRF_TCHAR ) {
- deRegisterToken( newVr->tszTokenString );
- hash = NameHashFunction( newVr->tszTokenString );
+ if (newVr->flags & TRF_TCHAR) {
+ deRegisterToken(newVr->tszTokenString);
+ hash = NameHashFunction(newVr->tszTokenString);
}
else {
-
- WCHAR *wtoken;
-
- wtoken = mir_a2t( newVr->szTokenString );
- deRegisterToken( wtoken );
- hash = NameHashFunction( wtoken );
- mir_free( wtoken );
-
+ WCHAR *wtoken = mir_a2t(newVr->szTokenString);
+ deRegisterToken(wtoken);
+ hash = NameHashFunction(wtoken);
+ mir_free(wtoken);
}
- tre = ( TokenRegisterEntry* )mir_alloc( sizeof( TokenRegisterEntry ));
+ TokenRegisterEntry *tre = (TokenRegisterEntry*)mir_alloc(sizeof(TokenRegisterEntry));
if (tre == NULL)
return -1;
- memcpy( &tre->tr, newVr, newVr->cbSize );
+ memcpy(&tre->tr, newVr, newVr->cbSize);
tre->nameHash = hash;
- if (!_tcscmp( newVr->tszTokenString, _T("alias")))
+ if (!_tcscmp(newVr->tszTokenString, _T("alias")))
log_debugA("alias");
- if (!( newVr->flags & TRF_PARSEFUNC ) && newVr->szService != NULL)
- tre->tr.szService = mir_strdup( newVr->szService );
+ if (!(newVr->flags & TRF_PARSEFUNC) && newVr->szService != NULL)
+ tre->tr.szService = mir_strdup(newVr->szService);
- if (newVr->flags & TRF_TCHAR )
- tre->tr.tszTokenString = mir_tstrdup( newVr->tszTokenString );
+ if (newVr->flags & TRF_TCHAR)
+ tre->tr.tszTokenString = mir_tstrdup(newVr->tszTokenString);
else
-
- tre->tr.tszTokenString = mir_a2t( newVr->szTokenString );
-
+ tre->tr.tszTokenString = mir_a2t(newVr->szTokenString);
if (newVr->szHelpText != NULL)
- tre->tr.szHelpText = mir_strdup( newVr->szHelpText );
+ tre->tr.szHelpText = mir_strdup(newVr->szHelpText);
- if (( newVr->flags & TRF_CLEANUP ) && !( newVr->flags & TRF_CLEANUPFUNC ) && newVr->szCleanupService != NULL)
- tre->tr.szCleanupService = mir_strdup( newVr->szCleanupService );
-
- EnterCriticalSection(&csRegister);
- List_GetIndex(( SortedList* )&tokens, tre, &idx );
- List_Insert(( SortedList* )&tokens, tre, idx );
- LeaveCriticalSection(&csRegister);
+ if ((newVr->flags & TRF_CLEANUP) && !(newVr->flags & TRF_CLEANUPFUNC) && newVr->szCleanupService != NULL)
+ tre->tr.szCleanupService = mir_strdup(newVr->szCleanupService);
+ mir_cslock lck(csRegister);
+ List_GetIndex((SortedList*)&tokens, tre, &idx);
+ List_Insert((SortedList*)&tokens, tre, idx);
return 0;
}
TOKENREGISTEREX *searchRegister(TCHAR *tvar, int type)
{
- TokenRegisterEntry *tre;
- TOKENREGISTEREX *retVr;
-
if (tvar == NULL)
return 0;
- EnterCriticalSection( &csRegister );
- tre = FindTokenRegisterByName( tvar );
- if (tre == NULL || ( type != 0 && (tre->tr.flags & ( TRF_FIELD | TRF_FUNCTION )) != 0 && !( tre->tr.flags & type )))
- retVr = NULL;
- else
- retVr = &tre->tr;
+ mir_cslock lck(csRegister);
+ TokenRegisterEntry *tre = FindTokenRegisterByName(tvar);
+ if (tre == NULL || (type != 0 && (tre->tr.flags & (TRF_FIELD | TRF_FUNCTION)) != 0 && !(tre->tr.flags & type)))
+ return NULL;
- LeaveCriticalSection(&csRegister);
- return retVr;
+ return &tre->tr;
}
TCHAR *parseFromRegister(ARGUMENTSINFO *ai)
@@ -185,133 +166,99 @@ TCHAR *parseFromRegister(ARGUMENTSINFO *ai)
INT_PTR callRes = 0;
TCHAR *temp = NULL, *res = NULL;
- EnterCriticalSection(&csRegister);
+ mir_cslock lck(csRegister);
/* note the following limitation: you cannot add/remove tokens during a call from a different thread */
- TOKENREGISTEREX *thisVr = searchRegister( ai->targv[0], 0 );
- if (thisVr == NULL) {
- LeaveCriticalSection(&csRegister);
+ TOKENREGISTEREX *thisVr = searchRegister(ai->targv[0], 0);
+ if (thisVr == NULL)
return NULL;
- }
TOKENREGISTEREX trCopy = *thisVr;
// ai contains WCHARs, convert to chars because the tr doesn't support WCHARs
- if (!( thisVr->flags & TRF_TCHAR )) {
+ if (!(thisVr->flags & TRF_TCHAR)) {
// unicode variables calls a non-unicode plugin
- unsigned int j;
ARGUMENTSINFO cAi;
-
memcpy(&cAi, ai, sizeof(ARGUMENTSINFO));
- cAi.argv = ( char** )mir_alloc(ai->argc*sizeof(char *));
- for ( j=0; j < ai->argc; j++ )
- cAi.argv[j] = mir_t2a( ai->targv[j] );
+ cAi.argv = (char**)mir_alloc(ai->argc*sizeof(char *));
+ for (unsigned j = 0; j < ai->argc; j++)
+ cAi.argv[j] = mir_t2a(ai->targv[j]);
- if (thisVr->flags & TRF_PARSEFUNC )
- callRes = (INT_PTR)thisVr->parseFunction( &cAi );
+ if (thisVr->flags & TRF_PARSEFUNC)
+ callRes = (INT_PTR)thisVr->parseFunction(&cAi);
else if (thisVr->szService != NULL)
- callRes = CallService(thisVr->szService, 0, (LPARAM)&cAi );
+ callRes = CallService(thisVr->szService, 0, (LPARAM)&cAi);
- for ( j=0; j < cAi.argc; j++ )
- if (cAi.argv[j] != NULL)
- mir_free( cAi.argv[j] );
+ for (unsigned j = 0; j < cAi.argc; j++)
+ mir_free(cAi.argv[j]);
if ((char *)callRes != NULL)
- res = mir_a2t(( char* )callRes );
+ res = mir_a2t((char*)callRes);
}
else {
// unicode variables calls unicode plugin
- if (thisVr->flags & TRF_PARSEFUNC )
- callRes = (INT_PTR)thisVr->parseFunctionT( ai );
+ if (thisVr->flags & TRF_PARSEFUNC)
+ callRes = (INT_PTR)thisVr->parseFunctionT(ai);
else if (thisVr->szService != NULL)
- callRes = CallService(thisVr->szService, 0, (LPARAM)ai );
+ callRes = CallService(thisVr->szService, 0, (LPARAM)ai);
if ((TCHAR*)callRes != NULL)
- res = mir_tstrdup((TCHAR*)callRes );
+ res = mir_tstrdup((TCHAR*)callRes);
}
-
- if (( void* )callRes != NULL) {
- if (trCopy.flags & TRF_CLEANUP ) {
- if (trCopy.flags & TRF_CLEANUPFUNC )
- trCopy.cleanupFunctionT((TCHAR*)callRes );
+ if (callRes != NULL) {
+ if (trCopy.flags & TRF_CLEANUP) {
+ if (trCopy.flags & TRF_CLEANUPFUNC)
+ trCopy.cleanupFunctionT((TCHAR*)callRes);
else if (trCopy.szCleanupService != NULL)
- CallService(trCopy.szCleanupService, 0, (LPARAM)callRes );
+ CallService(trCopy.szCleanupService, 0, (LPARAM)callRes);
}
- if (trCopy.flags & TRF_FREEMEM)
- if (trCopy.memType == TR_MEM_MIRANDA)
- mir_free(( void* )callRes );
+ if ((trCopy.flags & TRF_FREEMEM) && trCopy.memType == TR_MEM_MIRANDA)
+ mir_free((void*)callRes);
}
- LeaveCriticalSection(&csRegister);
return res;
}
-TOKENREGISTEREX* getTokenRegister( int i )
+TOKENREGISTEREX* getTokenRegister(int i)
{
- TOKENREGISTEREX *retVr;
-
- EnterCriticalSection(&csRegister);
- retVr = ( i >= tokens.count || i < 0 ) ? NULL : &tokens.items[i]->tr;
- LeaveCriticalSection( &csRegister );
- // beware! a pointer is returned here, no copy
- return retVr;
+ mir_cslock lck(csRegister);
+ return (i >= tokens.getCount() || i < 0) ? NULL : &tokens[i]->tr;
}
int getTokenRegisterCount()
{
- int retVal;
-
- EnterCriticalSection(&csRegister);
- retVal = tokens.count;
- LeaveCriticalSection(&csRegister);
-
- return retVal;
+ mir_cslock lck(csRegister);
+ return tokens.getCount();
}
/////////////////////////////////////////////////////////////////////////////////////////
-static int CompareTokens( const TokenRegisterEntry* p1, const TokenRegisterEntry* p2 )
-{
- if (p1->nameHash == p2->nameHash )
- return 0;
-
- return ( p1->nameHash < p2->nameHash ) ? -1 : 1;
-}
-
int initTokenRegister()
{
InitializeCriticalSection(&csRegister);
-
- tokens.sortFunc = ( FSortFunc )CompareTokens;
- tokens.increment = 100;
return 0;
}
int deinitTokenRegister()
{
- int i;
- EnterCriticalSection(&csRegister);
-
- for ( i=0; i < tokens.count; i++ ) {
- TokenRegisterEntry *tre = tokens.items[ i ];
- if (!( tre->tr.flags & TRF_PARSEFUNC ) && tre->tr.szService != NULL)
- mir_free( tre->tr.szService );
+ for (int i = 0; i < tokens.getCount(); i++) {
+ TokenRegisterEntry *tre = tokens[i];
+ if (!(tre->tr.flags & TRF_PARSEFUNC) && tre->tr.szService != NULL)
+ mir_free(tre->tr.szService);
if (tre->tr.tszTokenString != NULL)
- mir_free( tre->tr.tszTokenString );
+ mir_free(tre->tr.tszTokenString);
if (tre->tr.szHelpText != NULL)
- mir_free( tre->tr.szHelpText );
+ mir_free(tre->tr.szHelpText);
- if (( tre->tr.flags & TRF_CLEANUP ) && !( tre->tr.flags & TRF_CLEANUPFUNC ) && tre->tr.szCleanupService != NULL)
- mir_free( tre->tr.szCleanupService );
+ if ((tre->tr.flags & TRF_CLEANUP) && !(tre->tr.flags & TRF_CLEANUPFUNC) && tre->tr.szCleanupService != NULL)
+ mir_free(tre->tr.szCleanupService);
- mir_free( tre );
+ mir_free(tre);
}
- List_Destroy(( SortedList* )&tokens );
+ tokens.destroy();
- LeaveCriticalSection(&csRegister);
DeleteCriticalSection(&csRegister);
-
return 0;
}