Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 1 | /** |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame^] | 2 | * Copyright (C) ARM Limited 2010-2014. All rights reserved. |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | static void gator_buffer_write_packed_int(int cpu, int buftype, int x) |
| 11 | { |
| 12 | uint32_t write = per_cpu(gator_buffer_write, cpu)[buftype]; |
| 13 | uint32_t mask = gator_buffer_mask[buftype]; |
| 14 | char *buffer = per_cpu(gator_buffer, cpu)[buftype]; |
| 15 | int packedBytes = 0; |
| 16 | int more = true; |
| 17 | while (more) { |
| 18 | // low order 7 bits of x |
| 19 | char b = x & 0x7f; |
| 20 | x >>= 7; |
| 21 | |
| 22 | if ((x == 0 && (b & 0x40) == 0) || (x == -1 && (b & 0x40) != 0)) { |
| 23 | more = false; |
| 24 | } else { |
| 25 | b |= 0x80; |
| 26 | } |
| 27 | |
| 28 | buffer[(write + packedBytes) & mask] = b; |
| 29 | packedBytes++; |
| 30 | } |
| 31 | |
| 32 | per_cpu(gator_buffer_write, cpu)[buftype] = (write + packedBytes) & mask; |
| 33 | } |
| 34 | |
| 35 | static void gator_buffer_write_packed_int64(int cpu, int buftype, long long x) |
| 36 | { |
| 37 | uint32_t write = per_cpu(gator_buffer_write, cpu)[buftype]; |
| 38 | uint32_t mask = gator_buffer_mask[buftype]; |
| 39 | char *buffer = per_cpu(gator_buffer, cpu)[buftype]; |
| 40 | int packedBytes = 0; |
| 41 | int more = true; |
| 42 | while (more) { |
| 43 | // low order 7 bits of x |
| 44 | char b = x & 0x7f; |
| 45 | x >>= 7; |
| 46 | |
| 47 | if ((x == 0 && (b & 0x40) == 0) || (x == -1 && (b & 0x40) != 0)) { |
| 48 | more = false; |
| 49 | } else { |
| 50 | b |= 0x80; |
| 51 | } |
| 52 | |
| 53 | buffer[(write + packedBytes) & mask] = b; |
| 54 | packedBytes++; |
| 55 | } |
| 56 | |
| 57 | per_cpu(gator_buffer_write, cpu)[buftype] = (write + packedBytes) & mask; |
| 58 | } |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame^] | 59 | |
| 60 | static void gator_buffer_write_bytes(int cpu, int buftype, const char *x, int len) |
| 61 | { |
| 62 | int i; |
| 63 | u32 write = per_cpu(gator_buffer_write, cpu)[buftype]; |
| 64 | u32 mask = gator_buffer_mask[buftype]; |
| 65 | char *buffer = per_cpu(gator_buffer, cpu)[buftype]; |
| 66 | |
| 67 | for (i = 0; i < len; i++) { |
| 68 | buffer[write] = x[i]; |
| 69 | write = (write + 1) & mask; |
| 70 | } |
| 71 | |
| 72 | per_cpu(gator_buffer_write, cpu)[buftype] = write; |
| 73 | } |
| 74 | |
| 75 | static void gator_buffer_write_string(int cpu, int buftype, const char *x) |
| 76 | { |
| 77 | int len = strlen(x); |
| 78 | gator_buffer_write_packed_int(cpu, buftype, len); |
| 79 | gator_buffer_write_bytes(cpu, buftype, x, len); |
| 80 | } |