aboutsummaryrefslogtreecommitdiffstats
path: root/bin/color.c
blob: 2ec97a496971fb439168c70e596c8218166ceae7 (plain) (blame)
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
#include <stdlib.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/fcntl.h>
#include <unistd.h>

#include <litmus/rt_param.h>

#include "color.h"

#define LITMUS_COLOR_ALLOC	"/dev/litmus/color_alloc"
#define LITMUS_COLOR_CTRL	"/dev/litmus/color_ctrl"

static int map_file(const char* filename, void **addr, size_t size)
{
	int error = 0;
	int fd;

	if (size > 0) {
		fd = open(filename, O_RDWR);
		if (fd >= 0) {
			*addr = mmap(NULL, size,
				     PROT_READ | PROT_WRITE,
				     MAP_PRIVATE,
				     fd, 0);
			if (*addr == MAP_FAILED)
				error = -1;
			close(fd);
		} else
			error = fd;
	} else
		*addr = NULL;
	return error;
}

int map_color_ctrl(void **addr)
{
	return map_file(LITMUS_COLOR_CTRL, addr, PAGE_SIZE);
}

void* color_malloc(size_t size)
{
	int err;
	void *mem;

	err = map_file(LITMUS_COLOR_ALLOC, &mem, size);
	if (err)
		mem = NULL;
	return mem;
}