Skip to content

Commit

Permalink
common(extra): turn round_up, round_down and div_ceil into macros
Browse files Browse the repository at this point in the history
  • Loading branch information
mosmeh committed Oct 25, 2024
1 parent 4931f04 commit 769e4a4
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 47 deletions.
16 changes: 4 additions & 12 deletions common/extra.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion kernel/console/screen/psf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion kernel/console/vt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion kernel/containers/vec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions kernel/drivers/virtio/virtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions kernel/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions kernel/memory/page.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions kernel/memory/page_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions kernel/memory/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions kernel/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions kernel/syscall/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

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

Expand Down
2 changes: 1 addition & 1 deletion kernel/syscall/select.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion kernel/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion userland/lib/crt0.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions userland/lib/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions userland/ls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 769e4a4

Please sign in to comment.