aboutsummaryrefslogtreecommitdiff
path: root/refimpl/bvb_sysdeps.h
blob: ed16f2b60c7566bde525f4ac3f8c05615f0887fd (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#if !defined (BVB_INSIDE_BVB_REFIMPL_H) && !defined (BVB_REFIMPL_COMPILATION)
#error "Never include this file directly, include bvb_refimpl.h instead."
#endif

#ifndef BVB_SYSDEPS_H_
#define BVB_SYSDEPS_H_

#ifdef __cplusplus
extern "C" {
#endif

/* Change these includes to match your platform to bring in the
 * equivalent types available in a normal C runtime, as well as
 * printf()-format specifiers such as PRIx64.
 */
#include <stddef.h>
#include <stdint.h>
#include <inttypes.h>

#ifdef BVB_ENABLE_DEBUG
/* Aborts the program if |expr| is false.
 *
 * This has no effect unless BVB_ENABLE_DEBUG is defined.
 */
#define bvb_assert(expr) do { if (!(expr)) { \
    bvb_error("assert fail: %s at %s:%d\n", \
              #expr, __FILE__, __LINE__); }} while(0)
#else
#define bvb_assert(expr)
#endif

/* Size in bytes used for word-alignment.
 *
 * Change this to match your architecture - must be a power of two.
 */
#define BVB_WORD_ALIGNMENT_SIZE 8

/* Aborts the program if |addr| is not word-aligned.
 *
 * This has no effect unless BVB_ENABLE_DEBUG is defined.
 */
#define bvb_assert_word_aligned(addr) \
    bvb_assert((((uintptr_t) addr) & (BVB_WORD_ALIGNMENT_SIZE-1)) == 0)

/* Compare |n| bytes in |src1| and |src2|.
 *
 * Returns an integer less than, equal to, or greater than zero if the
 * first |n| bytes of |src1| is found, respectively, to be less than,
 * to match, or be greater than the first |n| bytes of |src2|. */
int bvb_memcmp(const void* src1, const void* src2, size_t n);

/* Copy |n| bytes from |src| to |dest|. */
void* bvb_memcpy(void* dest, const void* src, size_t n);

/* Set |n| bytes starting at |s| to |c|.  Returns |dest|. */
void* bvb_memset(void* dest, const int c, size_t n);

/* Compare |n| bytes starting at |s1| with |s2| and return 0 if they
 * match, 1 if they don't.  Returns 0 if |n|==0, since no bytes
 * mismatched.
 *
 * Time taken to perform the comparison is only dependent on |n| and
 * not on the relationship of the match between |s1| and |s2|.
 *
 * Note that unlike bvb_memcmp(), this only indicates inequality, not
 * whether |s1| is less than or greater than |s2|.
 */
int bvb_safe_memcmp(const void* s1, const void* s2, size_t n);

#ifdef BVB_ENABLE_DEBUG
/* printf()-style function, used for diagnostics.
 *
 * This has no effect unless BVB_ENABLE_DEBUG is defined.
 */
void bvb_debug(const char* format, ...) __attribute__((format(printf, 1, 2)));
#else
static inline void bvb_debug(const char* format, ...)
    __attribute__((format(printf, 1, 2)));
static inline void bvb_debug(const char* format, ...) {}
#endif

/* Prints out a message (defined by |format|, printf()-style) and
 * aborts the program or reboots the device.
 *
 * Unlike bvb_debug(), this function does not depend on BVB_ENABLE_DEBUG.
 */
void bvb_error(const char* format, ...) __attribute__((format(printf, 1, 2)));

/* Allocates |size| bytes. Returns NULL if no memory is available,
 * otherwise a pointer to the allocated memory.
 *
 * The memory is not initialized.
 *
 * The pointer returned is guaranteed to be word-aligned.
 *
 * The memory should be freed with bvb_free() when you are done with it.
 */
void* bvb_malloc(size_t size);

/* Frees memory previously allocated with bvb_malloc(). */
void bvb_free(void* ptr);

/* Returns the lenght of |str|, excluding the terminating NUL-byte. */
size_t bvb_strlen(const char* str);

#ifdef __cplusplus
}
#endif

#endif  /* BVB_SYSDEPS_H_ */