[11] in Pthreads mailing list archive
Re: netdb interfaces
daemon@ATHENA.MIT.EDU (Greg Hudson)
Sat Jun 10 09:48:33 1995
To: Rich Salz <rsalz@osf.org>
Cc: ghudson@MIT.EDU, raeburn@cygnus.com, pthreads@MIT.EDU
In-Reply-To: Your message of "Sat, 10 Jun 1995 02:57:55 EDT."
<9506100657.AA04768@sulphur.osf.org>
Date: Sat, 10 Jun 1995 09:33:06 EDT
From: Greg Hudson <ghudson@MIT.EDU>
> I don't understand the problem -- what are you trying to do?
> Does this solve it, perhaps at the cost of wasting a few bytes?
Please keep in mind the following issues:
* The netdb functions return data with up to three
variable-length fields of different types (a name, a list of
addresses, and a list of aliases, in the case of
gethostbyname()). At least two people have proposed
"solutions" which handle the case of a single
variable-length vector of character pointers, but not the
general case.
* To my knowledge, none of the reentrant interfaces in the C
library have resorted to using malloc() inside the library.
They all have the user pass in a buffer and length, and
return an error if the buffer isn't big enough.
* I am trying to develop reentrant interfaces for
gethostbyname() and friends. When you propose a solution,
please show how much extra code the caller will have to
write (relative to the simple but non-reentrant "declare a
struct hostent * and assign the result of gethostbyname() to
it).
In a language with garbage collection, libraries are usually free to
do as much heap allocation as they please, since heap allocation is
cheap and you don't have to rely on the caller to explicitly destroy
the data later. Unfortunately, C doesn't give us this kind of luxury.
> I can't find anything in the standard that limits the alignment
> requirements an object can have.
Greg Stark pointed out in private mail that the standard guarantees
that
N*sizeof(T) = sizeof(T[N])
which requires that the alignment restrictions on T be no greater than
its size. (This isn't just a fine point of the standard, either; how
many times have you divided the size of a table by the size of its
first element to determine the number of entries?) So the size of
struct x { short s; char c; };
is going to be sufficiently large that you can pack them together in
an array; in the case of gcc on NetBSD/i386, it comes out to be 4.