lkml.org 
[lkml]   [2000]   [Jul]   [8]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [RFC] solution for the inet_ntoa problem, buffer allocator
Date
From
> > 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/

\
 
 \ /
  Last update: 2005-03-22 13:57    [W:0.090 / U:0.568 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site