From f779b2e3c4eba5c15d20cc13ebd08276a66ac012 Mon Sep 17 00:00:00 2001 From: Mike Gabriel Date: Mon, 27 Jun 2016 12:12:31 +0200 Subject: 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 Date: Tue Jul 8 13:24:25 2014 -0400 os: Remove deprecated malloc/free wrappers Reviewed-by: Alex Deucher Signed-off-by: Adam Jackson commit e983848ab44b0769f97f6207f1aa8b4f127be6a9 Author: Mikhail Gusarov 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: Mikhail Gusarov Reviewed-by: Peter Hutterer Backport to nx-libs: Mike Gabriel --- nx-X11/programs/Xserver/include/os.h | 14 +--- nx-X11/programs/Xserver/os/utils.c | 149 ++++------------------------------- 2 files changed, 18 insertions(+), 145 deletions(-) diff --git a/nx-X11/programs/Xserver/include/os.h b/nx-X11/programs/Xserver/include/os.h index 342913b4a..aaff63811 100644 --- a/nx-X11/programs/Xserver/include/os.h +++ b/nx-X11/programs/Xserver/include/os.h @@ -79,15 +79,11 @@ typedef void * FID; typedef struct _FontPathRec *FontPathPtr; typedef struct _NewClientRec *NewClientPtr; -#ifndef xalloc +#ifndef xnfalloc #define xnfalloc(size) XNFalloc((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 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 xnfstrdup(s) XNFstrdup(s) #endif @@ -229,14 +225,6 @@ extern int set_font_authorizations( int * /*authlen */, 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 * XNFcalloc(unsigned long /*amount*/); extern void * XNFrealloc(void * /*ptr*/, unsigned long /*amount*/); diff --git a/nx-X11/programs/Xserver/os/utils.c b/nx-X11/programs/Xserver/os/utils.c index 63dd920b3..f5f09c320 100644 --- a/nx-X11/programs/Xserver/os/utils.c +++ b/nx-X11/programs/Xserver/os/utils.c @@ -1336,80 +1336,20 @@ set_font_authorizations(char **authorizations, int *authlen, void * client) #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 - * "no failure" realloc, alternate interface to Xalloc w/o Must_have_memory + * "no failure" alloc *****************/ void * XNFalloc(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); - ptr = (void *)malloc(amount); + void *ptr = malloc(amount); if (!ptr) { FatalError("Out of memory"); } - return ((unsigned long *)ptr); -} - -/***************** - * Xcalloc - *****************/ - -void * -Xcalloc(unsigned long amount) -{ - unsigned long *ret; - - ret = Xalloc (amount); - if (ret) - bzero ((char *) ret, (int) amount); - return ret; + return ptr; } /***************** @@ -1419,72 +1359,24 @@ Xcalloc(unsigned long amount) void * XNFcalloc(unsigned long amount) { - unsigned long *ret; - - ret = Xalloc (amount); - if (ret) - bzero ((char *) ret, (int) amount); - else if ((long)amount > 0) - FatalError("Out of memory"); + void *ret = calloc(1, amount); + if (!ret) + FatalError("XNFcalloc: Out of memory"); 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 - * "no failure" realloc, alternate interface to Xrealloc w/o Must_have_memory + * "no failure" realloc *****************/ void * XNFrealloc(void * ptr, unsigned long amount) { - if (( ptr = (void *)Xrealloc( ptr, amount ) ) == NULL) - { - if ((long)amount > 0) - FatalError( "Out of memory" ); - } - return ((unsigned long *)ptr); -} - -/***************** - * Xfree - * calls free - *****************/ - -void -Xfree(void * ptr) -{ - if (ptr) - free((char *)ptr); + void *ret = realloc(ptr, amount); + if (!ret) + FatalError("XNFrealloc: Out of memory"); + return ret; } void @@ -1500,35 +1392,28 @@ OsInitAllocator (void) been_here = 1; #endif } -#endif /* !INTERNAL_MALLOC */ - char * Xstrdup(const char *s) { - char *sd; - if (s == NULL) return NULL; - - sd = (char *)malloc(strlen(s) + 1); - if (sd != NULL) - strcpy(sd, s); - return sd; + return strdup(s); } char * XNFstrdup(const char *s) { - char *sd; + char *ret; if (s == NULL) return NULL; - sd = (char *)XNFalloc(strlen(s) + 1); - strcpy(sd, s); - return sd; + ret = strdup(s); + if (!ret) + FatalError("XNFstrdup: Out of memory"); + return ret; } #ifdef SMART_SCHEDULE -- cgit v1.2.3