Re: [RFC] solution for the inet_ntoa problem, buffer allocator

From: Olaf Titz (olaf@bigred.inka.de)
Date: Sat Jul 08 2000 - 06:51:39 EDT

  • Next message: Meelis Roos: "Compile failure in test3-pre6 (apm.c)"

    > > sometimes use sort of a round-robbin buffer, shared by several
    > > functions. This way, I can call the same function several times
    > > without allocating memory, and the number of calls is just limited
    > > by the total size. Here comes an example (from scratch, completely
    > > untested).
    > >
    > > Any comments ?
    >
    > Not threadsafe.

    This one is threadsafe (in the limit given by the number of buffers,
    you probably want to configure that depending on the number of CPUs).

    #define NTOABUFS 8
    static char ntoabuf[NTOABUFS][16];
    static int ntoaptr=0;
    spinlock_t cipe_ntoalock=SPIN_LOCK_UNLOCKED;

    const char *cipe_ntoa(const __u32 addr)
    {
        const unsigned char *x=(const unsigned char *)&addr;
        char *p;
        int b, i;
        unsigned long flags;

        spin_lock_irqsave(&cipe_ntoalock, flags);
        b=ntoaptr;
        if (++b>=NTOABUFS)
            b=0;
        ntoaptr=b;
        spin_unlock_irqrestore(&cipe_ntoalock, flags);

        p=ntoabuf[b];
        for (i=0; i<4; ++i) {
            int k=x[i]/100;
            if (k)
                *p++=k+'0';
            k=(x[i]/10)%10;
            if (k)
                *p++=k+'0';
            *p++=(x[i]%10)+'0';
            if (i<3)
                *p++='.';
        }
        *p='\0';
        return ntoabuf[b];
    }

    Olaf

    PS. Why does gcc (2.95.2) say "variable defined but not used" for
    cipe_ntoalock when I make it static?

    -
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to majordomo@vger.rutgers.edu
    Please read the FAQ at http://www.tux.org/lkml/



    This archive was generated by hypermail 2b29 : Sat Jul 08 2000 - 07:05:50 EDT