| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <string.h> |
| 5 |
|
| 6 |
#include "basic-forward.h" |
| 7 |
|
| 8 |
#include "../fundamental/memory-util.h" /* IWYU pragma: export */ |
| 9 |
|
| 10 |
size_t page_size(void) _pure_; |
| 11 |
#define PAGE_ALIGN(l) ALIGN_TO(l, page_size()) |
| 12 |
#define PAGE_ALIGN_U64(l) ALIGN_TO_U64(l, page_size()) |
| 13 |
#define PAGE_ALIGN_DOWN(l) ALIGN_DOWN(l, page_size()) |
| 14 |
#define PAGE_ALIGN_DOWN_U64(l) ALIGN_DOWN_U64(l, page_size()) |
| 15 |
#define PAGE_OFFSET(l) ALIGN_OFFSET(l, page_size()) |
| 16 |
#define PAGE_OFFSET_U64(l) ALIGN_OFFSET_U64(l, page_size()) |
| 17 |
|
| 18 |
/* Normal memcpy() requires src to be nonnull. We do nothing if n is 0. */ |
| 19 |
static inline void* memcpy_safe(void *dst, const void *src, size_t n) { |
| 20 |
if (n == 0) |
| 21 |
return dst; |
| 22 |
assert(src); |
| 23 |
return memcpy(dst, src, n); |
| 24 |
} |
| 25 |
|
| 26 |
/* Normal mempcpy() requires src to be nonnull. We do nothing if n is 0. */ |
| 27 |
static inline void* mempcpy_safe(void *dst, const void *src, size_t n) { |
| 28 |
if (n == 0) |
| 29 |
return dst; |
| 30 |
assert(src); |
| 31 |
return mempcpy(dst, src, n); |
| 32 |
} |
| 33 |
|
| 34 |
#define _mempcpy_typesafe(dst, src, n, sz) \ |
| 35 |
({ \ |
| 36 |
size_t sz; \ |
| 37 |
assert_se(MUL_SAFE(&sz, sizeof((dst)[0]), n)); \ |
| 38 |
(typeof((dst)[0])*) mempcpy_safe(dst, src, sz); \ |
| 39 |
}) |
| 40 |
|
| 41 |
#define mempcpy_typesafe(dst, src, n) \ |
| 42 |
_mempcpy_typesafe(dst, src, n, UNIQ_T(sz, UNIQ)) |
| 43 |
|
| 44 |
/* Normal memcmp() requires s1 and s2 to be nonnull. We do nothing if n is 0. */ |
| 45 |
static inline int memcmp_safe(const void *s1, const void *s2, size_t n) { |
| 46 |
if (n == 0) |
| 47 |
return 0; |
| 48 |
assert(s1); |
| 49 |
assert(s2); |
| 50 |
return memcmp(s1, s2, n); |
| 51 |
} |
| 52 |
|
| 53 |
/* Compare s1 (length n1) with s2 (length n2) in lexicographic order. */ |
| 54 |
static inline int memcmp_nn(const void *s1, size_t n1, const void *s2, size_t n2) { |
| 55 |
return memcmp_safe(s1, s2, MIN(n1, n2)) |
| 56 |
?: CMP(n1, n2); |
| 57 |
} |
| 58 |
|
| 59 |
#define zero(x) (memzero(&(x), sizeof(x))) |
| 60 |
|
| 61 |
static inline void* mempset(void *s, int c, size_t n) { |
| 62 |
memset(s, c, n); |
| 63 |
return (uint8_t*) s + n; |
| 64 |
} |
| 65 |
|
| 66 |
/* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */ |
| 67 |
static inline void* memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) { |
| 68 |
|
| 69 |
if (needlelen <= 0) |
| 70 |
return (void*) haystack; |
| 71 |
|
| 72 |
if (haystacklen < needlelen) |
| 73 |
return NULL; |
| 74 |
|
| 75 |
assert(haystack); |
| 76 |
assert(needle); |
| 77 |
|
| 78 |
return memmem(haystack, haystacklen, needle, needlelen); |
| 79 |
} |
| 80 |
|
| 81 |
static inline void* mempmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) { |
| 82 |
const uint8_t *p; |
| 83 |
|
| 84 |
p = memmem_safe(haystack, haystacklen, needle, needlelen); |
| 85 |
if (!p) |
| 86 |
return NULL; |
| 87 |
|
| 88 |
return (uint8_t*) p + needlelen; |
| 89 |
} |
| 90 |
|
| 91 |
void* erase_and_free(void *p); |
| 92 |
|
| 93 |
static inline void erase_and_freep(void *p) { |
| 94 |
erase_and_free(*(void**) p); |
| 95 |
} |
| 96 |
|
| 97 |
/* Use with _cleanup_ to erase a single 'char' when leaving scope */ |
| 98 |
static inline void erase_char(char *p) { |
| 99 |
explicit_bzero_safe(p, sizeof(char)); |
| 100 |
} |
| 101 |
|
| 102 |
/* Makes a copy of the buffer with reversed order of bytes */ |
| 103 |
void* memdup_reverse(const void *mem, size_t size); |