blob: f0f5b751e7810287dff322de96c7c8c98940e977 [file] [log] [blame]
#include "THGeneral.h"
#ifndef TH_HAVE_THREAD
#define __thread
#endif
/* Torch Error Handling */
static void defaultTorchErrorHandlerFunction(const char *msg, void *data)
{
printf("$ Error: %s\n", msg);
exit(-1);
}
static __thread void (*torchErrorHandlerFunction)(const char *msg, void *data) = defaultTorchErrorHandlerFunction;
static __thread void *torchErrorHandlerData;
void THError(const char *fmt, ...)
{
char msg[1024];
va_list args;
/* vasprintf not standard */
/* vsnprintf: how to handle if does not exists? */
va_start(args, fmt);
vsnprintf(msg, 1024, fmt, args);
va_end(args);
(*torchErrorHandlerFunction)(msg, torchErrorHandlerData);
}
void THSetErrorHandler( void (*torchErrorHandlerFunction_)(const char *msg, void *data), void *data )
{
if(torchErrorHandlerFunction_)
torchErrorHandlerFunction = torchErrorHandlerFunction_;
else
torchErrorHandlerFunction = defaultTorchErrorHandlerFunction;
torchErrorHandlerData = data;
}
/* Torch Arg Checking Handling */
static void defaultTorchArgErrorHandlerFunction(int argNumber, const char *msg, void *data)
{
if(msg)
printf("$ Invalid argument %d: %s\n", argNumber, msg);
else
printf("$ Invalid argument %d\n", argNumber);
exit(-1);
}
static __thread void (*torchArgErrorHandlerFunction)(int argNumber, const char *msg, void *data) = defaultTorchArgErrorHandlerFunction;
static __thread void *torchArgErrorHandlerData;
void THArgCheck(int condition, int argNumber, const char *msg)
{
if(!condition)
(*torchArgErrorHandlerFunction)(argNumber, msg, torchArgErrorHandlerData);
}
void THSetArgErrorHandler( void (*torchArgErrorHandlerFunction_)(int argNumber, const char *msg, void *data), void *data )
{
if(torchArgErrorHandlerFunction_)
torchArgErrorHandlerFunction = torchArgErrorHandlerFunction_;
else
torchArgErrorHandlerFunction = defaultTorchArgErrorHandlerFunction;
torchArgErrorHandlerData = data;
}
void* THAlloc(long size)
{
void *ptr;
if(size < 0)
THError("$ Torch: invalid memory size -- maybe an overflow?");
if(size == 0)
return NULL;
ptr = malloc(size);
if(!ptr)
THError("$ Torch: not enough memory: you tried to allocate %dGB. Buy new RAM!", size/1073741824);
return ptr;
}
void* THRealloc(void *ptr, long size)
{
if(!ptr)
return(THAlloc(size));
if(size == 0)
{
THFree(ptr);
return NULL;
}
if(size < 0)
THError("$ Torch: invalid memory size -- maybe an overflow?");
ptr = realloc(ptr, size);
if(!ptr)
THError("$ Torch: not enough memory: you tried to reallocate %dGB. Buy new RAM!", size/1073741824);
return ptr;
}
void THFree(void *ptr)
{
free(ptr);
}
double THLog1p(const double x)
{
#ifdef _MSC_VER
volatile double y = 1 + x;
return log(y) - ((y-1)-x)/y ; /* cancels errors with IEEE arithmetic */
#else
return log1p(x);
#endif
}