Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/helpers.cpp

    ra67d19 r112b09  
    44 */
    55
     6#include "Helpers/MemDebug.hpp"
    67
    78#include "helpers.hpp"
     9#include "Helpers/fast_functions.hpp"
    810#include "log.hpp"
    911#include "memoryusageobserver.hpp"
     
    5153};
    5254
    53 /** Returns the power of \a n with respect to \a base.
    54  * \param base basis
    55  * \param n power
    56  * \return \f$base^n\f$
    57  */
    58 int pot(int base, int n)
    59 {
    60   int res = 1;
    61   int j;
    62   for (j=n;j--;)
    63     res *= base;
    64   return res;
    65 };
    66 
    6755/** Counts lines in file.
    6856 * Note we are scanning lines from current position, not from beginning.
     
    7159int CountLinesinFile(ifstream &InputFile)
    7260{
    73   char *buffer = Malloc<char>(MAXSTRINGSIZE, "CountLinesinFile: *buffer");
     61  char *buffer = new char[MAXSTRINGSIZE];
    7462  int lines=0;
    7563
     
    8371  }
    8472  InputFile.seekg(PositionMarker, ios::beg);
    85   Free(&buffer);
     73  delete[](buffer);
    8674  return lines;
    8775};
     
    10391  }
    10492  // allocate string
    105   returnstring = Malloc<char>(order + 2, "FixedDigitNumber: *returnstring");
     93  returnstring = new char[order + 2];
    10694  // terminate  and fill string array from end backward
    10795  returnstring[order] = '\0';
     
    135123double * ReturnFullMatrixforSymmetric(const double * const symm)
    136124{
    137   double *matrix = Malloc<double>(NDIM * NDIM, "molecule::ReturnFullMatrixforSymmetric: *matrix");
     125  double *matrix = new double[NDIM * NDIM];
    138126  matrix[0] = symm[0];
    139127  matrix[1] = symm[1];
     
    153141double * InverseMatrix( const double * const A)
    154142{
    155   double *B = Malloc<double>(NDIM * NDIM, "Vector::InverseMatrix: *B");
     143  double *B = new double[NDIM * NDIM];
    156144  double detA = RDET3(A);
    157145  double detAReci;
     
    175163};
    176164
    177 /** hard-coded determinant of a 3x3 matrix.
    178  * \param a[9] matrix
    179  * \return \f$det(a)\f$
    180  */
    181 double RDET3(const double a[NDIM*NDIM])
    182 {
    183   return ((a)[0]*(a)[4]*(a)[8] + (a)[3]*(a)[7]*(a)[2] + (a)[6]*(a)[1]*(a)[5] - (a)[2]*(a)[4]*(a)[6] - (a)[5]*(a)[7]*(a)[0] - (a)[8]*(a)[1]*(a)[3]);
    184 };
    185165
    186 /** hard-coded determinant of a 2x2 matrix.
    187  * \param a[4] matrix
    188  * \return \f$det(a)\f$
    189  */
    190 double RDET2(const double a[4])
    191 {
    192   return ((a[0])*(a[3])-(a[1])*(a[2]));
    193 };
    194 
    195 /** hard-coded determinant of a 2x2 matrix.
    196  * \param a0 (0,0) entry of matrix
    197  * \param a1 (0,1) entry of matrix
    198  * \param a2 (1,0) entry of matrix
    199  * \param a3 (1,1) entry of matrix
    200  * \return \f$det(a)\f$
    201  */
    202 double RDET2(const double a0, const double a1, const double a2, const double a3)
    203 {
    204   return ((a0)*(a3)-(a1)*(a2));
    205 };
    206166
    207167/** Comparison function for GSL heapsort on distances in two molecules.
     
    221181
    222182
    223 /** Allocates a memory range using malloc().
    224  * Prints the provided error message in case of a failure.
    225  *
    226  * \param number of memory slices of type X to allocate
    227  * \param failure message which is printed if the allocation fails
    228  * \return pointer to the allocated memory range, will be NULL if a failure occurred
    229  */
    230 template <> char* Malloc<char>(size_t size, const char* output)
    231 {
    232   char* buffer = NULL;
    233   buffer = (char*) malloc(sizeof(char) * (size + 1));
    234   for (size_t i = size; i--;)
    235     buffer[i] = (i % 2 == 0) ? 'p': 'c';
    236   buffer[size] = '\0';
    237 
    238   if (buffer != NULL) {
    239     MemoryUsageObserver::getInstance()->addMemory(buffer, size);
    240   } else {
    241     Log() << Verbose(0) << "Malloc for datatype " << typeid(char).name()
    242       << " failed - pointer is NULL: " << output << endl;
    243   }
    244 
    245   return buffer;
    246 };
    247 
    248183/**
    249  * Frees all memory registered by the memory observer and calls exit(225) afterwards.
     184 * Calls exit(255).
    250185 */
    251186void performCriticalExit() {
    252   map<void*, size_t> pointers = MemoryUsageObserver::getInstance()->getPointersToAllocatedMemory();
    253   for (map<void*, size_t>::iterator runner = pointers.begin(); runner != pointers.end(); runner++) {
    254     Free(((void**) &runner->first));
    255   }
    256 
    257187  exit(255);
    258188}
Note: See TracChangeset for help on using the changeset viewer.