Feature #331 ยป 0030-mem.-ch-Replace-NULLs-with-nullptrs.patch
| utility/mem.c | ||
|---|---|---|
| #include "mem.h" | ||
| /******************************************************************//** | ||
|   Do whatever we should do when malloc fails. | ||
|   Do whatever we should do when fc_malloc() fails. | ||
|   At the moment this just prints a log message and calls exit(EXIT_FAILURE) | ||
| **********************************************************************/ | ||
| static void handle_alloc_failure(size_t size, const char *called_as, | ||
| ... | ... | |
| #endif /* FREECIV_DEBUG */ | ||
| /******************************************************************//** | ||
|   Function used by fc_malloc macro, malloc() replacement | ||
|   Function used by fc_malloc() macro, malloc() replacement | ||
|   There's no need for the caller to check return value; this function will | ||
|   always return a valid pointer (even for a 0-byte malloc). | ||
| ... | ... | |
|   sanity_check_size(size, called_as, line, file); | ||
| #endif /* FREECIV_DEBUG */ | ||
|   /* Some systems return NULL on malloc(0) | ||
|   /* Some systems return nullptr on malloc(0) | ||
|    * According to ANSI C, the return is implementation-specific,  | ||
|    * this is a safe guard. Having the extra byte is, of course, harmless. */ | ||
| #ifndef MALLOC_ZERO_OK | ||
| ... | ... | |
| #endif | ||
|   ptr = malloc(size); | ||
|   if (ptr == NULL) { | ||
|   if (ptr == nullptr) { | ||
|     handle_alloc_failure(size, called_as, line, file); | ||
|   } | ||
| ... | ... | |
| } | ||
| /******************************************************************//** | ||
|  Function used by fc_realloc macro, realloc() replacement | ||
|  No need to check return value. | ||
|   Function used by fc_realloc() macro, realloc() replacement | ||
|   No need to check return value. | ||
| **********************************************************************/ | ||
| void *fc_real_realloc(void *ptr, size_t size, | ||
|                       const char *called_as, int line, const char *file) | ||
| ... | ... | |
| } | ||
| /******************************************************************//** | ||
|   Function used by fc_calloc macro, calloc() replacement | ||
|   Function used by fc_calloc() macro, calloc() replacement | ||
|   No need to check return value. | ||
|   I'm pretty sure only the product of nelem and elsize can ever | ||
| ... | ... | |
| } | ||
| /******************************************************************//** | ||
|   Function used by fc_strdup macro, strdup() replacement | ||
|   Function used by fc_strdup() macro, strdup() replacement | ||
|   No need to check return value. | ||
| **********************************************************************/ | ||
| char *real_fc_strdup(const char *str, | ||
| ... | ... | |
| { | ||
|   char *dest = fc_real_malloc(strlen(str) + 1, called_as, line, file); | ||
|   /* No need to check whether dest is non-NULL! */ | ||
|   /* No need to check whether dest is nullptr! */ | ||
|   strcpy(dest, str); | ||
|   return dest; | ||
| utility/mem.h | ||
|---|---|---|
| /* fc_malloc(), fc_realloc(), fc_calloc(): | ||
|  * fc_ stands for freeciv; the return value is checked, | ||
|  * and freeciv-specific processing occurs if it is NULL: | ||
|  * and freeciv-specific processing occurs if it is nullptr: | ||
|  * a log message, possibly cleanup, and ending with exit(EXIT_FAILURE) | ||
|  */ | ||
| ... | ... | |
| #define fc_calloc(n,esz)   fc_real_calloc((n), (esz), "calloc", \ | ||
|                                           __FC_LINE__, __FILE__) | ||
| #define FC_FREE(ptr)       do { free(ptr); (ptr) = NULL; } while (FALSE) | ||
| #define FC_FREE(ptr)       do { free(ptr); (ptr) = nullptr; } while (FALSE) | ||
| #define fc_strdup(str) real_fc_strdup((str), "strdup", __FC_LINE__, __FILE__) | ||