mirror of
https://github.com/X11Libre/xserver.git
synced 2026-09-25 21:02:21 +00:00
include: introduce byte counting functions.
This patch adds the following three functions:
bits_to_bytes(bits) - the number of bytes needed to hold 'bits'
bytes_to_int32(bytes) - the number of 4-byte units to hold 'bytes'
pad_to_int32(bytes) - the closest multiple of 4 equal to or larger than
'bytes'.
All three operations are common in protocol processing and currently the
server has ((foo + 7)/8 + 3)/4 operations all over the place. A common set
of functions reduce the error rate of these (albeit simple) calculations and
improve readability of the code.
The functions do not check for overflow.
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
parent
2c535b6f13
commit
912402fd71
2 changed files with 69 additions and 0 deletions
|
|
@ -180,6 +180,36 @@ typedef struct _xReq *xReqPtr;
|
|||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Calculate the number of bytes needed to hold bits.
|
||||
* @param bits The minimum number of bits needed.
|
||||
* @return The number of bytes needed to hold bits.
|
||||
*/
|
||||
static inline int
|
||||
bits_to_bytes(const int bits) {
|
||||
return ((bits + 7) >> 3);
|
||||
}
|
||||
/**
|
||||
* Calculate the number of 4-byte units needed to hold the given number of
|
||||
* bytes.
|
||||
* @param bytes The minimum number of bytes needed.
|
||||
* @return The number of 4-byte units needed to hold bytes.
|
||||
*/
|
||||
static inline int
|
||||
bytes_to_int32(const int bytes) {
|
||||
return (((bytes) + 3) >> 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the number of bytes (in multiples of 4) needed to hold bytes.
|
||||
* @param bytes The minimum number of bytes needed.
|
||||
* @return The closest multiple of 4 that is equal or higher than bytes.
|
||||
*/
|
||||
static inline int
|
||||
pad_to_int32(const int bytes) {
|
||||
return (((bytes) + 3) & ~3);
|
||||
}
|
||||
|
||||
/* some macros to help swap requests, replies, and events */
|
||||
|
||||
#define LengthRestB(stuff) \
|
||||
|
|
|
|||
Loading…
Reference in a new issue