Changeset a33931 for src/helpers.cpp


Ignore:
Timestamp:
Oct 5, 2009, 4:05:53 PM (16 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
c26f44
Parents:
7dea7c (diff), c0917c (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'ConcaveHull' into ConvexHull

Conflicts:

.gitignore
molecuilder/src/Makefile.am
molecuilder/src/atom.cpp
molecuilder/src/tesselation.cpp

no serious overlaps, just a free Frees that were not present in ConcaveHull were MemoryAllocator class was added.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/helpers.cpp

    • Property mode changed from 100755 to 100644
    r7dea7c ra33931  
    66
    77#include "helpers.hpp"
     8#include "memoryusageobserver.hpp"
    89
    910/********************************************** helpful functions *********************************/
     
    3435void debug_in(const char *output, const char *file, const int line) {}  // print nothing
    3536#endif
    36 
    37 /** Wrapper for allocating'ing a memory range with *output if it fails.
    38  * \param size number of chars to alloc for \a *buffer
    39  * \param *output message if malloc fails
    40  * \return pointer to memory range
    41  */
    42 void * Malloc(size_t size, const char* output)
    43 {
    44   void *buffer = NULL;
    45   buffer = (void *)malloc(size); // alloc
    46   if (buffer == NULL)
    47     cout << Verbose(0) << "Malloc failed - pointer is NULL: " << output << endl;
    48   return(buffer);
    49 };
    50 
    51 /** Wrapper for allocating'ing a memory range with *output if it fails.
    52  * \param size number of chars to alloc for \a *buffer
    53  * \param *output message if malloc fails
    54  * \return pointer to memory range
    55  */
    56 void * Calloc(size_t size, const char* output)
    57 {
    58   void *buffer = NULL;
    59   buffer = (void *)calloc(size, (size_t)0); // alloc
    60   if (buffer == NULL)
    61     cout << Verbose(0) << "Calloc failed - pointer is NULL: " << output << endl;
    62   return(buffer);
    63 };
    64 
    65 /** Wrapper for reallocating'ing a memory range with *output if it fails.
    66  * \param *OldPointer pointer to old memory range
    67  * \param size number of chars to alloc for \a *buffer
    68  * \param *output message if malloc fails
    69  * \return pointer to memory range
    70  */
    71 void * ReAlloc(void * OldPointer, size_t size, const char* output)
    72 {
    73   void *buffer = NULL;
    74   if (OldPointer == NULL)
    75     //cout << Verbose(0) << "ReAlloc impossible - old is NULL: " << output << endl;
    76     buffer = (void *)malloc(size); // malloc
    77   else
    78     buffer = (void *)realloc(OldPointer, size); // realloc
    79   if (buffer == NULL)
    80     cout << Verbose(0) << "ReAlloc failed - new is NULL: " << output << endl;
    81   return(buffer);
    82 };
    83 
    84 /** Wrapper for free'ing an allocated memory range with *output if it fails.
    85  * \param *buffer pointer to buffer to be free'd
    86  * \param *output message if free fails
    87  */
    88 void Free(void ** buffer, const char* output)
    89 {
    90   if (*buffer == NULL) {
    91     //cout << Verbose(5) << "Free not necesary: " << output << endl;
    92   } else {
    93     free(*buffer);
    94     *buffer = NULL;
    95   }
    96 };
    97 
    98 /** Malloc string array and set its length to the allocated length.
    99  * \param *output message if malloc fails
    100  * \param size number of chars to alloc for \a *buffer
    101  * \return pointer to string array
    102  */
    103 char* MallocString(size_t size, const char* output)
    104 {
    105   size_t i;
    106   char *buffer;
    107   buffer = (char *)malloc(sizeof(char) * (size+1)); // alloc
    108   if (buffer == NULL)
    109     cout << Verbose(0) << output << endl;
    110   for (i=size;i--;)  // reset
    111     buffer[i] = i % 2 == 0 ? 'p': 'c';
    112   buffer[size] = '\0'; // and set length marker on its end
    113   return(buffer);
    114 }
    11537
    11638/** modulo operator for doubles.
     
    17092  }
    17193  // allocate string
    172   returnstring = (char *) Malloc(sizeof(char)*(order+2), "FixedDigitNumber: *returnstring");
     94  returnstring = Malloc<char>(order + 2, "FixedDigitNumber: *returnstring");
    17395  // terminate  and fill string array from end backward
    17496  returnstring[order] = '\0';
     
    196118};
    197119
     120/**
     121 * Allocates a memory range using malloc().
     122 * Prints the provided error message in case of a failure.
     123 *
     124 * \param number of memory slices of type X to allocate
     125 * \param failure message which is printed if the allocation fails
     126 * \return pointer to the allocated memory range, will be NULL if a failure occurred
     127 */
     128template <> char* Malloc<char>(size_t size, const char* output)
     129{
     130  char* buffer = NULL;
     131  buffer = (char*) malloc(sizeof(char) * (size + 1));
     132  for (size_t i = size; i--;)
     133    buffer[i] = (i % 2 == 0) ? 'p': 'c';
     134  buffer[size] = '\0';
    198135
     136  if (buffer != NULL) {
     137    MemoryUsageObserver::getInstance()->addMemory(buffer, size);
     138  } else {
     139    cout << Verbose(0) << "Malloc for datatype " << typeid(char).name()
     140      << " failed - pointer is NULL: " << output << endl;
     141  }
     142
     143  return buffer;
     144};
     145
     146
Note: See TracChangeset for help on using the changeset viewer.