[Xcb] XCB-XML: Specification for alignments?

Evgeny M. Zubok evgeny.zubok at tochka.ru
Sun Oct 31 16:43:35 PDT 2010


I'm currently working on X11 protocol port for... GNU Emacs. This is
very intresting experiment. :) In order to generate protocol
implementation for Emacs Lisp I use XML protocol description from XCB
project. XML parser and macros that generate a code are also written on
Emacs Lisp. The library currently in technical preview stage but already
works. I have written a simplest graphical application with simple event
loop in pure Emacs Lisp. :)

I have a question about padding (in terms of X Protocol Specification)
or alignment. I will use the latter term. XCB-XML specification has no
explicit tag or attribute to denote when the alignment is needed. As I
understand, in the requests and replies the alignment code could be
added to every list (list of chars, list of CARD8, list of structs,
etc.) even when the list always has the size that is multiple of
4. Examples:


Alignment for "keysyms" field is not neccesary because it is always
multiple of 4:

ChangeKeyboardMapping
  1    100            opcode
  1    n              keycode-count
  2    2+nm           request length
  1    KEYCODE        first-keycode
  1    m              keysyms-per-keycode
  2                   unused
  4nm LISTofKEYSYM    keysyms


Alignment is required for "name" list:

QueryExtension
  1    98        opcode
  1              unused
  2    2+(n+p)/4 request length
  2    n         length of name
  2              unused
  n    STRING8   name
  p              unused, p=pad(n)

This reply/request case (i. e. the question "To align or not to align")
could be resolved by the size analysis during code generation stage, but
that will complicate the code generation. In my implementation, I always
add an alignment code after every list. This simply adds some redundant
cycles to the procedures for packing the request into binary form or
unpacking replies and events from binary form when the alignment is not
required (in this case the calculated alignment at runtime always gives
0).

OTOH, there is a uncertainty with structures. Take, for example,
"SetupRequest" or "Setup" structures. They require the alignments for
the lists of chars, such as: "authorization-protocol-name",
"authorization-protocol-data", "vendor". At the same time, the alignment
for the list of chars in "STR" structure is not required, but is
required after the list of "STR" structures (as appeared in
"ListExtensions" reply). I don't know how to distinguish these cases. In
my current implementation, I insert alignment code manually for the
"Setup", "SetupFailed", "SetupRequest" structures in my connection setup
code. But I dream about uniformity.

What do you think about new tag for XCB-XML specification to explicitly
insert the alignment bytes into requests, replies, events and
structures? For example

Unused bytes, 4 byte align at this place:

<align><value>4</value></align> 

or 

Unused bytes that align the field/list to be multiple of 4:

<align bytes="4"><fieldref>vendor</fieldref></align>

or use attribute align="4" within field/list tag.

If the code generator doesn't need this information (like the XCB
implementation), it will free to omit the <align> tag/attribute.



More information about the Xcb mailing list