
/**
 * @file cpputils.c 
 * provide the necessary implementations for using C++..
 * The module short name is "cpputils". 
 * This module provides the necessary functions needed to create and delete
 * objects in C++.
 * C++ files do not need to include this module,
 * but it needs to be compiled and linked to the project.
 * The rest of this file is empty. All the functionality is in the file @a cpputils.c <br>
 * <p> Here we start with the documentation: </p>
 * <p>from <a href="http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59453">
 * avr-c++ micro How To</a> <br>
 */

/*
** Include Files
*/
#include <stdlib.h>
#include "cpputils.h"

/*
** Constant Definitions
*/


/*
** Type Definitions
*/
__extension__ typedef int __guard __attribute__((mode (__DI__))); 


/*
** Macro Definitions
*/


/*
** Function Prototypes
*/

/**
 * The new operator.
 * This function will call malloc to allocate memory.
 * @param size the size of the object in bytes
 * @return the pointer to the allocated memory or @a Null if not successful.
 */
void * operator new(size_t size); 

/**
 * The delete operator.
 * This function will call free to freee the previously allocated memory with @a new.
 * @param ptr the pointer to the memory (address) where the allocation range starts.
 */
void operator delete(void * ptr); 


/*
These array handlers may be useful too: 

void * operator new[](size_t size) 
{ 
    return malloc(size); 
} 

void operator delete[](void * ptr) 
{ 
    free(ptr); 
} 
*/

/**
 * The aquire guard function.
 * In order to use templates, virtual inheritance etc. 
 * Additional guard functions are needed that are also provided by @a cpputils.c
 * @param g guard
 * @return int .
 */
extern "C" int __cxa_guard_acquire(__guard *); 

/**
 * The release guard function.
 * In order to use templates, virtual inheritance etc. 
 * Additional guard functions are needed that are also provided by @a cpputils.c
 * @param g guard
 */
extern "C" void __cxa_guard_release (__guard *); 

/**
 * The abort guard function.
 * In order to use templates, virtual inheritance etc. 
 * Additional guard functions are needed that are also provided by @a cpputils.c
 * @param g guard
 */
extern "C" void __cxa_guard_abort (__guard *); 

/**
 * The pure virtual function.
 * In order to use templates, virtual inheritance etc. 
 * Function needed for pure virtual functions.
 */
extern "C" void __cxa_pure_virtual(void); 


/*
** Variable Definitions
*/


/*
** Internal Functions
*/


/*
** Exported Functions
*/
void * operator new(size_t size){ 
	return malloc(size); 
} 

void operator delete(void * ptr){ 
	free(ptr); 
} 

int __cxa_guard_acquire(__guard * g) {
	return !*(char *)(g);
}
 
void __cxa_guard_release (__guard * g) {
	*(char *)g = 1;
}

void __cxa_guard_abort (__guard * g) {

} 

void __cxa_pure_virtual(void){

}
