About Xalloc()/Xfree() function used in liblbxutil library.

Tadashi Koike t-koike at pop01.odn.ne.jp
Fri Sep 21 15:34:32 PDT 2007


Hi Joerg

I thank you for your suggestion below and continuous discussion about
AC_FUNC_MALLOC.

On Tue, 18 Sep 2007 14:09:13 +0200, Joerg Sonnenberger wrote:
> malloc(0) can implement one of two behaviours according to POSIX:
> (a) It can return NULL.
> (b) It can return a unique pointer.
>
> It is up to the implementation which one it does. A bunch of code in
> X11 depends on the second behaviour and the easiest way to force
> that is toturn malloc(0) into malloc(1). That is precisely what this
> code does.

I am ashamed that I did not know these malloc(0) behaviours are a
general topic on development on C. I thank you again.

After your suggestion, I continued to research into the way of
ridding liblbxutil library of dependence to Xalloc()/Xfree() which
is (probably) on libXdmcp.  And I found some clues to solve it.
(But sorry, these does not solve malloc(0) problem automatically
on cross-compile environment)

1) Xmalloc macro definition considered about malloc(0) behaviours

  I confirmed Xmalloc macro definition on <X11/Xlibint.h> again.
  That is ,
  --<X11/Xlibint.h>----------------
  /*
   * Note that some machines do not return a valid pointer for malloc(0), in
   * which case we provide an alternate under the control of the
   * define MALLOC_0_RETURNS_NULL.  This is necessary because some
   * Xlib code expects malloc(0) to return a valid pointer to storage.
   */
  #ifdef MALLOC_0_RETURNS_NULL

  # define Xmalloc(size) malloc(((size) == 0 ? 1 : (size)))
  # define Xrealloc(ptr, size) realloc((ptr), ((size) == 0 ? 1 : (size)))
  # define Xcalloc(nelem, elsize) calloc(((nelem) == 0 ? 1 : (nelem)),
(elsize))

  #else

  # define Xmalloc(size) malloc((size))
  # define Xrealloc(ptr, size) realloc((ptr), (size))
  # define Xcalloc(nelem, elsize) calloc((nelem), (elsize))

  #endif
  ---------------------------------

  So I think Xmalloc macro with MALLOC_0_RETURNS_NULL has same effect
  as Xalloc() function on libXdmcp.


2) XORG_CHECK_MALLOC_ZERO check mechanism on autoconf

   AC_FUNC_MALLOC needs wrapper function rpl_malloc(), and such
   function has to be provided by programmer.

   XORG_CHECK_MALLOC_ZERO is in <INST_DIR>/share/aclocal/xorg-macros.m4,
   it defines some compile flags when it detect that malloc(0) returns
   NULL. (but on cross-compile environment, it does not work probably)

  -- <INST_DIR>/share/aclocal/xorg-macros.m4 ----------------
  # XORG_CHECK_MALLOC_ZERO
  # ----------------------
  # Minimum version: 1.0.0
  #
  # Defines {MALLOC,XMALLOC,XTMALLOC}_ZERO_CFLAGS appropriately if
  # malloc(0) returns NULL.  Packages should add one of these cflags to
  # their AM_CFLAGS (or other appropriate *_CFLAGS) to use them.
  AC_DEFUN([XORG_CHECK_MALLOC_ZERO],[
  AC_ARG_ENABLE(malloc0returnsnull,
          AC_HELP_STRING([--enable-malloc0returnsnull],
                         [malloc(0) returns NULL (default: auto)]),
          [MALLOC_ZERO_RETURNS_NULL=$enableval],
          [MALLOC_ZERO_RETURNS_NULL=auto])

  AC_MSG_CHECKING([whether malloc(0) returns NULL])
  if test "x$MALLOC_ZERO_RETURNS_NULL" = xauto; then
          AC_RUN_IFELSE([
  char *malloc();
  char *realloc();
  char *calloc();
  main() {
      char *m0, *r0, *c0, *p;
      m0 = malloc(0);
      p = malloc(10);
      r0 = realloc(p,0);
      c0 = calloc(0);
      exit(m0 == 0 || r0 == 0 || c0 == 0 ? 0 : 1);
  }],
                  [MALLOC_ZERO_RETURNS_NULL=yes],
                  [MALLOC_ZERO_RETURNS_NULL=no])
  fi
  AC_MSG_RESULT([$MALLOC_ZERO_RETURNS_NULL])

  if test "x$MALLOC_ZERO_RETURNS_NULL" = xyes; then
          MALLOC_ZERO_CFLAGS="-DMALLOC_0_RETURNS_NULL"
          XMALLOC_ZERO_CFLAGS=$MALLOC_ZERO_CFLAGS
          XTMALLOC_ZERO_CFLAGS="$MALLOC_ZERO_CFLAGS -DXTMALLOC_BC"
  else
          MALLOC_ZERO_CFLAGS=""
          XMALLOC_ZERO_CFLAGS=""
          XTMALLOC_ZERO_CFLAGS=""
  fi

  AC_SUBST([MALLOC_ZERO_CFLAGS])
  AC_SUBST([XMALLOC_ZERO_CFLAGS])
  AC_SUBST([XTMALLOC_ZERO_CFLAGS])
  ]) # XORG_CHECK_MALLOC_ZERO
  -----------------------------------------------------------

I will continue to research into more better solution about
Xalloc()/Xfree() in liblbxutil .

Sincerely yours,

  - Tadashi Koike, Japan

(I am weak in English, so please forgive the error of the English
sentence)




More information about the xorg mailing list