Clean {X,XNF}{alloc,calloc,realloc,free,strdup} from pre-C89 baggage
C89 guarantees alignment of pointers returned from malloc/calloc/realloc, so stop fiddling with alignment manually and just pass the arguments to library functions. Also convert silent error when negative size is passed into function into warning in log file. Signed-off-by: Mikhail Gusarov <dottedmag@dottedmag.net> Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
parent
4f0006c220
commit
e983848ab4
14
include/os.h
14
include/os.h
|
@ -214,15 +214,15 @@ extern _X_EXPORT int set_font_authorizations(
|
||||||
|
|
||||||
#ifndef _HAVE_XALLOC_DECLS
|
#ifndef _HAVE_XALLOC_DECLS
|
||||||
#define _HAVE_XALLOC_DECLS
|
#define _HAVE_XALLOC_DECLS
|
||||||
extern _X_EXPORT pointer Xalloc(unsigned long /*amount*/);
|
extern _X_EXPORT void *Xalloc(unsigned long /*amount*/);
|
||||||
extern _X_EXPORT pointer Xcalloc(unsigned long /*amount*/);
|
extern _X_EXPORT void *Xcalloc(unsigned long /*amount*/);
|
||||||
extern _X_EXPORT pointer Xrealloc(pointer /*ptr*/, unsigned long /*amount*/);
|
extern _X_EXPORT void *Xrealloc(void * /*ptr*/, unsigned long /*amount*/);
|
||||||
extern _X_EXPORT void Xfree(pointer /*ptr*/);
|
extern _X_EXPORT void Xfree(void * /*ptr*/);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern _X_EXPORT pointer XNFalloc(unsigned long /*amount*/);
|
extern _X_EXPORT void *XNFalloc(unsigned long /*amount*/);
|
||||||
extern _X_EXPORT pointer XNFcalloc(unsigned long /*amount*/);
|
extern _X_EXPORT void *XNFcalloc(unsigned long /*amount*/);
|
||||||
extern _X_EXPORT pointer XNFrealloc(pointer /*ptr*/, unsigned long /*amount*/);
|
extern _X_EXPORT void *XNFrealloc(void * /*ptr*/, unsigned long /*amount*/);
|
||||||
|
|
||||||
extern _X_EXPORT char *Xstrdup(const char *s);
|
extern _X_EXPORT char *Xstrdup(const char *s);
|
||||||
extern _X_EXPORT char *XNFstrdup(const char *s);
|
extern _X_EXPORT char *XNFstrdup(const char *s);
|
||||||
|
|
104
os/utils.c
104
os/utils.c
|
@ -1024,32 +1024,30 @@ set_font_authorizations(char **authorizations, int *authlen, pointer client)
|
||||||
void *
|
void *
|
||||||
Xalloc(unsigned long amount)
|
Xalloc(unsigned long amount)
|
||||||
{
|
{
|
||||||
void *ptr;
|
/*
|
||||||
|
* Xalloc used to return NULL when large amount of memory is requested. In
|
||||||
|
* order to catch the buggy callers this warning has been added, slated to
|
||||||
|
* removal by anyone who touches this code (or just looks at it) in 2011.
|
||||||
|
*
|
||||||
|
* -- Mikhail Gusarov
|
||||||
|
*/
|
||||||
|
if ((long)amount <= 0)
|
||||||
|
ErrorF("Warning: Xalloc: "
|
||||||
|
"requesting unpleasantly large amount of memory: %lu bytes.\n",
|
||||||
|
amount);
|
||||||
|
|
||||||
if ((long)amount <= 0) {
|
return malloc(amount);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
/* aligned extra on long word boundary */
|
|
||||||
amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
|
|
||||||
ptr = malloc(amount);
|
|
||||||
return ptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************
|
/*****************
|
||||||
* XNFalloc
|
* XNFalloc
|
||||||
* "no failure" realloc
|
* "no failure" alloc
|
||||||
*****************/
|
*****************/
|
||||||
|
|
||||||
void *
|
void *
|
||||||
XNFalloc(unsigned long amount)
|
XNFalloc(unsigned long amount)
|
||||||
{
|
{
|
||||||
void *ptr;
|
void *ptr = malloc(amount);
|
||||||
|
|
||||||
if ((long)amount <= 0)
|
|
||||||
return NULL;
|
|
||||||
/* aligned extra on long word boundary */
|
|
||||||
amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
|
|
||||||
ptr = malloc(amount);
|
|
||||||
if (!ptr)
|
if (!ptr)
|
||||||
FatalError("Out of memory");
|
FatalError("Out of memory");
|
||||||
return ptr;
|
return ptr;
|
||||||
|
@ -1062,12 +1060,7 @@ XNFalloc(unsigned long amount)
|
||||||
void *
|
void *
|
||||||
Xcalloc(unsigned long amount)
|
Xcalloc(unsigned long amount)
|
||||||
{
|
{
|
||||||
void *ret;
|
return calloc(1, amount);
|
||||||
|
|
||||||
ret = Xalloc (amount);
|
|
||||||
if (ret)
|
|
||||||
bzero (ret, (int) amount);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************
|
/*****************
|
||||||
|
@ -1077,13 +1070,9 @@ Xcalloc(unsigned long amount)
|
||||||
void *
|
void *
|
||||||
XNFcalloc(unsigned long amount)
|
XNFcalloc(unsigned long amount)
|
||||||
{
|
{
|
||||||
void *ret;
|
void *ret = calloc(1, amount);
|
||||||
|
if (!ret)
|
||||||
ret = Xalloc (amount);
|
FatalError("XNFcalloc: Out of memory");
|
||||||
if (ret)
|
|
||||||
bzero (ret, (int) amount);
|
|
||||||
else if ((long)amount > 0)
|
|
||||||
FatalError("Out of memory");
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1092,21 +1081,21 @@ XNFcalloc(unsigned long amount)
|
||||||
*****************/
|
*****************/
|
||||||
|
|
||||||
void *
|
void *
|
||||||
Xrealloc(pointer ptr, unsigned long amount)
|
Xrealloc(void *ptr, unsigned long amount)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
* Xrealloc used to return NULL when large amount of memory is requested. In
|
||||||
|
* order to catch the buggy callers this warning has been added, slated to
|
||||||
|
* removal by anyone who touches this code (or just looks at it) in 2011.
|
||||||
|
*
|
||||||
|
* -- Mikhail Gusarov
|
||||||
|
*/
|
||||||
if ((long)amount <= 0)
|
if ((long)amount <= 0)
|
||||||
{
|
ErrorF("Warning: Xrealloc: "
|
||||||
if (ptr && !amount)
|
"requesting unpleasantly large amount of memory: %lu bytes.\n",
|
||||||
free(ptr);
|
amount);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
|
|
||||||
if (ptr)
|
|
||||||
ptr = realloc(ptr, amount);
|
|
||||||
else
|
|
||||||
ptr = malloc(amount);
|
|
||||||
|
|
||||||
return ptr;
|
return realloc(ptr, amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************
|
/*****************
|
||||||
|
@ -1115,14 +1104,12 @@ Xrealloc(pointer ptr, unsigned long amount)
|
||||||
*****************/
|
*****************/
|
||||||
|
|
||||||
void *
|
void *
|
||||||
XNFrealloc(pointer ptr, unsigned long amount)
|
XNFrealloc(void *ptr, unsigned long amount)
|
||||||
{
|
{
|
||||||
if ((ptr = Xrealloc(ptr, amount)) == NULL)
|
void *ret = realloc(ptr, amount);
|
||||||
{
|
if (!ret)
|
||||||
if ((long)amount > 0)
|
FatalError("XNFrealloc: Out of memory");
|
||||||
FatalError( "Out of memory" );
|
return ret;
|
||||||
}
|
|
||||||
return ptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************
|
/*****************
|
||||||
|
@ -1131,9 +1118,8 @@ XNFrealloc(pointer ptr, unsigned long amount)
|
||||||
*****************/
|
*****************/
|
||||||
|
|
||||||
void
|
void
|
||||||
Xfree(pointer ptr)
|
Xfree(void *ptr)
|
||||||
{
|
{
|
||||||
if (ptr)
|
|
||||||
free(ptr);
|
free(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1141,29 +1127,23 @@ Xfree(pointer ptr)
|
||||||
char *
|
char *
|
||||||
Xstrdup(const char *s)
|
Xstrdup(const char *s)
|
||||||
{
|
{
|
||||||
char *sd;
|
|
||||||
|
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
return strdup(s);
|
||||||
sd = Xalloc(strlen(s) + 1);
|
|
||||||
if (sd != NULL)
|
|
||||||
strcpy(sd, s);
|
|
||||||
return sd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char *
|
char *
|
||||||
XNFstrdup(const char *s)
|
XNFstrdup(const char *s)
|
||||||
{
|
{
|
||||||
char *sd;
|
char *ret;
|
||||||
|
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
sd = XNFalloc(strlen(s) + 1);
|
ret = strdup(s);
|
||||||
strcpy(sd, s);
|
if (!ret)
|
||||||
return sd;
|
FatalError("XNFstrdup: Out of memory");
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue