Created
November 15, 2017 14:26
-
-
Save FedericoPonzi/ab4f1f0bf7f1c663478d0715dc18e31b to your computer and use it in GitHub Desktop.
Malloc/callloc ecc wrappers from bwa source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <errno.h> | |
#ifdef USE_MALLOC_WRAPPERS | |
/* Don't wrap ourselves */ | |
# undef USE_MALLOC_WRAPPERS | |
#endif | |
#include "malloc_wrap.h" | |
void *wrap_calloc(size_t nmemb, size_t size, | |
const char *file, unsigned int line, const char *func) { | |
void *p = calloc(nmemb, size); | |
if (NULL == p) { | |
fprintf(stderr, | |
"[%s] Failed to allocate %zu bytes at %s line %u: %s\n", | |
func, nmemb * size, file, line, strerror(errno)); | |
exit(EXIT_FAILURE); | |
} | |
return p; | |
} | |
void *wrap_malloc(size_t size, | |
const char *file, unsigned int line, const char *func) { | |
void *p = malloc(size); | |
if (NULL == p) { | |
fprintf(stderr, | |
"[%s] Failed to allocate %zu bytes at %s line %u: %s\n", | |
func, size, file, line, strerror(errno)); | |
exit(EXIT_FAILURE); | |
} | |
return p; | |
} | |
void *wrap_realloc(void *ptr, size_t size, | |
const char *file, unsigned int line, const char *func) { | |
void *p = realloc(ptr, size); | |
if (NULL == p) { | |
fprintf(stderr, | |
"[%s] Failed to allocate %zu bytes at %s line %u: %s\n", | |
func, size, file, line, strerror(errno)); | |
exit(EXIT_FAILURE); | |
} | |
return p; | |
} | |
char *wrap_strdup(const char *s, | |
const char *file, unsigned int line, const char *func) { | |
char *p = strdup(s); | |
if (NULL == p) { | |
fprintf(stderr, | |
"[%s] Failed to allocate %zu bytes at %s line %u: %s\n", | |
func, strlen(s), file, line, strerror(errno)); | |
exit(EXIT_FAILURE); | |
} | |
return p; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MALLOC_WRAP_H | |
#define MALLOC_WRAP_H | |
#include <stdlib.h> /* Avoid breaking the usual definitions */ | |
#include <string.h> | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
void *wrap_calloc(size_t nmemb, size_t size, | |
const char *file, unsigned int line, const char *func); | |
void *wrap_malloc(size_t size, | |
const char *file, unsigned int line, const char *func); | |
void *wrap_realloc(void *ptr, size_t size, | |
const char *file, unsigned int line, const char *func); | |
char *wrap_strdup(const char *s, | |
const char *file, unsigned int line, const char *func); | |
#ifdef __cplusplus | |
} | |
#endif | |
#ifdef USE_MALLOC_WRAPPERS | |
# ifdef calloc | |
# undef calloc | |
# endif | |
# define calloc(n, s) wrap_calloc( (n), (s), __FILE__, __LINE__, __func__) | |
# ifdef malloc | |
# undef malloc | |
# endif | |
# define malloc(s) wrap_malloc( (s), __FILE__, __LINE__, __func__) | |
# ifdef realloc | |
# undef realloc | |
# endif | |
# define realloc(p, s) wrap_realloc((p), (s), __FILE__, __LINE__, __func__) | |
# ifdef strdup | |
# undef strdup | |
# endif | |
# define strdup(s) wrap_strdup( (s), __FILE__, __LINE__, __func__) | |
#endif /* USE_MALLOC_WRAPPERS */ | |
#endif /* MALLOC_WRAP_H */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment