Commit f779b2e3 authored by Mike Gabriel's avatar Mike Gabriel

os: Remove deprecated malloc/free wrappers, clean…

os: Remove deprecated malloc/free wrappers, clean {X,XNF}{alloc,calloc,realloc,free,strdup} from pre-C89 baggage Backported from X.org: commit cad9b053d52f62432dfd70e42e0240de77027cae Author: Adam Jackson <ajax@redhat.com> Date: Tue Jul 8 13:24:25 2014 -0400 os: Remove deprecated malloc/free wrappers Reviewed-by: 's avatarAlex Deucher <alexander.deucher@amd.com> Signed-off-by: 's avatarAdam Jackson <ajax@redhat.com> commit e983848ab44b0769f97f6207f1aa8b4f127be6a9 Author: Mikhail Gusarov <dottedmag@dottedmag.net> Date: Thu May 6 00:16:24 2010 +0700 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: 's avatarMikhail Gusarov <dottedmag@dottedmag.net> Reviewed-by: 's avatarPeter Hutterer <peter.hutterer@who-t.net> Backport to nx-libs: Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
parent ce40aec0
...@@ -79,15 +79,11 @@ typedef void * FID; ...@@ -79,15 +79,11 @@ typedef void * FID;
typedef struct _FontPathRec *FontPathPtr; typedef struct _FontPathRec *FontPathPtr;
typedef struct _NewClientRec *NewClientPtr; typedef struct _NewClientRec *NewClientPtr;
#ifndef xalloc #ifndef xnfalloc
#define xnfalloc(size) XNFalloc((unsigned long)(size)) #define xnfalloc(size) XNFalloc((unsigned long)(size))
#define xnfcalloc(_num, _size) XNFcalloc((unsigned long)(_num)*(unsigned long)(_size)) #define xnfcalloc(_num, _size) XNFcalloc((unsigned long)(_num)*(unsigned long)(_size))
#define xnfrealloc(ptr, size) XNFrealloc((void *)(ptr), (unsigned long)(size)) #define xnfrealloc(ptr, size) XNFrealloc((void *)(ptr), (unsigned long)(size))
#define xalloc(size) Xalloc((unsigned long)(size))
#define xcalloc(_num, _size) Xcalloc((unsigned long)(_num)*(unsigned long)(_size))
#define xrealloc(ptr, size) Xrealloc((void *)(ptr), (unsigned long)(size))
#define xfree(ptr) Xfree((void *)(ptr))
#define xstrdup(s) Xstrdup(s) #define xstrdup(s) Xstrdup(s)
#define xnfstrdup(s) XNFstrdup(s) #define xnfstrdup(s) XNFstrdup(s)
#endif #endif
...@@ -229,14 +225,6 @@ extern int set_font_authorizations( ...@@ -229,14 +225,6 @@ extern int set_font_authorizations(
int * /*authlen */, int * /*authlen */,
void * /* client */); void * /* client */);
#ifndef _HAVE_XALLOC_DECLS
#define _HAVE_XALLOC_DECLS
extern void * Xalloc(unsigned long /*amount*/);
extern void * Xcalloc(unsigned long /*amount*/);
extern void * Xrealloc(void * /*ptr*/, unsigned long /*amount*/);
extern void Xfree(void * /*ptr*/);
#endif
extern void * XNFalloc(unsigned long /*amount*/); extern void * XNFalloc(unsigned long /*amount*/);
extern void * XNFcalloc(unsigned long /*amount*/); extern void * XNFcalloc(unsigned long /*amount*/);
extern void * XNFrealloc(void * /*ptr*/, unsigned long /*amount*/); extern void * XNFrealloc(void * /*ptr*/, unsigned long /*amount*/);
......
...@@ -1336,80 +1336,20 @@ set_font_authorizations(char **authorizations, int *authlen, void * client) ...@@ -1336,80 +1336,20 @@ set_font_authorizations(char **authorizations, int *authlen, void * client)
#endif /* TCPCONN */ #endif /* TCPCONN */
} }
/* XALLOC -- X's internal memory allocator. Why does it return unsigned
* long * instead of the more common char *? Well, if you read K&R you'll
* see they say that alloc must return a pointer "suitable for conversion"
* to whatever type you really want. In a full-blown generic allocator
* there's no way to solve the alignment problems without potentially
* wasting lots of space. But we have a more limited problem. We know
* we're only ever returning pointers to structures which will have to
* be long word aligned. So we are making a stronger guarantee. It might
* have made sense to make Xalloc return char * to conform with people's
* expectations of malloc, but this makes lint happier.
*/
#ifndef INTERNAL_MALLOC
void *
Xalloc(unsigned long amount)
{
register void * ptr;
if ((long)amount <= 0) {
return (unsigned long *)NULL;
}
/* aligned extra on long word boundary */
amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
#ifdef MEMBUG
if (!Must_have_memory && Memory_fail &&
((random() % MEM_FAIL_SCALE) < Memory_fail))
return (unsigned long *)NULL;
#endif
if ((ptr = (void *)malloc(amount))) {
return (unsigned long *)ptr;
}
if (Must_have_memory)
FatalError("Out of memory");
return (unsigned long *)NULL;
}
/***************** /*****************
* XNFalloc * XNFalloc
* "no failure" realloc, alternate interface to Xalloc w/o Must_have_memory * "no failure" alloc
*****************/ *****************/
void * void *
XNFalloc(unsigned long amount) XNFalloc(unsigned long amount)
{ {
register void * ptr; void *ptr = malloc(amount);
if ((long)amount <= 0)
{
return (unsigned long *)NULL;
}
/* aligned extra on long word boundary */
amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
ptr = (void *)malloc(amount);
if (!ptr) if (!ptr)
{ {
FatalError("Out of memory"); FatalError("Out of memory");
} }
return ((unsigned long *)ptr); return ptr;
}
/*****************
* Xcalloc
*****************/
void *
Xcalloc(unsigned long amount)
{
unsigned long *ret;
ret = Xalloc (amount);
if (ret)
bzero ((char *) ret, (int) amount);
return ret;
} }
/***************** /*****************
...@@ -1419,72 +1359,24 @@ Xcalloc(unsigned long amount) ...@@ -1419,72 +1359,24 @@ Xcalloc(unsigned long amount)
void * void *
XNFcalloc(unsigned long amount) XNFcalloc(unsigned long amount)
{ {
unsigned long *ret; void *ret = calloc(1, amount);
if (!ret)
ret = Xalloc (amount); FatalError("XNFcalloc: Out of memory");
if (ret)
bzero ((char *) ret, (int) amount);
else if ((long)amount > 0)
FatalError("Out of memory");
return ret; return ret;
} }
/***************** /*****************
* Xrealloc
*****************/
void *
Xrealloc(void * ptr, unsigned long amount)
{
#ifdef MEMBUG
if (!Must_have_memory && Memory_fail &&
((random() % MEM_FAIL_SCALE) < Memory_fail))
return (unsigned long *)NULL;
#endif
if ((long)amount <= 0)
{
if (ptr && !amount)
free(ptr);
return (unsigned long *)NULL;
}
amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
if (ptr)
ptr = (void *)realloc((char *)ptr, amount);
else
ptr = (void *)malloc(amount);
if (ptr)
return (unsigned long *)ptr;
if (Must_have_memory)
FatalError("Out of memory");
return (unsigned long *)NULL;
}
/*****************
* XNFrealloc * XNFrealloc
* "no failure" realloc, alternate interface to Xrealloc w/o Must_have_memory * "no failure" realloc
*****************/ *****************/
void * void *
XNFrealloc(void * ptr, unsigned long amount) XNFrealloc(void * ptr, unsigned long amount)
{ {
if (( ptr = (void *)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 ((unsigned long *)ptr);
}
/*****************
* Xfree
* calls free
*****************/
void
Xfree(void * ptr)
{
if (ptr)
free((char *)ptr);
} }
void void
...@@ -1500,35 +1392,28 @@ OsInitAllocator (void) ...@@ -1500,35 +1392,28 @@ OsInitAllocator (void)
been_here = 1; been_here = 1;
#endif #endif
} }
#endif /* !INTERNAL_MALLOC */
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 = (char *)malloc(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 = (char *)XNFalloc(strlen(s) + 1); ret = strdup(s);
strcpy(sd, s); if (!ret)
return sd; FatalError("XNFstrdup: Out of memory");
return ret;
} }
#ifdef SMART_SCHEDULE #ifdef SMART_SCHEDULE
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment