Replace padlength tables with inline functions from misc.h

Adds new function padding_for_int32() and uses existing pad_to_int32()
depending on required results.

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Reviewed-by: Keith Packard <keithp@keithp.com>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Tested-by: Daniel Stone <daniel@fooishbar.org>
This commit is contained in:
Alan Coopersmith 2012-07-09 19:12:44 -07:00 committed by Keith Packard
parent 2b1c1300cc
commit ad4092cf7d
5 changed files with 50 additions and 13 deletions

View File

@ -466,8 +466,6 @@ Dispatch(void)
static int VendorRelease = VENDOR_RELEASE;
static char *VendorString = VENDOR_NAME;
static const int padlength[4] = { 0, 3, 2, 1 };
void
SetVendorRelease(int release)
{
@ -528,7 +526,7 @@ CreateConnectionBlock(void)
memmove(pBuf, VendorString, (int) setup.nbytesVendor);
sizesofar += setup.nbytesVendor;
pBuf += setup.nbytesVendor;
i = padlength[setup.nbytesVendor & 3];
i = padding_for_int32(setup.nbytesVendor);
sizesofar += i;
while (--i >= 0)
*pBuf++ = 0;

View File

@ -232,6 +232,20 @@ pad_to_int32(const int bytes)
return (((bytes) + 3) & ~3);
}
/**
* Calculate padding needed to bring the number of bytes to an even
* multiple of 4.
* @param bytes The minimum number of bytes needed.
* @return The bytes of padding needed to arrive at the closest multiple of 4
* that is equal or higher than bytes.
*/
static inline int
padding_for_int32(const int bytes)
{
return ((-bytes) & 3);
}
extern char **xstrtokenize(const char *str, const char *separators);
extern void FormatUInt64(uint64_t num, char *string);
extern void FormatUInt64Hex(uint64_t num, char *string);

View File

@ -578,8 +578,6 @@ ResetCurrentRequest(ClientPtr client)
}
}
static const int padlength[4] = { 0, 3, 2, 1 };
/********************
* FlushAllOutput()
* Flush all clients with output. However, if some client still
@ -757,7 +755,7 @@ WriteToClient(ClientPtr who, int count, const void *__buf)
oc->output = oco;
}
padBytes = padlength[count & 3];
padBytes = padding_for_int32(count);
if (ReplyCallback) {
ReplyInfoRec replyinfo;
@ -850,7 +848,7 @@ FlushClient(ClientPtr who, OsCommPtr oc, const void *__extraBuf, int extraCount)
if (!oco)
return 0;
written = 0;
padsize = padlength[extraCount & 3];
padsize = padding_for_int32(extraCount);
notWritten = oco->count + extraCount + padsize;
todo = notWritten;
while (notWritten) {

View File

@ -22,8 +22,6 @@
#include "randrstr.h"
static const int padlength[4] = { 0, 3, 2, 1 };
static CARD16
RR10CurrentSizeID(ScreenPtr pScreen);
@ -46,8 +44,7 @@ RREditConnectionInfo(ScreenPtr pScreen)
connSetup = (xConnSetup *) ConnectionInfo;
vendor = (char *) connSetup + sizeof(xConnSetup);
formats = (xPixmapFormat *) ((char *) vendor +
connSetup->nbytesVendor +
padlength[connSetup->nbytesVendor & 3]);
pad_to_int32(connSetup->nbytesVendor));
root = (xWindowRoot *) ((char *) formats +
sizeof(xPixmapFormat) *
screenInfo.numPixmapFormats);

View File

@ -964,6 +964,19 @@ test_pad_to_int32(int i)
assert(expected_bytes == pad_to_int32(i));
}
static void
test_padding_for_int32(int i)
{
static const int padlength[4] = { 0, 3, 2, 1 };
int expected_bytes = (((i + 3) / 4) * 4) - i;
assert(padding_for_int32(i) >= 0);
assert(padding_for_int32(i) <= 3);
assert(padding_for_int32(i) == expected_bytes);
assert(padding_for_int32(i) == padlength[i & 3]);
assert((padding_for_int32(i) + i) == pad_to_int32(i));
}
static void
include_byte_padding_macros(void)
{
@ -996,12 +1009,12 @@ include_byte_padding_macros(void)
test_bytes_to_int32(INT_MAX - 4);
test_bytes_to_int32(INT_MAX - 3);
printf("Testing pad_to_int32\n");
printf("Testing pad_to_int32()\n");
test_pad_to_int32(0);
test_pad_to_int32(0);
test_pad_to_int32(1);
test_pad_to_int32(2);
test_pad_to_int32(3);
test_pad_to_int32(7);
test_pad_to_int32(8);
test_pad_to_int32(0xFF);
@ -1012,6 +1025,23 @@ include_byte_padding_macros(void)
test_pad_to_int32(0x1000000);
test_pad_to_int32(INT_MAX - 4);
test_pad_to_int32(INT_MAX - 3);
printf("Testing padding_for_int32()\n");
test_padding_for_int32(0);
test_padding_for_int32(1);
test_padding_for_int32(2);
test_padding_for_int32(3);
test_padding_for_int32(7);
test_padding_for_int32(8);
test_padding_for_int32(0xFF);
test_padding_for_int32(0x100);
test_padding_for_int32(0xFFFF);
test_padding_for_int32(0x10000);
test_padding_for_int32(0xFFFFFF);
test_padding_for_int32(0x1000000);
test_padding_for_int32(INT_MAX - 4);
test_padding_for_int32(INT_MAX - 3);
}
static void