summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-10-27 03:01:06 +0100
committerLennart Poettering <lennart@poettering.net>2015-10-27 13:45:53 +0100
commitb5efdb8af40ea759a1ea584c1bc44ecc81dd00ce (patch)
tree8b8b3411e13a6c92d59e1b071b910aa6c44c7495 /src/basic
parent7d50b32a129e781401cf897475f388f682de1368 (diff)
util-lib: split out allocation calls into alloc-util.[ch]
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/alloc-util.c81
-rw-r--r--src/basic/alloc-util.h108
-rw-r--r--src/basic/audit-util.c1
-rw-r--r--src/basic/bitmap.c4
-rw-r--r--src/basic/btrfs-util.c1
-rw-r--r--src/basic/bus-label.c3
-rw-r--r--src/basic/calendarspec.c1
-rw-r--r--src/basic/capability-util.c1
-rw-r--r--src/basic/cgroup-util.c1
-rw-r--r--src/basic/copy.c1
-rw-r--r--src/basic/cpu-set-util.c1
-rw-r--r--src/basic/env-util.c3
-rw-r--r--src/basic/escape.c1
-rw-r--r--src/basic/extract-word.c1
-rw-r--r--src/basic/fileio.c1
-rw-r--r--src/basic/fs-util.c1
-rw-r--r--src/basic/hashmap.c1
-rw-r--r--src/basic/hexdecoct.c1
-rw-r--r--src/basic/in-addr-util.c1
-rw-r--r--src/basic/json.c1
-rw-r--r--src/basic/lockfile-util.c1
-rw-r--r--src/basic/log.c1
-rw-r--r--src/basic/memfd-util.c1
-rw-r--r--src/basic/mount-util.c1
-rw-r--r--src/basic/path-util.c1
-rw-r--r--src/basic/prioq.c3
-rw-r--r--src/basic/proc-cmdline.c1
-rw-r--r--src/basic/process-util.c1
-rw-r--r--src/basic/replace-var.c1
-rw-r--r--src/basic/selinux-util.c1
-rw-r--r--src/basic/smack-util.c1
-rw-r--r--src/basic/socket-label.c1
-rw-r--r--src/basic/socket-util.c1
-rw-r--r--src/basic/strbuf.c3
-rw-r--r--src/basic/string-util.c1
-rw-r--r--src/basic/strv.c1
-rw-r--r--src/basic/terminal-util.c1
-rw-r--r--src/basic/time-util.c1
-rw-r--r--src/basic/unit-name.c1
-rw-r--r--src/basic/user-util.c1
-rw-r--r--src/basic/utf8.c1
-rw-r--r--src/basic/util.c59
-rw-r--r--src/basic/util.h109
-rw-r--r--src/basic/virt.c3
-rw-r--r--src/basic/xattr-util.c1
45 files changed, 252 insertions, 159 deletions
diff --git a/src/basic/alloc-util.c b/src/basic/alloc-util.c
new file mode 100644
index 0000000000..48183e381f
--- /dev/null
+++ b/src/basic/alloc-util.c
@@ -0,0 +1,81 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "alloc-util.h"
+#include "util.h"
+
+void* memdup(const void *p, size_t l) {
+ void *r;
+
+ assert(p);
+
+ r = malloc(l);
+ if (!r)
+ return NULL;
+
+ memcpy(r, p, l);
+ return r;
+}
+
+void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) {
+ size_t a, newalloc;
+ void *q;
+
+ assert(p);
+ assert(allocated);
+
+ if (*allocated >= need)
+ return *p;
+
+ newalloc = MAX(need * 2, 64u / size);
+ a = newalloc * size;
+
+ /* check for overflows */
+ if (a < size * need)
+ return NULL;
+
+ q = realloc(*p, a);
+ if (!q)
+ return NULL;
+
+ *p = q;
+ *allocated = newalloc;
+ return q;
+}
+
+void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size) {
+ size_t prev;
+ uint8_t *q;
+
+ assert(p);
+ assert(allocated);
+
+ prev = *allocated;
+
+ q = greedy_realloc(p, allocated, need, size);
+ if (!q)
+ return NULL;
+
+ if (*allocated > prev)
+ memzero(q + prev * size, (*allocated - prev) * size);
+
+ return q;
+}
diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h
new file mode 100644
index 0000000000..12b602e185
--- /dev/null
+++ b/src/basic/alloc-util.h
@@ -0,0 +1,108 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <alloca.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "macro.h"
+
+#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
+
+#define new0(t, n) ((t*) calloc((n), sizeof(t)))
+
+#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
+
+#define newa0(t, n) ((t*) alloca0(sizeof(t)*(n)))
+
+#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
+
+#define malloc0(n) (calloc(1, (n)))
+
+static inline void *mfree(void *memory) {
+ free(memory);
+ return NULL;
+}
+
+void* memdup(const void *p, size_t l) _alloc_(2);
+
+static inline void freep(void *p) {
+ free(*(void**) p);
+}
+
+#define _cleanup_free_ _cleanup_(freep)
+
+_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
+ if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
+ return NULL;
+
+ return malloc(a * b);
+}
+
+_alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) {
+ if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
+ return NULL;
+
+ return realloc(p, a * b);
+}
+
+_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
+ if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
+ return NULL;
+
+ return memdup(p, a * b);
+}
+
+void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
+void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
+
+#define GREEDY_REALLOC(array, allocated, need) \
+ greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
+
+#define GREEDY_REALLOC0(array, allocated, need) \
+ greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
+
+#define alloca0(n) \
+ ({ \
+ char *_new_; \
+ size_t _len_ = n; \
+ _new_ = alloca(_len_); \
+ (void *) memset(_new_, 0, _len_); \
+ })
+
+/* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
+#define alloca_align(size, align) \
+ ({ \
+ void *_ptr_; \
+ size_t _mask_ = (align) - 1; \
+ _ptr_ = alloca((size) + _mask_); \
+ (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
+ })
+
+#define alloca0_align(size, align) \
+ ({ \
+ void *_new_; \
+ size_t _size_ = (size); \
+ _new_ = alloca_align(_size_, (align)); \
+ (void*)memset(_new_, 0, _size_); \
+ })
diff --git a/src/basic/audit-util.c b/src/basic/audit-util.c
index 93d1adaf99..4612297334 100644
--- a/src/basic/audit-util.c
+++ b/src/basic/audit-util.c
@@ -22,6 +22,7 @@
#include <errno.h>
#include <stdio.h>
+#include "alloc-util.h"
#include "audit-util.h"
#include "fd-util.h"
#include "fileio.h"
diff --git a/src/basic/bitmap.c b/src/basic/bitmap.c
index 2eabf3e1c1..1449e2ea85 100644
--- a/src/basic/bitmap.c
+++ b/src/basic/bitmap.c
@@ -19,9 +19,9 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include "util.h"
-
+#include "alloc-util.h"
#include "bitmap.h"
+#include "util.h"
struct Bitmap {
uint64_t *bitmaps;
diff --git a/src/basic/btrfs-util.c b/src/basic/btrfs-util.c
index 7457e9f218..4c90bc0c80 100644
--- a/src/basic/btrfs-util.c
+++ b/src/basic/btrfs-util.c
@@ -26,6 +26,7 @@
#include <linux/btrfs.h>
#endif
+#include "alloc-util.h"
#include "btrfs-ctree.h"
#include "btrfs-util.h"
#include "copy.h"
diff --git a/src/basic/bus-label.c b/src/basic/bus-label.c
index 6f39528f13..c1534657ac 100644
--- a/src/basic/bus-label.c
+++ b/src/basic/bus-label.c
@@ -21,9 +21,10 @@
#include <stdlib.h>
+#include "alloc-util.h"
#include "bus-label.h"
-#include "macro.h"
#include "hexdecoct.h"
+#include "macro.h"
#include "util.h"
char *bus_label_escape(const char *s) {
diff --git a/src/basic/calendarspec.c b/src/basic/calendarspec.c
index 62b03a913c..a6a906f453 100644
--- a/src/basic/calendarspec.c
+++ b/src/basic/calendarspec.c
@@ -22,6 +22,7 @@
#include <stdlib.h>
#include <string.h>
+#include "alloc-util.h"
#include "string-util.h"
#include "calendarspec.h"
#include "fileio.h"
diff --git a/src/basic/capability-util.c b/src/basic/capability-util.c
index 3a6ceed6f5..4479200306 100644
--- a/src/basic/capability-util.c
+++ b/src/basic/capability-util.c
@@ -26,6 +26,7 @@
#include <sys/prctl.h>
#include <unistd.h>
+#include "alloc-util.h"
#include "capability-util.h"
#include "fileio.h"
#include "log.h"
diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c
index 0f0e144b5e..f7fc2c2c97 100644
--- a/src/basic/cgroup-util.c
+++ b/src/basic/cgroup-util.c
@@ -29,6 +29,7 @@
#include <sys/types.h>
#include <unistd.h>
+#include "alloc-util.h"
#include "cgroup-util.h"
#include "dirent-util.h"
#include "extract-word.h"
diff --git a/src/basic/copy.c b/src/basic/copy.c
index a3a4099b4d..a187ae08fe 100644
--- a/src/basic/copy.c
+++ b/src/basic/copy.c
@@ -22,6 +22,7 @@
#include <sys/sendfile.h>
#include <sys/xattr.h>
+#include "alloc-util.h"
#include "btrfs-util.h"
#include "chattr-util.h"
#include "copy.h"
diff --git a/src/basic/cpu-set-util.c b/src/basic/cpu-set-util.c
index cbeae0bf20..9122ea5d48 100644
--- a/src/basic/cpu-set-util.c
+++ b/src/basic/cpu-set-util.c
@@ -20,6 +20,7 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include "alloc-util.h"
#include "cpu-set-util.h"
#include "extract-word.h"
#include "parse-util.h"
diff --git a/src/basic/env-util.c b/src/basic/env-util.c
index a392af737c..94cb251698 100644
--- a/src/basic/env-util.c
+++ b/src/basic/env-util.c
@@ -22,12 +22,13 @@
#include <limits.h>
#include <unistd.h>
+#include "alloc-util.h"
#include "def.h"
+#include "env-util.h"
#include "string-util.h"
#include "strv.h"
#include "utf8.h"
#include "util.h"
-#include "env-util.h"
#define VALID_CHARS_ENV_NAME \
DIGITS LETTERS \
diff --git a/src/basic/escape.c b/src/basic/escape.c
index e763b5dac2..add0d7795b 100644
--- a/src/basic/escape.c
+++ b/src/basic/escape.c
@@ -19,6 +19,7 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include "alloc-util.h"
#include "escape.h"
#include "hexdecoct.h"
#include "utf8.h"
diff --git a/src/basic/extract-word.c b/src/basic/extract-word.c
index f2b74802fa..c0f9394fad 100644
--- a/src/basic/extract-word.c
+++ b/src/basic/extract-word.c
@@ -19,6 +19,7 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include "alloc-util.h"
#include "escape.h"
#include "utf8.h"
#include "util.h"
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index 3dfd3af8af..619dafb517 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -21,6 +21,7 @@
#include <unistd.h>
+#include "alloc-util.h"
#include "ctype.h"
#include "escape.h"
#include "fd-util.h"
diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c
index 6ad381afb4..7aee404bfc 100644
--- a/src/basic/fs-util.c
+++ b/src/basic/fs-util.c
@@ -19,6 +19,7 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include "alloc-util.h"
#include "dirent-util.h"
#include "fd-util.h"
#include "fileio.h"
diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c
index 015cc629b1..4109a08c6c 100644
--- a/src/basic/hashmap.c
+++ b/src/basic/hashmap.c
@@ -24,6 +24,7 @@
#include <pthread.h>
#include <stdlib.h>
+#include "alloc-util.h"
#include "hashmap.h"
#include "macro.h"
#include "mempool.h"
diff --git a/src/basic/hexdecoct.c b/src/basic/hexdecoct.c
index f958582e68..4eb566b15a 100644
--- a/src/basic/hexdecoct.c
+++ b/src/basic/hexdecoct.c
@@ -22,6 +22,7 @@
#include <ctype.h>
#include <inttypes.h>
+#include "alloc-util.h"
#include "hexdecoct.h"
#include "util.h"
diff --git a/src/basic/in-addr-util.c b/src/basic/in-addr-util.c
index d88864b598..f4e24121e7 100644
--- a/src/basic/in-addr-util.c
+++ b/src/basic/in-addr-util.c
@@ -21,6 +21,7 @@
#include <arpa/inet.h>
+#include "alloc-util.h"
#include "in-addr-util.h"
int in_addr_is_null(int family, const union in_addr_union *u) {
diff --git a/src/basic/json.c b/src/basic/json.c
index efe635e20e..716705e5ff 100644
--- a/src/basic/json.c
+++ b/src/basic/json.c
@@ -22,6 +22,7 @@
#include <math.h>
#include <sys/types.h>
+#include "alloc-util.h"
#include "json.h"
#include "macro.h"
#include "hexdecoct.h"
diff --git a/src/basic/lockfile-util.c b/src/basic/lockfile-util.c
index 60235cc972..87c3aef7af 100644
--- a/src/basic/lockfile-util.c
+++ b/src/basic/lockfile-util.c
@@ -27,6 +27,7 @@
#include <limits.h>
#include <sys/file.h>
+#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
diff --git a/src/basic/log.c b/src/basic/log.c
index 505c0d072c..1582fce61c 100644
--- a/src/basic/log.c
+++ b/src/basic/log.c
@@ -31,6 +31,7 @@
#include "sd-messages.h"
+#include "alloc-util.h"
#include "fd-util.h"
#include "formats-util.h"
#include "io-util.h"
diff --git a/src/basic/memfd-util.c b/src/basic/memfd-util.c
index 9d638b27f0..92630f6b25 100644
--- a/src/basic/memfd-util.c
+++ b/src/basic/memfd-util.c
@@ -27,6 +27,7 @@
#include <sys/mman.h>
#include <sys/prctl.h>
+#include "alloc-util.h"
#include "fd-util.h"
#include "memfd-util.h"
#include "missing.h"
diff --git a/src/basic/mount-util.c b/src/basic/mount-util.c
index 24e88babcd..fefa50709c 100644
--- a/src/basic/mount-util.c
+++ b/src/basic/mount-util.c
@@ -23,6 +23,7 @@
#include <sys/mount.h>
#include <sys/statvfs.h>
+#include "alloc-util.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
diff --git a/src/basic/path-util.c b/src/basic/path-util.c
index 3783129f69..ec90c432a4 100644
--- a/src/basic/path-util.c
+++ b/src/basic/path-util.c
@@ -33,6 +33,7 @@
#include <libgen.h>
#undef basename
+#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
diff --git a/src/basic/prioq.c b/src/basic/prioq.c
index d55b348c22..7590698911 100644
--- a/src/basic/prioq.c
+++ b/src/basic/prioq.c
@@ -29,8 +29,9 @@
* The underlying algorithm used in this implementation is a Heap.
*/
-#include "util.h"
+#include "alloc-util.h"
#include "prioq.h"
+#include "util.h"
struct prioq_item {
void *data;
diff --git a/src/basic/proc-cmdline.c b/src/basic/proc-cmdline.c
index dd0e18ed6e..dd91ce7dbc 100644
--- a/src/basic/proc-cmdline.c
+++ b/src/basic/proc-cmdline.c
@@ -19,6 +19,7 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include "alloc-util.h"
#include "extract-word.h"
#include "fileio.h"
#include "macro.h"
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index c5d7dc3089..c534656c97 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -28,6 +28,7 @@
#include <sys/wait.h>
#include <unistd.h>
+#include "alloc-util.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
diff --git a/src/basic/replace-var.c b/src/basic/replace-var.c
index 478fc43a38..18b49a9227 100644
--- a/src/basic/replace-var.c
+++ b/src/basic/replace-var.c
@@ -21,6 +21,7 @@
#include <string.h>
+#include "alloc-util.h"
#include "macro.h"
#include "util.h"
#include "replace-var.h"
diff --git a/src/basic/selinux-util.c b/src/basic/selinux-util.c
index 7a7dc90e3c..a821a3d5bb 100644
--- a/src/basic/selinux-util.c
+++ b/src/basic/selinux-util.c
@@ -29,6 +29,7 @@
#include <selinux/context.h>
#endif
+#include "alloc-util.h"
#include "strv.h"
#include "path-util.h"
#include "selinux-util.h"
diff --git a/src/basic/smack-util.c b/src/basic/smack-util.c
index a96fb5b594..fcc046098d 100644
--- a/src/basic/smack-util.c
+++ b/src/basic/smack-util.c
@@ -23,6 +23,7 @@
#include <sys/xattr.h>
+#include "alloc-util.h"
#include "fileio.h"
#include "path-util.h"
#include "process-util.h"
diff --git a/src/basic/socket-label.c b/src/basic/socket-label.c
index 4099ea6f9f..e5d4efc719 100644
--- a/src/basic/socket-label.c
+++ b/src/basic/socket-label.c
@@ -25,6 +25,7 @@
#include <sys/stat.h>
#include <unistd.h>
+#include "alloc-util.h"
#include "fd-util.h"
#include "macro.h"
#include "missing.h"
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
index ef5b764e60..1acab1ef95 100644
--- a/src/basic/socket-util.c
+++ b/src/basic/socket-util.c
@@ -30,6 +30,7 @@
#include <sys/types.h>
#include <unistd.h>
+#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "formats-util.h"
diff --git a/src/basic/strbuf.c b/src/basic/strbuf.c
index 01a076c2ba..f4f702a05a 100644
--- a/src/basic/strbuf.c
+++ b/src/basic/strbuf.c
@@ -22,8 +22,9 @@
#include <stdlib.h>
#include <string.h>
-#include "util.h"
+#include "alloc-util.h"
#include "strbuf.h"
+#include "util.h"
/*
* Strbuf stores given strings in a single continuous allocated memory
diff --git a/src/basic/string-util.c b/src/basic/string-util.c
index a1781c505a..63b9b79df9 100644
--- a/src/basic/string-util.c
+++ b/src/basic/string-util.c
@@ -19,6 +19,7 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include "alloc-util.h"
#include "gunicode.h"
#include "utf8.h"
#include "util.h"
diff --git a/src/basic/strv.c b/src/basic/strv.c
index f5df269006..ba6df716a7 100644
--- a/src/basic/strv.c
+++ b/src/basic/strv.c
@@ -24,6 +24,7 @@
#include <stdlib.h>
#include <string.h>
+#include "alloc-util.h"
#include "escape.h"
#include "string-util.h"
#include "util.h"
diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c
index 7d694e4664..b96bfcb8ef 100644
--- a/src/basic/terminal-util.c
+++ b/src/basic/terminal-util.c
@@ -31,6 +31,7 @@
#include <time.h>
#include <unistd.h>
+#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
diff --git a/src/basic/time-util.c b/src/basic/time-util.c
index 867e7b2430..9dc280efc6 100644
--- a/src/basic/time-util.c
+++ b/src/basic/time-util.c
@@ -23,6 +23,7 @@
#include <sys/timerfd.h>
#include <sys/timex.h>
+#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c
index 43f52ce7c8..0775ae7c14 100644
--- a/src/basic/unit-name.c
+++ b/src/basic/unit-name.c
@@ -22,6 +22,7 @@
#include <errno.h>
#include <string.h>
+#include "alloc-util.h"
#include "bus-label.h"
#include "def.h"
#include "hexdecoct.h"
diff --git a/src/basic/user-util.c b/src/basic/user-util.c
index bdee77a7d2..d6c936db37 100644
--- a/src/basic/user-util.c
+++ b/src/basic/user-util.c
@@ -22,6 +22,7 @@
#include <pwd.h>
#include <grp.h>
+#include "alloc-util.h"
#include "fd-util.h"
#include "macro.h"
#include "parse-util.h"
diff --git a/src/basic/utf8.c b/src/basic/utf8.c
index de124abbde..7600d99903 100644
--- a/src/basic/utf8.c
+++ b/src/basic/utf8.c
@@ -49,6 +49,7 @@
#include <string.h>
#include <stdbool.h>
+#include "alloc-util.h"
#include "hexdecoct.h"
#include "utf8.h"
#include "util.h"
diff --git a/src/basic/util.c b/src/basic/util.c
index 6da311ad1e..62d58c13fd 100644
--- a/src/basic/util.c
+++ b/src/basic/util.c
@@ -69,6 +69,7 @@
* otherwise conflicts with sys/mount.h. Yay, Linux is great! */
#include <linux/fs.h>
+#include "alloc-util.h"
#include "build.h"
#include "def.h"
#include "device-nodes.h"
@@ -487,19 +488,6 @@ int prot_from_flags(int flags) {
}
}
-void* memdup(const void *p, size_t l) {
- void *r;
-
- assert(p);
-
- r = malloc(l);
- if (!r)
- return NULL;
-
- memcpy(r, p, l);
- return r;
-}
-
int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...) {
bool stdout_is_tty, stderr_is_tty;
pid_t parent_pid, agent_pid;
@@ -725,51 +713,6 @@ int on_ac_power(void) {
return found_online || !found_offline;
}
-void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) {
- size_t a, newalloc;
- void *q;
-
- assert(p);
- assert(allocated);
-
- if (*allocated >= need)
- return *p;
-
- newalloc = MAX(need * 2, 64u / size);
- a = newalloc * size;
-
- /* check for overflows */
- if (a < size * need)
- return NULL;
-
- q = realloc(*p, a);
- if (!q)
- return NULL;
-
- *p = q;
- *allocated = newalloc;
- return q;
-}
-
-void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size) {
- size_t prev;
- uint8_t *q;
-
- assert(p);
- assert(allocated);
-
- prev = *allocated;
-
- q = greedy_realloc(p, allocated, need, size);
- if (!q)
- return NULL;
-
- if (*allocated > prev)
- memzero(q + prev * size, (*allocated - prev) * size);
-
- return q;
-}
-
bool id128_is_valid(const char *s) {
size_t i, l;
diff --git a/src/basic/util.h b/src/basic/util.h
index 3074029f38..e2fceafd9a 100644
--- a/src/basic/util.h
+++ b/src/basic/util.h
@@ -54,23 +54,6 @@
size_t page_size(void) _pure_;
#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
-#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
-
-#define new0(t, n) ((t*) calloc((n), sizeof(t)))
-
-#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
-
-#define newa0(t, n) ((t*) alloca0(sizeof(t)*(n)))
-
-#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
-
-#define malloc0(n) (calloc(1, (n)))
-
-static inline void *mfree(void *memory) {
- free(memory);
- return NULL;
-}
-
static inline const char* yes_no(bool b) {
return b ? "yes" : "no";
}
@@ -118,60 +101,35 @@ bool kexec_loaded(void);
int prot_from_flags(int flags) _const_;
-void* memdup(const void *p, size_t l) _alloc_(2);
-
int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
bool in_initrd(void);
-static inline void freep(void *p) {
- free(*(void**) p);
-}
-
-#define _cleanup_free_ _cleanup_(freep)
-
-_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
- if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
- return NULL;
-
- return malloc(a * b);
-}
-
-_alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) {
- if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
- return NULL;
-
- return realloc(p, a * b);
-}
-
-_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
- if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
- return NULL;
-
- return memdup(p, a * b);
-}
-
void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
int (*compar) (const void *, const void *, void *),
void *arg);
-int on_ac_power(void);
+/**
+ * Normal qsort requires base to be nonnull. Here were require
+ * that only if nmemb > 0.
+ */
+static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
+ if (nmemb <= 1)
+ return;
-static inline void *mempset(void *s, int c, size_t n) {
- memset(s, c, n);
- return (uint8_t*)s + n;
+ assert(base);
+ qsort(base, nmemb, size, compar);
}
+int on_ac_power(void);
+
#define memzero(x,l) (memset((x), 0, (l)))
#define zero(x) (memzero(&(x), sizeof(x)))
-void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
-void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
-#define GREEDY_REALLOC(array, allocated, need) \
- greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
-
-#define GREEDY_REALLOC0(array, allocated, need) \
- greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
+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) {
errno = *saved_errno;
@@ -225,45 +183,8 @@ static inline unsigned log2u_round_up(unsigned x) {
return log2u(x - 1) + 1;
}
-#define alloca0(n) \
- ({ \
- char *_new_; \
- size_t _len_ = n; \
- _new_ = alloca(_len_); \
- (void *) memset(_new_, 0, _len_); \
- })
-
-/* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
-#define alloca_align(size, align) \
- ({ \
- void *_ptr_; \
- size_t _mask_ = (align) - 1; \
- _ptr_ = alloca((size) + _mask_); \
- (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
- })
-
-#define alloca0_align(size, align) \
- ({ \
- void *_new_; \
- size_t _size_ = (size); \
- _new_ = alloca_align(_size_, (align)); \
- (void*)memset(_new_, 0, _size_); \
- })
-
bool id128_is_valid(const char *s) _pure_;
-/**
- * Normal qsort requires base to be nonnull. Here were require
- * that only if nmemb > 0.
- */
-static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
- if (nmemb <= 1)
- return;
-
- assert(base);
- qsort(base, nmemb, size, compar);
-}
-
int container_get_leader(const char *machine, pid_t *pid);
int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
diff --git a/src/basic/virt.c b/src/basic/virt.c
index 19f93b95aa..fb181e5b55 100644
--- a/src/basic/virt.c
+++ b/src/basic/virt.c
@@ -23,9 +23,10 @@
#include <string.h>
#include <unistd.h>
+#include "alloc-util.h"
#include "fileio.h"
-#include "stat-util.h"
#include "process-util.h"
+#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
#include "util.h"
diff --git a/src/basic/xattr-util.c b/src/basic/xattr-util.c
index 7351337c6b..6abdaedc3e 100644
--- a/src/basic/xattr-util.c
+++ b/src/basic/xattr-util.c
@@ -21,6 +21,7 @@
#include <sys/xattr.h>
+#include "alloc-util.h"
#include "fd-util.h"
#include "sparse-endian.h"
#include "stdio-util.h"