X Tutup
Skip to content

Commit b5efdb8

Browse files
committed
util-lib: split out allocation calls into alloc-util.[ch]
1 parent 7d50b32 commit b5efdb8

File tree

399 files changed

+691
-232
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

399 files changed

+691
-232
lines changed

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,8 @@ libbasic_la_SOURCES = \
943943
src/basic/rm-rf.h \
944944
src/basic/copy.c \
945945
src/basic/copy.h \
946+
src/basic/alloc-util.h \
947+
src/basic/alloc-util.c \
946948
src/basic/formats-util.h
947949

948950
nodist_libbasic_la_SOURCES = \

src/activate/activate.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828

2929
#include "sd-daemon.h"
3030

31+
#include "alloc-util.h"
32+
#include "fd-util.h"
3133
#include "log.h"
3234
#include "macro.h"
3335
#include "signal-util.h"
3436
#include "socket-util.h"
3537
#include "string-util.h"
3638
#include "strv.h"
37-
#include "fd-util.h"
3839

3940
static char** arg_listen = NULL;
4041
static bool arg_accept = false;

src/analyze/analyze-verify.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <stdlib.h>
2323

24+
#include "alloc-util.h"
2425
#include "analyze-verify.h"
2526
#include "bus-util.h"
2627
#include "log.h"

src/analyze/analyze.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "sd-bus.h"
2929

30+
#include "alloc-util.h"
3031
#include "analyze-verify.h"
3132
#include "bus-error.h"
3233
#include "bus-util.h"

src/backlight/backlight.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "libudev.h"
2323

24+
#include "alloc-util.h"
2425
#include "def.h"
2526
#include "escape.h"
2627
#include "fileio.h"

src/basic/alloc-util.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2+
3+
/***
4+
This file is part of systemd.
5+
6+
Copyright 2010 Lennart Poettering
7+
8+
systemd is free software; you can redistribute it and/or modify it
9+
under the terms of the GNU Lesser General Public License as published by
10+
the Free Software Foundation; either version 2.1 of the License, or
11+
(at your option) any later version.
12+
13+
systemd is distributed in the hope that it will be useful, but
14+
WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
Lesser General Public License for more details.
17+
18+
You should have received a copy of the GNU Lesser General Public License
19+
along with systemd; If not, see <http://www.gnu.org/licenses/>.
20+
***/
21+
22+
#include "alloc-util.h"
23+
#include "util.h"
24+
25+
void* memdup(const void *p, size_t l) {
26+
void *r;
27+
28+
assert(p);
29+
30+
r = malloc(l);
31+
if (!r)
32+
return NULL;
33+
34+
memcpy(r, p, l);
35+
return r;
36+
}
37+
38+
void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) {
39+
size_t a, newalloc;
40+
void *q;
41+
42+
assert(p);
43+
assert(allocated);
44+
45+
if (*allocated >= need)
46+
return *p;
47+
48+
newalloc = MAX(need * 2, 64u / size);
49+
a = newalloc * size;
50+
51+
/* check for overflows */
52+
if (a < size * need)
53+
return NULL;
54+
55+
q = realloc(*p, a);
56+
if (!q)
57+
return NULL;
58+
59+
*p = q;
60+
*allocated = newalloc;
61+
return q;
62+
}
63+
64+
void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size) {
65+
size_t prev;
66+
uint8_t *q;
67+
68+
assert(p);
69+
assert(allocated);
70+
71+
prev = *allocated;
72+
73+
q = greedy_realloc(p, allocated, need, size);
74+
if (!q)
75+
return NULL;
76+
77+
if (*allocated > prev)
78+
memzero(q + prev * size, (*allocated - prev) * size);
79+
80+
return q;
81+
}

src/basic/alloc-util.h

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2+
3+
#pragma once
4+
5+
/***
6+
This file is part of systemd.
7+
8+
Copyright 2010 Lennart Poettering
9+
10+
systemd is free software; you can redistribute it and/or modify it
11+
under the terms of the GNU Lesser General Public License as published by
12+
the Free Software Foundation; either version 2.1 of the License, or
13+
(at your option) any later version.
14+
15+
systemd is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
Lesser General Public License for more details.
19+
20+
You should have received a copy of the GNU Lesser General Public License
21+
along with systemd; If not, see <http://www.gnu.org/licenses/>.
22+
***/
23+
24+
#include <alloca.h>
25+
#include <stdlib.h>
26+
#include <string.h>
27+
28+
#include "macro.h"
29+
30+
#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
31+
32+
#define new0(t, n) ((t*) calloc((n), sizeof(t)))
33+
34+
#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
35+
36+
#define newa0(t, n) ((t*) alloca0(sizeof(t)*(n)))
37+
38+
#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
39+
40+
#define malloc0(n) (calloc(1, (n)))
41+
42+
static inline void *mfree(void *memory) {
43+
free(memory);
44+
return NULL;
45+
}
46+
47+
void* memdup(const void *p, size_t l) _alloc_(2);
48+
49+
static inline void freep(void *p) {
50+
free(*(void**) p);
51+
}
52+
53+
#define _cleanup_free_ _cleanup_(freep)
54+
55+
_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
56+
if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
57+
return NULL;
58+
59+
return malloc(a * b);
60+
}
61+
62+
_alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) {
63+
if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
64+
return NULL;
65+
66+
return realloc(p, a * b);
67+
}
68+
69+
_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
70+
if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
71+
return NULL;
72+
73+
return memdup(p, a * b);
74+
}
75+
76+
void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
77+
void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
78+
79+
#define GREEDY_REALLOC(array, allocated, need) \
80+
greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
81+
82+
#define GREEDY_REALLOC0(array, allocated, need) \
83+
greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
84+
85+
#define alloca0(n) \
86+
({ \
87+
char *_new_; \
88+
size_t _len_ = n; \
89+
_new_ = alloca(_len_); \
90+
(void *) memset(_new_, 0, _len_); \
91+
})
92+
93+
/* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
94+
#define alloca_align(size, align) \
95+
({ \
96+
void *_ptr_; \
97+
size_t _mask_ = (align) - 1; \
98+
_ptr_ = alloca((size) + _mask_); \
99+
(void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
100+
})
101+
102+
#define alloca0_align(size, align) \
103+
({ \
104+
void *_new_; \
105+
size_t _size_ = (size); \
106+
_new_ = alloca_align(_size_, (align)); \
107+
(void*)memset(_new_, 0, _size_); \
108+
})

src/basic/audit-util.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <errno.h>
2323
#include <stdio.h>
2424

25+
#include "alloc-util.h"
2526
#include "audit-util.h"
2627
#include "fd-util.h"
2728
#include "fileio.h"

src/basic/bitmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
along with systemd; If not, see <http://www.gnu.org/licenses/>.
2020
***/
2121

22-
#include "util.h"
23-
22+
#include "alloc-util.h"
2423
#include "bitmap.h"
24+
#include "util.h"
2525

2626
struct Bitmap {
2727
uint64_t *bitmaps;

src/basic/btrfs-util.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/btrfs.h>
2727
#endif
2828

29+
#include "alloc-util.h"
2930
#include "btrfs-ctree.h"
3031
#include "btrfs-util.h"
3132
#include "copy.h"

0 commit comments

Comments
 (0)
X Tutup