aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/src/nolibc.c
blob: 22d23ec50dfa75e45284847c02a5feaa76e215c5 (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
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */

#ifndef CONFIG_NOLIBC
#error "This file should only be compiled for no libc build"
#endif

#include "lib.h"
#include "syscall.h"

void *__uring_memset(void *s, int c, size_t n)
{
	size_t i;
	unsigned char *p = s;

	for (i = 0; i < n; i++) {
		p[i] = (unsigned char) c;

		/*
		 * An empty inline ASM to avoid auto-vectorization
		 * because it's too bloated for liburing.
		 */
		__asm__ volatile ("");
	}

	return s;
}

struct uring_heap {
	size_t		len;
	char		user_p[] __attribute__((__aligned__));
};

void *__uring_malloc(size_t len)
{
	struct uring_heap *heap;

	heap = __sys_mmap(NULL, sizeof(*heap) + len, PROT_READ | PROT_WRITE,
			  MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
	if (IS_ERR(heap))
		return NULL;

	heap->len = sizeof(*heap) + len;
	return heap->user_p;
}

void __uring_free(void *p)
{
	struct uring_heap *heap;

	if (uring_unlikely(!p))
		return;

	heap = container_of(p, struct uring_heap, user_p);
	__sys_munmap(heap, heap->len);
}