From 2d82ad93dd5f2c8ea911ec588c3bfbc41def24ce Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 29 Nov 2021 19:56:37 -0800 Subject: [PATCH] Macro -> template --- native/jni/magiskboot/bootimg.cpp | 6 +++--- native/jni/magiskboot/bootimg.hpp | 2 +- native/jni/magiskboot/cpio.cpp | 4 ++-- native/jni/magiskboot/dtb.cpp | 4 ++-- native/jni/utils/files.hpp | 12 ++++++++++-- native/jni/zygisk/memory.cpp | 2 +- 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/native/jni/magiskboot/bootimg.cpp b/native/jni/magiskboot/bootimg.cpp index ee08d4faa..7bd6b66d3 100644 --- a/native/jni/magiskboot/bootimg.cpp +++ b/native/jni/magiskboot/bootimg.cpp @@ -291,11 +291,11 @@ dyn_img_hdr *boot_img::create_hdr(uint8_t *addr, format_t type) { #define get_block(name) \ name = hdr_addr + off; \ off += hdr->name##_size(); \ -off = do_align(off, hdr->page_size()); +off = align_to(off, hdr->page_size()); #define get_ignore(name) \ if (hdr->name##_size()) { \ - auto blk_sz = do_align(hdr->name##_size(), hdr->page_size()); \ + auto blk_sz = align_to(hdr->name##_size(), hdr->page_size()); \ ignore_size += blk_sz; \ off += blk_sz; \ } @@ -505,7 +505,7 @@ int unpack(const char *image, bool skip_decomp, bool hdr) { } #define file_align_with(page_size) \ -write_zero(fd, align_off(lseek(fd, 0, SEEK_CUR) - off.header, page_size)) +write_zero(fd, align_padding(lseek(fd, 0, SEEK_CUR) - off.header, page_size)) #define file_align() file_align_with(boot.hdr->page_size()) diff --git a/native/jni/magiskboot/bootimg.hpp b/native/jni/magiskboot/bootimg.hpp index 5bf02b912..eb9f960bd 100644 --- a/native/jni/magiskboot/bootimg.hpp +++ b/native/jni/magiskboot/bootimg.hpp @@ -523,7 +523,7 @@ struct dyn_img_vnd_v3 : public dyn_img_hdr_vendor { impl_val(header_size) impl_val(dtb_size) - size_t hdr_space() override { auto sz = page_size(); return do_align(hdr_size(), sz); } + size_t hdr_space() override { return align_to(hdr_size(), page_size()); } // Make API compatible char *extra_cmdline() override { return &v4_vnd->cmdline[BOOT_ARGS_SIZE]; } diff --git a/native/jni/magiskboot/cpio.cpp b/native/jni/magiskboot/cpio.cpp index d71fbf8c5..eca5cc8be 100644 --- a/native/jni/magiskboot/cpio.cpp +++ b/native/jni/magiskboot/cpio.cpp @@ -111,7 +111,7 @@ bool cpio::exists(const char *name) { } #define do_out(buf, len) pos += fwrite(buf, 1, len, out); -#define out_align() do_out(zeros, align_off(pos, 4)) +#define out_align() do_out(zeros, align_padding(pos, 4)) void cpio::dump(FILE *out) { size_t pos = 0; unsigned inode = 300000; @@ -211,7 +211,7 @@ bool cpio::mv(const char *from, const char *to) { return false; } -#define pos_align(p) p = do_align(p, 4) +#define pos_align(p) p = align_to(p, 4) void cpio::load_cpio(const char *buf, size_t sz) { size_t pos = 0; diff --git a/native/jni/magiskboot/dtb.cpp b/native/jni/magiskboot/dtb.cpp index c35948cda..fa6f839c6 100644 --- a/native/jni/magiskboot/dtb.cpp +++ b/native/jni/magiskboot/dtb.cpp @@ -297,8 +297,8 @@ static bool dt_table_patch(const Header *hdr, const char *out) { auto size = fdt_totalsize(fdt); total_size += xwrite(fd, fdt, size); if constexpr (!is_aosp) { - val.second.len = do_align(size, align); - write_zero(fd, align_off(lseek(fd, 0, SEEK_CUR), align)); + val.second.len = align_to(size, align); + write_zero(fd, align_padding(lseek(fd, 0, SEEK_CUR), align)); } free(fdt); } diff --git a/native/jni/utils/files.hpp b/native/jni/utils/files.hpp index e5e38f898..9e78c4afd 100644 --- a/native/jni/utils/files.hpp +++ b/native/jni/utils/files.hpp @@ -10,8 +10,16 @@ #include "xwrap.hpp" -#define do_align(p, a) (((p) + (a) - 1) / (a) * (a)) -#define align_off(p, a) (do_align(p, a) - (p)) +template +static inline T align_to(T v, int a) { + static_assert(std::is_integral::value); + return (v + a - 1) / a * a; +} + +template +static inline T align_padding(T v, int a) { + return align_to(v, a) - v; +} struct file_attr { struct stat st; diff --git a/native/jni/zygisk/memory.cpp b/native/jni/zygisk/memory.cpp index 10b051ba8..ed928d302 100644 --- a/native/jni/zygisk/memory.cpp +++ b/native/jni/zygisk/memory.cpp @@ -20,7 +20,7 @@ void *memory_block::allocate(size_t sz) { nullptr, CAPACITY, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); _curr = _area; } - return _curr.fetch_add(do_align(sz, ALIGN)); + return _curr.fetch_add(align_to(sz, ALIGN)); } void memory_block::release() {