Messages in this thread | | | Subject | Re: [RFC] solution for the inet_ntoa problem, buffer allocator | Date | Sat, 08 Jul 2000 12:51:39 +0200 | From | Olaf Titz <> |
| |
> > 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/
| |