Ignore:
Timestamp:
Sep 21, 2009, 11:48:42 AM (16 years ago)
Author:
Saskia Metzler <metzler@…>
Children:
14db08, 39d983
Parents:
bc3953
Message:

Ticket 11: use templates and/or traits to fix Malloc/ReAlloc-Free warnings in a clean manner

File:
1 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/helpers.cpp

    rbc3953 r390a2b  
    3434void debug_in(const char *output, const char *file, const int line) {}  // print nothing
    3535#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 }
    11536
    11637/** modulo operator for doubles.
     
    17091  }
    17192  // allocate string
    172   returnstring = (char *) Malloc(sizeof(char)*(order+2), "FixedDigitNumber: *returnstring");
     93  returnstring = Malloc<char>(order + 2, "FixedDigitNumber: *returnstring");
    17394  // terminate  and fill string array from end backward
    17495  returnstring[order] = '\0';
     
    196117};
    197118
     119/**
     120 * Allocates a memory range using malloc().
     121 * Prints the provided error message in case of a failure.
     122 *
     123 * \param number of memory slices of type X to allocate
     124 * \param failure message which is printed if the allocation fails
     125 * \return pointer to the allocated memory range, will be NULL if a failure occurred
     126 */
     127template <> char* Malloc<char>(size_t size, const char* output)
     128{
     129  char* buffer = NULL;
     130  buffer = (char*) malloc(sizeof(char) * (size + 1));
     131  for (size_t i = size; i--;)
     132    buffer[i] = (i % 2 == 0) ? 'p': 'c';
     133  buffer[size] = '\0';
    198134
     135  if (buffer == NULL)
     136    cout << Verbose(0) << "Malloc for datatype " << typeid(char).name()
     137      << " failed - pointer is NULL: " << output << endl;
     138
     139  return buffer;
     140};
     141
     142
Note: See TracChangeset for help on using the changeset viewer.