From 9fb26641ab497eda9138f9af75cbeb02ed59b5ae Mon Sep 17 00:00:00 2001 From: Orit Wasserman Date: Mon, 6 Aug 2012 21:42:50 +0300 Subject: Add cache handling functions Add MRU page cache mechanism. The page are accessed by their address. Signed-off-by: Benoit Hudzia Signed-off-by: Petter Svard Signed-off-by: Aidan Shribman Signed-off-by: Orit Wasserman Reviewed-by: Luiz Capitulino Reviewed-by: Eric Blake --- cutils.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'cutils.c') diff --git a/cutils.c b/cutils.c index 9d4c570939..35e2e2bf44 100644 --- a/cutils.c +++ b/cutils.c @@ -382,3 +382,12 @@ int qemu_parse_fd(const char *param) } return fd; } + +/* round down to the nearest power of 2*/ +int64_t pow2floor(int64_t value) +{ + if (!is_power_of_2(value)) { + value = 0x8000000000000000ULL >> clz64(value); + } + return value; +} -- cgit v1.2.1 From e6546bb938f5326269b6669d6cbb44d72458caa4 Mon Sep 17 00:00:00 2001 From: Orit Wasserman Date: Mon, 6 Aug 2012 21:42:51 +0300 Subject: Add uleb encoding/decoding functions Implement Unsigned Little Endian Base 128. Signed-off-by: Orit Wasserman Reviewed-by: Luiz Capitulino Reviewed-by: Eric Blake --- cutils.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'cutils.c') diff --git a/cutils.c b/cutils.c index 35e2e2bf44..ee4614d378 100644 --- a/cutils.c +++ b/cutils.c @@ -391,3 +391,36 @@ int64_t pow2floor(int64_t value) } return value; } + +/* + * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128) + * Input is limited to 14-bit numbers + */ +int uleb128_encode_small(uint8_t *out, uint32_t n) +{ + g_assert(n <= 0x3fff); + if (n < 0x80) { + *out++ = n; + return 1; + } else { + *out++ = (n & 0x7f) | 0x80; + *out++ = n >> 7; + return 2; + } +} + +int uleb128_decode_small(const uint8_t *in, uint32_t *n) +{ + if (!(*in & 0x80)) { + *n = *in++; + return 1; + } else { + *n = *in++ & 0x7f; + /* we exceed 14 bit number */ + if (*in & 0x80) { + return -1; + } + *n |= *in++ << 7; + return 2; + } +} -- cgit v1.2.1