Always use build + use our own allocator functions
- Consistent across platforms and will allow some things macros dont and memory checkers these days are smart enough to get decent output anyway - Minimal namespacing with r-prefix, add compatibility macros to system.h for now so we dont have to change the entire codebase for this - Also make rpmutil.h where the declarations and gcc __attribute__ macros are available everywhere
This commit is contained in:
parent
3ef73d4d9d
commit
1492581139
|
@ -9,16 +9,14 @@
|
|||
#define EXIT_FAILURE 1
|
||||
#endif
|
||||
|
||||
void *vmefail(size_t size)
|
||||
static void *vmefail(size_t size)
|
||||
{
|
||||
fprintf(stderr, _("memory alloc (%u bytes) returned NULL.\n"), (unsigned)size);
|
||||
exit(EXIT_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if !(HAVE_MCHECK_H && defined(__GNUC__))
|
||||
|
||||
void * xmalloc (size_t size)
|
||||
void * rmalloc (size_t size)
|
||||
{
|
||||
register void *value;
|
||||
if (size == 0) size++;
|
||||
|
@ -28,7 +26,7 @@ void * xmalloc (size_t size)
|
|||
return value;
|
||||
}
|
||||
|
||||
void * xcalloc (size_t nmemb, size_t size)
|
||||
void * rcalloc (size_t nmemb, size_t size)
|
||||
{
|
||||
register void *value;
|
||||
if (size == 0) size++;
|
||||
|
@ -39,7 +37,7 @@ void * xcalloc (size_t nmemb, size_t size)
|
|||
return value;
|
||||
}
|
||||
|
||||
void * xrealloc (void *ptr, size_t size)
|
||||
void * rrealloc (void *ptr, size_t size)
|
||||
{
|
||||
register void *value;
|
||||
if (size == 0) size++;
|
||||
|
@ -49,7 +47,7 @@ void * xrealloc (void *ptr, size_t size)
|
|||
return value;
|
||||
}
|
||||
|
||||
char * xstrdup (const char *str)
|
||||
char * rstrdup (const char *str)
|
||||
{
|
||||
size_t size = strlen(str) + 1;
|
||||
char *newstr = (char *) malloc (size);
|
||||
|
@ -59,4 +57,8 @@ char * xstrdup (const char *str)
|
|||
return newstr;
|
||||
}
|
||||
|
||||
#endif /* !(HAVE_MCHECK_H && defined(__GNUC__)) */
|
||||
void * rfree (void *ptr)
|
||||
{
|
||||
free(ptr);
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef _RPMUTIL_H
|
||||
#define _RPMUTIL_H
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
/*
|
||||
* Miscellanous utility macros:
|
||||
* - portability wrappers for various gcc extensions like __attribute__()
|
||||
|
@ -32,6 +34,14 @@
|
|||
#define RPM_GNUC_MALLOC
|
||||
#endif
|
||||
|
||||
#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
|
||||
#define RPM_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))
|
||||
#define RPM_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y)))
|
||||
#else
|
||||
#define RPM_GNUC_ALLOC_SIZE(x)
|
||||
#define RPM_GNUC_ALLOC_SIZE2(x,y)
|
||||
#endif
|
||||
|
||||
#if __GNUC__ >= 4
|
||||
#define RPM_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
|
||||
#else
|
||||
|
@ -102,4 +112,19 @@
|
|||
# define RPM_END_DECLS
|
||||
#endif
|
||||
|
||||
/* Rpm specific allocators which never return NULL but terminate on failure */
|
||||
RPM_GNUC_MALLOC RPM_GNUC_ALLOC_SIZE(1)
|
||||
void * rmalloc(size_t size);
|
||||
|
||||
RPM_GNUC_MALLOC RPM_GNUC_ALLOC_SIZE2(1,2)
|
||||
void * rcalloc(size_t nmemb, size_t size);
|
||||
|
||||
RPM_GNUC_ALLOC_SIZE(2)
|
||||
void * rrealloc(void *ptr, size_t size);
|
||||
|
||||
char * rstrdup(const char *str);
|
||||
|
||||
/* Rpm specific free() which returns NULL */
|
||||
void * rfree(void *ptr);
|
||||
|
||||
#endif /* _RPMUTIL_H */
|
||||
|
|
65
system.h
65
system.h
|
@ -225,18 +225,6 @@ typedef char * security_context_t;
|
|||
#define rpm_execcon(_v, _fn, _av, _envp) (0)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Wrapper to free(3), permit NULL, return NULL.
|
||||
* @param p memory to free
|
||||
* @return NULL always
|
||||
*/
|
||||
static inline
|
||||
void * _free(void * p)
|
||||
{
|
||||
if (p != NULL) free(p);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if WITH_CAP
|
||||
#include <sys/capability.h>
|
||||
#else
|
||||
|
@ -247,54 +235,19 @@ typedef void * cap_t;
|
|||
#include <acl/libacl.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Wrapper to free(3), permit NULL, return NULL.
|
||||
* For documenting cases where const is used to protect long-lived
|
||||
* non-const data that's supposed to be freed.
|
||||
* @param p memory to free
|
||||
* @return NULL always
|
||||
*/
|
||||
static inline
|
||||
void * _constfree(const void * p)
|
||||
{
|
||||
if (p != NULL) free((void *)p);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* FIX: these are macros */
|
||||
/**
|
||||
*/
|
||||
void * xmalloc (size_t size);
|
||||
|
||||
/**
|
||||
*/
|
||||
void * xcalloc (size_t nmemb, size_t size);
|
||||
|
||||
/**
|
||||
* @todo Annotate ptr with returned/out.
|
||||
*/
|
||||
void * xrealloc (void * ptr,
|
||||
size_t size);
|
||||
|
||||
/**
|
||||
*/
|
||||
char * xstrdup (const char *str);
|
||||
|
||||
/**
|
||||
*/
|
||||
void * vmefail(size_t size);
|
||||
|
||||
#if HAVE_MCHECK_H
|
||||
#include <mcheck.h>
|
||||
/* Memory allocation via macro defs to get meaningful locations from mtrace() */
|
||||
#if defined(__GNUC__)
|
||||
#define xmalloc(_size) (malloc(_size) ? : vmefail(_size))
|
||||
#define xcalloc(_nmemb, _size) (calloc((_nmemb), (_size)) ? : vmefail(_size))
|
||||
#define xrealloc(_ptr, _size) (realloc((_ptr), (_size)) ? : vmefail(_size))
|
||||
#define xstrdup(_str) (strcpy((malloc(strlen(_str)+1) ? : vmefail(strlen(_str)+1)), (_str)))
|
||||
#endif /* defined(__GNUC__) */
|
||||
#endif /* HAVE_MCHECK_H */
|
||||
|
||||
#include "rpmio/rpmutil.h"
|
||||
/* compatibility macros to avoid a mass-renaming all over the codebase */
|
||||
#define xmalloc(_size) rmalloc((_size))
|
||||
#define xcalloc(_nmemb, _size) rcalloc((_nmemb), (_size))
|
||||
#define xrealloc(_ptr, _size) rrealloc((_ptr), (_size))
|
||||
#define xstrdup(_str) rstrdup((_str))
|
||||
#define _free(_ptr) rfree((_ptr))
|
||||
#define _constfree(_ptr) rfree((void *)(_ptr))
|
||||
|
||||
/* Retrofit glibc __progname */
|
||||
#if defined __GLIBC__ && __GLIBC__ >= 2
|
||||
#if __GLIBC_MINOR__ >= 1
|
||||
|
|
Loading…
Reference in New Issue