gator_buffer_write.c 2.01 KB
Newer Older
Abhijith PA's avatar
Abhijith PA committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/**
 * Copyright (C) ARM Limited 2010-2014. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

static void gator_buffer_write_packed_int(int cpu, int buftype, int x)
{
	uint32_t write = per_cpu(gator_buffer_write, cpu)[buftype];
	uint32_t mask = gator_buffer_mask[buftype];
	char *buffer = per_cpu(gator_buffer, cpu)[buftype];
	int packedBytes = 0;
	int more = true;

	while (more) {
		/* low order 7 bits of x */
		char b = x & 0x7f;

		x >>= 7;

		if ((x == 0 && (b & 0x40) == 0) || (x == -1 && (b & 0x40) != 0))
			more = false;
		else
			b |= 0x80;

		buffer[(write + packedBytes) & mask] = b;
		packedBytes++;
	}

	per_cpu(gator_buffer_write, cpu)[buftype] = (write + packedBytes) & mask;
}

static void gator_buffer_write_packed_int64(int cpu, int buftype, long long x)
{
	uint32_t write = per_cpu(gator_buffer_write, cpu)[buftype];
	uint32_t mask = gator_buffer_mask[buftype];
	char *buffer = per_cpu(gator_buffer, cpu)[buftype];
	int packedBytes = 0;
	int more = true;

	while (more) {
		/* low order 7 bits of x */
		char b = x & 0x7f;

		x >>= 7;

		if ((x == 0 && (b & 0x40) == 0) || (x == -1 && (b & 0x40) != 0))
			more = false;
		else
			b |= 0x80;

		buffer[(write + packedBytes) & mask] = b;
		packedBytes++;
	}

	per_cpu(gator_buffer_write, cpu)[buftype] = (write + packedBytes) & mask;
}

static void gator_buffer_write_bytes(int cpu, int buftype, const char *x, int len)
{
	int i;
	u32 write = per_cpu(gator_buffer_write, cpu)[buftype];
	u32 mask = gator_buffer_mask[buftype];
	char *buffer = per_cpu(gator_buffer, cpu)[buftype];

	for (i = 0; i < len; i++) {
		buffer[write] = x[i];
		write = (write + 1) & mask;
	}

	per_cpu(gator_buffer_write, cpu)[buftype] = write;
}

static void gator_buffer_write_string(int cpu, int buftype, const char *x)
{
	int len = strlen(x);

	gator_buffer_write_packed_int(cpu, buftype, len);
	gator_buffer_write_bytes(cpu, buftype, x, len);
}