aboutsummaryrefslogtreecommitdiff
path: root/src/scratch.c
blob: ae48ff659146251e52c943c941cacbd0140a7e97 (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
51
52
53
54
55
56
57
58
59
#include <sys/mman.h>

#include <errno.h>
#include <stdint.h>

#include "alloc.h"
#include "common.h"
#include "errors.h"

/* TODO: Support malloc() backend for systems without MAP_ANON */
#ifndef MAP_ANON
#	error "System not supported (missing MAP_ANON)"
#endif

#if DEBUG
#	define TMP_DFLT_BUFSZ (8)
#else /* Probably super overkill, but that’s fine */
#	define TMP_DFLT_BUFSZ (1 * 1024)
#endif

#define MAX(x, y) ((x) > (y) ? (x) : (y))

void *
tmpalloc(scratch_t *s, size_t nmemb, size_t size)
{
	if (unlikely(size > SIZE_MAX / nmemb)) {
		errno = ENOMEM;
		err("%s:", __func__);
	}
	size *= nmemb;

	if (s->p == NULL) {
#if !__linux__
do_mmap:
#endif
		s->sz = MAX(size, TMP_DFLT_BUFSZ);
		s->p = mmap(s->p, s->sz, PROT_READ | PROT_WRITE,
		            MAP_PRIVATE | MAP_ANON, -1, 0);
		if (s->p == MAP_FAILED)
			err("mmap:");
	} else if (size > s->sz) {
#if __linux__
		if ((s->p = mremap(s->p, s->sz, size, MREMAP_MAYMOVE)) == MAP_FAILED)
			err("mremap:");
		s->sz = size;
#else
		munmap(s->p, s->sz);
		goto do_mmap;
#endif
	}

	return s->p;
}

void
tmpfree(scratch_t *s)
{
	munmap(s->p, s->sz);
}