diff options
author | Lennart Poettering <lennart@poettering.net> | 2019-03-13 12:02:21 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2019-03-13 12:16:43 +0100 |
commit | 0a9707187b7d7b02751b068336fa27757797d44c (patch) | |
tree | d3273629ef7ef4f45d3153c83ca8e81a814c92b9 /src/basic/memory-util.h | |
parent | eefc66aa8f77c96a13a78d6c40c79ed7f3d6dc9d (diff) | |
download | systemd-0a9707187b7d7b02751b068336fa27757797d44c.tar.gz |
util: split out memcmp()/memset() related calls into memory-util.[ch]
Just some source rearranging.
Diffstat (limited to 'src/basic/memory-util.h')
-rw-r--r-- | src/basic/memory-util.h | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h new file mode 100644 index 0000000000..2d74b14a20 --- /dev/null +++ b/src/basic/memory-util.h @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +#pragma once + +#include <inttypes.h> +#include <stdbool.h> +#include <string.h> +#include <sys/types.h> + +#include "macro.h" + +size_t page_size(void) _pure_; +#define PAGE_ALIGN(l) ALIGN_TO((l), page_size()) + +/* Normal memcpy requires src to be nonnull. We do nothing if n is 0. */ +static inline void memcpy_safe(void *dst, const void *src, size_t n) { + if (n == 0) + return; + assert(src); + memcpy(dst, src, n); +} + +/* Normal memcmp requires s1 and s2 to be nonnull. We do nothing if n is 0. */ +static inline int memcmp_safe(const void *s1, const void *s2, size_t n) { + if (n == 0) + return 0; + assert(s1); + assert(s2); + return memcmp(s1, s2, n); +} + +/* Compare s1 (length n1) with s2 (length n2) in lexicographic order. */ +static inline int memcmp_nn(const void *s1, size_t n1, const void *s2, size_t n2) { + return memcmp_safe(s1, s2, MIN(n1, n2)) + ?: CMP(n1, n2); +} + +#define memzero(x,l) \ + ({ \ + size_t _l_ = (l); \ + void *_x_ = (x); \ + _l_ == 0 ? _x_ : memset(_x_, 0, _l_); \ + }) + +#define zero(x) (memzero(&(x), sizeof(x))) + +bool memeqzero(const void *data, size_t length); + +#define eqzero(x) memeqzero(x, sizeof(x)) + +static inline void *mempset(void *s, int c, size_t n) { + memset(s, c, n); + return (uint8_t*)s + n; +} |