Skip to content

Commit 5f2a6a7

Browse files
committed
QgsMalloc, QgsCalloc, QgsFree
1 parent 65141e2 commit 5f2a6a7

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

scripts/astyle-all.sh

100644100755
File mode changed.

scripts/astyle-rollback.sh

100644100755
File mode changed.

scripts/astyle.sh

100644100755
File mode changed.

src/core/qgis.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#endif
2222
#include <QCoreApplication>
2323
#include "qgsconfig.h"
24+
#include "qgslogger.h"
2425

2526
#include <ogr_api.h>
2627

@@ -104,3 +105,38 @@ QString QGis::tr( QGis::UnitType unit )
104105
{
105106
return QCoreApplication::translate( "QGis::UnitType", qPrintable( toLiteral( unit ) ) );
106107
}
108+
109+
void *QgsMalloc( size_t size )
110+
{
111+
if ( size == 0 || long( size ) < 0 )
112+
{
113+
QgsDebugMsg( QString( "Negative or zero size %1." ).arg( size ) );
114+
return NULL;
115+
}
116+
void *p = malloc( size );
117+
if ( p == NULL )
118+
{
119+
QgsDebugMsg( QString( "Allocation of %1 bytes failed." ).arg( size ) );
120+
}
121+
return p;
122+
}
123+
124+
void *QgsCalloc( size_t nmemb, size_t size )
125+
{
126+
if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
127+
{
128+
QgsDebugMsg( QString( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
129+
return NULL;
130+
}
131+
void *p = QgsMalloc( nmemb * size );
132+
if ( p != NULL )
133+
{
134+
memset( p, 0, nmemb * size );
135+
}
136+
return p;
137+
}
138+
139+
void QgsFree( void *ptr )
140+
{
141+
free( ptr );
142+
}

src/core/qgis.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <QEvent>
2222
#include <QString>
2323
#include <QMetaType>
24+
#include <stdlib.h>
2425
#include <cfloat>
2526
#include <cmath>
2627
#include <qnumeric.h>
@@ -172,6 +173,25 @@ inline bool doubleNearSig( double a, double b, int significantDigits = 10 )
172173
qRound( ar * pow( 10.0, significantDigits ) ) == qRound( br * pow( 10.0, significantDigits ) ) ;
173174
}
174175

176+
/** Allocates size bytes and returns a pointer to the allocated memory.
177+
Works like C malloc() but prints debug message by QgsLogger if allocation fails.
178+
@param size size in bytes
179+
*/
180+
void *QgsMalloc( size_t size );
181+
182+
/** Allocates memory for an array of nmemb elements of size bytes each and returns
183+
a pointer to the allocated memory. Works like C calloc() but prints debug message
184+
by QgsLogger if allocation fails.
185+
@param nmemb number of elements
186+
@param size size of element in bytes
187+
*/
188+
void *QgsCalloc( size_t nmemb, size_t size );
189+
190+
/** Frees the memory space pointed to by ptr. Works like C free().
191+
@param ptr pointer to memory space
192+
*/
193+
void QgsFree( void *ptr );
194+
175195
/** Wkt string that represents a geographic coord sys
176196
* @note added in 1.8 to replace GEOWkt
177197
*/

0 commit comments

Comments
 (0)