MemoryHelper: update coding style

This commit is contained in:
Vesa
2014-04-26 18:10:36 +03:00
parent ff3e94bd75
commit e638bb8ae1
2 changed files with 24 additions and 27 deletions

View File

@@ -22,18 +22,18 @@
*/
#ifndef _MEMORY_HELPER_H_
#define _MEMORY_HELPER_H_
#ifndef MEMORY_HELPER_H
#define MEMORY_HELPER_H
/**
* Helper class to alocate aligned memory and free it.
*/
class MemoryHelper {
public:
static void* alignedMalloc(int);
static void alignedFree(void*);
static void* alignedMalloc( int );
static void alignedFree( void* );
private:
};

View File

@@ -27,42 +27,39 @@
#include "MemoryHelper.h"
/**
* Allocate a number of bytes and return them.
* @param _byteNum is the number of bytes
* Allocate a number of bytes and return them.
* @param byteNum is the number of bytes
*/
void* MemoryHelper::alignedMalloc(int _byteNum) {
char *ptr,*ptr2,*aligned_ptr;
int align_mask = ALIGN_SIZE- 1;
void* MemoryHelper::alignedMalloc( int byteNum )
{
char *ptr, *ptr2, *aligned_ptr;
int align_mask = ALIGN_SIZE - 1;
ptr = (char *) malloc(_byteNum + ALIGN_SIZE + sizeof(int));
ptr = static_cast<char*>( malloc( byteNum + ALIGN_SIZE + sizeof( int ) ) );
if(ptr==NULL) return(NULL);
if( ptr == NULL ) return NULL;
ptr2 = ptr + sizeof(int);
aligned_ptr = ptr2 + (ALIGN_SIZE- ((size_t)ptr2 & align_mask));
ptr2 = ptr + sizeof( int );
aligned_ptr = ptr2 + ( ALIGN_SIZE - ( ( size_t ) ptr2 & align_mask ) );
ptr2 = aligned_ptr - sizeof(int);
*((int *)ptr2)=(int)(aligned_ptr - ptr);
ptr2 = aligned_ptr - sizeof( int );
*( ( int* ) ptr2 ) = ( int )( aligned_ptr - ptr );
return(aligned_ptr);
return aligned_ptr;
}
/**
* Free an aligned buffer
* @param _buffer is the buffer to free
*/
void MemoryHelper::alignedFree(void* _buffer) {
if( _buffer != NULL )
void MemoryHelper::alignedFree( void* _buffer )
{
if( _buffer )
{
int *ptr2=(int *)_buffer - 1;
_buffer = (char *)_buffer - *ptr2;
free(_buffer);
int *ptr2 = static_cast<int*>( _buffer ) - 1;
_buffer = static_cast<char*>( _buffer ) - *ptr2;
free( _buffer );
}
}