util: split out memcmp()/memset() related calls into memory-util.[ch]
Just some source rearranging.
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "macro.h"
|
||||
#include "util.h"
|
||||
#include "memory-util.h"
|
||||
|
||||
void* memdup(const void *p, size_t l) {
|
||||
void *ret;
|
||||
|
||||
+1
-1
@@ -9,6 +9,7 @@
|
||||
#include "fileio.h"
|
||||
#include "hashmap.h"
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
#include "mempool.h"
|
||||
#include "process-util.h"
|
||||
#include "random-util.h"
|
||||
@@ -16,7 +17,6 @@
|
||||
#include "siphash24.h"
|
||||
#include "string-util.h"
|
||||
#include "strv.h"
|
||||
#include "util.h"
|
||||
|
||||
#if ENABLE_DEBUG_HASHMAP
|
||||
#include <pthread.h>
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
#include "alloc-util.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
#include "string-util.h"
|
||||
#include "util.h"
|
||||
|
||||
char octchar(int x) {
|
||||
return '0' + (x & 7);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "alloc-util.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "limits-util.h"
|
||||
#include "memory-util.h"
|
||||
#include "parse-util.h"
|
||||
#include "process-util.h"
|
||||
#include "procfs-util.h"
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "memory-util.h"
|
||||
|
||||
size_t page_size(void) {
|
||||
static thread_local size_t pgsz = 0;
|
||||
long r;
|
||||
|
||||
if (_likely_(pgsz > 0))
|
||||
return pgsz;
|
||||
|
||||
r = sysconf(_SC_PAGESIZE);
|
||||
assert(r > 0);
|
||||
|
||||
pgsz = (size_t) r;
|
||||
return pgsz;
|
||||
}
|
||||
|
||||
bool memeqzero(const void *data, size_t length) {
|
||||
/* Does the buffer consist entirely of NULs?
|
||||
* Copied from https://github.com/systemd/casync/, copied in turn from
|
||||
* https://github.com/rustyrussell/ccan/blob/master/ccan/mem/mem.c#L92,
|
||||
* which is licensed CC-0.
|
||||
*/
|
||||
|
||||
const uint8_t *p = data;
|
||||
size_t i;
|
||||
|
||||
/* Check first 16 bytes manually */
|
||||
for (i = 0; i < 16; i++, length--) {
|
||||
if (length == 0)
|
||||
return true;
|
||||
if (p[i])
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Now we know first 16 bytes are NUL, memcmp with self. */
|
||||
return memcmp(data, p + i, length) == 0;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "env-util.h"
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
#include "mempool.h"
|
||||
#include "process-util.h"
|
||||
#include "util.h"
|
||||
|
||||
@@ -91,6 +91,8 @@ basic_sources = files('''
|
||||
macro.h
|
||||
memfd-util.c
|
||||
memfd-util.h
|
||||
memory-util.c
|
||||
memory-util.h
|
||||
mempool.c
|
||||
mempool.h
|
||||
missing.h
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "ioprio.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
#include "missing.h"
|
||||
#include "namespace-util.h"
|
||||
#include "process-util.h"
|
||||
@@ -43,7 +44,6 @@
|
||||
#include "string-util.h"
|
||||
#include "terminal-util.h"
|
||||
#include "user-util.h"
|
||||
#include "util.h"
|
||||
|
||||
int get_process_state(pid_t pid) {
|
||||
const char *p;
|
||||
|
||||
+1
-1
@@ -6,8 +6,8 @@
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
#include "sigbus.h"
|
||||
#include "util.h"
|
||||
|
||||
#define SIGBUS_QUEUE_MAX 64
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "format-util.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
#include "missing.h"
|
||||
#include "parse-util.h"
|
||||
#include "path-util.h"
|
||||
@@ -31,7 +32,6 @@
|
||||
#include "strv.h"
|
||||
#include "user-util.h"
|
||||
#include "utf8.h"
|
||||
#include "util.h"
|
||||
|
||||
#if ENABLE_IDN
|
||||
# define IDN_FLAGS NI_IDN
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "macro.h"
|
||||
#include "util.h"
|
||||
#include "memory-util.h"
|
||||
|
||||
#define snprintf_ok(buf, len, fmt, ...) \
|
||||
((size_t) snprintf(buf, len, fmt, __VA_ARGS__) < (len))
|
||||
|
||||
@@ -51,20 +51,6 @@ int saved_argc = 0;
|
||||
char **saved_argv = NULL;
|
||||
static int saved_in_initrd = -1;
|
||||
|
||||
size_t page_size(void) {
|
||||
static thread_local size_t pgsz = 0;
|
||||
long r;
|
||||
|
||||
if (_likely_(pgsz > 0))
|
||||
return pgsz;
|
||||
|
||||
r = sysconf(_SC_PAGESIZE);
|
||||
assert(r > 0);
|
||||
|
||||
pgsz = (size_t) r;
|
||||
return pgsz;
|
||||
}
|
||||
|
||||
bool plymouth_running(void) {
|
||||
return access("/run/plymouth/pid", F_OK) >= 0;
|
||||
}
|
||||
@@ -165,28 +151,6 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool memeqzero(const void *data, size_t length) {
|
||||
/* Does the buffer consist entirely of NULs?
|
||||
* Copied from https://github.com/systemd/casync/, copied in turn from
|
||||
* https://github.com/rustyrussell/ccan/blob/master/ccan/mem/mem.c#L92,
|
||||
* which is licensed CC-0.
|
||||
*/
|
||||
|
||||
const uint8_t *p = data;
|
||||
size_t i;
|
||||
|
||||
/* Check first 16 bytes manually */
|
||||
for (i = 0; i < 16; i++, length--) {
|
||||
if (length == 0)
|
||||
return true;
|
||||
if (p[i])
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Now we know first 16 bytes are NUL, memcmp with self. */
|
||||
return memcmp(data, p + i, length) == 0;
|
||||
}
|
||||
|
||||
int on_ac_power(void) {
|
||||
bool found_offline = false, found_online = false;
|
||||
_cleanup_closedir_ DIR *d = NULL;
|
||||
|
||||
@@ -27,9 +27,6 @@
|
||||
#include "macro.h"
|
||||
#include "time-util.h"
|
||||
|
||||
size_t page_size(void) _pure_;
|
||||
#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
|
||||
|
||||
static inline const char* yes_no(bool b) {
|
||||
return b ? "yes" : "no";
|
||||
}
|
||||
@@ -130,49 +127,8 @@ static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, __compar_
|
||||
qsort_r_safe((p), (n), sizeof((p)[0]), (__compar_d_fn_t) _func_, userdata); \
|
||||
})
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
int on_ac_power(void);
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
static inline void _reset_errno_(int *saved_errno) {
|
||||
if (*saved_errno < 0) /* Invalidated by UNPROTECT_ERRNO? */
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user