Macro -> template

This commit is contained in:
topjohnwu 2021-11-29 19:56:37 -08:00
parent 384c257a74
commit 2d82ad93dd
6 changed files with 19 additions and 11 deletions

View File

@ -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())

View File

@ -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]; }

View File

@ -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;

View File

@ -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);
}

View File

@ -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 <typename T>
static inline T align_to(T v, int a) {
static_assert(std::is_integral<T>::value);
return (v + a - 1) / a * a;
}
template <typename T>
static inline T align_padding(T v, int a) {
return align_to(v, a) - v;
}
struct file_attr {
struct stat st;

View File

@ -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() {