[Xcb] [EXTERNAL] - Details on using xcb_send_request()?

Peter Harris pharris at opentext.com
Thu Feb 4 14:55:23 UTC 2021


On 2021-02-03 Junk Mail wrote:
> xcb_parts[3].iov_base = 0;
> xcb_parts[3].iov_len = -xcb_parts[2].iov_len & 3; // why?

Why? Padding / alignment. The next thing on the wire must be aligned to its natural size (a multiple of 4 bytes in this case).

> xcb_parts[4].iov_base = (char *) value_list; xcb_parts[4].iov_len =
> xcb_popcount(value_mask) * sizeof(uint32_t);
> 
> xcb_parts[5].iov_base = 0;
> xcb_parts[5].iov_len = -xcb_parts[4].iov_len & 3; // why?

Why? Padding / alignment. Every X11 request must be a multiple of 4 bytes. In this particular case, we can see that it is already aligned, so this will always have an iov_len of 0 (and thus nothing will be sent to the server for this part). The xcb generator is conservative and always emits this at the end of every request.

> It seems like there are "delimiters" (index 3 and 5 of xcb_parts, 
> which is of type struct iovec), and their lengths are always the 
> negative value of the length of the previous index AND'd with 3, I 
> have found nothing obvious that could shed light

It's a fast way to calculate the number of bytes of padding needed to "round up" to the required alignment.

See, for example, https://stackoverflow.com/questions/20844983/what-is-the-best-way-to-calculate-number-of-padding-bytes for other ways to calculate this value.

Peter Harris


More information about the Xcb mailing list