/*
xmd - molecular dynamics for metals and ceramics
By Jonathan Rifkin <jon.rifkin@uconn.edu>
Copyright 1995-2004 Jonathan Rifkin
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef __CDHOUSE_H
#define __CDHOUSE_H
/*
************************************************************************
Function Prototypes
************************************************************************
*/
void StartProgramTimer(void);
void CleanAfterError(void);
void CleanBeforeEnd(void);
char *GetCurrentTimeStr (void);
/*
************************************************************************
Debugging Macros
************************************************************************
*/
/*
Debugging Macro: Print current file and line number
*/
#define WRITEMSG \
{ \
if (Debug_g) \
printf ("In file %s at line %i.\n", __FILE__, __LINE__); \
fflush (stdin); \
}
/*
Debugging Macro: Test assertion, if failed list file and line number
*/
#define ASSERT(TEST) if (!(TEST)) \
printf ("INTERNAL ERROR: Failed assertion (%s) in file %s line %i.\n", \
#TEST, __FILE__, __LINE__);
/*
Debugging Macro: Print out variable name and value
*/
#define WRITEVAR(VAR_NAME,VAR_TYPE) \
if (Debug_g) \
{ \
printf ("FILE %s LINE %i :", __FILE__, __LINE__); \
printf ("%s = ", #VAR_NAME); \
printf (#VAR_TYPE, (VAR_NAME) ); \
printf ("\n"); \
fflush (stdin); \
}
/*
Debugging Macro: Print out debugging note
*/
#define WRITENOT(MSG) \
if (Debug_g) \
{ \
printf ("FILE %s LINE %i :", __FILE__, __LINE__); \
printf (" (%s)\n", MSG); \
}
/*
Debugging Macro: Use BORLAND C++ DOS routine for checking memory heap
*/
#if 0
#define CHECK_HEAP \
if (heapcheck()<0) \
{ \
printf ("*** INTERNAL ERROR in %s line %i. ", __FILE__, __LINE__); \
printf ("Memory heap is corrupted.\n"); \
}
#else
#define CHECK_HEAP
#endif
/*
************************************************************************
Utility Macros
************************************************************************
*/
/*
NOTE:
The following macro replaces the standard C for(;;) construct
If you prefer to replace all occurances of this macro with
the corresponding for(;;) statement, copy the following
4 line perl script to the file fixLOOP.pl, run as follows.
chmod u+x fixLOOP.pl
fixLOOP.pl *.c
rm *.bak (to remove backup files)
This will edit in place all the *.c files and replace
LOOP with for().
------ start of script ------------------------------
#!/usr/bin/perl -pi.bak
if (!m/^#define/) {
s/LOOP\s*\(\s*([^,]*?)\s*,\s*([^)]*?)\s*\)/for ( $1=0; $1<$2; $1++ )/;
}
------ end of script --------------------------------
*/
/* Simple loop (this macro helps reduce typos) */
#define LOOP(INDEX,LIMIT) \
for (INDEX=0; INDEX<(LIMIT); INDEX++)
/*
Allocate array and test for NULL allocations
If NULL allocation then print error and call CleanAfterError()
*/
#define ALLOCATE(VAR,TYPE,NUM) \
{ \
if (VAR != NULL) \
{ \
printf ("FILE %s LINE %i: ALLOCATE is freeing variable %s\n", \
__FILE__, __LINE__, #VAR); \
if (CheckMem_g) \
printf ("CHECK MEM FREE %s %p %s %i\n", \
#VAR, VAR, __FILE__, __LINE__); \
free (VAR); \
VAR = NULL; \
} \
if (NUM!=0) \
{ \
VAR = (TYPE *) calloc ( (NUM), sizeof ( TYPE ) ); \
if (CheckMem_g) \
printf ("CHECK MEM ALLO %s %p %s %i %li\n", \
#VAR, VAR, __FILE__, __LINE__, \
( (long) (NUM) ) * sizeof(TYPE) ); \
if ((VAR) == NULL) \
{ \
printf ("ERROR: FILE %s LINE %i\n", __FILE__, __LINE__); \
printf (" Cannot allocate variable %s.\n", #VAR); \
printf (" Number of Elements %li\n", (long) NUM); \
printf (" Size of Elements %li\n", sizeof(TYPE)); \
CleanAfterError(); \
} \
} \
}
/*
Reallocate storage
- if new storage is zero, call free() and set pointer to NULL
- if old storage was zero, call calloc()
- if new storage > old, call realloc() and initialize new storage
- if new storage < old, call realloc()
*/
#define REALLOC(VAR,TYPE,OLD_NUM,NEW_NUM) \
{ \
/* Free existing pointer */ \
if ((NEW_NUM)==0) \
{ \
if ((VAR)!=NULL) \
{ \
if (CheckMem_g) \
printf ("CHECK MEM FREE(R0) %s %p %s %i\n", \
#VAR, VAR, __FILE__, __LINE__); \
free (VAR); \
(VAR) = NULL; \
} \
} \
/* Get new storage */ \
else \
{ \
if ((VAR)==NULL) \
{ \
(VAR) = (TYPE *) calloc ( (NEW_NUM), sizeof (TYPE) ); \
if (CheckMem_g) \
printf ("CHECK MEM ALLO(R) %s %p %s %i %li\n", \
#VAR, VAR, __FILE__, __LINE__, \
( (long) (NEW_NUM) ) * sizeof(TYPE) ); \
} \
else \
{ \
if (CheckMem_g) \
printf ("CHECK MEM FREE(R1) %s %p %s %i\n", \
#VAR, VAR, __FILE__, __LINE__); \
(VAR) = (TYPE *) realloc ( (VAR), (NEW_NUM) * sizeof (TYPE) ) ; \
if (CheckMem_g) \
printf ("CHECK MEM REALLO %s %p %s %i %li\n", \
#VAR, VAR, __FILE__, __LINE__, \
( (long) (NEW_NUM) ) * sizeof(TYPE) ); \
} \
if ((VAR) == NULL) \
{ \
printf ("ERROR\n"); \
printf (" Cannot allocate variable %s.\n", #VAR); \
printf (" Number of Elements %i\n", (NEW_NUM)); \
printf (" Size of Elements %li\n", sizeof(TYPE)); \
CleanAfterError(); \
} \
if ((NEW_NUM)>(OLD_NUM)) \
memset ( (VAR)+(OLD_NUM), 0, ((NEW_NUM)-(OLD_NUM)) * sizeof(TYPE)); \
} \
}
#define FREE(VAR) \
if ((VAR)!=NULL) \
{ \
if (CheckMem_g) \
printf ("CHECK MEM FREE %s %p %s %i\n", \
#VAR, VAR, __FILE__, __LINE__); \
free (VAR); \
(VAR) = NULL; \
}
/* Error Message Prefix */
#define ERROR_PREFIX printf("\nERROR xmd:%s:%i: ", __FILE__, __LINE__);
/* Square a token */
#define SQR(X) ((X)*(X))
/* Get combined type */
#define COMBINED_TYPE(T1,T2) \
((T1) > (T2) ? ((T1)*((T1)+1)/2 + (T2)) : ((T2)*((T2)+1)/2 + (T1)))
/*
************************************************************************
Global Variables
************************************************************************
*/
/*
Do not define Debug_g if header is called from cdhouse.c
*/
#ifndef IS_CDHOUSE_H
extern int Debug_g;
extern int CheckMem_g;
#endif
#endif