summaryrefslogtreecommitdiff
path: root/alloc.c
blob: 34058b3055ac53b2208eb43bbab6b82aed91503b (plain)
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
/* uuterm, Copyright (C) 2006 Rich Felker; licensed under GNU GPL v2 only */

#include <stdlib.h>
#include <sys/mman.h>

#include "uuterm.h"

void *uuterm_alloc(size_t len)
{
#ifdef MAP_ANONYMOUS
	size_t *mem = mmap(0, len+sizeof(size_t),
		PROT_READ|PROT_WRITE,
		MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
	if (mem == MAP_FAILED) return 0;
	*mem++ = len;
	return mem;
#else
	return malloc(len);
#endif
}

void uuterm_free(void *buf)
{
#ifdef MAP_ANONYMOUS
	size_t *mem = buf;
	mem--;
	munmap(mem, *mem);
#else
	free(buf);
#endif
}

void *uuterm_buf_alloc(int w, int h)
{
	/* FIXME: do we care about overflows? */
	return uuterm_alloc(UU_BUF_SIZE(w, h));
}