From 769e4a4538a08acee7c95dc9c99c028983ef036d Mon Sep 17 00:00:00 2001 From: Yuta Imazu Date: Fri, 4 Oct 2024 22:28:55 +0900 Subject: [PATCH] common(extra): turn round_up, round_down and div_ceil into macros --- common/extra.h | 16 ++++------------ kernel/console/screen/psf.c | 2 +- kernel/console/vt.c | 2 +- kernel/containers/vec.c | 2 +- kernel/drivers/virtio/virtio.c | 12 ++++++------ kernel/exec.c | 6 +++--- kernel/memory/page.c | 8 ++++---- kernel/memory/page_table.c | 4 ++-- kernel/memory/vm.c | 8 ++++---- kernel/random.c | 4 ++-- kernel/smp.c | 2 +- kernel/syscall/fs.c | 6 +++--- kernel/syscall/select.c | 2 +- kernel/task.c | 2 +- userland/lib/crt0.c | 2 +- userland/lib/stdlib.c | 4 ++-- userland/ls.c | 4 ++-- 17 files changed, 39 insertions(+), 47 deletions(-) diff --git a/common/extra.h b/common/extra.h index 56de808f..e9417447 100644 --- a/common/extra.h +++ b/common/extra.h @@ -15,24 +15,16 @@ #define SIZEOF_FIELD(t, f) sizeof(((t*)0)->f) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#define ROUND_UP(x, align) (((x) + ((align) - 1)) & ~((align) - 1)) +#define ROUND_DOWN(x, align) ((x) & ~((align) - 1)) +#define DIV_CEIL(lhs, rhs) (((lhs) + (rhs) - 1) / (rhs)) + #define NODISCARD __attribute__((__warn_unused_result__)) #define NOINLINE __attribute__((noinline)) #define STATIC_ASSERT(x) _Static_assert(x, "Static assertion failed") #define PRINTF_LIKE(a, b) __attribute__((format(printf, a, b))) -static inline uintptr_t round_up(uintptr_t x, size_t align) { - return (x + (align - 1)) & ~(align - 1); -} - -static inline uintptr_t round_down(uintptr_t x, size_t align) { - return x & ~(align - 1); -} - -static inline size_t div_ceil(size_t lhs, size_t rhs) { - return (lhs + rhs - 1) / rhs; -} - static inline size_t next_power_of_two(size_t x) { if (x <= 1) return 1; diff --git a/kernel/console/screen/psf.c b/kernel/console/screen/psf.c index 9c8b0c41..3f479d82 100644 --- a/kernel/console/screen/psf.c +++ b/kernel/console/screen/psf.c @@ -78,7 +78,7 @@ static struct font* load_psf2(struct file* file) { font->glyph_width = header.width; font->glyph_height = header.height; font->bytes_per_glyph = header.bytesperglyph; - if (div_ceil(font->glyph_width, 8) * font->glyph_height != + if (DIV_CEIL(font->glyph_width, 8) * font->glyph_height != font->bytes_per_glyph) { kfree(font); return ERR_PTR(-EINVAL); diff --git a/kernel/console/vt.c b/kernel/console/vt.c index ab7612ba..f22909ff 100644 --- a/kernel/console/vt.c +++ b/kernel/console/vt.c @@ -150,7 +150,7 @@ static void handle_ground(struct vt* vt, char c) { set_cursor(vt, vt->cursor_x - 1, vt->cursor_y); break; case '\t': - set_cursor(vt, round_up(vt->cursor_x + 1, TAB_STOP), vt->cursor_y); + set_cursor(vt, ROUND_UP(vt->cursor_x + 1, TAB_STOP), vt->cursor_y); break; default: if ((unsigned)c > 127) diff --git a/kernel/containers/vec.c b/kernel/containers/vec.c index 7bdbf837..035e88ff 100644 --- a/kernel/containers/vec.c +++ b/kernel/containers/vec.c @@ -23,7 +23,7 @@ NODISCARD static int grow_capacity(struct vec* vec, size_t requested_size) { new_capacity = MAX(new_capacity, requested_size); if (new_capacity == 0) new_capacity = PAGE_SIZE; - new_capacity = round_up(new_capacity, PAGE_SIZE); + new_capacity = ROUND_UP(new_capacity, PAGE_SIZE); if (new_capacity == 0) return -EOVERFLOW; ASSERT(new_capacity > vec->capacity); diff --git a/kernel/drivers/virtio/virtio.c b/kernel/drivers/virtio/virtio.c index 6ce0c4e1..64af9101 100644 --- a/kernel/drivers/virtio/virtio.c +++ b/kernel/drivers/virtio/virtio.c @@ -24,9 +24,9 @@ static struct virtq* virtq_create(uint16_t queue_size) { size_t used_size = sizeof(struct virtq_used) + sizeof(struct virtq_used_elem) * queue_size; - size_t alloc_size = round_up(sizeof(struct virtq), DESC_ALIGN); - alloc_size = round_up(alloc_size + desc_size, AVAIL_ALIGN); - alloc_size = round_up(alloc_size + avail_size, USED_ALIGN); + size_t alloc_size = ROUND_UP(sizeof(struct virtq), DESC_ALIGN); + alloc_size = ROUND_UP(alloc_size + desc_size, AVAIL_ALIGN); + alloc_size = ROUND_UP(alloc_size + avail_size, USED_ALIGN); alloc_size += used_size; struct virtq* virtq = kaligned_alloc(DESC_ALIGN, alloc_size); @@ -38,15 +38,15 @@ static struct virtq* virtq_create(uint16_t queue_size) { virtq->num_free_descs = queue_size; uintptr_t ptr = - round_up((uintptr_t)virtq + sizeof(struct virtq), DESC_ALIGN); + ROUND_UP((uintptr_t)virtq + sizeof(struct virtq), DESC_ALIGN); ASSERT(ptr % DESC_ALIGN == 0); virtq->desc = (struct virtq_desc*)ptr; - ptr = round_up(ptr + desc_size, AVAIL_ALIGN); + ptr = ROUND_UP(ptr + desc_size, AVAIL_ALIGN); ASSERT(ptr % AVAIL_ALIGN == 0); virtq->avail = (struct virtq_avail*)ptr; - ptr = round_up(ptr + avail_size, USED_ALIGN); + ptr = ROUND_UP(ptr + avail_size, USED_ALIGN); ASSERT(ptr % USED_ALIGN == 0); virtq->used = (struct virtq_used*)ptr; diff --git a/kernel/exec.c b/kernel/exec.c index be776f7f..02ed9051 100644 --- a/kernel/exec.c +++ b/kernel/exec.c @@ -255,9 +255,9 @@ static int execve(const char* pathname, struct string_vec* argv, ehdr->e_phoff < phdr->p_offset + phdr->p_filesz) phdr_virt_addr = ehdr->e_phoff - phdr->p_offset + phdr->p_vaddr; - uintptr_t region_start = round_down(phdr->p_vaddr, PAGE_SIZE); + uintptr_t region_start = ROUND_DOWN(phdr->p_vaddr, PAGE_SIZE); uintptr_t region_end = - round_up(phdr->p_vaddr + phdr->p_memsz, PAGE_SIZE); + ROUND_UP(phdr->p_vaddr + phdr->p_memsz, PAGE_SIZE); size_t region_size = region_end - region_start; void* addr = vm_alloc_at((void*)region_start, region_size, VM_READ | VM_WRITE | VM_USER); @@ -353,7 +353,7 @@ static int execve(const char* pathname, struct string_vec* argv, kfree(exe_buf); exe_buf = NULL; - sp = round_down(sp, 16); + sp = ROUND_DOWN(sp, 16); for (ssize_t i = ARRAY_SIZE(auxv) - 1; i >= 0; --i) { Elf32_auxv_t* aux = auxv + i; diff --git a/kernel/memory/page.c b/kernel/memory/page.c index 175609fa..895ac701 100644 --- a/kernel/memory/page.c +++ b/kernel/memory/page.c @@ -71,7 +71,7 @@ static struct memory_stats stats; static void bitmap_init(const multiboot_info_t* mb_info, uintptr_t lower_bound, uintptr_t upper_bound) { - bitmap_len = div_ceil(upper_bound, PAGE_SIZE * 32); + bitmap_len = DIV_CEIL(upper_bound, PAGE_SIZE * 32); ASSERT(bitmap_len <= BITMAP_MAX_LEN); if (mb_info->flags & MULTIBOOT_INFO_MEM_MAP) { @@ -98,12 +98,12 @@ static void bitmap_init(const multiboot_info_t* mb_info, uintptr_t lower_bound, if (entry_start >= entry_end) continue; - for (size_t i = div_ceil(entry_start, PAGE_SIZE); + for (size_t i = DIV_CEIL(entry_start, PAGE_SIZE); i < entry_end / PAGE_SIZE; ++i) bitmap_set(i); } } else { - for (size_t i = div_ceil(lower_bound, PAGE_SIZE); + for (size_t i = DIV_CEIL(lower_bound, PAGE_SIZE); i < upper_bound / PAGE_SIZE; ++i) bitmap_set(i); } @@ -115,7 +115,7 @@ static void bitmap_init(const multiboot_info_t* mb_info, uintptr_t lower_bound, kprintf("page: module P0x%08x - P0x%08x (%u MiB)\n", mod->mod_start, mod->mod_end, (mod->mod_end - mod->mod_start) / 0x100000); for (size_t i = mod->mod_start / PAGE_SIZE; - i < div_ceil(mod->mod_end, PAGE_SIZE); ++i) + i < DIV_CEIL(mod->mod_end, PAGE_SIZE); ++i) bitmap_clear(i); ++mod; } diff --git a/kernel/memory/page_table.c b/kernel/memory/page_table.c index e110a777..047b9913 100644 --- a/kernel/memory/page_table.c +++ b/kernel/memory/page_table.c @@ -438,7 +438,7 @@ void page_table_unmap(uintptr_t virt_addr, uintptr_t size) { ASSERT((virt_addr % PAGE_SIZE) == 0); ASSERT((size % PAGE_SIZE) == 0); - uintptr_t virt_end = virt_addr + round_up(size, PAGE_SIZE); + uintptr_t virt_end = virt_addr + ROUND_UP(size, PAGE_SIZE); for (uintptr_t virt_cursor = virt_addr; virt_cursor < virt_end; virt_cursor += PAGE_SIZE) { volatile page_table_entry* pte = get_pte(virt_cursor); @@ -453,7 +453,7 @@ void page_table_set_flags(uintptr_t virt_addr, uintptr_t size, uint16_t flags) { ASSERT((virt_addr % PAGE_SIZE) == 0); ASSERT((size % PAGE_SIZE) == 0); - uintptr_t virt_end = virt_addr + round_up(size, PAGE_SIZE); + uintptr_t virt_end = virt_addr + ROUND_UP(size, PAGE_SIZE); for (uintptr_t virt_cursor = virt_addr; virt_cursor < virt_end; virt_cursor += PAGE_SIZE) { volatile page_table_entry* pte = get_pte(virt_cursor); diff --git a/kernel/memory/vm.c b/kernel/memory/vm.c index cec7e590..91b3d516 100644 --- a/kernel/memory/vm.c +++ b/kernel/memory/vm.c @@ -613,8 +613,8 @@ static int unmap(struct vm* vm, void* virt_addr, size_t size) { // original range. // Returns the aligned start address. The size is updated to the aligned size. static uintptr_t page_align_range(uintptr_t start, size_t* size) { - uintptr_t aligned_start = round_down(start, PAGE_SIZE); - uintptr_t aligned_end = round_up(start + *size, PAGE_SIZE); + uintptr_t aligned_start = ROUND_DOWN(start, PAGE_SIZE); + uintptr_t aligned_end = ROUND_UP(start + *size, PAGE_SIZE); *size = aligned_end - aligned_start; return aligned_start; } @@ -624,7 +624,7 @@ void* vm_alloc(size_t size, int vm_flags) { return ERR_PTR(-EINVAL); if (!validate_vm_flags(vm_flags)) return ERR_PTR(-EINVAL); - size = round_up(size, PAGE_SIZE); + size = ROUND_UP(size, PAGE_SIZE); struct vm* vm = vm_for_flags(vm_flags); mutex_lock(&vm->lock); @@ -745,7 +745,7 @@ int vm_free(void* addr) { mutex_unlock(&vm->lock); return -ENOENT; } - if (region->start != round_down((uintptr_t)addr, PAGE_SIZE)) { + if (region->start != ROUND_DOWN((uintptr_t)addr, PAGE_SIZE)) { mutex_unlock(&vm->lock); return -EINVAL; } diff --git a/kernel/random.c b/kernel/random.c index 4c971432..315f1720 100644 --- a/kernel/random.c +++ b/kernel/random.c @@ -136,10 +136,10 @@ static bool fill_remainder(unsigned char* buffer, uint8_t count) { ssize_t random_get(void* buffer, size_t count) { unsigned char* prefix = buffer; unsigned char* aligned = - (unsigned char*)round_up((uintptr_t)buffer, alignof(uint32_t)); + (unsigned char*)ROUND_UP((uintptr_t)buffer, alignof(uint32_t)); unsigned char* end = prefix + count; unsigned char* suffix = - (unsigned char*)round_down((uintptr_t)end, alignof(uint32_t)); + (unsigned char*)ROUND_DOWN((uintptr_t)end, alignof(uint32_t)); if (prefix < aligned) { if (!fill_remainder(prefix, aligned - prefix)) diff --git a/kernel/smp.c b/kernel/smp.c index 08f17742..0d9d7592 100644 --- a/kernel/smp.c +++ b/kernel/smp.c @@ -87,7 +87,7 @@ void smp_start(void) { // APs start in real mode (no paging), so they need identity mapping of // the initialization code. - size_t init_size = round_up((uintptr_t)init_end, PAGE_SIZE); + size_t init_size = ROUND_UP((uintptr_t)init_end, PAGE_SIZE); ASSERT_OK(page_table_map_phys(0, 0, init_size, PTE_WRITE)); kprintf("smp: starting %u APs\n", num_cpus - 1); diff --git a/kernel/syscall/fs.c b/kernel/syscall/fs.c index 9cfb5cd0..e8b859d4 100644 --- a/kernel/syscall/fs.c +++ b/kernel/syscall/fs.c @@ -727,7 +727,7 @@ static ssize_t fill_dir_old(void* user_buf, size_t buf_size, const char* name, size_t name_size = name_len + 1; size_t rec_len = offsetof(struct linux_old_dirent, d_name) // + name_size; // d_name - rec_len = round_up(rec_len, alignof(struct linux_old_dirent)); + rec_len = ROUND_UP(rec_len, alignof(struct linux_old_dirent)); if (buf_size < rec_len) return 0; @@ -753,7 +753,7 @@ static ssize_t fill_dir(void* user_buf, size_t buf_size, const char* name, + name_size // d_name + sizeof(char) // pad + sizeof(char); // d_type - rec_len = round_up(rec_len, alignof(struct linux_dirent)); + rec_len = ROUND_UP(rec_len, alignof(struct linux_dirent)); if (buf_size < rec_len) return 0; @@ -779,7 +779,7 @@ static ssize_t fill_dir64(void* user_buf, size_t buf_size, const char* name, size_t name_size = strlen(name) + 1; size_t rec_len = offsetof(struct linux_dirent64, d_name) // + name_size; // d_name - rec_len = round_up(rec_len, alignof(struct linux_dirent64)); + rec_len = ROUND_UP(rec_len, alignof(struct linux_dirent64)); if (buf_size < rec_len) return 0; diff --git a/kernel/syscall/select.c b/kernel/syscall/select.c index eba98f04..fd443e53 100644 --- a/kernel/syscall/select.c +++ b/kernel/syscall/select.c @@ -183,7 +183,7 @@ int sys_select(int nfds, unsigned long* user_readfds, struct pollfd* pollfds = NULL; if (nfds > 0) { - size_t num_fd_longs = div_ceil(nfds, NUM_FD_BITS); + size_t num_fd_longs = DIV_CEIL(nfds, NUM_FD_BITS); num_fd_bytes = sizeof(unsigned long) * num_fd_longs; fds = kmalloc(num_fd_bytes * 3); diff --git a/kernel/task.c b/kernel/task.c index 244596dd..20822479 100644 --- a/kernel/task.c +++ b/kernel/task.c @@ -572,7 +572,7 @@ void task_handle_signal(struct registers* regs, int signum, ASSERT(0 < signum && signum < NSIG); ASSERT(action->sa_flags & SA_RESTORER); - uintptr_t esp = round_down(regs->user_esp, 16); + uintptr_t esp = ROUND_DOWN(regs->user_esp, 16); // Push the context of the interrupted task struct sigcontext ctx = { diff --git a/userland/lib/crt0.c b/userland/lib/crt0.c index ee92f577..c5d3daec 100644 --- a/userland/lib/crt0.c +++ b/userland/lib/crt0.c @@ -37,7 +37,7 @@ struct pthread* __init_tls(void* tls) { memset(tls, 0, __tls_size); uintptr_t p = (uintptr_t)tls + __tls_size - sizeof(struct pthread); - p = round_down((uintptr_t)p, tls_phdr->p_align); + p = ROUND_DOWN(p, tls_phdr->p_align); memcpy((unsigned char*)p - tls_phdr->p_memsz, (void*)tls_phdr->p_vaddr, tls_phdr->p_filesz); diff --git a/userland/lib/stdlib.c b/userland/lib/stdlib.c index 5021706a..8aaacb0a 100644 --- a/userland/lib/stdlib.c +++ b/userland/lib/stdlib.c @@ -48,7 +48,7 @@ void* aligned_alloc(size_t alignment, size_t size) { ASSERT(alignment <= getauxval(AT_PAGESZ)); size_t data_offset = - round_up(offsetof(struct malloc_header, data), alignment); + ROUND_UP(offsetof(struct malloc_header, data), alignment); size_t real_size = data_offset + size; void* addr = mmap(NULL, real_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); @@ -79,7 +79,7 @@ void* calloc(size_t num, size_t size) { static struct malloc_header* header_from_ptr(void* ptr) { size_t page_size = getauxval(AT_PAGESZ); - uintptr_t addr = round_down((uintptr_t)ptr, page_size); + uintptr_t addr = ROUND_DOWN((uintptr_t)ptr, page_size); if ((uintptr_t)ptr - addr < sizeof(struct malloc_header)) addr -= page_size; diff --git a/userland/ls.c b/userland/ls.c index 50b06d65..f3cfe4c5 100644 --- a/userland/ls.c +++ b/userland/ls.c @@ -122,9 +122,9 @@ static int list_dir(const char* path, size_t terminal_width, bool long_format) { putchar('\n'); } else { - size_t next_pos = round_up(x_pos + len + 1, TAB_STOP); + size_t next_pos = ROUND_UP(x_pos + len + 1, TAB_STOP); if (next_pos >= terminal_width) { - x_pos = round_up(len + 1, TAB_STOP); + x_pos = ROUND_UP(len + 1, TAB_STOP); putchar('\n'); } else { x_pos = next_pos;