diff options
| author | Michael Biebl <biebl@debian.org> | 2020-04-11 21:30:33 +0200 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2020-04-11 21:30:33 +0200 |
| commit | f914c41532a9e4f2bb44be074466b62e4cac8db3 (patch) | |
| tree | 046e8e7a186afecf299cd80d62fa1c93903dbecf /shared/systemd/src/basic/macro.h | |
| parent | 19e73abd360f0198f94ce49f36ddf009056f6324 (diff) | |
| parent | 1e5977b62f896e844b548c3007ace9e1dfa7f9ed (diff) | |
Update upstream source from tag 'upstream/1.23.90'
Update to upstream version '1.23.90' with Debian dir 38c3873648b51c73a9e448ccb2ac18152c2b3f85
Diffstat (limited to 'shared/systemd/src/basic/macro.h')
| -rw-r--r-- | shared/systemd/src/basic/macro.h | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/shared/systemd/src/basic/macro.h b/shared/systemd/src/basic/macro.h index fc733366..6cd9c516 100644 --- a/shared/systemd/src/basic/macro.h +++ b/shared/systemd/src/basic/macro.h @@ -172,6 +172,11 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) { /* align to next higher power-of-2 (except for: 0 => 0, overflow => 0) */ static inline unsigned long ALIGN_POWER2(unsigned long u) { + + /* Avoid subtraction overflow */ + if (u == 0) + return 0; + /* clz(0) is undefined */ if (u == 1) return 1; @@ -183,6 +188,29 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) { return 1UL << (sizeof(u) * 8 - __builtin_clzl(u - 1UL)); } +static inline size_t GREEDY_ALLOC_ROUND_UP(size_t l) { + size_t m; + + /* Round up allocation sizes a bit to some reasonable, likely larger value. This is supposed to be + * used for cases which are likely called in an allocation loop of some form, i.e. that repetitively + * grow stuff, for example strv_extend() and suchlike. + * + * Note the difference to GREEDY_REALLOC() here, as this helper operates on a single size value only, + * and rounds up to next multiple of 2, needing no further counter. + * + * Note the benefits of direct ALIGN_POWER2() usage: type-safety for size_t, sane handling for very + * small (i.e. <= 2) and safe handling for very large (i.e. > SSIZE_MAX) values. */ + + if (l <= 2) + return 2; /* Never allocate less than 2 of something. */ + + m = ALIGN_POWER2(l); + if (m == 0) /* overflow? */ + return l; + + return m; +} + #ifndef __COVERITY__ # define VOID_0 ((void)0) #else |