small bitmap.c patch [off-topic]

Michael Sweet mike at easysw.com
Mon Sep 24 09:49:01 GMT 2001


[This is waaayyy off-topic, but what the hell, it's Monday...]

"Christopher R. Hertel" wrote:
> ...
> > Then there is a lot of bad code in Linux, *BSD, etc. :)
> 
> Those are OS-level constructs with a known compiler and known target
> platforms.  Samba is *portable* across many platforms.  There are
> assumptions that we cannot make, which are not even assumptions to an OS
> coder.

Given the number of systems that the Linux and *BSD OS code
supports, I would hazard to say that they are also portable;
maybe not as portable as SAMBA, but still pretty good.

Anyways, I'm not advocating the use of structures for sending
binary messages - even if you limit yourself to particular
hardware and software this can be difficult.  I merely point
out that the C standards committee made a big mistake by
opening up the idea of compiler-optimized structures.  There
will always be size and alignment issues which the spec has to
acknowledge, but to add *reordering* to the list of things a
compiler can do is just dangerous.  The following structure,
according to the current specs, could be reordered causing
problems in the compiled program:

    struct
    {
      int  foo;
      char bar[1];
    }

This is a pretty common way to store variable-length data;
the last member of the structure is an array of length 1.
When the structure is allocated, the memory for the array is
also allocated, in one chunk, and in a way that is guaranteed
to be OK for the processor's alignment requirements.  More
importantly, it is a more compact representation and provides
contiguous storage, which is usually more important for
overall performance.

The spec would call for a structure like:

    struct
    {
      int  foo;
      char *bar;
    }

You *could* save on a double memory allocation overhead by
allocating one chunk of memory and setting "bar" to the
pointer + sizeof(struct), but the result is still less efficient
code (not only for initialization, but when accessing the "bar"
member array) and you will use more memory for the base structure
(8 or 16 bytes when using a pointer, vs. 4 or 8 bytes for the
member array)

Anyways, I'm just an old 6502 and 6809 asm programmer who (at the
time) thought that 64k was cool, and still wants to write code that
works well on today's hardware... :)

-- 
______________________________________________________________________
Michael Sweet, Easy Software Products                  mike at easysw.com
Printing Software for UNIX                       http://www.easysw.com




More information about the samba-technical mailing list