From: Barry Margolin on
In article <82qj54Fob0U1(a)mid.uni-berlin.de>,
jt(a)toerring.de (Jens Thoms Toerring) wrote:

> In comp.unix.programmer Barry Margolin <barmar(a)alum.mit.edu> wrote:
> > In article <82pg9eFdqgU1(a)mid.uni-berlin.de>,
> > jt(a)toerring.de (Jens Thoms Toerring) wrote:
>
> > > In comp.unix.programmer Mark Hobley
> > > <markhobley(a)hotpop.donottypethisbit.com>
> > > wrote:
> > > > buffertable.addr = malloc(tablesize * sizeof(struct buffer));
> > >
> > > What I don't like is your lying to the compiler about the type
> > > the 'addr' member is obviously going to point to. What keeps
> > > you from using the real type, i.e 'struct buffer *', as the
> > > type of the 'addr' member? Using a 'char *' (or 'void *' which
> > > would look a bit more natural since at least shows it's not
> > > about a char array but some opaque data type) only will force
> > > you to use ugly casts all over the place. It's completely ok
> > > to write
>
> > What makes you think that buffer::addr points to a struct buffer and not
> > a character string? This is an array element, why do you think it's an
> > element of a linked list?
>
> My impression isn't that 'addr' is meant as part of a linked list
> but as a pointer to an array of structures - the way the alloca-
> tion is done would seem to me to be very strange if really meant
> for an array of chars as the type of 'addr' (char *) is expres-
> sing. What would one do with a char array of a length that's a
> multiple of the length of a structure, given that the amount of
> memory allocated can depend on the compiler and the system being
> used? But, of course, I could be wrong and there's some hiddens
> sense behind this I am not discerning.

I think you're confusing buffer::addr with buffertable::addr. The
latter *is* declared as struct buffer*, and that's what he's assigning
in the above statement.

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Moi on
On Fri, 16 Apr 2010 22:54:27 -0400, Barry Margolin wrote:

> In article <82qj54Fob0U1(a)mid.uni-berlin.de>,
> jt(a)toerring.de (Jens Thoms Toerring) wrote:
>
>> In comp.unix.programmer Barry Margolin <barmar(a)alum.mit.edu> wrote:
>> > In article <82pg9eFdqgU1(a)mid.uni-berlin.de>,
>> > jt(a)toerring.de (Jens Thoms Toerring) wrote:
>>
>> > > In comp.unix.programmer Mark Hobley
>> > > <markhobley(a)hotpop.donottypethisbit.com> wrote:
>> > > > buffertable.addr = malloc(tablesize * sizeof(struct buffer));

>
> I think you're confusing buffer::addr with buffertable::addr. The
> latter *is* declared as struct buffer*, and that's what he's assigning
> in the above statement.

IMHO the OP could avoid confusing himself by choosing different names
for the two elements, e.g. "buff" and "data" (maybe with a 'p' somewhere ;-)

HTH,
AvK

From: Mark Hobley on
In comp.unix.programmer Moi <root(a)invalid.address.org> wrote:
> IMHO the OP could avoid confusing himself by choosing different names
> for the two elements, e.g. "buff" and "data" (maybe with a 'p' somewhere ;-)

I am open to suggestions.

Currently "buffertable" is the master record that points to the buffer
information table. The buffer information table contains records of type
"buffer", which point to the dynamically allocated buffers.

I am thinking of renaming buffertable to buffermaster to indicate that it is
a master record. I didn't do this initially because I thought that it
could potentially cause confusion with master/slave naming conventions.

I am interesting in hearing what conventions other people would have used.

Mark.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

From: Moi on
On Sat, 17 Apr 2010 13:27:17 +0100, Mark Hobley wrote:

> In comp.unix.programmer Moi <root(a)invalid.address.org> wrote:

> I am open to suggestions.
>
> Currently "buffertable" is the master record that points to the buffer
> information table. The buffer information table contains records of type
> "buffer", which point to the dynamically allocated buffers.
>
> I am thinking of renaming buffertable to buffermaster to indicate that
> it is a master record. I didn't do this initially because I thought that
> it could potentially cause confusion with master/slave naming
> conventions.
>
> I am interesting in hearing what conventions other people would have
> used.

On second thought, the master->buff thing points to an array of buffs,
so I would either call it .array, or .buffs.
For the mutex-field, I would use the same name, but probably shorter,
maybe .mutex, or .lock or something.

Also, to avoid realloc copying your mutexes, you could have the
buffmaster maintain an array of pointers to the actual buffs, instead of
a pointer to an array of buffs.

so it would become:

struct buffer {
mutex_t mutex;
unsigned size;
unsigned head, tail;
char *data;
};

struct buffermaster {
mutex_t mutex;
unsigned count;
unsigned used;
...
struct buffer **ptrs;
};


But, of course it is all a matter of personal taste...
HTH,
AvK