[PATCH 1/2] Turn deprecated allocation functions into inline ones

Mikhail Gusarov dottedmag at dottedmag.net
Tue May 18 12:01:18 PDT 2010


This changes ABI of server as Xalloc/Xfree/Xrealloc/Xstrdup are
no longer exported. OTOH, API is not changed.

Signed-off-by: Mikhail Gusarov <dottedmag at dottedmag.net>
---
 include/os.h |  163 +++++++++++++++++++++++++++++++++++++++++++++------------
 os/utils.c   |   57 --------------------
 2 files changed, 128 insertions(+), 92 deletions(-)

diff --git a/include/os.h b/include/os.h
index efa202c..d0f7191 100644
--- a/include/os.h
+++ b/include/os.h
@@ -50,7 +50,10 @@ SOFTWARE.
 #define OS_H
 
 #include "misc.h"
+#include <stdio.h>
 #include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
 
 #define SCREEN_SAVER_ON   0
 #define SCREEN_SAVER_OFF  1
@@ -67,21 +70,135 @@ SOFTWARE.
 typedef struct _FontPathRec *FontPathPtr;
 typedef struct _NewClientRec *NewClientPtr;
 
-#ifndef xalloc
+/* Deprecated allocation functions */
+
+/*
+ * There are functions similar to defined below in Xlib. Given there are DDXes
+ * which include Xlib, protect those by not defining this deprecated set of
+ * functions.
+ */
+#ifndef Xfree
+
+/*
+ * Use malloc(3) instead.
+ */
+static inline void *xalloc(size_t size) _X_DEPRECATED;
+
+static inline void*
+xalloc(size_t size)
+{
+    return malloc(size);
+}
+
+/*
+ * Use malloc(3) instead.
+ */
+static inline void *Xalloc(size_t size) _X_DEPRECATED;
+
+static inline void *
+Xalloc(size_t size)
+{
+    return malloc(size);
+}
+
+/*
+ * Use calloc(3) instead.
+ */
+static inline void *xcalloc(size_t nmemb, size_t size) _X_DEPRECATED;
+
+static inline void *
+xcalloc(size_t nmemb, size_t size)
+{
+    return calloc(nmemb, size);
+}
+
+/*
+ * Use calloc(3) instead. Note the missing argument.
+ */
+static inline void *Xcalloc(size_t size) _X_DEPRECATED;
+
+static inline void *
+Xcalloc(size_t size)
+{
+    return calloc(1, size);
+}
+
+/*
+ * Use realloc(3) instead
+ */
+static inline void *xrealloc(void *ptr, size_t size) _X_DEPRECATED;
+
+static inline void *
+xrealloc(void *ptr, size_t size)
+{
+    return realloc(ptr, size);
+}
+
+/*
+ * Use realloc(3) instead
+ */
+static inline void *Xrealloc(void *ptr, size_t size) _X_DEPRECATED;
+
+static inline void *
+Xrealloc(void *ptr, size_t size)
+{
+    return realloc(ptr, size);
+}
+
+/*
+ * Use free(3) instead
+ */
+static inline void xfree(void *ptr) _X_DEPRECATED;
+
+static inline void
+xfree(void *ptr)
+{
+    free(ptr);
+}
+
+/*
+ * Use free(3) instead
+ */
+static inline void Xfree(void *ptr) _X_DEPRECATED;
+
+static inline void
+Xfree(void *ptr)
+{
+    free(ptr);
+}
+
+/*
+ * Use strdup(3) instead. The only difference from the library function that it
+ * is safe to pass NULL, and NULL will be returned.
+ */
+static inline char *xstrdup(const char *s) _X_DEPRECATED;
+
+static inline char *
+xstrdup(const char *s)
+{
+    return s ? strdup(s) : NULL;
+}
+
+/*
+ * Use strdup(3) instead. The only difference from the library function that it
+ * is safe to pass NULL, and NULL will be returned.
+ */
+static inline char *Xstrdup(const char *s) _X_DEPRECATED;
+
+static inline char *
+Xstrdup(const char *s)
+{
+    return s ? strdup(s) : NULL;
+}
+
+#endif
+
+/* Not yet fully deprecated allocation functions */
+
 #define xnfalloc(size) XNFalloc((unsigned long)(size))
 #define xnfcalloc(_num, _size) XNFcalloc((unsigned long)(_num)*(unsigned long)(_size))
 #define xnfrealloc(ptr, size) XNFrealloc((pointer)(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((pointer)(ptr), (unsigned long)(size))
-#define xfree(ptr) Xfree((pointer)(ptr))
-#define xstrdup(s) Xstrdup(s)
 #define xnfstrdup(s) XNFstrdup(s)
-#endif
-
-#include <stdio.h>
-#include <stdarg.h>
 
 #ifdef DDXBEFORERESET
 extern void ddxBeforeReset (void);
@@ -215,24 +332,6 @@ extern _X_EXPORT int set_font_authorizations(
 #ifndef _HAVE_XALLOC_DECLS
 #define _HAVE_XALLOC_DECLS
 
-/*
- * Use malloc(3) instead.
- */
-extern _X_EXPORT void *Xalloc(unsigned long /*amount*/) _X_DEPRECATED;
-/*
- * Use calloc(3) instead
- */
-extern _X_EXPORT void *Xcalloc(unsigned long /*amount*/) _X_DEPRECATED;
-/*
- * Use realloc(3) instead
- */
-extern _X_EXPORT void *Xrealloc(void * /*ptr*/, unsigned long /*amount*/)
-    _X_DEPRECATED;
-/*
- * Use free(3) instead
- */
-extern _X_EXPORT void Xfree(void * /*ptr*/) _X_DEPRECATED;
-
 #endif
 
 /*
@@ -252,12 +351,6 @@ extern _X_EXPORT void *XNFcalloc(unsigned long /*amount*/);
 extern _X_EXPORT void *XNFrealloc(void * /*ptr*/, unsigned long /*amount*/);
 
 /*
- * This function strdup(3)s passed string. The only difference from the library
- * function that it is safe to pass NULL, as NULL will be returned.
- */
-extern _X_EXPORT char *Xstrdup(const char *s);
-
-/*
  * This function strdup(3)s passed string, terminating the server if there is
  * not enough memory. If NULL is passed to this function, NULL is returned.
  */
diff --git a/os/utils.c b/os/utils.c
index 7aa392a..611d5e1 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -1022,24 +1022,6 @@ set_font_authorizations(char **authorizations, int *authlen, pointer client)
 }
 
 void *
-Xalloc(unsigned long amount)
-{
-    /*
-     * 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);
-
-    return malloc(amount);
-}
-
-void *
 XNFalloc(unsigned long amount)
 {
     void *ptr = malloc(amount);
@@ -1049,12 +1031,6 @@ XNFalloc(unsigned long amount)
 }
 
 void *
-Xcalloc(unsigned long amount)
-{
-    return calloc(1, amount);
-}
-
-void *
 XNFcalloc(unsigned long amount)
 {
     void *ret = calloc(1, amount);
@@ -1064,24 +1040,6 @@ XNFcalloc(unsigned long amount)
 }
 
 void *
-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)
-	ErrorF("Warning: Xrealloc: "
-	       "requesting unpleasantly large amount of memory: %lu bytes.\n",
-               amount);
-
-    return realloc(ptr, amount);
-}
-
-void *
 XNFrealloc(void *ptr, unsigned long amount)
 {
     void *ret = realloc(ptr, amount);
@@ -1090,21 +1048,6 @@ XNFrealloc(void *ptr, unsigned long amount)
     return ret;
 }
 
-void
-Xfree(void *ptr)
-{
-    free(ptr);
-}
-
-
-char *
-Xstrdup(const char *s)
-{
-    if (s == NULL)
-	return NULL;
-    return strdup(s);
-}
-
 char *
 XNFstrdup(const char *s)
 {
-- 
1.7.1



More information about the xorg-devel mailing list