tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: getting Android's bionic C library back in sync with upstream



On Wed, Jul 25, 2012 at 03:14:49PM -0700, enh wrote:
> * how do you feel about #ifdef ANDROID and/or #ifdef __linux__? (i
> realize this might sound crazy, but you _are_ known for your focus on
> portability!) sometimes i "fork" a file for no good reason other than
> it refers to something linux doesn't have. for example, in
> https://android-review.googlesource.com/#/c/37804/1 i had to add #ifs
> to getnameinfo.c because linux doesn't have AF_LINK. other than that,
> the NetBSD getnameinfo works fine on linux. obviously my life would be
> easier if i could get my linux portability stuff upstream. (i realize
> there might be a continuum where something like that change might be
> acceptable but something really invasive might not. for now i'm just
> interested in principle. so i guess a related question would be
> whether you'd consider patches that moved non-portable stuff into
> separate files?)

Speaking just for myself, I avoid configuring programs using the C
preprocessor if I can help it because the result is ordinarily so much
easier for me to understand and to maintain.  Usually I can use the
makefile and/or linker to configure a program instead of using the
preprocessor.

Just for example, ordinarily I would treat the absence of AF_LINK
on Linux by deleting the AF_LINK case from getnameinfo.c:getnameinfo()
and changing the getnameinfo() default case to

        default:
                return getnameinfo_os_specific(sa, salen, host, hostlen,
                    serv, servlen, flags);

Then I would move getnameinfo_link() from getnameinfo.c to a new
source file---getnameinfo_netbsd.c, say---and add a NetBSD-specific
implementation of getnameinfo_os_specific():

        int
        getnameinfo_os_specific(const struct sockaddr* sa, socklen_t salen,
            char* host, size_t hostlen, char* serv, size_t servlen, int flags)
        {
                switch (sa->sa_family)
                case AF_LINK:
                        return getnameinfo_link(sa, salen, host, hostlen,
                            serv, servlen, flags);
                default:
                        return EAI_FAMILY;
                }
        }

Finally, I would provide a Linux-specific getnameinfo_os_specific()
implementation in another source file, getnameinfo_linux.c:

        int
        getnameinfo_os_specific(const struct sockaddr* sa, socklen_t salen,
            char* host, size_t hostlen, char* serv, size_t servlen, int flags)
        {
                return EAI_FAMILY;
        }

In the Makefile, I'd write some conditionals that built the right
OS-specific module depending on the target OS:

SRCS+=getnameinfo.c     # ... rest of sources here ...

.if $(TARGET_OS) == "NetBSD"
SRCS+=getnameinfo_netbsd.c
.elif $(TARGET_OS) == "Linux"
SRCS+=getnameinfo_linux.c
.else
# error here
.fi

Dave

-- 
David Young
dyoung%pobox.com@localhost    Urbana, IL    (217) 721-9981


Home | Main Index | Thread Index | Old Index