Skip to content

Commit

Permalink
Do not use size_t definition and replace all with uint64_t
Browse files Browse the repository at this point in the history
  • Loading branch information
jjwang committed Oct 27, 2024
1 parent 595b2ad commit 24e1b1a
Show file tree
Hide file tree
Showing 45 changed files with 309 additions and 306 deletions.
2 changes: 1 addition & 1 deletion kernel/base/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void ht_init(ht_t *ht, uint64_t size)
{
ht->size = size;
ht->array = (ht_item_t*)kmalloc(ht->size * sizeof(ht_item_t));
for (size_t i = 0; i < ht->size; i++) {
for (uint64_t i = 0; i < ht->size; i++) {
ht->array[i].key = -1;
ht->array[i].data = NULL;
}
Expand Down
5 changes: 4 additions & 1 deletion kernel/base/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ bool bmp_load_from_file(image_t *image, char *fn)
return false;
}

size_t bmp_size = vfs_tell(fh);
uint64_t bmp_size = vfs_tell(fh);
uint8_t *bmp_buff = kmalloc(bmp_size);

if (bmp_buff != NULL) {
vfs_read(fh, bmp_size, bmp_buff);
} else {
kloge("bmp: cannot malloc %d bytes\n", bmp_size);
return false;
}

vfs_close(fh);
Expand Down
4 changes: 2 additions & 2 deletions kernel/base/klog.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ static void klog_putint(int mode, int64_t n, int width, bool zero_filling)
if (n == 0)
klog_putch(mode, '0');

size_t div = 1, temp = n;
uint64_t div = 1, temp = n;
while (temp > 0) {
temp /= 10;
div *= 10;
Expand Down Expand Up @@ -195,7 +195,7 @@ void klog_init()

void klog_vprintf_core(int mode, const char* s, va_list args)
{
for (size_t i = 0; s[i] != '\0'; i++) {
for (uint64_t i = 0; s[i] != '\0'; i++) {
switch (s[i]) {
case '%': {
uint32_t arg_width = 0;
Expand Down
20 changes: 10 additions & 10 deletions kernel/base/kmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

#define SLAB_ALLOCATOR_USED 1

size_t kmalloc_checkno = 0;
uint64_t kmalloc_checkno = 0;

void *kmalloc_core(uint64_t size, const char *func, size_t line)
void *kmalloc_core(uint64_t size, const char *func, uint64_t line)
{
#if SLAB_ALLOCATOR_USED != 0
if (size >= ALLOC_MAX_SIZE) {
Expand All @@ -43,7 +43,7 @@ void *kmalloc_core(uint64_t size, const char *func, size_t line)
#endif
}

void *kmalloc_chunk(uint64_t size, const char *func, size_t line)
void *kmalloc_chunk(uint64_t size, const char *func, uint64_t line)
{
memory_metadata_t *alloc = (memory_metadata_t*)
PHYS_TO_VIRT(pmm_get(NUM_PAGES(size) + 1, 0x0, func, line));
Expand All @@ -66,16 +66,16 @@ void *kmalloc_chunk(uint64_t size, const char *func, size_t line)

alloc->lineno = line;

size_t *buf = (size_t*)(((uint8_t*)alloc) + PAGE_SIZE);
uint64_t *buf = (uint64_t*)(((uint8_t*)alloc) + PAGE_SIZE);
*(buf - 1) = size;

return ((uint8_t*)alloc) + PAGE_SIZE;
}

void kmfree_core(void *addr, const char *func, size_t line)
void kmfree_core(void *addr, const char *func, uint64_t line)
{
#if SLAB_ALLOCATOR_USED != 0
size_t *buf = (size_t*)addr;
uint64_t *buf = (uint64_t*)addr;
if (*(buf - 1) >= ALLOC_MAX_SIZE) {
klogd("kmfree: %s:%d will free %d bytes memory (>= %d)\n",
func, line, ALLOC_MAX_SIZE);
Expand All @@ -88,7 +88,7 @@ void kmfree_core(void *addr, const char *func, size_t line)
#endif
}

void kmfree_chunk(void *addr, const char *func, size_t line)
void kmfree_chunk(void *addr, const char *func, uint64_t line)
{
(void)func;

Expand All @@ -102,7 +102,7 @@ void kmfree_chunk(void *addr, const char *func, size_t line)
}
}

void *kmrealloc_core(void *addr, size_t newsize, const char *func, size_t line)
void *kmrealloc_core(void *addr, uint64_t newsize, const char *func, uint64_t line)
{
if (newsize >= ALLOC_MAX_SIZE) {
klogd("kmalloc: realloc %d bytes (>= %d)\n",
Expand All @@ -113,7 +113,7 @@ void *kmrealloc_core(void *addr, size_t newsize, const char *func, size_t line)
if (!addr)
return kmalloc_core(newsize, func, line);

size_t *buf = (size_t*)addr;
uint64_t *buf = (uint64_t*)addr;
void *newaddr = kmalloc_core(newsize, func, line);
if (newaddr != NULL) {
memcpy(newaddr, addr, MIN(*(buf - 1), newsize));
Expand All @@ -125,7 +125,7 @@ void *kmrealloc_core(void *addr, size_t newsize, const char *func, size_t line)
#endif
}

void *kmrealloc_chunk(void *addr, size_t newsize, const char *func, size_t line)
void *kmrealloc_chunk(void *addr, uint64_t newsize, const char *func, uint64_t line)
{
void *ret_addr = NULL;

Expand Down
26 changes: 13 additions & 13 deletions kernel/base/kmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@
#define MEM_MAGIC_NUM 0xABEEABEE

typedef struct {
size_t magic;
size_t checkno;
size_t numpages;
size_t size;
char filename[512];
size_t lineno;
uint64_t magic;
uint64_t checkno;
uint64_t numpages;
uint64_t size;
char filename[512];
uint64_t lineno;
} memory_metadata_t;

extern size_t kmalloc_checkno;
extern uint64_t kmalloc_checkno;

void* kmalloc_core(uint64_t size, const char *func, size_t line);
void kmfree_core(void* addr, const char *func, size_t line);
void* kmrealloc_core(void* addr, size_t newsize, const char *func, size_t line);
void* kmalloc_core(uint64_t size, const char *func, uint64_t line);
void kmfree_core(void* addr, const char *func, uint64_t line);
void* kmrealloc_core(void* addr, uint64_t newsize, const char *func, uint64_t line);

void* kmalloc_chunk(uint64_t size, const char *func, size_t line);
void kmfree_chunk(void* addr, const char *func, size_t line);
void* kmrealloc_chunk(void* addr, size_t newsize, const char *func, size_t line);
void* kmalloc_chunk(uint64_t size, const char *func, uint64_t line);
void kmfree_chunk(void* addr, const char *func, uint64_t line);
void* kmrealloc_chunk(void* addr, uint64_t newsize, const char *func, uint64_t line);

#define kmalloc(x) kmalloc_core(x, __func__, __LINE__)
#define kmfree(x) kmfree_core(x, __func__, __LINE__)
Expand Down
8 changes: 4 additions & 4 deletions kernel/base/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

#define vec_struct(type) \
struct { \
size_t len; \
size_t capacity; \
type* data; \
uint64_t len; \
uint64_t capacity; \
type* data; \
}

#define vec_extern(type, name) extern vec_struct(type) name
Expand Down Expand Up @@ -68,7 +68,7 @@

#define vec_erase_val(vec, val) \
{ \
for(size_t __i = 0; __i < (vec)->len; __i++) { \
for(int64_t __i = 0; __i < (vec)->len; __i++) { \
if (vec_at(vec, __i) == (val)) { \
vec_erase(vec, __i); \
break; \
Expand Down
38 changes: 19 additions & 19 deletions kernel/device/display/fb.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,24 @@ bool fb_set_bg_image(fb_info_t *fb, image_t *img)
fb->swapbuffer = (uint8_t*)kmalloc(fb->width * fb->height * 4);
}

for (size_t y = 0; y < fb->height; y++) {
for (size_t x = 0; x < fb->width; x++) {
size_t x2, y2;
for (uint64_t y = 0; y < fb->height; y++) {
for (uint64_t x = 0; x < fb->width; x++) {
uint64_t x2, y2;

/* Determine the best-fit point's (x, y) in original bitmap */
if (img->img_width == fb->width && img->img_height == fb->height) {
x2 = x;
y2 = y;
} else {
size_t x_new, y_new, x_rb, y_rb;
uint64_t x_new, y_new, x_rb, y_rb;
x_new = x * 100 * img->img_width / fb->width;
y_new = y * 100 * img->img_height / fb->height;
x2 = MAX(MIN(DIV_ROUNDUP(x_new, 100), img->img_width), 1);
y2 = MAX(MIN(DIV_ROUNDUP(y_new, 100), img->img_height), 1);

uint64_t max_dist = (100 ^ 4), cur_dist;
for (size_t k = 0; k < 2; k++) {
for (size_t m = 0; m < 2; m++) {
for (uint64_t k = 0; k < 2; k++) {
for (uint64_t m = 0; m < 2; m++) {
cur_dist = ((x_new - (x_rb - k) * 100) ^ 2)
+ ((y_new - (y_rb - m) * 100) ^ 2);
if (cur_dist < max_dist) {
Expand All @@ -80,7 +80,7 @@ bool fb_set_bg_image(fb_info_t *fb, image_t *img)
}
}

size_t off = img->pitch * (img->img_height - 1 - y2);
uint64_t off = img->pitch * (img->img_height - 1 - y2);
uint8_t *img_pixel = (uint8_t*)img->img + off + x2 * img->bpp / 8;
uint8_t r, g, b;
uint8_t shift = 2;
Expand All @@ -106,8 +106,8 @@ void fb_putch(fb_info_t *fb, uint32_t x, uint32_t y,
font_psf1_t *term_font = bold ? &term_font_bold : &term_font_norm;
uint32_t offset = ((uint32_t)ch) * term_font->charsize;
static const uint8_t masks[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
for (size_t i = 0; i < FONT_HEIGHT; i++) {
for (size_t k = 0; k < FONT_WIDTH; k++) {
for (uint64_t i = 0; i < FONT_HEIGHT; i++) {
for (uint64_t k = 0; k < FONT_WIDTH; k++) {
if (i < term_font->charsize
&& (term_font->data[offset + i] & masks[k]))
{
Expand All @@ -124,16 +124,16 @@ void fb_putlogo(fb_info_t *fb, uint32_t fgcolor, uint32_t bgcolor)
if((uint64_t)fb->addr == (uint64_t)fb->backbuffer) return;

char *logo = "HNK";
size_t len = strlen(logo);
uint64_t len = strlen(logo);
uint32_t x = (fb->width - len * 8 * LOGO_SCALE) / 2;
uint32_t y = (fb->height - 16 * LOGO_SCALE) / 2;
for (size_t idx = 0; idx < len; idx++) {
for (uint64_t idx = 0; idx < len; idx++) {
uint32_t offset = ((uint32_t)logo[idx]) * term_font_bold.charsize;
static const uint8_t masks[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
for(size_t i = 0; i < term_font_bold.charsize; i++) {
for(size_t k = 0; k < 8; k++) {
for(size_t m = 1; m < LOGO_SCALE; m++) {
for(size_t n = 1; n < LOGO_SCALE; n++) {
for(uint64_t i = 0; i < term_font_bold.charsize; i++) {
for(uint64_t k = 0; k < 8; k++) {
for(uint64_t m = 1; m < LOGO_SCALE; m++) {
for(uint64_t n = 1; n < LOGO_SCALE; n++) {
uint32_t bg_img_color = bgcolor;
if (fb->bgbuffer != NULL) {
bg_img_color = ((uint32_t*)(fb->bgbuffer + fb->pitch * y + x * 4))[0];
Expand All @@ -151,9 +151,9 @@ void fb_putlogo(fb_info_t *fb, uint32_t fgcolor, uint32_t bgcolor)
char desc_str[64] = "- Microkernel-based General Purpose OS Kernel for x86-64 v";
strcat(desc_str, VERSION);
strcat(desc_str, " -");
size_t desc_len = strlen(desc_str);
uint64_t desc_len = strlen(desc_str);
x = (fb->width - 8 * desc_len) / 2;
for (size_t desc_idx = 0; desc_idx < desc_len; desc_idx++) {
for (uint64_t desc_idx = 0; desc_idx < desc_len; desc_idx++) {
fb_putch(fb, x + 8 * desc_idx, (fb->height + 16 * LOGO_SCALE) / 2,
COLOR_GREY, bgcolor, desc_str[desc_idx], false);
}
Expand Down Expand Up @@ -219,8 +219,8 @@ void fb_refresh(fb_info_t *fb)
memcpy(fb->swapbuffer, fb->bgbuffer, len);
uint32_t *src = (uint32_t*)fb->backbuffer;
uint32_t *dst = (uint32_t*)fb->swapbuffer;
size_t pt_num = len / 4;
for (size_t i = 0; i < pt_num; i++) {
uint64_t pt_num = len / 4;
for (uint64_t i = 0; i < pt_num; i++) {
if (src[i] != DEFAULT_BGCOLOR) dst[i] = src[i];
}
memcpy(fb->addr, fb->swapbuffer, len);
Expand Down
4 changes: 2 additions & 2 deletions kernel/device/display/gfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void gfx_init_mem_manager(gfx_pci_t* pci, gfx_gtt_t* gtt, gfx_mem_manager_t *mgr
mgr->priv.top = ((uint64_t)gtt->num_total_entries) << GTT_PAGE_SHIFT;

/* Clear all fence registers (provide linear access to mem to cpu) */
for (size_t fence_num = 0; fence_num < FENCE_COUNT; fence_num++) {
for (uint64_t fence_num = 0; fence_num < FENCE_COUNT; fence_num++) {
gfx_outl(pci, FENCE_BASE + sizeof(uint64_t) * fence_num, 0);
}

Expand Down Expand Up @@ -235,7 +235,7 @@ bool pci_get_gfx_device(pci_device_t *gfx_dev)
bool found = false;

/* Find Intel HD graphics device */
for (size_t i = 0; i < vec_length(&pci_devices); i++) {
for (uint64_t i = 0; i < vec_length(&pci_devices); i++) {
dev = vec_at(&pci_devices, i);
if ((dev.vendor_id != VENDOR_INTEL) ||
(dev.device_id != DEVICE_HD5500 && dev.device_id != DEVICE_HD520))
Expand Down
32 changes: 16 additions & 16 deletions kernel/device/display/term.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ bool term_parse_cmd(term_info_t* term_act, uint8_t byte)

if (mode >= 0)
{
for (size_t y = term_act->cursor_y * FONT_HEIGHT;
for (uint64_t y = term_act->cursor_y * FONT_HEIGHT;
y < MIN((term_act->cursor_y + 1) * FONT_HEIGHT,
term_act->fb.height);
y++)
{
for (size_t x = 0; x < term_act->fb.width; x++) {
for (uint64_t x = 0; x < term_act->fb.width; x++) {
if (!(mode == 0 && x >= term_act->cursor_x * FONT_WIDTH))
continue;
if (!(mode == 1 && x < term_act->cursor_x * FONT_WIDTH))
Expand All @@ -175,8 +175,8 @@ bool term_parse_cmd(term_info_t* term_act, uint8_t byte)
goto err;
}
if (term_act->cparams[0] == 0) {
for (size_t y = 0; y < term_act->fb.height; y++) {
for (size_t x = 0; x < term_act->fb.width; x++) {
for (uint64_t y = 0; y < term_act->fb.height; y++) {
for (uint64_t x = 0; x < term_act->fb.width; x++) {
if (y >= term_act->cursor_y * FONT_HEIGHT
&& y < (term_act->cursor_y + 1) * FONT_HEIGHT)
{
Expand All @@ -188,8 +188,8 @@ bool term_parse_cmd(term_info_t* term_act, uint8_t byte)
}
}
} else if (term_act->cparams[0] == 1) {
for (size_t y = 0; y < term_act->fb.height; y++) {
for (size_t x = 0; x < term_act->fb.width; x++) {
for (uint64_t y = 0; y < term_act->fb.height; y++) {
for (uint64_t x = 0; x < term_act->fb.width; x++) {
if (y >= term_act->cursor_y * FONT_HEIGHT
&& y < (term_act->cursor_y + 1) * FONT_HEIGHT)
{
Expand All @@ -201,8 +201,8 @@ bool term_parse_cmd(term_info_t* term_act, uint8_t byte)
}
}
} else if (term_act->cparams[0] == 2) {
for (size_t y = 0; y < term_act->fb.height; y++) {
for (size_t x = 0; x < term_act->fb.width; x++) {
for (uint64_t y = 0; y < term_act->fb.height; y++) {
for (uint64_t x = 0; x < term_act->fb.width; x++) {
fb_putpixel(&(term_act->fb), x, y, term_act->bgcolor);
}
}
Expand All @@ -215,7 +215,7 @@ bool term_parse_cmd(term_info_t* term_act, uint8_t byte)
term_act->bold = false;
}

size_t idx = 0;
uint64_t idx = 0;
if (term_act->cparamcount > 0) idx = term_act->cparamcount - 1;
if (term_act->cparams[term_act->cparamcount - 1] == 0) {
term_act->fgcolor = DEFAULT_FGCOLOR;
Expand Down Expand Up @@ -310,18 +310,18 @@ void term_scroll(term_info_t* term_act)
return;
}

for (size_t y = 0; y < (term_act->cursor_y - 1) * FONT_HEIGHT; y++) {
for (size_t x = 0; x < term_act->fb.width; x++) {
for (uint64_t y = 0; y < (term_act->cursor_y - 1) * FONT_HEIGHT; y++) {
for (uint64_t x = 0; x < term_act->fb.width; x++) {
uint32_t c = fb_getpixel(&(term_act->fb), x, y + FONT_HEIGHT);
fb_putpixel(&(term_act->fb), x, y, c);
}
}

for (size_t y = (term_act->cursor_y - 1) * FONT_HEIGHT;
for (uint64_t y = (term_act->cursor_y - 1) * FONT_HEIGHT;
y < term_act->fb.height;
y++)
{
for (size_t x = 0; x < term_act->fb.width; x++) {
for (uint64_t x = 0; x < term_act->fb.width; x++) {
fb_putpixel(&(term_act->fb), x, y, term_act->bgcolor);
}
}
Expand Down Expand Up @@ -396,8 +396,8 @@ void term_clear(int mode)
term_act->fb.width * term_act->fb.height * 4);
}

for (size_t y = 0; y < term_act->fb.height; y++)
for (size_t x = 0; x < term_act->fb.width; x++)
for (uint64_t y = 0; y < term_act->fb.height; y++)
for (uint64_t x = 0; x < term_act->fb.width; x++)
fb_putpixel(&(term_act->fb), x, y, term_act->bgcolor);

term_act->cursor_x = 0;
Expand Down Expand Up @@ -563,7 +563,7 @@ void term_init(struct limine_framebuffer* s)

term_lock = lock_new();

for (size_t i = 0; i <= 1; i++) {
for (uint64_t i = 0; i <= 1; i++) {
term_act = ((i == 0) ? &term_info : &term_cli);

fb_init(&(term_act->fb), s);
Expand Down
Loading

0 comments on commit 24e1b1a

Please sign in to comment.