diff mbox

[v9,02/10] Add uleb encoding/decoding functions

Message ID 1334170153-9503-3-git-send-email-owasserm@redhat.com
State New
Headers show

Commit Message

Orit Wasserman April 11, 2012, 6:49 p.m. UTC
Implement Unsigned Little Endian Base 128.

Signed-off-by: Orit Wasserman <owasserm@redhat.com>
---
 migration.h |    4 ++++
 savevm.c    |   28 ++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 0 deletions(-)

Comments

Juan Quintela April 18, 2012, 2:37 p.m. UTC | #1
Orit Wasserman <owasserm@redhat.com> wrote:
> Implement Unsigned Little Endian Base 128.
>
> Signed-off-by: Orit Wasserman <owasserm@redhat.com>

> +
> +/* ULEB128 */

Can we add at least an url to what ULEB128 is?

https://en.wikipedia.org/wiki/LEB128

Rest is ok.

Thanks, Juan.
Anthony Liguori April 18, 2012, 5:20 p.m. UTC | #2
On 04/11/2012 01:49 PM, Orit Wasserman wrote:
> Implement Unsigned Little Endian Base 128.
>
> Signed-off-by: Orit Wasserman<owasserm@redhat.com>
> ---
>   migration.h |    4 ++++
>   savevm.c    |   28 ++++++++++++++++++++++++++++
>   2 files changed, 32 insertions(+), 0 deletions(-)
>
> diff --git a/migration.h b/migration.h
> index 691b367..d798fac 100644
> --- a/migration.h
> +++ b/migration.h
> @@ -92,4 +92,8 @@ void migrate_add_blocker(Error *reason);
>    */
>   void migrate_del_blocker(Error *reason);
>
> +/* ULEB128 */
> +int uleb128_encode_small(uint8_t *out, uint32_t n);
> +int uleb128_decode_small(const uint8 *in, uint32_t *n);

Please stick this in cutils.h and add documentation for the functions.

> +
>   #endif
> diff --git a/savevm.c b/savevm.c
> index 12fb209..0b2fe38 100644
> --- a/savevm.c
> +++ b/savevm.c
> @@ -2368,3 +2368,31 @@ void vmstate_register_ram_global(MemoryRegion *mr)
>   {
>       vmstate_register_ram(mr, NULL);
>   }
> +
> +/* ULEB128 */
> +int uleb128_encode_small(uint8_t *out, uint32_t n)
> +{
> +    assert(n<= 0x3fff);

g_assert

Regards,

Anthony Liguori

> +    if (n<  0x80) {
> +        *out++ = n;
> +        return 1;
> +    } else {
> +        *out++ = (n&  0x7f) | 0x80;
> +        *out++ = n>>  7;
> +        return 2;
> +    }
> +}
> +
> +int uleb128_decode_small(const uint8 *in, uint32_t *n)
> +{
> +    if (!(*in&  0x80)) {
> +        *n = *in++;
> +        return 1;
> +    } else {
> +        *n = *in++&  0x7f;
> +        assert(!(*in&  0x80));
> +        *n |= *in++<<  7;
> +        return 2;
> +    }
> +}
> +
Avi Kivity April 19, 2012, 8:17 a.m. UTC | #3
On 04/18/2012 08:20 PM, Anthony Liguori wrote:
> On 04/11/2012 01:49 PM, Orit Wasserman wrote:
>> Implement Unsigned Little Endian Base 128.
>>
>> Signed-off-by: Orit Wasserman<owasserm@redhat.com>
>> ---
>>   migration.h |    4 ++++
>>   savevm.c    |   28 ++++++++++++++++++++++++++++
>>   2 files changed, 32 insertions(+), 0 deletions(-)
>>
>> diff --git a/migration.h b/migration.h
>> index 691b367..d798fac 100644
>> --- a/migration.h
>> +++ b/migration.h
>> @@ -92,4 +92,8 @@ void migrate_add_blocker(Error *reason);
>>    */
>>   void migrate_del_blocker(Error *reason);
>>
>> +/* ULEB128 */
>> +int uleb128_encode_small(uint8_t *out, uint32_t n);
>> +int uleb128_decode_small(const uint8 *in, uint32_t *n);
>
> Please stick this in cutils.h and add documentation for the functions.

These aren't generic, they're limited to 14-bit numbers.
diff mbox

Patch

diff --git a/migration.h b/migration.h
index 691b367..d798fac 100644
--- a/migration.h
+++ b/migration.h
@@ -92,4 +92,8 @@  void migrate_add_blocker(Error *reason);
  */
 void migrate_del_blocker(Error *reason);
 
+/* ULEB128 */
+int uleb128_encode_small(uint8_t *out, uint32_t n);
+int uleb128_decode_small(const uint8 *in, uint32_t *n);
+
 #endif
diff --git a/savevm.c b/savevm.c
index 12fb209..0b2fe38 100644
--- a/savevm.c
+++ b/savevm.c
@@ -2368,3 +2368,31 @@  void vmstate_register_ram_global(MemoryRegion *mr)
 {
     vmstate_register_ram(mr, NULL);
 }
+
+/* ULEB128 */
+int uleb128_encode_small(uint8_t *out, uint32_t n)
+{
+    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 *in, uint32_t *n)
+{
+    if (!(*in & 0x80)) {
+        *n = *in++;
+        return 1;
+    } else {
+        *n = *in++ & 0x7f;
+        assert(!(*in & 0x80));
+        *n |= *in++ << 7;
+        return 2;
+    }
+}
+