utils: Add utility function to calculate padding length

This commit is contained in:
Tobias Brunner
2013-10-17 10:25:34 +02:00
parent 32fef0c6e9
commit 812ae898bf
2 changed files with 24 additions and 13 deletions
+16 -12
View File
@@ -679,26 +679,30 @@ static inline u_int64_t untoh64(void *network)
}
/**
* Round up size to be multiple of alignement
* Get the padding required to make size a multiple of alignment
*/
static inline size_t round_up(size_t size, int alignement)
static inline size_t pad_len(size_t size, size_t alignment)
{
int remainder;
size_t remainder;
remainder = size % alignement;
if (remainder)
{
size += alignement - remainder;
}
return size;
remainder = size % alignment;
return remainder ? alignment - remainder : 0;
}
/**
* Round down size to be a multiple of alignement
* Round up size to be multiple of alignment
*/
static inline size_t round_down(size_t size, int alignement)
static inline size_t round_up(size_t size, size_t alignment)
{
return size - (size % alignement);
return size + pad_len(size, alignment);
}
/**
* Round down size to be a multiple of alignment
*/
static inline size_t round_down(size_t size, size_t alignment)
{
return size - (size % alignment);
}
/**