summaryrefslogtreecommitdiff
path: root/src/lib.h
blob: bd02805921086fce6c8779fc37199b75b14be700 (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
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
/* SPDX-License-Identifier: MIT */
#ifndef LIBURING_LIB_H
#define LIBURING_LIB_H

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef CONFIG_NOLIBC
#  if defined(__x86_64__) || defined(__i386__)
#    include "arch/x86/lib.h"
#  else
#    error "This arch doesn't support building liburing without libc"
#  endif
#endif

#ifndef offsetof
# define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD)
#endif

#ifndef container_of
# define container_of(PTR, TYPE, FIELD) ({				\
	__typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR = (PTR);		\
	(TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD));		\
})
#endif

void *__uring_malloc(size_t len);
void __uring_free(void *p);

static inline void *uring_malloc(size_t len)
{
#ifdef CONFIG_NOLIBC
	return __uring_malloc(len);
#else
	return malloc(len);
#endif
}

static inline void uring_free(void *ptr)
{
#ifdef CONFIG_NOLIBC
	__uring_free(ptr);
#else
	free(ptr);
#endif
}

static inline long get_page_size(void)
{
#ifdef CONFIG_NOLIBC
	return __arch_impl_get_page_size();
#else
	long page_size;

	page_size = sysconf(_SC_PAGESIZE);
	if (page_size < 0)
		page_size = 4096;

	return page_size;
#endif
}

#endif /* #ifndef LIBURING_LIB_H */