Skip to content

Commit

Permalink
QgsMalloc, QgsCalloc, QgsFree
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Oct 9, 2012
1 parent 65141e2 commit 5f2a6a7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
Empty file modified scripts/astyle-all.sh
100644 → 100755
Empty file.
Empty file modified scripts/astyle-rollback.sh
100644 → 100755
Empty file.
Empty file modified scripts/astyle.sh
100644 → 100755
Empty file.
36 changes: 36 additions & 0 deletions src/core/qgis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#endif
#include <QCoreApplication>
#include "qgsconfig.h"
#include "qgslogger.h"

#include <ogr_api.h>

Expand Down Expand Up @@ -104,3 +105,38 @@ QString QGis::tr( QGis::UnitType unit )
{
return QCoreApplication::translate( "QGis::UnitType", qPrintable( toLiteral( unit ) ) );
}

void *QgsMalloc( size_t size )
{
if ( size == 0 || long( size ) < 0 )
{
QgsDebugMsg( QString( "Negative or zero size %1." ).arg( size ) );
return NULL;
}
void *p = malloc( size );
if ( p == NULL )
{
QgsDebugMsg( QString( "Allocation of %1 bytes failed." ).arg( size ) );
}
return p;
}

void *QgsCalloc( size_t nmemb, size_t size )
{
if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
{
QgsDebugMsg( QString( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
return NULL;
}
void *p = QgsMalloc( nmemb * size );
if ( p != NULL )
{
memset( p, 0, nmemb * size );
}
return p;
}

void QgsFree( void *ptr )
{
free( ptr );
}
20 changes: 20 additions & 0 deletions src/core/qgis.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <QEvent>
#include <QString>
#include <QMetaType>
#include <stdlib.h>
#include <cfloat>
#include <cmath>
#include <qnumeric.h>
Expand Down Expand Up @@ -172,6 +173,25 @@ inline bool doubleNearSig( double a, double b, int significantDigits = 10 )
qRound( ar * pow( 10.0, significantDigits ) ) == qRound( br * pow( 10.0, significantDigits ) ) ;
}

/** Allocates size bytes and returns a pointer to the allocated memory.
Works like C malloc() but prints debug message by QgsLogger if allocation fails.
@param size size in bytes
*/
void *QgsMalloc( size_t size );

/** Allocates memory for an array of nmemb elements of size bytes each and returns
a pointer to the allocated memory. Works like C calloc() but prints debug message
by QgsLogger if allocation fails.
@param nmemb number of elements
@param size size of element in bytes
*/
void *QgsCalloc( size_t nmemb, size_t size );

/** Frees the memory space pointed to by ptr. Works like C free().
@param ptr pointer to memory space
*/
void QgsFree( void *ptr );

/** Wkt string that represents a geographic coord sys
* @note added in 1.8 to replace GEOWkt
*/
Expand Down

0 comments on commit 5f2a6a7

Please sign in to comment.