[PATCH 1/3] Turn deprecated allocation functions into deprecated macros

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


This changes ABI of server as Xalloc/Xfree/Xrealloc/Xstrdup are
no longer exported. OTOH, API is not changed as deprecated
functions are replaced with macros which call C functions instead.

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

diff --git a/include/os.h b/include/os.h
index efa202c..b870135 100644
--- a/include/os.h
+++ b/include/os.h
@@ -67,18 +67,64 @@ 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 DDXs
+ * which include Xlib, protect those by not defining this deprecated set of
+ * functions.
+ */
+#ifndef Xfree
+
+#ifndef XorgLoader /* Hide this function from hw/xfree86/loader/sdksyms.sh */
+/*
+ * Helper function to mark deprecated allocation macros.
+ *
+ * If you are getting warnings related to deprecated_Xxalloc, you're using
+ * deprecated macro from below.
+ */
+void deprecated_Xxalloc(void) _X_DEPRECATED;
+#endif
+
+/*
+ * Use malloc(3) instead.
+ */
+#define xalloc(size) (deprecated_Xxalloc, malloc(size))
+#define Xalloc(size) (deprecated_Xxalloc, malloc(size))
+
+/*
+ * Use calloc(3) instead. Note the missing argument of Xcalloc.
+ */
+#define xcalloc(_num, _size) (deprecated_Xxalloc, calloc((_num), (_size)))
+#define Xcalloc(_size) (deprecated_Xxalloc, calloc(1, (_size)))
+
+/*
+ * Use realloc(3) instead
+ */
+#define xrealloc(ptr, size) (deprecated_Xxalloc, realloc((ptr), (size)))
+#define Xrealloc(ptr, size) (deprecated_Xxalloc, realloc((ptr), (size)))
+
+/*
+ * Use free(3) instead
+ */
+#define xfree(ptr) (deprecated_Xxalloc, free(ptr))
+#define Xfree(ptr) (deprecated_Xxalloc, 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.
+ */
+#define xstrdup(s) (deprecated_Xxalloc, (s) ? strdup(s) : NULL)
+#define Xstrdup(s) (deprecated_Xxalloc, (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>
@@ -215,24 +261,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 +280,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