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

From: Willy Tarreau (willy@novworld.Novecom.Fr)
Date: Fri Jul 07 2000 - 03:32:37 EST


Hi !

when I need function which return statically allocated strings, I
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 ?

Willy

---------

#define TOTALSIZE 256
#define NTOA_SIZE 16

static char stringbuffer[TOTALSIZE];
static char *strbufptr=stringbuffer;

char *inet_ntoa(u32 addr) {
   char *retptr;

   if (strbufptr+NTOA_SIZE > stringbuffer+TOTALSIZE)
      strbufptr = stringbuffer;

   addr=htonl(addr);

   strbufptr += sprintf(retptr=strbufptr, "%d.%d.%d.%d",
        (uchar)(addr>>24), (uchar)(addr>>16), (uchar)(addr>>8), (uchar)addr);

   return retptr;
}

void otherfunction(u32 src, u32 dst) {
   printf("src=%s, dst=%s\n", inet_ntoa(src), inet_ntoa(dst));
}

-
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 : Fri Jul 07 2000 - 21:00:20 EST