From 603a3f37027141773f72a1a63cfe647aeebaceaa Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 18 Jul 2025 16:51:56 +0200 Subject: [PATCH 01/33] updated make, string and kstring centralized makefile vars, added helper target, improved fs directory management updated kstring and string to be null-safe. --- Makefile | 75 +++++++++++++++++++++------ kernel/Makefile | 31 +++++------- kernel/kstring.c | 121 +++++++++++++++++++++++++++++++------------- shared/Makefile | 22 ++++---- shared/std/string.c | 85 +++++++++++++++++++++---------- user/Makefile | 34 ++++++------- 6 files changed, 245 insertions(+), 123 deletions(-) diff --git a/Makefile b/Makefile index 3e320cf5..18ab8921 100644 --- a/Makefile +++ b/Makefile @@ -1,36 +1,60 @@ +#top mk +#toolchain +ARCH ?= aarch64-none-elf +CC := $(ARCH)-gcc +LD := $(ARCH)-ld +AR := $(ARCH)-ar +OBJCOPY := $(ARCH)-objcopy + +#common flags +CFLAGS_BASE ?= -g -O0 -std=c17 -nostdlib -ffreestanding \ + -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables \ + -Wall -Wextra -mcpu=cortex-a72 + +LDFLAGS_BASE ?= +#build var +LOAD_ADDR ?= 0x41000000 MODE ?= virt -LOAD_ADDR ?= 0x41000000 -OS := $(shell uname) + +#export +export ARCH CC LD AR OBJCOPY CFLAGS_BASE LDFLAGS_BASE LOAD_ADDR + +#config fs +OS := $(shell uname) +FS_DIRS := fs/redos/user ifeq ($(OS),Darwin) -BOOTFS=/Volumes/bootfs +BOOTFS := /Volumes/bootfs else -BOOTFS=/media/bootfs +BOOTFS := /media/bootfs endif -.PHONY: all kernel user shared clean raspi virt run debug dump +#targets +.PHONY: all shared user kernel clean raspi virt run debug dump prepare-fs help install all: shared user kernel @echo "Build complete." ./createfs -dump: - aarch64-none-elf-objdump -D kernel.elf > dump - shared: $(MAKE) -C shared user: + $(MAKE) prepare-fs $(MAKE) -C user kernel: - $(MAKE) -C kernel LOAD_ADDR=$(LOAD_ADDR) + $(MAKE) -C kernel clean: $(MAKE) -C shared clean + $(MAKE) -C user clean $(MAKE) -C kernel clean - $(MAKE) -C user clean + @echo "removing fs dirs" + rm -rf $(FS_DIRS) + @echo "removing images" + rm -f kernel.img kernel.elf disk.img dump raspi: $(MAKE) LOAD_ADDR=0x80000 all @@ -38,16 +62,35 @@ raspi: virt: $(MAKE) LOAD_ADDR=0x41000000 all +run: + $(MAKE) $(MODE) + ./run_$(MODE) + debug: $(MAKE) $(MODE) ./rundebug MODE=$(MODE) $(ARGS) -install: - $(MAKE) clean - $(MAKE) LOAD_ADDR=0x80000 all +dump: + $(OBJCOPY) -O binary kernel.elf kernel.img + aarch64-none-elf-objdump -D kernel.elf > dump + +install: clean raspi cp kernel.img $(BOOTFS)/kernel8.img cp kernel.img $(BOOTFS)/kernel_2712.img -run: - $(MAKE) $(MODE) - ./run_$(MODE) \ No newline at end of file +prepare-fs: + @echo "creating dirs" + @mkdir -p $(FS_DIRS) + +help: + @printf "usage:\n\ + make all build the os, accepts MODE parameter\n\ + make clean remove all build artifacts\n\ + make raspi build for raspberry\n\ + make virt build for qemu virt board\n\ + make run build and run in virt mode(accepts MODE parameter)\n\ + make debug build and run with debugger(accepts MODE parameter)\n\ + make dump disassemble kernel.elf\n\ + make install create raspi kernel and mount it on a bootable partition\n\ + make prepare-fs create directories for the filesystem\n\ +\n" \ No newline at end of file diff --git a/kernel/Makefile b/kernel/Makefile index cf9780d7..9c498543 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,26 +1,23 @@ -ARCH= aarch64-none-elf -CC = $(ARCH)-gcc -LD = $(ARCH)-ld -OBJCOPY = $(ARCH)-objcopy +#kernel +CFLAGS := $(CFLAGS_BASE) -I. -I../shared -I../user +LDFLAGS := $(LDFLAGS_BASE) -T $(shell ls *.ld) --defsym=LOAD_ADDR=$(LOAD_ADDR) -CFLAGS = -g -O0 -std=c17 -nostdlib -ffreestanding -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -Wall -Wextra -mcpu=cortex-a72 -I. -I../shared -I../user +C_SRC := $(shell find . -name '*.c') +ASM_SRC := $(shell find . -name '*.S') +CPP_SRC := $(shell find . -name '*.cpp') +OBJ := $(C_SRC:.c=.o) $(ASM_SRC:.S=.o) $(CPP_SRC:.cpp=.o) +OBJL := $(filter-out ./boot.o, $(OBJ)) -LDFLAGS = -T $(shell ls *.ld) --defsym=LOAD_ADDR=$(LOAD_ADDR) +ELF := ../kernel.elf +TARGET := kernel.img -C_SRC = $(shell find . -name '*.c') -ASM_SRC = $(shell find . -name '*.S') -CPP_SRC = $(shell find . -name '*.cpp') -OBJ = $(C_SRC:.c=.o) $(CPP_SRC:.cpp=.o) $(ASM_SRC:.S=.o) -OBJL = $(filter-out ./boot.o, $(OBJ)) - -TARGET = kernel.img -ELF = kernel.elf +.PHONY: all clean all: $(TARGET) $(TARGET): ../shared/libshared.a $(OBJ) - $(LD) $(LDFLAGS) -o ../$(ELF) $(OBJL) ../shared/libshared.a - $(OBJCOPY) -O binary ../$(ELF) ../$(TARGET) + $(LD) $(LDFLAGS) -o $(ELF) $(OBJL) ../shared/libshared.a + $(OBJCOPY) -O binary $(ELF) $@ %.o: %.S $(CC) $(CFLAGS) -c $< -o $@ @@ -32,4 +29,4 @@ $(TARGET): ../shared/libshared.a $(OBJ) $(CC) $(CFLAGS) -fno-rtti -c $< -o $@ clean: - rm -f $(shell find . -name '*.o') ../$(ELF) ../$(TARGET) $(TARGET) \ No newline at end of file + rm -f $(OBJ) $(ELF) $(TARGET) diff --git a/kernel/kstring.c b/kernel/kstring.c index 3cfe197b..f12b266e 100644 --- a/kernel/kstring.c +++ b/kernel/kstring.c @@ -6,6 +6,9 @@ //TODO: we can most likely get rid of this class now and use string entirely static uint32_t compute_length(const char *s, uint32_t max_length) { + if (s == NULL) { + return 0; + } uint32_t len = 0; while ((max_length == 0 || len < max_length) && s[len] != '\0') { len++; @@ -14,47 +17,73 @@ static uint32_t compute_length(const char *s, uint32_t max_length) { } kstring kstring_l(const char *literal) { + if (literal == NULL) { + return (kstring){ .data = NULL, .length = 0 }; + } uint32_t len = compute_length(literal, 0); - char *buf = (char*)talloc(len+1); - for (uint32_t i = 0; i < len; i++) + char *buf = (char*)talloc(len + 1); + if (!buf) { + return (kstring){ .data = NULL, .length = 0 }; + } + for (uint32_t i = 0; i < len; i++) { buf[i] = literal[i]; - buf[len] = 0; + } + buf[len] = '\0'; return (kstring){ .data = buf, .length = len }; } kstring kstring_repeat(char symbol, uint32_t amount){ char *buf = (char*)talloc(amount + 1); + if (!buf) { + return (kstring){ .data = NULL, .length = 0 }; + } memset(buf, symbol, amount); - buf[amount] = 0; + buf[amount] = '\0'; return (kstring){ .data = buf, .length = amount }; } kstring kstring_tail(const char *array, uint32_t max_length){ + if (array == NULL) { + return (kstring){ .data = NULL, .length = 0 }; + } uint32_t len = compute_length(array, 0); - int offset = len-max_length; - if (offset < 0) - offset = 0; + int offset = (int)len - (int)max_length; + if (offset < 0) offset = 0; uint32_t adjusted_len = len - offset; char *buf = (char*)talloc(adjusted_len + 1); - for (uint32_t i = offset; i < len; i++) - buf[i-offset] = array[i]; - buf[len-offset] = 0; + if (!buf) { + return (kstring){ .data = NULL, .length = 0 }; + } + for (uint32_t i = 0; i < adjusted_len; i++) { + buf[i] = array[offset + i]; + } + buf[adjusted_len] = '\0'; return (kstring){ .data = buf, .length = adjusted_len }; } kstring kstring_ca_max(const char *array, uint32_t max_length) { + if (array == NULL) { + return (kstring){ .data = NULL, .length = 0 }; + } uint32_t len = compute_length(array, max_length); char *buf = (char*)talloc(len + 1); - for (uint32_t i = 0; i < len; i++) + if (!buf) { + return (kstring){ .data = NULL, .length = 0 }; + } + for (uint32_t i = 0; i < len; i++) { buf[i] = array[i]; - buf[len] = 0; + } + buf[len] = '\0'; return (kstring){ .data = buf, .length = len }; } kstring kstring_c(const char c){ char *buf = (char*)talloc(2); + if (!buf) { + return (kstring){ .data = NULL, .length = 0 }; + } buf[0] = c; - buf[1] = 0; + buf[1] = '\0'; return (kstring){ .data = buf, .length = 1 }; } @@ -104,54 +133,79 @@ bool kstring_equals(kstring a, kstring b) { } kstring kstring_format_args(const char *fmt, const uint64_t *args, uint32_t arg_count) { + if (fmt == NULL) { + return (kstring){ .data = NULL, .length = 0 }; + } + // args può essere NULL se arg_count==0, altrimenti va validato + if (arg_count > 0 && args == NULL) { + return (kstring){ .data = NULL, .length = 0 }; + } + char *buf = (char*)talloc(256); + if (!buf) { + return (kstring){ .data = NULL, .length = 0 }; + } + uint32_t len = 0; uint32_t arg_index = 0; - for (uint32_t i = 0; fmt[i] && len < 255; i++) { if (fmt[i] == '%' && fmt[i+1]) { i++; - if (arg_index >= arg_count) break; - if (fmt[i] == 'x') { + if (arg_index >= arg_count) { + // troppi placeholder, copio il resto letterale + buf[len++] = '%'; + buf[len++] = fmt[i]; + continue; + } + switch (fmt[i]) { + case 'x': { uint64_t val = args[arg_index++]; kstring hex = kstring_from_hex(val); for (uint32_t j = 0; j < hex.length && len < 255; j++) buf[len++] = hex.data[j]; - temp_free(hex.data,hex.length); - } else if (fmt[i] == 'b') { + temp_free(hex.data, hex.length); + break; + } + case 'b': { uint64_t val = args[arg_index++]; kstring bin = kstring_from_bin(val); for (uint32_t j = 0; j < bin.length && len < 255; j++) buf[len++] = bin.data[j]; - temp_free(bin.data,bin.length); - } else if (fmt[i] == 'c') { + temp_free(bin.data, bin.length); + break; + } + case 'c': { uint64_t val = args[arg_index++]; buf[len++] = (char)val; - } else if (fmt[i] == 's') { + break; + } + case 's': { const char *str = (const char *)(uintptr_t)args[arg_index++]; - for (uint32_t j = 0; str[j] && len < 255; j++) buf[len++] = str[j]; - } else if (fmt[i] == 'i') { + if (str) { + for (uint32_t j = 0; str[j] && len < 255; j++) buf[len++] = str[j]; + } + break; + } + case 'i': { uint64_t val = args[arg_index++]; char temp[21]; uint32_t temp_len = 0; bool negative = false; - - if ((int)val < 0) { + if ((int64_t)val < 0) { negative = true; - val = (uint64_t)(-(int)val); + val = (uint64_t)(-(int64_t)val); } - do { temp[temp_len++] = '0' + (val % 10); val /= 10; - } while (val && temp_len < 20); - - if (negative && temp_len < 20) { + } while (val && temp_len < sizeof(temp)-1); + if (negative && temp_len < sizeof(temp)-1) { temp[temp_len++] = '-'; } - for (int j = temp_len - 1; j >= 0 && len < 255; j--) { buf[len++] = temp[j]; } - } else { + break; + } + default: buf[len++] = '%'; buf[len++] = fmt[i]; } @@ -159,7 +213,6 @@ kstring kstring_format_args(const char *fmt, const uint64_t *args, uint32_t arg_ buf[len++] = fmt[i]; } } - - buf[len] = 0; + buf[len] = '\0'; return (kstring){ .data = buf, .length = len }; } \ No newline at end of file diff --git a/shared/Makefile b/shared/Makefile index fb7390db..8fe0d3d0 100644 --- a/shared/Makefile +++ b/shared/Makefile @@ -1,21 +1,19 @@ -ARCH= aarch64-none-elf -CC = $(ARCH)-gcc -AR = $(ARCH)-ar -OBJCOPY = $(ARCH)-objcopy +#shared +CFLAGS := $(CFLAGS_BASE) -I. -I../kernel -Wno-unused-parameter -CFLAGS = -g -O0 -std=c17 -nostdlib -nolibc -ffreestanding -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -Wall -Wextra -mcpu=cortex-a72 -I. -I../kernel -Wno-unused-parameter +C_SRC := $(shell find . -name '*.c') +CPP_SRC := $(shell find . -name '*.cpp') +ASM_SRC := $(shell find . -name '*.S') +OBJ := $(C_SRC:.c=.o) $(ASM_SRC:.S=.o) $(CPP_SRC:.cpp=.o) -C_SRC = $(shell find . -name '*.c') -CPP_SRC = $(shell find . -name '*.cpp') -ASM_SRC = $(shell find . -name '*.S') -OBJ = $(C_SRC:.c=.o) $(ASM_SRC:.S=.o) $(CPP_SRC:.cpp=.o) +TARGET := libshared.a -TARGET = libshared.a +.PHONY: all clean all: $(TARGET) $(TARGET): $(OBJ) - $(AR) rcs $(TARGET) $(OBJ) + $(AR) rcs $@ $^ %.o: %.S $(CC) $(CFLAGS) -c $< -o $@ @@ -27,4 +25,4 @@ $(TARGET): $(OBJ) $(CC) $(CFLAGS) -fno-rtti -c $< -o $@ clean: - rm -f $(shell find . -name '*.o') $(TARGET) \ No newline at end of file + rm -f $(OBJ) $(TARGET) diff --git a/shared/std/string.c b/shared/std/string.c index b0ee1623..8b134769 100644 --- a/shared/std/string.c +++ b/shared/std/string.c @@ -3,7 +3,10 @@ #include "memory/memory_access.h" #include "std/memfunctions.h" -static uint32_t compute_length( char *s, uint32_t max_length) { +static uint32_t compute_length(char *s, uint32_t max_length) { + if (s == NULL) { + return 0; + } uint32_t len = 0; while ((max_length == 0 || len < max_length) && s[len] != '\0') { len++; @@ -11,13 +14,22 @@ static uint32_t compute_length( char *s, uint32_t max_length) { return len; } -string string_l( char *literal) { +string string_l(char *literal) { + if (literal == NULL) { + // ritorna stringa vuota + return (string){ .data = NULL, .length = 0, .mem_length = 0 }; + } uint32_t len = compute_length(literal, 0); - char *buf = (char*)malloc(len+1); - for (uint32_t i = 0; i < len; i++) + char *buf = (char*)malloc(len + 1); + if (!buf) { + //manage malloc + return (string){ .data = NULL, .length = 0, .mem_length = 0 }; + } + for (uint32_t i = 0; i < len; i++) { buf[i] = literal[i]; - buf[len] = 0; - return (string){ .data = buf, .length = len, .mem_length = len+1 }; + } + buf[len] = '\0'; + return (string){ .data = buf, .length = len, .mem_length = len + 1 }; } string string_repeat(char symbol, uint32_t amount){ @@ -27,26 +39,41 @@ string string_repeat(char symbol, uint32_t amount){ return (string){ .data = buf, .length = amount, .mem_length = amount+1 }; } -string string_tail( char *array, uint32_t max_length){ +string string_tail(char *array, uint32_t max_length) { + if (array == NULL) { + return (string){ .data = NULL, .length = 0, .mem_length = 0 }; + } uint32_t len = compute_length(array, 0); - int offset = len-max_length; - if (offset < 0) - offset = 0; + int offset = (int)len - (int)max_length; + if (offset < 0) { + offset = 0; + } uint32_t adjusted_len = len - offset; char *buf = (char*)malloc(adjusted_len + 1); - for (uint32_t i = offset; i < len; i++) - buf[i-offset] = array[i]; - buf[len-offset] = 0; - return (string){ .data = buf, .length = adjusted_len }; + if (!buf) { + return (string){ .data = NULL, .length = 0, .mem_length = 0 }; + } + for (uint32_t i = 0; i < adjusted_len; i++) { + buf[i] = array[offset + i]; + } + buf[adjusted_len] = '\0'; + return (string){ .data = buf, .length = adjusted_len, .mem_length = adjusted_len + 1 }; } -string string_ca_max( char *array, uint32_t max_length) { +string string_ca_max(char *array, uint32_t max_length) { + if (array == NULL) { + return (string){ .data = NULL, .length = 0, .mem_length = 0 }; + } uint32_t len = compute_length(array, max_length); char *buf = (char*)malloc(len + 1); - for (uint32_t i = 0; i < len; i++) + if (!buf) { + return (string){ .data = NULL, .length = 0, .mem_length = 0 }; + } + for (uint32_t i = 0; i < len; i++) { buf[i] = array[i]; - buf[len] = 0; - return (string){ .data = buf, .length = len }; + } + buf[len] = '\0'; + return (string){ .data = buf, .length = len, .mem_length = len + 1 }; } string string_c( char c){ @@ -109,11 +136,15 @@ bool string_equals(string a, string b) { return strcmp(a.data,b.data, false) == 0; } -string string_format( char *fmt, ...) { +string string_format(char *fmt, ...) { + if (fmt == NULL) { + return (string){ .data = NULL, .length = 0, .mem_length = 0 }; + } va_list args; va_start(args, fmt); - string_format_va(fmt, args); + string result = string_format_va(fmt, args); va_end(args); + return result; } string string_format_va( char *fmt, va_list args){ @@ -206,21 +237,23 @@ char tolower(char c){ } int strcmp(char *a, char *b, bool case_insensitive) { + if (a == NULL && b == NULL) return 0; //i guess + if (a == NULL) return -1; + if (b == NULL) return 1; + while (*a && *b) { char ca = *a; char cb = *b; - if (case_insensitive) { ca = tolower((unsigned char)ca); cb = tolower((unsigned char)cb); } - - if (ca != cb) return ca - cb; - + if (ca != cb) return ca - cb; a++; b++; } - if (case_insensitive) return tolower(*a) - tolower(*b); - return *a - *b; + if (case_insensitive) + return tolower((unsigned char)*a) - tolower((unsigned char)*b); + return (unsigned char)*a - (unsigned char)*b; } int strstart(char *a, char *b, bool case_insensitive) { diff --git a/user/Makefile b/user/Makefile index fb6b8328..e85c133e 100644 --- a/user/Makefile +++ b/user/Makefile @@ -1,25 +1,23 @@ -ARCH= aarch64-none-elf -CC = $(ARCH)-gcc -LD = $(ARCH)-ld -OBJCOPY = $(ARCH)-objcopy +#user +CFLAGS := $(CFLAGS_BASE) -I. -I../shared -Wno-unused-parameter +LDFLAGS := -T $(shell ls *.ld) -CFLAGS = -g -O0 -std=c17 -nostdlib -ffreestanding -Wall -Wextra -mcpu=cortex-a72 -I. -I../shared -Wno-unused-parameter -LDFLAGS = -T $(shell ls *.ld) +C_SRC := $(shell find . -name '*.c') +CPP_SRC := $(shell find . -name '*.cpp') +OBJ := $(C_SRC:.c=.o) $(CPP_SRC:.cpp=.o) -C_SRC = $(shell find . -name '*.c') -CPP_SRC = $(shell find . -name '*.cpp') -OBJ = $(C_SRC:.c=.o) $(CPP_SRC:.cpp=.o) +NAME := $(notdir $(CURDIR)) +ELF := $(NAME).elf +TARGET := $(NAME).bin +LOCATION := ../fs/redos/user/ -NAME = $(shell basename "$$PWD") -TARGET = $(NAME).bin -ELF = $(NAME).elf -LOCATION = ../fs/redos/user/ +.PHONY: all clean -all: $(TARGET) +all: $(LOCATION)$(TARGET) -$(TARGET): $(OBJ) +$(LOCATION)$(TARGET): $(OBJ) $(LD) $(LDFLAGS) -o $(LOCATION)$(ELF) $(OBJ) ../shared/libshared.a - $(OBJCOPY) -O binary $(LOCATION)$(ELF) $(LOCATION)$(TARGET) + $(OBJCOPY) -O binary $(LOCATION)$(ELF) $@ %.o: %.S $(CC) $(CFLAGS) -c $< -o $@ @@ -28,7 +26,7 @@ $(TARGET): $(OBJ) $(CC) $(CFLAGS) -c $< -o $@ %.o: %.cpp - $(CC) $(CFLAGS) -c $< -o $@ + $(CC) $(CFLAGS) -fno-rtti -c $< -o $@ clean: - rm -f $(shell find . -name '*.o') $(TARGET) \ No newline at end of file + rm -f $(OBJ) $(TARGET) From 8a709183b49162c61a29f5a0a4117ffebbcf0d06 Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 18 Jul 2025 17:02:50 +0200 Subject: [PATCH 02/33] Update kstring.c --- kernel/kstring.c | 105 ++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 55 deletions(-) diff --git a/kernel/kstring.c b/kernel/kstring.c index f12b266e..3e5d99a5 100644 --- a/kernel/kstring.c +++ b/kernel/kstring.c @@ -5,99 +5,97 @@ #include "std/memfunctions.h" //TODO: we can most likely get rid of this class now and use string entirely -static uint32_t compute_length(const char *s, uint32_t max_length) { - if (s == NULL) { - return 0; - } +static uint32_t compute_length(const char *s, uint32_t max_length){ + if (s == NULL){return 0;} uint32_t len = 0; - while ((max_length == 0 || len < max_length) && s[len] != '\0') { + while((max_length == 0 || len < max_length) && s[len] != '\0'){ len++; } return len; } -kstring kstring_l(const char *literal) { +kstring kstring_l(const char *literal){ if (literal == NULL) { - return (kstring){ .data = NULL, .length = 0 }; + return (kstring){.data = NULL, .length = 0}; } uint32_t len = compute_length(literal, 0); - char *buf = (char*)talloc(len + 1); - if (!buf) { - return (kstring){ .data = NULL, .length = 0 }; + char *buf = (char*)talloc(len+1); + if (!buf){ + return (kstring){ .data = NULL, .length = 0}; } - for (uint32_t i = 0; i < len; i++) { + for (uint32_t i = 0; i < len; i++){ buf[i] = literal[i]; } buf[len] = '\0'; - return (kstring){ .data = buf, .length = len }; + return (kstring){.data = buf, .length = len}; } kstring kstring_repeat(char symbol, uint32_t amount){ - char *buf = (char*)talloc(amount + 1); + char *buf = (char*)talloc(amount+ 1); if (!buf) { - return (kstring){ .data = NULL, .length = 0 }; + return (kstring){ .data = NULL, .length = 0}; } memset(buf, symbol, amount); buf[amount] = '\0'; - return (kstring){ .data = buf, .length = amount }; + return (kstring){.data = buf, .length = amount}; } kstring kstring_tail(const char *array, uint32_t max_length){ - if (array == NULL) { - return (kstring){ .data = NULL, .length = 0 }; + if (array == NULL){ + return (kstring){.data = NULL, .length = 0}; } uint32_t len = compute_length(array, 0); - int offset = (int)len - (int)max_length; - if (offset < 0) offset = 0; + int offset = (int)len-(int)max_length; + if (offset < 0) offset=0; uint32_t adjusted_len = len - offset; - char *buf = (char*)talloc(adjusted_len + 1); - if (!buf) { - return (kstring){ .data = NULL, .length = 0 }; + char *buf = (char*)talloc(adjusted_len+1); + if (!buf){ + return (kstring){.data = NULL, .length = 0}; } - for (uint32_t i = 0; i < adjusted_len; i++) { + for (uint32_t i = 0; i < adjusted_len; i++){ buf[i] = array[offset + i]; } buf[adjusted_len] = '\0'; - return (kstring){ .data = buf, .length = adjusted_len }; + return (kstring){ .data = buf, .length = adjusted_len}; } -kstring kstring_ca_max(const char *array, uint32_t max_length) { - if (array == NULL) { +kstring kstring_ca_max(const char *array, uint32_t max_length){ + if (array == NULL){ return (kstring){ .data = NULL, .length = 0 }; } - uint32_t len = compute_length(array, max_length); - char *buf = (char*)talloc(len + 1); + uint32_t len=compute_length(array, max_length); + char *buf = (char*)talloc(len+1); if (!buf) { - return (kstring){ .data = NULL, .length = 0 }; + return (kstring){.data = NULL, .length = 0}; } - for (uint32_t i = 0; i < len; i++) { + for (uint32_t i = 0; i < len; i++){ buf[i] = array[i]; } buf[len] = '\0'; - return (kstring){ .data = buf, .length = len }; + return (kstring){.data = buf, .length = len}; } kstring kstring_c(const char c){ - char *buf = (char*)talloc(2); - if (!buf) { + char *buf=(char*)talloc(2); + if (!buf){ return (kstring){ .data = NULL, .length = 0 }; } buf[0] = c; buf[1] = '\0'; - return (kstring){ .data = buf, .length = 1 }; + return (kstring){.data = buf, .length = 1}; } -kstring kstring_from_hex(uint64_t value) { +kstring kstring_from_hex(uint64_t value){ char *buf = (char*)talloc(18); uint32_t len = 0; buf[len++] = '0'; buf[len++] = 'x'; bool started = false; - for (uint32_t i = 60;; i -= 4) { - uint8_t nibble = (value >> i) & 0xF; - char curr_char = nibble < 10 ? '0' + nibble : 'A' + (nibble - 10); - if (started || curr_char != '0' || i == 0) { + for (uint32_t i = 60;; i -= 4){ + uint8_t nibble = (value>>i)&0xF; + char curr_char=nibble<10?'0' + nibble : 'A'+(nibble - 10); + if (started||curr_char != '0'||i == 0) { started = true; buf[len++] = curr_char; } @@ -105,59 +103,56 @@ kstring kstring_from_hex(uint64_t value) { } buf[len] = 0; - return (kstring){ .data = buf, .length = len }; + return (kstring){.data = buf, .length = len}; } -kstring kstring_from_bin(uint64_t value) { +kstring kstring_from_bin(uint64_t value){ char *buf = (char*)talloc(67); uint32_t len = 0; buf[len++] = '0'; buf[len++] = 'b'; bool started = false; - for (uint32_t i = 60;; i --) { + for (uint32_t i = 60;; i --){ char bit = (value >> i) & 1 ? '1' : '0'; - if (started || bit != '0' || i == 0) { + if (started || bit != '0' || i == 0){ started = true; buf[len++] = bit; } if (i == 0) break; } - buf[len] = 0; return (kstring){ .data = buf, .length = len }; } -bool kstring_equals(kstring a, kstring b) { +bool kstring_equals(kstring a, kstring b){ return strcmp(a.data,b.data, false) == 0; } -kstring kstring_format_args(const char *fmt, const uint64_t *args, uint32_t arg_count) { - if (fmt == NULL) { +kstring kstring_format_args(const char *fmt, const uint64_t *args, uint32_t arg_count){ + if (fmt == NULL){ return (kstring){ .data = NULL, .length = 0 }; } - // args può essere NULL se arg_count==0, altrimenti va validato - if (arg_count > 0 && args == NULL) { + if (arg_count > 0 && args == NULL){ return (kstring){ .data = NULL, .length = 0 }; } char *buf = (char*)talloc(256); - if (!buf) { + if (!buf){ return (kstring){ .data = NULL, .length = 0 }; } uint32_t len = 0; uint32_t arg_index = 0; - for (uint32_t i = 0; fmt[i] && len < 255; i++) { + for (uint32_t i = 0; fmt[i] && len < 255; i++){ if (fmt[i] == '%' && fmt[i+1]) { i++; - if (arg_index >= arg_count) { - // troppi placeholder, copio il resto letterale + if (arg_index >= arg_count){ buf[len++] = '%'; buf[len++] = fmt[i]; continue; } - switch (fmt[i]) { + switch (fmt[i]){ case 'x': { uint64_t val = args[arg_index++]; kstring hex = kstring_from_hex(val); @@ -215,4 +210,4 @@ kstring kstring_format_args(const char *fmt, const uint64_t *args, uint32_t arg_ } buf[len] = '\0'; return (kstring){ .data = buf, .length = len }; -} \ No newline at end of file +} From 5578ed5f90502853e3181b0ee7e45a900b792e3a Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 18 Jul 2025 17:07:57 +0200 Subject: [PATCH 03/33] Update string.c --- shared/std/string.c | 166 +++++++++++++++++++++----------------------- 1 file changed, 80 insertions(+), 86 deletions(-) diff --git a/shared/std/string.c b/shared/std/string.c index 8b134769..d906302e 100644 --- a/shared/std/string.c +++ b/shared/std/string.c @@ -3,21 +3,20 @@ #include "memory/memory_access.h" #include "std/memfunctions.h" -static uint32_t compute_length(char *s, uint32_t max_length) { - if (s == NULL) { +static uint32_t compute_length(char *s, uint32_t max_length){ + if (s == NULL){ return 0; } uint32_t len = 0; - while ((max_length == 0 || len < max_length) && s[len] != '\0') { + while ((max_length == 0 || len < max_length) && s[len] != '\0'){ len++; } return len; } -string string_l(char *literal) { - if (literal == NULL) { - // ritorna stringa vuota - return (string){ .data = NULL, .length = 0, .mem_length = 0 }; +string string_l(char *literal){ + if (literal == NULL){ + return (string){ .data = NULL, .length = 0, .mem_length = 0}; } uint32_t len = compute_length(literal, 0); char *buf = (char*)malloc(len + 1); @@ -25,7 +24,7 @@ string string_l(char *literal) { //manage malloc return (string){ .data = NULL, .length = 0, .mem_length = 0 }; } - for (uint32_t i = 0; i < len; i++) { + for (uint32_t i = 0; i < len; i++){ buf[i] = literal[i]; } buf[len] = '\0'; @@ -39,48 +38,48 @@ string string_repeat(char symbol, uint32_t amount){ return (string){ .data = buf, .length = amount, .mem_length = amount+1 }; } -string string_tail(char *array, uint32_t max_length) { - if (array == NULL) { +string string_tail(char *array, uint32_t max_length){ + if (array == NULL){ return (string){ .data = NULL, .length = 0, .mem_length = 0 }; } uint32_t len = compute_length(array, 0); int offset = (int)len - (int)max_length; - if (offset < 0) { + if(offset < 0){ offset = 0; } uint32_t adjusted_len = len - offset; char *buf = (char*)malloc(adjusted_len + 1); - if (!buf) { + if(!buf){ return (string){ .data = NULL, .length = 0, .mem_length = 0 }; } - for (uint32_t i = 0; i < adjusted_len; i++) { + for (uint32_t i = 0; i < adjusted_len; i++){ buf[i] = array[offset + i]; } buf[adjusted_len] = '\0'; - return (string){ .data = buf, .length = adjusted_len, .mem_length = adjusted_len + 1 }; + return (string){.data = buf, .length = adjusted_len, .mem_length = adjusted_len + 1 }; } -string string_ca_max(char *array, uint32_t max_length) { - if (array == NULL) { - return (string){ .data = NULL, .length = 0, .mem_length = 0 }; +string string_ca_max(char *array, uint32_t max_length){ + if(array == NULL){ + return (string){.data = NULL, .length = 0, .mem_length= 0 }; } uint32_t len = compute_length(array, max_length); char *buf = (char*)malloc(len + 1); - if (!buf) { - return (string){ .data = NULL, .length = 0, .mem_length = 0 }; + if(!buf){ + return (string){ .data = NULL, .length = 0, .mem_length=0 }; } - for (uint32_t i = 0; i < len; i++) { + for(uint32_t i = 0; i < len; i++){ buf[i] = array[i]; } buf[len] = '\0'; - return (string){ .data = buf, .length = len, .mem_length = len + 1 }; + return (string){ .data = buf, .length = len, .mem_length = len+1}; } string string_c( char c){ char *buf = (char*)malloc(2); buf[0] = c; buf[1] = 0; - return (string){ .data = buf, .length = 1, .mem_length = 2 }; + return (string){.data = buf, .length = 1, .mem_length = 2}; } uint32_t parse_hex(uint64_t value, char* buf){ @@ -102,7 +101,7 @@ uint32_t parse_hex(uint64_t value, char* buf){ return len; } -string string_from_hex(uint64_t value) { +string string_from_hex(uint64_t value){ char *buf = (char*)malloc(18); uint32_t len = parse_hex(value, buf); return (string){ .data = buf, .length = len, .mem_length = 18 }; @@ -113,9 +112,9 @@ uint32_t parse_bin(uint64_t value, char* buf){ buf[len++] = '0'; buf[len++] = 'b'; bool started = false; - for (uint32_t i = 63;; i --) { + for (uint32_t i = 63;; i --){ char bit = (value >> i) & 1 ? '1' : '0'; - if (started || bit != '0' || i == 0) { + if (started || bit != '0' || i == 0){ started = true; buf[len++] = bit; } @@ -126,19 +125,19 @@ uint32_t parse_bin(uint64_t value, char* buf){ return len; } -string string_from_bin(uint64_t value) { +string string_from_bin(uint64_t value){ char *buf = (char*)malloc(66); uint32_t len = parse_bin(value, buf); return (string){ .data = buf, .length = len, .mem_length = 66 }; } -bool string_equals(string a, string b) { +bool string_equals(string a, string b){ return strcmp(a.data,b.data, false) == 0; } -string string_format(char *fmt, ...) { - if (fmt == NULL) { - return (string){ .data = NULL, .length = 0, .mem_length = 0 }; +string string_format(char *fmt, ...){ + if(fmt == NULL){ + return (string){ .data = NULL, .length = 0, .mem_length = 0}; } va_list args; va_start(args, fmt); @@ -147,134 +146,129 @@ string string_format(char *fmt, ...) { return result; } -string string_format_va( char *fmt, va_list args){ +string string_format_va(char *fmt, va_list args){ char *buf = (char*)malloc(256); uint32_t len = 0; uint32_t arg_index = 0; - - for (uint32_t i = 0; fmt[i] && len < 255; i++) { - if (fmt[i] == '%' && fmt[i+1]) { + for(uint32_t i = 0; fmt[i] && len < 255; i++){ + if(fmt[i] == '%' && fmt[i+1]){ i++; - if (fmt[i] == 'x') { + if(fmt[i] == 'x'){ uint64_t val = va_arg(args, uint64_t); len += parse_hex(val,(char*)(buf + len)); - } else if (fmt[i] == 'b') { + }else if(fmt[i] == 'b') { uint64_t val = va_arg(args, uint64_t); string bin = string_from_bin(val); - for (uint32_t j = 0; j < bin.length && len < 255; j++) buf[len++] = bin.data[j]; + for(uint32_t j = 0; j < bin.length && len < 255; j++) buf[len++] = bin.data[j]; free(bin.data,bin.mem_length); - } else if (fmt[i] == 'c') { + }else if(fmt[i] == 'c') { uint64_t val = va_arg(args, uint64_t); buf[len++] = (char)val; - } else if (fmt[i] == 's') { + }else if(fmt[i] == 's') { char *str = ( char *)va_arg(args, uintptr_t); for (uint32_t j = 0; str[j] && len < 255; j++) buf[len++] = str[j]; - } else if (fmt[i] == 'i') { + }else if(fmt[i] == 'i') { uint64_t val = va_arg(args, long int); char temp[21]; uint32_t temp_len = 0; bool negative = false; - if ((int)val < 0) { + if((int)val < 0) { negative = true; buf[len++] = '-'; val = (uint64_t)(-(int)val); } - - do { + do{ temp[temp_len++] = '0' + (val % 10); val /= 10; - } while (val && temp_len < 20); + }while(val && temp_len < 20); - for (int j = temp_len - 1; j >= 0 && len < 255; j--) { + for(int j = temp_len - 1; j >= 0 && len < 255; j--){ buf[len++] = temp[j]; } - }else if (fmt[i] == 'f' || fmt[i] == 'd') { + }else if(fmt[i] == 'f' || fmt[i] == 'd') { double val = va_arg(args, double); - if (val < 0) { + if(val < 0){ buf[len++] = '-'; val = -val; } - uint64_t whole = (uint64_t)val; double frac = val - (double)whole; char temp[21]; uint32_t temp_len = 0; - do { - temp[temp_len++] = '0' + (whole % 10); + do{ + temp[temp_len++] = '0' +(whole % 10); whole /= 10; - } while (whole && temp_len < 20); + }while(whole && temp_len < 20); - for (int j = temp_len - 1; j >= 0 && len < 255; j--) { + for(int j = temp_len - 1; j >= 0 && len < 255; j--){ buf[len++] = temp[j]; } - - if (len < 255) buf[len++] = '.'; - + if(len < 255) buf[len++] = '.'; for (int d = 0; d < 6 && len < 255; d++) { frac *= 10; int digit = (int)frac; buf[len++] = '0' + digit; frac -= digit; } - } else { + }else{ buf[len++] = '%'; buf[len++] = fmt[i]; } - } else { + }else{ buf[len++] = fmt[i]; } } - buf[len] = 0; + buf[len]=0; return (string){ .data = buf, .length = len, .mem_length = 256 }; } char tolower(char c){ - if (c >= 'A' && c <= 'Z') return c + 'a' - 'A'; + if(c >= 'A' && c <= 'Z') return c + 'a' - 'A'; return c; } -int strcmp(char *a, char *b, bool case_insensitive) { - if (a == NULL && b == NULL) return 0; //i guess - if (a == NULL) return -1; - if (b == NULL) return 1; +int strcmp(char *a, char *b, bool case_insensitive){ + if(a == NULL && b == NULL)return 0; //i guess + if(a == NULL) return -1; + if(b == NULL) return 1; - while (*a && *b) { + while(*a && *b){ char ca = *a; char cb = *b; - if (case_insensitive) { + if(case_insensitive){ ca = tolower((unsigned char)ca); cb = tolower((unsigned char)cb); } - if (ca != cb) return ca - cb; + if(ca != cb) return ca - cb; a++; b++; } - if (case_insensitive) + if(case_insensitive) return tolower((unsigned char)*a) - tolower((unsigned char)*b); return (unsigned char)*a - (unsigned char)*b; } -int strstart(char *a, char *b, bool case_insensitive) { +int strstart(char *a, char *b, bool case_insensitive){ int index = 0; - while (*a && *b) { + while(*a && *b){ char ca = *a; char cb = *b; - if (case_insensitive) { + if (case_insensitive){ ca = tolower(ca); cb = tolower(cb); } - if (ca != cb) return index; + if(ca != cb) return index; a++; b++; index++; } return 0; } -int strindex( char *a, char *b) { - for (int i = 0; a[i]; i++) { +int strindex( char *a, char *b){ + for(int i = 0; a[i]; i++){ int j = 0; while (b[j] && a[i + j] == b[j]) j++; if (!b[j]) return i; @@ -282,14 +276,14 @@ int strindex( char *a, char *b) { return -1; } -int strend(char *a, char *b, bool case_insensitive) { - while (*a && *b) { +int strend(char *a, char *b, bool case_insensitive){ + while(*a && *b){ char ca = case_insensitive ? tolower((unsigned char)*a) : *a; char cb = case_insensitive ? tolower((unsigned char)*b) : *b; - if (ca == cb) { + if(ca == cb){ char *pa = a, *pb = b; - while (1) { + while(1){ char cpa = case_insensitive ? tolower((unsigned char)*pa) : *pa; char cpb = case_insensitive ? tolower((unsigned char)*pb) : *pb; @@ -304,21 +298,21 @@ int strend(char *a, char *b, bool case_insensitive) { return 1; } -bool strcont( char *a, char *b) { - while (*a) { +bool strcont( char *a, char *b){ + while(*a){ char *p = a, *q = b; - while (*p && *q && *p == *q) { + while(*p && *q && *p == *q){ p++; q++; } - if (*q == 0) return 1; + if(*q == 0) return 1; a++; } return 0; } -bool utf16tochar( uint16_t* str_in, char* out_str, size_t max_len) { +bool utf16tochar(uint16_t* str_in, char* out_str, size_t max_len){ size_t out_i = 0; - for (int i = 0; i < max_len && str_in[i]; i++){ + for(int i = 0; i < max_len && str_in[i]; i++){ uint16_t wc = str_in[i]; out_str[out_i++] = (wc <= 0x7F) ? (char)(wc & 0xFF) : '?'; } @@ -326,9 +320,9 @@ bool utf16tochar( uint16_t* str_in, char* out_str, size_t max_len) { return true; } -uint64_t parse_hex_u64(char* str, size_t size) { +uint64_t parse_hex_u64(char* str, size_t size){ uint64_t result = 0; - for (uint32_t i = 0; i < size; i++) { + for(uint32_t i = 0; i < size; i++){ char c = str[i]; uint8_t digit = 0; if (c >= '0' && c <= '9') digit = c - '0'; @@ -338,4 +332,4 @@ uint64_t parse_hex_u64(char* str, size_t size) { result = (result << 4) | digit; } return result; -} \ No newline at end of file +} From 4cb955758a626a5856491a212765355435cd1255 Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 18 Jul 2025 17:11:19 +0200 Subject: [PATCH 04/33] Update kstring.c --- kernel/kstring.c | 66 ++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/kernel/kstring.c b/kernel/kstring.c index 3e5d99a5..6cce742a 100644 --- a/kernel/kstring.c +++ b/kernel/kstring.c @@ -6,7 +6,7 @@ //TODO: we can most likely get rid of this class now and use string entirely static uint32_t compute_length(const char *s, uint32_t max_length){ - if (s == NULL){return 0;} + if(s == NULL){return 0;} uint32_t len = 0; while((max_length == 0 || len < max_length) && s[len] != '\0'){ len++; @@ -15,15 +15,15 @@ static uint32_t compute_length(const char *s, uint32_t max_length){ } kstring kstring_l(const char *literal){ - if (literal == NULL) { + if(literal == NULL){ return (kstring){.data = NULL, .length = 0}; } uint32_t len = compute_length(literal, 0); char *buf = (char*)talloc(len+1); - if (!buf){ + if(!buf){ return (kstring){ .data = NULL, .length = 0}; } - for (uint32_t i = 0; i < len; i++){ + for(uint32_t i = 0; i < len; i++){ buf[i] = literal[i]; } buf[len] = '\0'; @@ -32,7 +32,7 @@ kstring kstring_l(const char *literal){ kstring kstring_repeat(char symbol, uint32_t amount){ char *buf = (char*)talloc(amount+ 1); - if (!buf) { + if(!buf){ return (kstring){ .data = NULL, .length = 0}; } memset(buf, symbol, amount); @@ -41,7 +41,7 @@ kstring kstring_repeat(char symbol, uint32_t amount){ } kstring kstring_tail(const char *array, uint32_t max_length){ - if (array == NULL){ + if(array == NULL){ return (kstring){.data = NULL, .length = 0}; } uint32_t len = compute_length(array, 0); @@ -49,10 +49,10 @@ kstring kstring_tail(const char *array, uint32_t max_length){ if (offset < 0) offset=0; uint32_t adjusted_len = len - offset; char *buf = (char*)talloc(adjusted_len+1); - if (!buf){ + if(!buf){ return (kstring){.data = NULL, .length = 0}; } - for (uint32_t i = 0; i < adjusted_len; i++){ + for(uint32_t i = 0; i < adjusted_len; i++){ buf[i] = array[offset + i]; } buf[adjusted_len] = '\0'; @@ -60,15 +60,15 @@ kstring kstring_tail(const char *array, uint32_t max_length){ } kstring kstring_ca_max(const char *array, uint32_t max_length){ - if (array == NULL){ + if(array == NULL){ return (kstring){ .data = NULL, .length = 0 }; } uint32_t len=compute_length(array, max_length); char *buf = (char*)talloc(len+1); - if (!buf) { + if(!buf) { return (kstring){.data = NULL, .length = 0}; } - for (uint32_t i = 0; i < len; i++){ + for(uint32_t i = 0; i < len; i++){ buf[i] = array[i]; } buf[len] = '\0'; @@ -77,7 +77,7 @@ kstring kstring_ca_max(const char *array, uint32_t max_length){ kstring kstring_c(const char c){ char *buf=(char*)talloc(2); - if (!buf){ + if(!buf){ return (kstring){ .data = NULL, .length = 0 }; } buf[0] = c; @@ -92,14 +92,14 @@ kstring kstring_from_hex(uint64_t value){ buf[len++] = 'x'; bool started = false; - for (uint32_t i = 60;; i -= 4){ + for(uint32_t i = 60;; i -= 4){ uint8_t nibble = (value>>i)&0xF; char curr_char=nibble<10?'0' + nibble : 'A'+(nibble - 10); - if (started||curr_char != '0'||i == 0) { + if(started||curr_char != '0'||i == 0) { started = true; buf[len++] = curr_char; } - if (i == 0) break; + if(i == 0) break; } buf[len] = 0; @@ -113,13 +113,13 @@ kstring kstring_from_bin(uint64_t value){ buf[len++] = 'b'; bool started = false; - for (uint32_t i = 60;; i --){ + for(uint32_t i = 60;; i --){ char bit = (value >> i) & 1 ? '1' : '0'; - if (started || bit != '0' || i == 0){ + if(started || bit != '0' || i == 0){ started = true; buf[len++] = bit; } - if (i == 0) break; + if(i == 0) break; } buf[len] = 0; return (kstring){ .data = buf, .length = len }; @@ -130,29 +130,29 @@ bool kstring_equals(kstring a, kstring b){ } kstring kstring_format_args(const char *fmt, const uint64_t *args, uint32_t arg_count){ - if (fmt == NULL){ + if(fmt == NULL){ return (kstring){ .data = NULL, .length = 0 }; } - if (arg_count > 0 && args == NULL){ + if(arg_count > 0 && args == NULL){ return (kstring){ .data = NULL, .length = 0 }; } char *buf = (char*)talloc(256); - if (!buf){ + if(!buf){ return (kstring){ .data = NULL, .length = 0 }; } uint32_t len = 0; uint32_t arg_index = 0; - for (uint32_t i = 0; fmt[i] && len < 255; i++){ - if (fmt[i] == '%' && fmt[i+1]) { + for(uint32_t i = 0; fmt[i] && len < 255; i++){ + if(fmt[i] == '%' && fmt[i+1]) { i++; - if (arg_index >= arg_count){ + if(arg_index >= arg_count){ buf[len++] = '%'; buf[len++] = fmt[i]; continue; } - switch (fmt[i]){ + switch(fmt[i]){ case 'x': { uint64_t val = args[arg_index++]; kstring hex = kstring_from_hex(val); @@ -174,8 +174,8 @@ kstring kstring_format_args(const char *fmt, const uint64_t *args, uint32_t arg_ } case 's': { const char *str = (const char *)(uintptr_t)args[arg_index++]; - if (str) { - for (uint32_t j = 0; str[j] && len < 255; j++) buf[len++] = str[j]; + if(str){ + for(uint32_t j = 0; str[j] && len < 255; j++) buf[len++] = str[j]; } break; } @@ -184,18 +184,18 @@ kstring kstring_format_args(const char *fmt, const uint64_t *args, uint32_t arg_ char temp[21]; uint32_t temp_len = 0; bool negative = false; - if ((int64_t)val < 0) { + if((int64_t)val < 0){ negative = true; val = (uint64_t)(-(int64_t)val); } - do { + do{ temp[temp_len++] = '0' + (val % 10); val /= 10; - } while (val && temp_len < sizeof(temp)-1); - if (negative && temp_len < sizeof(temp)-1) { + }while(val && temp_len < sizeof(temp)-1); + if(negative && temp_len < sizeof(temp)-1){ temp[temp_len++] = '-'; } - for (int j = temp_len - 1; j >= 0 && len < 255; j--) { + for(int j = temp_len - 1; j >= 0 && len < 255; j--){ buf[len++] = temp[j]; } break; @@ -204,7 +204,7 @@ kstring kstring_format_args(const char *fmt, const uint64_t *args, uint32_t arg_ buf[len++] = '%'; buf[len++] = fmt[i]; } - } else { + }else{ buf[len++] = fmt[i]; } } From 82113811687af0dff243e39fe135d33774fd7a3a Mon Sep 17 00:00:00 2001 From: CodeAnarchist Date: Sun, 20 Jul 2025 18:25:50 +0200 Subject: [PATCH 05/33] implemented linked list, chunked list, ring buffer --- kernel/console/kconsole/kconsole.cpp | 168 +++++++-------- kernel/console/kconsole/kconsole.h | 2 +- kernel/console/kconsole/kconsole.hpp | 32 +-- kernel/filesystem/fat32.cpp | 2 +- kernel/input/xhci.cpp | 1 - .../drivers/virtio_net_pci/virtio_net_pci.cpp | 7 +- kernel/networking/processes/net_proc.c | 5 +- shared/data_struct/chunked_list.cpp | 173 +++++++++++++++ shared/data_struct/chunked_list.hpp | 202 ++++++++++++++++++ shared/data_struct/circular_linked_list.cpp | 0 shared/data_struct/circular_linked_list.hpp | 0 shared/data_struct/doubly_linked_list.cpp | 0 shared/data_struct/doubly_linked_list.hpp | 0 shared/data_struct/linked_list.cpp | 138 ++++++++++++ shared/data_struct/linked_list.hpp | 157 ++++++++++++++ shared/data_struct/queue.cpp | 0 shared/data_struct/queue.hpp | 0 shared/data_struct/ring_buffer.cpp | 58 +++++ shared/data_struct/ring_buffer.hpp | 78 +++++++ 19 files changed, 910 insertions(+), 113 deletions(-) create mode 100644 shared/data_struct/chunked_list.cpp create mode 100644 shared/data_struct/chunked_list.hpp create mode 100644 shared/data_struct/circular_linked_list.cpp create mode 100644 shared/data_struct/circular_linked_list.hpp create mode 100644 shared/data_struct/doubly_linked_list.cpp create mode 100644 shared/data_struct/doubly_linked_list.hpp create mode 100644 shared/data_struct/linked_list.cpp create mode 100644 shared/data_struct/linked_list.hpp create mode 100644 shared/data_struct/queue.cpp create mode 100644 shared/data_struct/queue.hpp create mode 100644 shared/data_struct/ring_buffer.cpp create mode 100644 shared/data_struct/ring_buffer.hpp diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index 948ddf4c..e8975005 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -1,123 +1,117 @@ #include "kconsole.hpp" -#include "memory/kalloc.h" -#include "graph/graphics.h" #include "console/serial/uart.h" -KernelConsole::KernelConsole() - : cursor_x(0), cursor_y(0), scroll_row_offset(0) -{ +KernelConsole::KernelConsole():cursor_x(0),cursor_y(0),is_initialized(false){ resize(); clear(); } bool KernelConsole::check_ready(){ - if (!gpu_ready()) return false; - if (!is_initialized){ - is_initialized = true; + if(!gpu_ready()) return false; + if(!is_initialized){ + is_initialized= true; resize(); clear(); } return true; } -void KernelConsole::resize() { - gpu_size screen = gpu_get_screen_size(); - columns = screen.width / char_width; - rows = screen.height / char_height; - - if (buffer_header_size > 0) - temp_free(buffer,buffer_header_size); - if (buffer_data_size > 0) - temp_free(row_data,buffer_data_size); - - buffer_header_size = rows * sizeof(char*); - buffer = (char**)talloc(rows * sizeof(char*)); - buffer_data_size = rows * columns * sizeof(char); - row_data = (char*)talloc(buffer_data_size); - - for (unsigned int i = 0; i < rows; i++) { - buffer[i] = row_data + (i * columns); - } -} - -void KernelConsole::put_char(char c) { - if (!check_ready()) - return; - - if (c == '\n') { - newline(); +void KernelConsole::resize(){ + gpu_size s=gpu_get_screen_size(); + columns=s.width/char_width; + rows=s.height/ char_height; + + if(row_data) temp_free(row_data,buffer_data_size); + buffer_data_size = rows*columns; + row_data= (char*)talloc(buffer_data_size); + if(!row_data){ + rows=columns=0; + row_ring.clear(); return; } - if (cursor_x >= columns) - newline(); - - buffer[(scroll_row_offset + cursor_y) % rows][cursor_x] = c; - gpu_draw_char({cursor_x * char_width, cursor_y * char_height}, c, 1, 0xFFFFFFFF); - cursor_x++; + row_ring.clear(); + for(uint32_t i=0;i=columns) newline(); + + uint32_t ri; + if(row_ring.pop(ri)){ + row_ring.push(ri); + char* line=row_data+ri*columns; + line[cursor_x]=c; + gpu_draw_char({cursor_x*char_width,cursor_y*char_height},c,1,0xFFFFFFFF); + cursor_x++; } +} + +void KernelConsole::put_string(const char* s){ + if(!check_ready()) return; + for(uint32_t i=0;s[i];i++) + put_char(s[i]); gpu_flush(); } -void KernelConsole::put_hex(uint64_t value) { - if (!check_ready()) - return; +void KernelConsole::put_hex(uint64_t v){ + if(!check_ready()) return; put_char('0'); put_char('x'); - bool started = false; - for (uint32_t i = 60;; i -= 4) { - uint8_t nibble = (value >> i) & 0xF; - char curr_char = nibble < 10 ? '0' + nibble : 'A' + (nibble - 10); - if (started || curr_char != '0' || i == 0) { - started = true; - put_char(curr_char); + bool started=false; + for(uint32_t i=60;;i-=4){ + uint8_t n=(v>>i)&0xF; + char c=n<10?'0'+n:'A'+(n-10); + if(started||c!='0' ||i==0){ + started=true; + put_char(c); } - if (i == 0) break; + if(i==0) break; } - gpu_flush(); } -void KernelConsole::newline() { - if (!check_ready()) - return; - for (unsigned x = cursor_x; x < columns; x++){ - buffer[(scroll_row_offset + cursor_y) % rows][x] = 0; +void KernelConsole::newline(){ + if(!check_ready()) return; + uint32_t ri; + if(row_ring.pop(ri)){ + char* line=row_data+ri*columns; + for(uint32_t x=cursor_x;x= rows) { + if(cursor_y>=rows){ scroll(); - cursor_y = rows - 1; + cursor_y= rows -1; } } -void KernelConsole::scroll() { - if (!check_ready()) - return; - - scroll_row_offset = (scroll_row_offset + 1) % rows; - - for (unsigned int x = 0; x < columns; x++) { - buffer[(scroll_row_offset + rows - 1) % rows][x] = 0; +void KernelConsole::scroll(){ + if(!check_ready()) return; + uint32_t ri; + if(row_ring.pop(ri)){ + char* line=row_data+ri*columns; + for(uint32_t x=0;x row_ring; + char* row_data; + uint32_t buffer_data_size; }; + extern KernelConsole kconsole; \ No newline at end of file diff --git a/kernel/filesystem/fat32.cpp b/kernel/filesystem/fat32.cpp index 0eeaac57..456750a9 100644 --- a/kernel/filesystem/fat32.cpp +++ b/kernel/filesystem/fat32.cpp @@ -6,6 +6,7 @@ #include "std/string.h" #include "std/memfunctions.h" #include "math/math.h" +#include "data_struct/linked_list.hpp" #define kprintfv(fmt, ...) \ ({ \ @@ -64,7 +65,6 @@ bool FAT32FS::init(uint32_t partition_sector){ kprintf("FAT32 Volume uses %i cluster size", bytes_per_sector); kprintf("Data start at %x",data_start_sector*512); - read_FAT(mbs->reserved_sectors, mbs->sectors_per_fat, mbs->number_of_fats); return true;} diff --git a/kernel/input/xhci.cpp b/kernel/input/xhci.cpp index a86d25d4..0cde8116 100644 --- a/kernel/input/xhci.cpp +++ b/kernel/input/xhci.cpp @@ -180,7 +180,6 @@ bool XHCIDriver::init(){ return false; } } - return true; } diff --git a/kernel/networking/drivers/virtio_net_pci/virtio_net_pci.cpp b/kernel/networking/drivers/virtio_net_pci/virtio_net_pci.cpp index 4011d439..66f63bb8 100644 --- a/kernel/networking/drivers/virtio_net_pci/virtio_net_pci.cpp +++ b/kernel/networking/drivers/virtio_net_pci/virtio_net_pci.cpp @@ -47,6 +47,8 @@ VirtioNetDriver* VirtioNetDriver::try_init(){ return nullptr; } + + bool VirtioNetDriver::init(){ uint64_t addr = find_pci_device(0x1AF4, 0x1000); if (!addr){ @@ -73,14 +75,12 @@ bool VirtioNetDriver::init(){ kprintf_raw("[VIRTIO_NET] Interrupts setup with MSI %i,%i",NET_IRQ,NET_IRQ+1); break; } - pci_enable_device(addr); if (!virtio_init_device(&vnp_net_dev)) { kprintf("[VIRTIO_NET error] Failed network initialization"); return false; } - kprintf("[VIRTIO_NET] Device set up at %x",(uintptr_t)vnp_net_dev.device_cfg); select_queue(&vnp_net_dev, RECEIVE_QUEUE); @@ -109,6 +109,7 @@ bool VirtioNetDriver::init(){ return true; } + void VirtioNetDriver::get_mac(network_connection_ctx *context){ virtio_net_config* net_config = (virtio_net_config*)vnp_net_dev.device_cfg; kprintfv("[VIRTIO_NET] %x:%x:%x:%x:%x:%x", net_config->mac[0], net_config->mac[1], net_config->mac[2], net_config->mac[3], net_config->mac[4], net_config->mac[5]); @@ -184,4 +185,4 @@ void VirtioNetDriver::send_packet(sizedptr packet){ void VirtioNetDriver::enable_verbose(){ verbose = true; -} \ No newline at end of file +} diff --git a/kernel/networking/processes/net_proc.c b/kernel/networking/processes/net_proc.c index 84b7bfdc..81122989 100644 --- a/kernel/networking/processes/net_proc.c +++ b/kernel/networking/processes/net_proc.c @@ -68,7 +68,6 @@ void test_network(){ unbind_port(8888); } - uint32_t negotiate_dhcp(){ kprintf("Sending DHCP request"); network_connection_ctx *ctx = network_get_context(); @@ -130,9 +129,7 @@ uint32_t negotiate_dhcp(){ //DNS (8 bytes) (6) //TODO: Make subsequent DHCP requests (renewals and requests) directed to the server ctx->ip = local_ip; - test_network(); - return lease_time; } @@ -150,4 +147,4 @@ void dhcp_daemon(){ process_t* launch_net_process(){ return create_kernel_process("dhcp_daemon",dhcp_daemon); -} \ No newline at end of file +} diff --git a/shared/data_struct/chunked_list.cpp b/shared/data_struct/chunked_list.cpp new file mode 100644 index 00000000..96ceaa02 --- /dev/null +++ b/shared/data_struct/chunked_list.cpp @@ -0,0 +1,173 @@ +#include "chunked_list.hpp" +#include "console/kio.h" + +extern "C" { + +cchunked_list_t* cchunked_list_create(uint64_t chunkSize){ + uintptr_t raw=malloc(sizeof(cchunked_node_t)+chunkSize*sizeof(void*)); + if(!raw) return NULL; + cchunked_list_t* list=(cchunked_list_t*)malloc(sizeof(cchunked_list_t)); + if(!list){ + free((void*)raw,sizeof(cchunked_node_t)+chunkSize*sizeof(void*)); + return NULL;} + list->chunkSize=chunkSize; + list->length=0; + list->head=(cchunked_node_t*)raw; + list->head->count=0; + list->head->next=NULL; + list->tail=list->head; + return list; +} + +void cchunked_list_destroy(cchunked_list_t* list){ + if(!list) return; + cchunked_node_t* node=list->head; + while(node){ + cchunked_node_t* next=node->next; + free(node,sizeof(cchunked_node_t)+list->chunkSize*sizeof(void*)); + node=next; + } + free(list,sizeof(cchunked_list_t)); +} +cchunked_list_t* cchunked_list_clone(const cchunked_list_t* list){ + if(!list) return NULL; + cchunked_list_t* clone=cchunked_list_create(list->chunkSize); + if(!clone) return NULL; + for(cchunked_node_t* it=list->head;it;it=it->next){ + for(uint64_t i=0;icount;i++) + cchunked_list_push_back(clone,it->data[i]); + } + return clone; +} + +void cchunked_list_push_back(cchunked_list_t* list, void* data){ + if(!list) return; + //first mnode + if(list->tail == NULL){ + uintptr_t m = malloc(sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); + if(!m) return; + cchunked_node_t* node = (cchunked_node_t*)m; + node->count = 0; + node->next = NULL; + list->head=list->tail = node; + } + if(list->tail->count == list->chunkSize){ //last chunk + uintptr_t m = malloc(sizeof(cchunked_node_t)+list->chunkSize * sizeof(void*)); + if(!m) return; + cchunked_node_t* node=(cchunked_node_t*)m; + node->count = 0; + node->next = NULL; + list->tail->next = node; + list->tail = node; + } + list->tail->data[list->tail->count++]=data; + list->length++; +} + +cchunked_node_t* cchunked_list_insert_after(cchunked_list_t* list,cchunked_node_t* node, void* data){ + if(!list) + return NULL; + if(!node){ + cchunked_list_push_back(list, data); + return list->tail; + } + if(node->count < list->chunkSize){//chunk isnt completrly full + node->data[node->count++]=data; + list->length++; + return node; + } + //new node + uintptr_t m = malloc(sizeof(cchunked_node_t) + list->chunkSize*sizeof(void*)); + if(!m) return NULL; + cchunked_node_t* new_node = (cchunked_node_t*)m; + new_node->count =1; + new_node->next = node->next; + new_node->data[0] = data; + + //linking new node + node->next = new_node; + if (list->tail == node) + list->tail = new_node; + list->length++; + return new_node; +} +void* cchunked_list_pop_front(cchunked_list_t* list){ + if(!list || list->length == 0 || list->head == NULL) + return NULL; + void* data = list->head->data[0]; + if(list->head->count > 1){ + for(uint64_t i = 1; i < list->head->count; ++i) + list->head->data[i-1] = list->head->data[i]; + list->head->count--; + }else{ + //uniq element + cchunked_node_t* old =list->head; + list->head = old->next; + if(list->head == NULL){ + list->tail = NULL; + } + free(old, sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); + } + list->length--; + return data; +} + + + +void* cchunked_list_remove_node(cchunked_list_t* list, cchunked_node_t* node){ + if(!list||!node|| !list->head) return NULL; + + //delegate to pop_front + if(node == list->head) + return cchunked_list_pop_front(list); + + cchunked_node_t* prev = list->head; + while(prev->next && prev->next != node) prev= prev->next; + if(prev->next != node) return NULL; + + void* data = node->data[0]; + for(uint64_t i = 1; i < node->count; ++i) + node->data[i-1] = node->data[i]; + node->count--; + list->length--; + if(node->count == 0){ + prev->next = node->next; + if(list->tail == node) list->tail = prev; + free(node, sizeof(cchunked_node_t)+list->chunkSize * sizeof(void*)); + } + + return data; +} + +void cchunked_list_update(cchunked_list_t* list, cchunked_node_t* node, void* new_data){ + (void)list; + if(node&&node->count) node->data[0] = new_data; +} + +uint64_t cchunked_list_length(const cchunked_list_t* list){ + return list ? list->length : 0; +} + +uint64_t cchunked_list_size_bytes(const cchunked_list_t* list){ + if(!list) return 0; + uint64_t nodes = 0; + for(cchunked_node_t* it = list->head; it; it = it->next) nodes++; + return nodes * (sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); +} + +void cchunked_list_for_each(const cchunked_list_t* list, void (*func)(void*)){ + if(!list||!func) return; + for(cchunked_node_t* it = list->head; it; it = it->next) + for(uint64_t i = 0; i < it->count; ++i) + func(it->data[i]); +} +void cchunked_list_update_at(cchunked_list_t* list, cchunked_node_t* node,uint64_t offset,void* new_data){ + if(!list || !node || offset >= node->count)//{ + return; + node->data[offset] = new_data; +} + +int cchunked_list_is_empty(const cchunked_list_t* list){ + return (!list ||list->length == 0) ? 1 : 0; +} +} diff --git a/shared/data_struct/chunked_list.hpp b/shared/data_struct/chunked_list.hpp new file mode 100644 index 00000000..d20c3142 --- /dev/null +++ b/shared/data_struct/chunked_list.hpp @@ -0,0 +1,202 @@ +#pragma once + +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +extern uintptr_t malloc(uint64_t size); +extern void free(void *ptr, uint64_t size); + +typedef struct cchunked_node{ + uint64_t count; + struct cchunked_node* next; + void* data[]; +}cchunked_node_t; + +typedef struct cchunked_list{ + uint64_t chunkSize; + uint64_t length; + cchunked_node_t* head; + cchunked_node_t* tail; +}cchunked_list_t; + +cchunked_list_t* cchunked_list_create(uint64_t chunkSize); +void cchunked_list_destroy(cchunked_list_t* list); +cchunked_list_t* cchunked_list_clone(const cchunked_list_t* list); +void cchunked_list_push_back(cchunked_list_t* list, void* data); +void* cchunked_list_pop_front(cchunked_list_t* list); +cchunked_node_t* cchunked_list_insert_after(cchunked_list_t* list, cchunked_node_t* node, void* data); +void* cchunked_list_remove_node(cchunked_list_t* list, cchunked_node_t* node); +void cchunked_list_update(cchunked_list_t* list, cchunked_node_t* node, void* new_data); +void cchunked_list_update_at(cchunked_list_t* list, cchunked_node_t* node,uint64_t offset, void* new_data); +uint64_t cchunked_list_length(const cchunked_list_t* list); +uint64_t cchunked_list_size_bytes(const cchunked_list_t* list); +void cchunked_list_for_each(const cchunked_list_t* list, void (*func)(void*)); +int cchunked_list_is_empty(const cchunked_list_t* list); + +#ifdef __cplusplus +} +template +class ChunkedList{ +public: + struct Node{ + uint64_t count; + Node* next; + T data[]; + }; + + explicit ChunkedList(uint64_t cs): head(nullptr), tail(nullptr), length_(0), chunkSize(cs){} + ~ChunkedList(){ + while(head){ + Node* nx = head->next; + ::free(head, sizeof(Node) + chunkSize * sizeof(T)); + head = nx; + } + } + + ChunkedList(const ChunkedList& other): head(nullptr), tail(nullptr), length_(0), chunkSize(other.chunkSize){ + for(Node* c = other.head; c; c = c->next) + for(uint64_t i = 0; i < c->count; ++i) + push_back(c->data[i]); + } + + ChunkedList& operator=(const ChunkedList& other){ + if(this != &other){ + clear(); + chunkSize = other.chunkSize; + for(Node* c = other.head; c; c = c->next) + for(uint64_t i = 0; i < c->count; ++i) + push_back(c->data[i]); + } + return *this; + } + + void push_back(const T& value){ + if(!tail) allocFirst(); + if(tail->count == chunkSize) allocChunk(); + tail->data[tail->count++] = value; + ++length_; + } + + T pop_front(){ + if(!head) return T(); + T val = head->data[0]; + if(head->count > 1){ + for(uint64_t i = 1; i < head->count; ++i) + head->data[i-1] = head->data[i]; + --head->count; + }else{ + Node* old = head; + head = head->next; + ::free(old, sizeof(Node) + chunkSize * sizeof(T)); + if(!head) tail = nullptr; + } + --length_; + return val; + } + + Node* insert_after(Node* node, const T& value){ + if(!node){ + push_back(value); + return tail; + } + if(node->count < chunkSize){ + node->data[node->count++] = value; + ++length_; + return node; + } + Node* n=allocNode(value); + n->next = node->next; + node->next = n; + if(tail ==node) tail = n; + ++length_; + return n; + } + +T remove_node(Node* node){ + if(!node|| !head) return T(); + if(node == head){ + T val = head->data[0]; + uint64_t removed = head->count; + Node* nxt = head->next; + ::free(head, sizeof(Node) + chunkSize * sizeof(T)); + head = nxt; + if(!head) tail = nullptr; + length_ -= removed; + return val; + } + Node* prev = head; + while(prev->next && prev->next != node) prev = prev->next; + if(prev->next != node) return T(); + T val = node->data[0]; + uint64_t removed = node->count; + prev->next= node->next; + if(tail==node) tail = prev; + ::free(node, sizeof(Node) + chunkSize * sizeof(T)); + length_ -= removed; + return val; +} + + void update(Node* node, const T& value){ + if (node && node->count) node->data[0] = value; + } + + void update_at(Node* node, uint64_t offset, const T& value){ + if (node && offset < node->count) node->data[offset] = value; + } + + uint64_t size() const noexcept{return length_;} + + uint64_t size_bytes() const noexcept{ + uint64_t nodes = 0; + for (Node* it = head; it; it = it->next) ++nodes; + return nodes * (sizeof(Node) + chunkSize * sizeof(T)); + } + + bool empty() const noexcept{return length_ == 0; } + + template + void for_each(Func f) const{ + for(Node* it = head; it; it = it->next) + for(uint64_t i = 0; i < it->count; ++i) + f(it->data[i]); + } + +private: + Node* head = nullptr; + Node* tail = nullptr; + uint64_t length_ = 0; + uint64_t chunkSize; + + Node* allocNode(const T& value){ + uintptr_t raw= ::malloc(sizeof(Node) + chunkSize * sizeof(T)); + Node* n = reinterpret_cast(raw); + n->count = 1; + n->next = nullptr; + n->data[0] = value; + return n; + } + + void allocFirst(){ + uintptr_t raw = ::malloc(sizeof(Node) +chunkSize*sizeof(T)); + head = tail =reinterpret_cast(raw); + head->count = 0; + head->next = nullptr; + } + + void allocChunk(){ + uintptr_t raw = ::malloc(sizeof(Node)+ chunkSize * sizeof(T)); + Node* n=reinterpret_cast(raw); + n->count = 0; + n->next = nullptr; + tail->next = n; + tail = n; + } + + void clear(){ + while(head) pop_front(); + } +}; +#endif \ No newline at end of file diff --git a/shared/data_struct/circular_linked_list.cpp b/shared/data_struct/circular_linked_list.cpp new file mode 100644 index 00000000..e69de29b diff --git a/shared/data_struct/circular_linked_list.hpp b/shared/data_struct/circular_linked_list.hpp new file mode 100644 index 00000000..e69de29b diff --git a/shared/data_struct/doubly_linked_list.cpp b/shared/data_struct/doubly_linked_list.cpp new file mode 100644 index 00000000..e69de29b diff --git a/shared/data_struct/doubly_linked_list.hpp b/shared/data_struct/doubly_linked_list.hpp new file mode 100644 index 00000000..e69de29b diff --git a/shared/data_struct/linked_list.cpp b/shared/data_struct/linked_list.cpp new file mode 100644 index 00000000..83c6b307 --- /dev/null +++ b/shared/data_struct/linked_list.cpp @@ -0,0 +1,138 @@ +#include "linked_list.hpp" +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +clinkedlist_t *clinkedlist_create(void){ + uintptr_t mem = malloc(sizeof(clinkedlist_t)); + if((void *)mem == NULL) return NULL; + clinkedlist_t *list = (clinkedlist_t *)mem; + list->head = NULL; + list->tail = NULL; + list->length = 0; + return list; +} + +void clinkedlist_destroy(clinkedlist_t *list){ + if(list == NULL) return; + clinkedlist_node_t *node = list->head; + while(node){ + clinkedlist_node_t *next = node->next; + free(node, sizeof(clinkedlist_node_t)); + node = next; + } + free(list, sizeof(clinkedlist_t)); +} + +clinkedlist_t *clinkedlist_clone(const clinkedlist_t *list){ + if(list == NULL) return NULL; + clinkedlist_t *clone = clinkedlist_create(); + if(clone == NULL) return NULL; + clinkedlist_node_t *it = list->head; + while(it){ + if(clone->tail){ + clinkedlist_node_t *new_node = (clinkedlist_node_t *)malloc(sizeof(clinkedlist_node_t)); + new_node->data = it->data; + new_node->next = NULL; + clone->tail->next = new_node; + clone->tail = new_node; + clone->length++; + }else{ + clinkedlist_push_front(clone, it->data); + } + it = it->next; + } + return clone; +} + +void clinkedlist_push_front(clinkedlist_t *list, void *data){ + if(list == NULL) return; + clinkedlist_node_t *node = (clinkedlist_node_t *)malloc(sizeof(clinkedlist_node_t)); + node->data = data; + node->next = list->head; + list->head = node; + if(list->tail == NULL) list->tail = node; + list->length++; +} + +void *clinkedlist_pop_front(clinkedlist_t *list){ + if(list == NULL || list->head== NULL) return NULL; + clinkedlist_node_t *node = list->head; + void *data = node->data; + list->head = node->next; + if (list->head == NULL) list->tail = NULL; + list->length--; + free(node, sizeof(clinkedlist_node_t)); + return data; +} + +clinkedlist_node_t *clinkedlist_insert_after(clinkedlist_t *list, clinkedlist_node_t *node, void *data) { + if(list == NULL) return NULL; + if(node == NULL){ + clinkedlist_push_front(list, data); + return list->head; + } + clinkedlist_node_t *new_node = (clinkedlist_node_t *)malloc(sizeof(clinkedlist_node_t)); + new_node->data = data; + new_node->next = node->next; + node->next = new_node; + if(list->tail == node) list->tail = new_node; + list->length++; + return new_node; +} + +void *clinkedlist_remove(clinkedlist_t *list, clinkedlist_node_t *node){ + if(list == NULL || node == NULL || list->head == NULL) return NULL; + if(node == list->head){ + return clinkedlist_pop_front(list); + } + clinkedlist_node_t *prev = list->head; + while(prev->next && prev->next != node){ + prev = prev->next; + } + if(prev->next != node) return NULL; + prev->next = node->next; + if(node == list->tail) list->tail = prev; + void *data = node->data; + list->length--; + free(node, sizeof(clinkedlist_node_t)); + return data; +} + +void clinkedlist_update(clinkedlist_t *list, clinkedlist_node_t *node, void *new_data){ + (void)list; + if(node) node->data = new_data; +} + +uint64_t clinkedlist_length(const clinkedlist_t *list){ + return list ? list->length : 0; +} + +uint64_t clinkedlist_size_bytes(const clinkedlist_t *list){ + return list ? list->length * sizeof(clinkedlist_node_t) : 0; +} + +clinkedlist_node_t *clinkedlist_find(clinkedlist_t *list, void *key, int (*cmp)(void *, void *)){ + if(list == NULL || cmp == NULL) return NULL; + clinkedlist_node_t *it = list->head; + while(it){ + if(cmp(it->data, key) == 0) return it; + it = it->next; + } + return NULL; +} + +void clinkedlist_for_each(const clinkedlist_t *list, void (*func)(void *)){ + if(list == NULL || func == NULL) return; + clinkedlist_node_t *it = list->head; + while(it){ + func(it->data); + it = it->next; + } +} + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/shared/data_struct/linked_list.hpp b/shared/data_struct/linked_list.hpp new file mode 100644 index 00000000..0387e237 --- /dev/null +++ b/shared/data_struct/linked_list.hpp @@ -0,0 +1,157 @@ +#pragma once +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif +typedef struct clinkedlist_node{ + void *data; + struct clinkedlist_node *next; +}clinkedlist_node_t; + +typedef struct clinkedlist{ + clinkedlist_node_t *head; + clinkedlist_node_t *tail; + uint64_t length; +}clinkedlist_t; + +extern uintptr_t malloc(uint64_t size); +extern void free(void *ptr, uint64_t size); + +clinkedlist_t *clinkedlist_create(void); +void clinkedlist_destroy(clinkedlist_t *list); +clinkedlist_t *clinkedlist_clone(const clinkedlist_t *list); +void clinkedlist_push_front(clinkedlist_t *list,void *data); +void *clinkedlist_pop_front(clinkedlist_t *list); +clinkedlist_node_t *clinkedlist_insert_after(clinkedlist_t *list, clinkedlist_node_t *node, void *data); +void *clinkedlist_remove(clinkedlist_t *list, clinkedlist_node_t *node); +void clinkedlist_update(clinkedlist_t *list, clinkedlist_node_t *node, void *new_data); +uint64_t clinkedlist_length(const clinkedlist_t *list); +uint64_t clinkedlist_size_bytes(const clinkedlist_t *list); +clinkedlist_node_t *clinkedlist_find(clinkedlist_t *list, void *key,int (*cmp)(void *, void *)); +void clinkedlist_for_each(const clinkedlist_t *list, void (*func)(void *)); + +#ifdef __cplusplus +} + +template class LinkedList{ +private: + struct Node{ + T data; + Node *next; + }; + Node *head = nullptr; + Node *tail = nullptr; + uint64_t length = 0; + + Node *alloc_node(const T &value){ + uintptr_t mem = malloc(sizeof(Node)); + Node *n = reinterpret_cast(mem); + n->data = value; + n->next = nullptr; + return n; + } + void free_node(Node *n){ + free(n, sizeof(Node)); + } + +public: + LinkedList() = default; + + LinkedList(const LinkedList &other){ + for(Node *it = other.head; it; it = it->next){ + push_front(it->data); + } + LinkedList tmp; + while(!empty()){ + tmp.push_front(pop_front()); + } + *this = tmp; + } + + ~LinkedList(){ + while(!empty()) pop_front(); + } + + LinkedList &operator=(const LinkedList &other){ + if(this != &other){ + while(!empty()) pop_front(); + for(Node *it = other.head; it; it = it->next){ + push_front(it->data); + } + LinkedList tmp; + while(!empty()){ + tmp.push_front(pop_front()); + } + head = tmp.head; + tail = tmp.tail; + length = tmp.length; + tmp.head = tmp.tail = nullptr; + tmp.length = 0; + } + return *this; + } + + void push_front(const T &value){ + Node *n = alloc_node(value); + n->next = head; + head = n; + if (tail == nullptr) tail = n; + ++length; + } + + T pop_front(){ + Node *n = head; + head = head->next; + if (head == nullptr) tail = nullptr; + T val = n->data; + free_node(n); + --length; + return val; + } + + Node *insert_after(Node *node, const T &value){ + if(node == nullptr){ + push_front(value); + return head; + } + Node *n = alloc_node(value); + n->next = node->next; + node->next = n; + if(tail == node) tail = n; + ++length; + return n; + } + + T remove(Node *node){ + if(node == nullptr) return T(); + if(node == head) return pop_front(); + Node *prev = head; + while(prev && prev->next != node) prev = prev->next; + if(prev == nullptr) return T(); + prev->next = node->next; + if(node == tail) tail = prev; + T val = node->data; + free_node(node); + --length; + return val; + } + + void update(Node *node, const T &value){ + if(node) node->data = value; + } + + uint64_t size() const{return length;} + bool empty() const{return length == 0; } + Node *begin() const{ return head; } + Node *end() const{ return nullptr;} + template + Node *find(Predicate pred) const{ + for(Node *it = head; it; it = it->next){ + if(pred(it->data)) return it; + } + return nullptr; + } +}; + +#endif diff --git a/shared/data_struct/queue.cpp b/shared/data_struct/queue.cpp new file mode 100644 index 00000000..e69de29b diff --git a/shared/data_struct/queue.hpp b/shared/data_struct/queue.hpp new file mode 100644 index 00000000..e69de29b diff --git a/shared/data_struct/ring_buffer.cpp b/shared/data_struct/ring_buffer.cpp new file mode 100644 index 00000000..281117f6 --- /dev/null +++ b/shared/data_struct/ring_buffer.cpp @@ -0,0 +1,58 @@ +#include "types.h" +#include "ring_buffer.hpp" +#include "std/memfunctions.h" + +void cring_init(struct CRingBuffer* rb, void* storage, uint64_t capacity, uint64_t elem_size){ + rb->buffer = storage; + rb->capacity = capacity; + rb->element_size = elem_size; + rb->head = 0; + rb->tail = 0; + rb->full = 0; +} +uint64_t cring_capacity(const struct CRingBuffer* rb){ + return rb->capacity; +} +int32_t cring_push(struct CRingBuffer* rb, const void* item){ + if (rb->full) return 0; + + uint8_t* base = (uint8_t*)rb->buffer; + void* dest = base + (rb->head * rb->element_size); + + for(uint32_t i = 0; i < rb->element_size; ++i){ + ((uint8_t*)dest)[i] =((const uint8_t*)item)[i]; + } + + rb->head = (rb->head + 1) % rb->capacity; + rb->full = (rb->head == rb->tail); + return 1; +} + +int32_t cring_pop(struct CRingBuffer* rb, void* out){ + if(!rb->full && (rb->head == rb->tail)) return 0; + + uint8_t* base = (uint8_t*)rb->buffer; + void* src = base + (rb->tail * rb->element_size); + + for(uint64_t i = 0; i < rb->element_size; ++i){ + ((uint8_t*)out)[i] = ((uint8_t*)src)[i]; + } + + rb->tail =(rb->tail + 1) % rb->capacity; + rb->full =0; + return 1; +} + +int32_t cring_is_empty(const struct CRingBuffer* rb){ + return (!rb->full && (rb->head == rb->tail)); +} + +int32_t cring_is_full(const struct CRingBuffer* rb){ + return rb->full; +} + +void cring_clear(struct CRingBuffer* rb){ + rb->head = 0; + rb->tail = 0; + rb->full = 0; +} diff --git a/shared/data_struct/ring_buffer.hpp b/shared/data_struct/ring_buffer.hpp new file mode 100644 index 00000000..73b09d5b --- /dev/null +++ b/shared/data_struct/ring_buffer.hpp @@ -0,0 +1,78 @@ +#pragma once +#include "types.h" + +extern "C" { +struct CRingBuffer{ + void* buffer; + uint64_t capacity; //TODO: define size_t as the same size of os architecture + uint64_t element_size; + uint64_t head; + uint64_t tail; + int32_t full; +}; + +void cring_init(struct CRingBuffer* rb, void* storage, uint64_t capacity, uint64_t elem_size); +int32_t cring_push(struct CRingBuffer* rb, const void* item); +int32_t cring_pop(struct CRingBuffer* rb, void* out); +int32_t cring_is_empty(const struct CRingBuffer* rb); +int32_t cring_is_full(const struct CRingBuffer* rb); +void cring_clear(struct CRingBuffer* rb); +uint64_t cring_capacity(const struct CRingBuffer* rb); +} + +template class RingBuffer{ +private: + T data[Capacity]; + uint64_t head = 0; + uint64_t tail = 0; + int32_t full = 0; + +public: + int32_t push(const T& item){ + if(full) return 0; + data[head] = item; + head = (head + 1) % Capacity; + full =(head == tail); + return 1; + } + + int32_t pop(T& out){ + if(is_empty()) return 0; + out = data[tail]; + tail = (tail + 1)% Capacity; + full = 0; + return 1; + } + + int32_t is_empty() const{ + return (!full && (head == tail)); + } + + int32_t is_full() const{ + return full; + } + + void clear(){ + head = tail= 0; + full = 0; + } + + uint64_t size() const{ + if(full) return Capacity; + if(head >= tail) return head - tail; + return Capacity + head - tail; + } + + const T& peek() const{ + return data[tail]; + } + + const T& at(uint32_t index) const{ + return data[(tail + index) % Capacity]; + } + + T& at(uint32_t index){ + return data[(tail + index)% Capacity]; + } + static constexpr uint64_t capacity(){return Capacity;} +}; From 28c285f491f1d11453e5444298b38dad01f6e6f7 Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Sun, 20 Jul 2025 18:58:21 +0200 Subject: [PATCH 06/33] Update Makefile --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 321e3d47..09ca7548 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ user: $(MAKE) -C user kernel: - $(MAKE) -C kernel LOAD_ADDR=$(LOAD_ADDR) XHCI_CTX_SIZE=$(XHCI_CTX_SIZE) + $(MAKE) -C kernel LOAD_ADDR=$(LOAD_ADDR) XHCI_CTX_SIZE=$(XHCI_CTX_SIZE) clean: $(MAKE) -C shared clean @@ -96,4 +96,4 @@ help: make dump disassemble kernel.elf\n\ make install create raspi kernel and mount it on a bootable partition\n\ make prepare-fs create directories for the filesystem\n\ -\n" \ No newline at end of file +\n" From ed32adab074adf6af83f1ccf8643c708ab7de0b3 Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Sun, 20 Jul 2025 19:04:53 +0200 Subject: [PATCH 07/33] Update Makefile --- kernel/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/Makefile b/kernel/Makefile index 1c47cf4a..68ffc091 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -5,7 +5,7 @@ LDFLAGS := $(LDFLAGS_BASE) -T $(shell ls *.ld) --defsym=LOAD_ADDR=$(LOAD_ADDR) C_SRC := $(shell find . -name '*.c') ASM_SRC := $(shell find . -name '*.S') CPP_SRC := $(shell find . -name '*.cpp') -OBJ := $(C_SRC:.c=.o) $(ASM_SRC:.S=.o) $(CPP_SRC:.cpp:.o) +OBJ := $(C_SRC:.c=.o) $(ASM_SRC:.S=.o) $(CPP_SRC:.cpp=.o) OBJL := $(filter-out ./boot.o,$(OBJ)) ELF := ../kernel.elf @@ -26,4 +26,4 @@ $(TARGET): ../shared/libshared.a $(OBJ) $(CC) $(CFLAGS) -fno-rtti -c $< -o $@ clean: - rm -f $(OBJ) $(ELF) $(TARGET) \ No newline at end of file + rm -f $(OBJ) $(ELF) $(TARGET) From 9a229f028ce954367977c59ae2d48dfe2163da7d Mon Sep 17 00:00:00 2001 From: CodeAnarchist Date: Tue, 22 Jul 2025 01:04:03 +0200 Subject: [PATCH 08/33] queues and double linkedlist --- shared/data_struct/circular_linked_list.cpp | 0 shared/data_struct/circular_linked_list.hpp | 0 shared/data_struct/doubly_linked_list.cpp | 181 +++++++++++++++ shared/data_struct/doubly_linked_list.hpp | 234 ++++++++++++++++++++ shared/data_struct/linked_list.hpp | 2 +- shared/data_struct/queue.cpp | 70 ++++++ shared/data_struct/queue.hpp | 45 ++++ 7 files changed, 531 insertions(+), 1 deletion(-) delete mode 100644 shared/data_struct/circular_linked_list.cpp delete mode 100644 shared/data_struct/circular_linked_list.hpp diff --git a/shared/data_struct/circular_linked_list.cpp b/shared/data_struct/circular_linked_list.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/shared/data_struct/circular_linked_list.hpp b/shared/data_struct/circular_linked_list.hpp deleted file mode 100644 index e69de29b..00000000 diff --git a/shared/data_struct/doubly_linked_list.cpp b/shared/data_struct/doubly_linked_list.cpp index e69de29b..8623c6bd 100644 --- a/shared/data_struct/doubly_linked_list.cpp +++ b/shared/data_struct/doubly_linked_list.cpp @@ -0,0 +1,181 @@ +#include "doubly_linked_list.hpp" + +cdouble_linked_list_t* cdouble_linked_list_create(void){ + uintptr_t raw= malloc((uint64_t)sizeof(cdouble_linked_list_t));//i believe that casting sizeof is better until size_t is defined correctly + if(raw == 0) return NULL; + cdouble_linked_list_t *list=(cdouble_linked_list_t*)raw; + list->head = list->tail =NULL; + list->length = 0; + return list; +} + +void cdouble_linked_list_destroy(cdouble_linked_list_t *list){ + if(!list) return; + cdouble_linked_list_node_t *node=list->head; + for(uint64_t i = 0; i < list->length; ++i){ + cdouble_linked_list_node_t *next = node->next; + free(node,(uint64_t)sizeof(cdouble_linked_list_node_t)); + node = next; + } + free(list, (uint64_t)sizeof(cdouble_linked_list_t)); +} + +cdouble_linked_list_t* cdouble_linked_list_clone(const cdouble_linked_list_t *list){ //could create data aliasing. be careful. TODO + if(!list) return NULL; + cdouble_linked_list_t *clone=cdouble_linked_list_create(); + if(!clone) return NULL; + cdouble_linked_list_node_t *it =list->head; + for(uint64_t i = 0; ilength; ++i){ + uintptr_t raw =malloc((uint64_t)sizeof(cdouble_linked_list_node_t)); + if(raw) { + cdouble_linked_list_node_t *node =(cdouble_linked_list_node_t*)raw; + node->data = it->data; + if(clone->length == 0){ + node->next=node->prev = node; + clone->head=clone->tail = node; + }else{ + node->prev=clone->tail; + node->next=clone->head; + clone->tail->next=node; + clone->head->prev= node; + clone->tail =node; + } + ++clone->length; + } + it = it->next; + } + return clone; +} + +void cdouble_linked_list_push_front(cdouble_linked_list_t *list, void *data){ + if(!list) return; + uintptr_t raw= malloc((uint64_t)sizeof(cdouble_linked_list_node_t)); + if(raw == 0) return; + cdouble_linked_list_node_t *node=(cdouble_linked_list_node_t*)raw; + node->data = data; + if(list->length == 0){ + node->next=node->prev = node; + list->head=list->tail = node; + }else{ + node->next=list->head; + node->prev=list->tail; + list->head->prev= node; + list->tail->next= node; + list->head= node; + } + ++list->length; +} + +void cdouble_linked_list_push_back(cdouble_linked_list_t* list, void* data){ + if(!list) return; + uintptr_t raw=malloc((uint64_t)sizeof(cdouble_linked_list_node_t)); + if(raw ==0) return; + cdouble_linked_list_node_t* node=(cdouble_linked_list_node_t*)raw; + node->data=data; + if(list->length==0){ + node->next=node->prev=node; + list->head=list->tail = node; + }else{ + node->prev= list->tail; + node->next=list->head; + list->tail->next=node; + list->head->prev=node; + list->tail =node; + } + ++list->length; +} + +void* cdouble_linked_list_pop_front(cdouble_linked_list_t* list){ + if(!list||list->length==0) return NULL; + cdouble_linked_list_node_t* node=list->head; + void* data=node->data; + if(list->length == 1){ + list->head=list->tail=NULL; + }else{ + list->head=node->next; + list->head->prev =list->tail; + list->tail->next=list->head; + } + --list->length; + free(node,(uint64_t)sizeof(cdouble_linked_list_node_t)); + return data; +} + +void* cdouble_linked_list_pop_back(cdouble_linked_list_t* list){ + if(!list||list->length==0) return NULL; + cdouble_linked_list_node_t* node=list->tail; + void* data=node->data; + if(list->length==1){ + list->head = list->tail=NULL; + }else{ + list->tail=node->prev; + list->tail->next=list->head; + list->head->prev=list->tail; + } + --list->length; + free(node, (uint64_t)sizeof(cdouble_linked_list_node_t)); + return data; +} + +cdouble_linked_list_node_t* cdouble_linked_list_insert_after(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* data){ + if(!list) return NULL; + if(!node){ + cdouble_linked_list_push_front(list, data); + return list->head; + } + uintptr_t raw=malloc((uint64_t)sizeof(cdouble_linked_list_node_t)); + if(raw==0) return NULL; + cdouble_linked_list_node_t* new_node=(cdouble_linked_list_node_t*)raw; + new_node->data=data; + new_node->next= node->next; + new_node->prev=node; + node->next->prev= new_node; + node->next= new_node; + if(list->tail == node) list->tail=new_node; + ++list->length; + return new_node; +} + +cdouble_linked_list_node_t* cdouble_linked_list_insert_before(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* data){ + if(!list) return NULL; + if(!node){ + cdouble_linked_list_push_back(list, data); + return list->tail; + } + return cdouble_linked_list_insert_after(list, node->prev, data); +} + +void* cdouble_linked_list_remove(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node){ + if(!list|| !node|| list->length==0) return NULL; + if(node==list->head) return cdouble_linked_list_pop_front(list); + if(node==list->tail) return cdouble_linked_list_pop_back(list); + node->prev->next=node->next; + node->next->prev=node->prev; + void* data=node->data; + --list->length; + free(node,(uint64_t)sizeof(cdouble_linked_list_node_t)); + return data; +} + +void cdouble_linked_list_update(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* new_data){ + (void)list; + if(node) node->data=new_data; +} + +uint64_t cdouble_linked_list_length(const cdouble_linked_list_t* list){ + return list?list-> length:0; +} + +uint64_t cdouble_linked_list_size_bytes(const cdouble_linked_list_t* list){ + return list?list-> length*sizeof(cdouble_linked_list_node_t):0; +} + +cdouble_linked_list_node_t* cdouble_linked_list_find(cdouble_linked_list_t* list, void* key, int (*cmp)(void*,void*)){ + if(!list||!cmp|| list->length==0) return NULL; + cdouble_linked_list_node_t* it=list->head; + for(uint64_t i=0; ilength; ++i){ + if(cmp(it->data,key)==0) return it; + it=it->next; + } + return NULL; +} diff --git a/shared/data_struct/doubly_linked_list.hpp b/shared/data_struct/doubly_linked_list.hpp index e69de29b..1c6d045e 100644 --- a/shared/data_struct/doubly_linked_list.hpp +++ b/shared/data_struct/doubly_linked_list.hpp @@ -0,0 +1,234 @@ +#pragma once +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct cdouble_linked_list_node{ + void *data; + struct cdouble_linked_list_node *next; + struct cdouble_linked_list_node *prev; +}cdouble_linked_list_node_t; + +typedef struct cdouble_linked_list{ + cdouble_linked_list_node_t *head; + cdouble_linked_list_node_t *tail; + uint64_t length; +}cdouble_linked_list_t; + +extern uintptr_t malloc(uint64_t size); +extern void free(void *ptr, uint64_t size); + +cdouble_linked_list_t *cdouble_linked_list_create(void); +void cdouble_linked_list_destroy(cdouble_linked_list_t *list); +cdouble_linked_list_t *cdouble_linked_list_clone(const cdouble_linked_list_t *list); +void cdouble_linked_list_push_front(cdouble_linked_list_t *list, void *data); +void cdouble_linked_list_push_back(cdouble_linked_list_t *list, void *data); +void *cdouble_linked_list_pop_front(cdouble_linked_list_t *list); +void *cdouble_linked_list_pop_back(cdouble_linked_list_t *list); +cdouble_linked_list_node_t *cdouble_linked_list_insert_after(cdouble_linked_list_t *list, cdouble_linked_list_node_t *node, void *data); +cdouble_linked_list_node_t *cdouble_linked_list_insert_before(cdouble_linked_list_t *list, cdouble_linked_list_node_t *node, void *data); +void *cdouble_linked_list_remove(cdouble_linked_list_t *list, cdouble_linked_list_node_t *node); +void cdouble_linked_list_update(cdouble_linked_list_t *list, cdouble_linked_list_node_t *node, void *new_data); +uint64_t cdouble_linked_list_length(const cdouble_linked_list_t *list); +uint64_t cdouble_linked_list_size_bytes(const cdouble_linked_list_t *list); +cdouble_linked_list_node_t* cdouble_linked_list_find(cdouble_linked_list_t *list, void *key, int (*cmp)(void *, void *)); +#ifdef __cplusplus +} + +template +class LinkedList{ +private: + struct Node{ + T data; + Node *next; + Node *prev; + }; + + Node *head; + Node *tail; + uint64_t length; + + Node* alloc_node(const T& value){ + uintptr_t raw= malloc((uint64_t)sizeof(Node)); + if(raw == 0) return nullptr; + Node* n =(Node*)raw; + n->data=value; + n->next=n->prev=nullptr; + return n; + } + void free_node(Node* n){ + if(!n) return; + free(n,(uint64_t)sizeof(Node)); + } + + static void swap(LinkedList& a, LinkedList& b) noexcept{ + Node* tmp_head = a.head; + a.head= b.head; + b.head= tmp_head; + Node* tmp_tail =a.tail; + a.tail= b.tail; + b.tail= tmp_tail; + uint64_t tmp_length = a.length; + a.length = b.length; + b.length = tmp_length; + } + +public: + LinkedList():head(nullptr), tail(nullptr), length(0){} + + LinkedList(const LinkedList& other):head(nullptr), tail(nullptr), length(0){ + if(other.head){ + Node* it = other.head; + do{ + push_back(it->data); + it = it->next; + }while(it != other.head); + } + } + + ~LinkedList(){ + while(!empty()) pop_front(); + } + + LinkedList& operator=(const LinkedList& other){ + if(this != &other){ + LinkedList tmp(other); + swap(*this, tmp); + } + return *this; + } + + void push_front(const T& value){ + Node* n = alloc_node(value); + if(!n) return; + if(!head){ + head = tail = n; + n->next = n->prev = n; + }else{ + n->next= head; + n->prev= tail; + head->prev=n; + tail->next= n; + head= n; + } + ++length; + } + + void push_back(const T& value){ + Node* n =alloc_node(value); + if(!n) return; + if(!tail){ + head = tail = n; + n->next = n->prev = n; + }else{ + n->prev = tail; + n->next = head; + tail->next = n; + head->prev = n; + tail = n; + } + ++length; + } + + T pop_front(){ + if(!head) return T(); + Node* n = head; + T val = n->data; + if(head == tail){ + head =tail=nullptr; + }else{ + head = head->next; + head->prev = tail; + tail->next = head; + } + --length; + free_node(n); + return val; + } + + T pop_back(){ + if(!tail) return T(); + Node* n = tail; + T val = n->data; + if(head == tail){ + head = tail = nullptr; + }else{ + tail = tail->prev; + tail->next = head; + head->prev = tail; + } + --length; + free_node(n); + return val; + } + + Node* insert_after(Node* node, const T& value){ + if(!node){ + push_front(value); + return head; + } + Node* n= alloc_node(value); + if(!n) return nullptr; + n->next=node->next; + n->prev=node; + node->next->prev = n; + node->next= n; + if(tail==node) tail = n; + ++length; + return n; + } + + Node* insert_before(Node* node, const T& value){ + if(!node){ + push_back(value); + return tail; + } + return insert_after(node->prev, value); + } + + T remove(Node* node){ + if(!node) return T(); + if(node == head) return pop_front(); + if(node == tail) return pop_back(); + node->prev->next=node->next; + node->next->prev=node->prev; + T val=node->data; + --length; + free_node(node); + return val; + } + + void update(Node* node, const T& value){ + if(!node) return; + node->data = value; + } + + uint64_t size()const{return length;} + bool empty() const{return length==0;} + Node* begin() const{return head;} + Node* end() const{return nullptr;} + + template + Node* find(Predicate pred) const{ + if(!head) return nullptr; + Node* it=head; + do{ + if(pred(it->data)) return it; + it=it->next; + }while(it != head); + return nullptr; + } + + template + void for_each(Func func) const{ + if(!head) return; + Node* it = head; + do{ + func(it->data); + it = it->next; + }while(it != head); + } +}; +#endif diff --git a/shared/data_struct/linked_list.hpp b/shared/data_struct/linked_list.hpp index 0387e237..c865d37a 100644 --- a/shared/data_struct/linked_list.hpp +++ b/shared/data_struct/linked_list.hpp @@ -28,7 +28,7 @@ void *clinkedlist_remove(clinkedlist_t *list, clinkedlist_node_t *node); void clinkedlist_update(clinkedlist_t *list, clinkedlist_node_t *node, void *new_data); uint64_t clinkedlist_length(const clinkedlist_t *list); uint64_t clinkedlist_size_bytes(const clinkedlist_t *list); -clinkedlist_node_t *clinkedlist_find(clinkedlist_t *list, void *key,int (*cmp)(void *, void *)); +clinkedlist_node_t *clinkedlist_find(clinkedlist_t *list, void *key,int (*cmp)(void *, void *));//so not const function doesnt rise an annoyng warning void clinkedlist_for_each(const clinkedlist_t *list, void (*func)(void *)); #ifdef __cplusplus diff --git a/shared/data_struct/queue.cpp b/shared/data_struct/queue.cpp index e69de29b..d5a9fa58 100644 --- a/shared/data_struct/queue.cpp +++ b/shared/data_struct/queue.cpp @@ -0,0 +1,70 @@ +#include "queue.hpp" +#include "std/memfunctions.h" + +void cqueue_init(CQueue* q,uint64_t max_capacity,uint64_t elem_size){ + q->buffer = NULL; + q->capacity = max_capacity; + q->max_capacity = max_capacity; + q->elem_size = elem_size; + q->head = q->tail = q->length = 0; + if(max_capacity>0){ + uintptr_t b = malloc(max_capacity*elem_size); + if(b) q->buffer = (void*)b; + } +} + +int32_t cqueue_enqueue(CQueue* q,const void* item){ + if(!q) return 0; + if(q->max_capacity>0){ + if(q->length==q->capacity) return 0; + } else { + if(q->length==q->capacity){ + uint64_t nc = q->capacity>0 ? q->capacity*2 : 4; + uintptr_t nb = malloc(nc*q->elem_size); + if(!nb) return 0; + void* newb = (void*)nb; + for(uint64_t i=0;ilength;++i){ + uint64_t idx = (q->tail + i)%q->capacity; + memcpy((uint8_t*)newb + i*q->elem_size, + (uint8_t*)q->buffer + idx*q->elem_size, + q->elem_size); + } + if(q->buffer) free(q->buffer,q->capacity*q->elem_size); + q->buffer=newb; + q->capacity=nc; + q->head=q->length; + q->tail=0; + } + } + memcpy((uint8_t*)q->buffer + q->head*q->elem_size, + item,q->elem_size); + q->head = (q->head+1)%q->capacity; + q->length++; + return 1; +} + +int32_t cqueue_dequeue(CQueue* q,void* out){ + if(!q||q->length==0) return 0; + memcpy(out,(uint8_t*)q->buffer + q->tail*q->elem_size,q->elem_size); + q->tail = (q->tail+1)%q->capacity; + q->length--; + return 1; +} + +int32_t cqueue_is_empty(const CQueue* q){ + return (!q || q->length==0) ? 1 : 0; +} + +uint64_t cqueue_size(const CQueue* q){ + return q ? q->length : 0; +} + +void cqueue_clear(CQueue* q){ + if(!q) return; + q->head=q->tail=q->length=0; +} + +void cqueue_destroy(CQueue* q){ + if(!q) return; + if(q->buffer) free(q->buffer,q->capacity*q->elem_size); +} diff --git a/shared/data_struct/queue.hpp b/shared/data_struct/queue.hpp index e69de29b..d31dcda8 100644 --- a/shared/data_struct/queue.hpp +++ b/shared/data_struct/queue.hpp @@ -0,0 +1,45 @@ +#pragma once +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct CQueue{ + void* buffer; + uint64_t capacity; //curremt queue size + uint64_t max_capacity; //0 is infinite + uint64_t elem_size; + uint64_t head; + uint64_t tail; + uint64_t length; +}CQueue; + +extern uintptr_t malloc(uint64_t); +extern void free(void*,uint64_t); + +void cqueue_init(CQueue* q,uint64_t max_capacity,uint64_t elem_size); +int32_t cqueue_enqueue(CQueue* q,const void* item); +int32_t cqueue_dequeue(CQueue* q,void* out); +int32_t cqueue_is_empty(const CQueue* q); +uint64_t cqueue_size(const CQueue* q); +void cqueue_clear(CQueue* q); +void cqueue_destroy(CQueue* q); + +#ifdef __cplusplus +} + +template +class Queue{ + CQueue q_; +public: + explicit Queue(uint64_t cap=0){cqueue_init(&q_,cap,sizeof(T));} + ~Queue(){ cqueue_destroy(&q_); } + bool enqueue(const T& v){return cqueue_enqueue(&q_,&v);} + bool dequeue(T& out){return cqueue_dequeue(&q_,&out);} + bool isEmpty() const{return cqueue_is_empty(&q_);} + uint64_t size() const{return cqueue_size(&q_);} + void clear(){cqueue_clear(&q_);} +}; + +#endif From 582a73e1ca9c375e74634668e4dec48b7587502f Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 11:33:46 +0200 Subject: [PATCH 09/33] Update kconsole.cpp format --- kernel/console/kconsole/kconsole.cpp | 96 ++++++++++++++-------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index e8975005..0524f096 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -7,8 +7,8 @@ KernelConsole::KernelConsole():cursor_x(0),cursor_y(0),is_initialized(false){ } bool KernelConsole::check_ready(){ - if(!gpu_ready()) return false; - if(!is_initialized){ + if (!gpu_ready()) return false; + if (!is_initialized){ is_initialized= true; resize(); clear(); @@ -17,86 +17,86 @@ bool KernelConsole::check_ready(){ } void KernelConsole::resize(){ - gpu_size s=gpu_get_screen_size(); - columns=s.width/char_width; - rows=s.height/ char_height; + gpu_size s = gpu_get_screen_size(); + columns = s.width / char_width; + rows = s.height / char_height; - if(row_data) temp_free(row_data,buffer_data_size); - buffer_data_size = rows*columns; - row_data= (char*)talloc(buffer_data_size); - if(!row_data){ - rows=columns=0; + if (row_data) temp_free(row_data, buffer_data_size); + buffer_data_size = rows * columns; + row_data = (char*)talloc(buffer_data_size); + if (!row_data){ + rows = columns = 0; row_ring.clear(); return; } row_ring.clear(); - for(uint32_t i=0;i=columns) newline(); + return; + } + if (cursor_x >= columns) newline(); uint32_t ri; - if(row_ring.pop(ri)){ + if (row_ring.pop(ri)){ row_ring.push(ri); - char* line=row_data+ri*columns; - line[cursor_x]=c; - gpu_draw_char({cursor_x*char_width,cursor_y*char_height},c,1,0xFFFFFFFF); + char* line = row_data + ri * columns; + line[cursor_x] = c; + gpu_draw_char({cursor_x * char_width,cursor_y*char_height}, c, 1, 0xFFFFFFFF); cursor_x++; } } void KernelConsole::put_string(const char* s){ - if(!check_ready()) return; - for(uint32_t i=0;s[i];i++) - put_char(s[i]); + if (!check_ready()) return; + for (uint32_t i=0; s[i]; i++) put_char(s[i]); gpu_flush(); } void KernelConsole::put_hex(uint64_t v){ - if(!check_ready()) return; + if (!check_ready()) return; put_char('0'); put_char('x'); - bool started=false; - for(uint32_t i=60;;i-=4){ - uint8_t n=(v>>i)&0xF; - char c=n<10?'0'+n:'A'+(n-10); - if(started||c!='0' ||i==0){ - started=true; + bool started = false; + for(uint32_t i =60 ;; i-=4){ + uint8_t n = (v>>i)&0xF; + char c = n < 10 ? '0' + n : 'A' + (n - 10); + if (started || c!='0' || i==0){ + started = true; put_char(c); } - if(i==0) break; + if (i == 0) break; } gpu_flush(); } void KernelConsole::newline(){ - if(!check_ready()) return; + if (!check_ready()) return; uint32_t ri; - if(row_ring.pop(ri)){ + if (row_ring.pop(ri)){ char* line=row_data+ri*columns; - for(uint32_t x=cursor_x;x=rows){ + if (cursor_y >= rows){ scroll(); - cursor_y= rows -1; + cursor_y = rows -1; } } void KernelConsole::scroll(){ - if(!check_ready()) return; + if (!check_ready()) return; uint32_t ri; - if(row_ring.pop(ri)){ + if (row_ring.pop(ri)){ char* line=row_data+ri*columns; - for(uint32_t x=0;x Date: Fri, 25 Jul 2025 11:34:28 +0200 Subject: [PATCH 10/33] Update kconsole.cpp format --- kernel/console/kconsole/kconsole.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index 0524f096..3581472e 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -1,7 +1,7 @@ #include "kconsole.hpp" #include "console/serial/uart.h" -KernelConsole::KernelConsole():cursor_x(0),cursor_y(0),is_initialized(false){ +KernelConsole::KernelConsole() : cursor_x(0), cursor_y(0), is_initialized(false){ resize(); clear(); } From c7a28967b127bb6faf9745cc752fc0f03363711b Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 11:35:10 +0200 Subject: [PATCH 11/33] Update kconsole.h format --- kernel/console/kconsole/kconsole.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/console/kconsole/kconsole.h b/kernel/console/kconsole/kconsole.h index 7f8aee41..c381bbc2 100644 --- a/kernel/console/kconsole/kconsole.h +++ b/kernel/console/kconsole/kconsole.h @@ -7,8 +7,8 @@ extern "C" { void kconsole_putc(char c); void kconsole_puts(const char *s); void kconsole_puthex(uint64_t value); -void kconsole_clear(); +void kconsole_clear(); #ifdef __cplusplus } -#endif \ No newline at end of file +#endif From 2673f8ecc737a4be7e6672be82e97ec11ff69680 Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 11:35:55 +0200 Subject: [PATCH 12/33] Update kconsole.hpp format --- kernel/console/kconsole/kconsole.hpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/kernel/console/kconsole/kconsole.hpp b/kernel/console/kconsole/kconsole.hpp index 8272d22e..feefa40f 100644 --- a/kernel/console/kconsole/kconsole.hpp +++ b/kernel/console/kconsole/kconsole.hpp @@ -1,4 +1,5 @@ #pragma once + #include "types.h" #include "data_struct/ring_buffer.hpp" #include "graph/graphics.h" @@ -7,9 +8,11 @@ class KernelConsole{ public: KernelConsole(); + void put_char(char c); void put_string(const char* str); void put_hex(uint64_t value); + void newline(); void scroll(); void clear(); @@ -28,9 +31,9 @@ class KernelConsole{ static constexpr uint32_t char_height=16; static constexpr uint32_t max_rows=128; - RingBuffer row_ring; + RingBuffer row_ring; char* row_data; uint32_t buffer_data_size; }; -extern KernelConsole kconsole; \ No newline at end of file +extern KernelConsole kconsole; From 9a5890746bfa26e894328769e1d3a46d1c74294b Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 11:36:50 +0200 Subject: [PATCH 13/33] Update fat32.cpp format --- kernel/filesystem/fat32.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kernel/filesystem/fat32.cpp b/kernel/filesystem/fat32.cpp index 0da4fa9b..00eca4dd 100644 --- a/kernel/filesystem/fat32.cpp +++ b/kernel/filesystem/fat32.cpp @@ -6,7 +6,6 @@ #include "std/string.h" #include "std/memfunctions.h" #include "math/math.h" -#include "data_struct/linked_list.hpp" #define kprintfv(fmt, ...) \ ({ \ @@ -290,4 +289,4 @@ string_list* FAT32FS::list_contents(char *path){ uint32_t count = count_FAT(mbs->first_cluster_of_root_directory); return (string_list*)walk_directory(count, mbs->first_cluster_of_root_directory, path, list_entries_handler); -} \ No newline at end of file +} From 38b04b7ae91d071050f2e2ea5113f08fbbb5564c Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 11:44:40 +0200 Subject: [PATCH 14/33] Update kstring.c format --- kernel/kstring.c | 126 +++++++++++++++++++++-------------------------- 1 file changed, 56 insertions(+), 70 deletions(-) diff --git a/kernel/kstring.c b/kernel/kstring.c index 6cce742a..e4157794 100644 --- a/kernel/kstring.c +++ b/kernel/kstring.c @@ -6,80 +6,71 @@ //TODO: we can most likely get rid of this class now and use string entirely static uint32_t compute_length(const char *s, uint32_t max_length){ - if(s == NULL){return 0;} + if (s == NULL) return 0; + uint32_t len = 0; - while((max_length == 0 || len < max_length) && s[len] != '\0'){ - len++; - } + while ((max_length == 0 || len < max_length) && s[len] != '\0') len++; + return len; } kstring kstring_l(const char *literal){ - if(literal == NULL){ - return (kstring){.data = NULL, .length = 0}; - } + + if (literal == NULL) return (kstring){.data = NULL, .length = 0}; + uint32_t len = compute_length(literal, 0); - char *buf = (char*)talloc(len+1); - if(!buf){ - return (kstring){ .data = NULL, .length = 0}; - } - for(uint32_t i = 0; i < len; i++){ - buf[i] = literal[i]; - } + char *buf = (char*)talloc(len + 1); + if (!buf) return (kstring){ .data = NULL, .length = 0}; + + for (uint32_t i = 0; i < len; i++) buf[i] = literal[i]; + buf[len] = '\0'; return (kstring){.data = buf, .length = len}; } kstring kstring_repeat(char symbol, uint32_t amount){ - char *buf = (char*)talloc(amount+ 1); - if(!buf){ - return (kstring){ .data = NULL, .length = 0}; - } + char *buf = (char*)talloc(amount + 1); + if(!buf) return (kstring){ .data = NULL, .length = 0}; + memset(buf, symbol, amount); buf[amount] = '\0'; return (kstring){.data = buf, .length = amount}; } kstring kstring_tail(const char *array, uint32_t max_length){ - if(array == NULL){ - return (kstring){.data = NULL, .length = 0}; - } + if (array == NULL) return (kstring){.data = NULL, .length = 0}; + uint32_t len = compute_length(array, 0); - int offset = (int)len-(int)max_length; - if (offset < 0) offset=0; + int offset = (int)len - (int)max_length; + if (offset < 0) offset = 0; + uint32_t adjusted_len = len - offset; char *buf = (char*)talloc(adjusted_len+1); - if(!buf){ - return (kstring){.data = NULL, .length = 0}; - } - for(uint32_t i = 0; i < adjusted_len; i++){ - buf[i] = array[offset + i]; - } + if (!buf) return (kstring){.data = NULL, .length = 0}; + + for (uint32_t i = 0; i < adjusted_len; i++) buf[i] = array[offset + i]; + buf[adjusted_len] = '\0'; return (kstring){ .data = buf, .length = adjusted_len}; } kstring kstring_ca_max(const char *array, uint32_t max_length){ - if(array == NULL){ - return (kstring){ .data = NULL, .length = 0 }; - } + if(array == NULL) return (kstring){ .data = NULL, .length = 0 }; + uint32_t len=compute_length(array, max_length); char *buf = (char*)talloc(len+1); - if(!buf) { - return (kstring){.data = NULL, .length = 0}; - } - for(uint32_t i = 0; i < len; i++){ - buf[i] = array[i]; - } + if(!buf) return (kstring){.data = NULL, .length = 0}; + + for(uint32_t i = 0; i < len; i++) buf[i] = array[i]; + buf[len] = '\0'; return (kstring){.data = buf, .length = len}; } kstring kstring_c(const char c){ - char *buf=(char*)talloc(2); - if(!buf){ - return (kstring){ .data = NULL, .length = 0 }; - } + char *buf = (char*)talloc(2); + if(!buf) return (kstring){ .data = NULL, .length = 0 }; + buf[0] = c; buf[1] = '\0'; return (kstring){.data = buf, .length = 1}; @@ -92,14 +83,14 @@ kstring kstring_from_hex(uint64_t value){ buf[len++] = 'x'; bool started = false; - for(uint32_t i = 60;; i -= 4){ + for (uint32_t i = 60;; i -= 4){ uint8_t nibble = (value>>i)&0xF; - char curr_char=nibble<10?'0' + nibble : 'A'+(nibble - 10); - if(started||curr_char != '0'||i == 0) { + char curr_char = nibble < 10 ? '0' + nibble : 'A'+(nibble - 10); + if (started || curr_char != '0' || i == 0) { started = true; buf[len++] = curr_char; } - if(i == 0) break; + if (i == 0) break; } buf[len] = 0; @@ -113,41 +104,36 @@ kstring kstring_from_bin(uint64_t value){ buf[len++] = 'b'; bool started = false; - for(uint32_t i = 60;; i --){ + for (uint32_t i = 60;; i--){ char bit = (value >> i) & 1 ? '1' : '0'; - if(started || bit != '0' || i == 0){ + if (started || bit != '0' || i == 0){ started = true; buf[len++] = bit; } - if(i == 0) break; + if (i == 0) break; } buf[len] = 0; return (kstring){ .data = buf, .length = len }; } bool kstring_equals(kstring a, kstring b){ - return strcmp(a.data,b.data, false) == 0; + return strcmp(a.data, b.data, false) == 0; } kstring kstring_format_args(const char *fmt, const uint64_t *args, uint32_t arg_count){ - if(fmt == NULL){ - return (kstring){ .data = NULL, .length = 0 }; - } - if(arg_count > 0 && args == NULL){ - return (kstring){ .data = NULL, .length = 0 }; - } + if(fmt == NULL) return (kstring){ .data = NULL, .length = 0 }; + + if(arg_count > 0 && args == NULL) return (kstring){ .data = NULL, .length = 0 }; char *buf = (char*)talloc(256); - if(!buf){ - return (kstring){ .data = NULL, .length = 0 }; - } + if (!buf) return (kstring){ .data = NULL, .length = 0 }; uint32_t len = 0; uint32_t arg_index = 0; - for(uint32_t i = 0; fmt[i] && len < 255; i++){ - if(fmt[i] == '%' && fmt[i+1]) { + for (uint32_t i = 0; fmt[i] && len < 255; i++){ + if (fmt[i] == '%' && fmt[i + 1]) { i++; - if(arg_index >= arg_count){ + if (arg_index >= arg_count){ buf[len++] = '%'; buf[len++] = fmt[i]; continue; @@ -174,8 +160,8 @@ kstring kstring_format_args(const char *fmt, const uint64_t *args, uint32_t arg_ } case 's': { const char *str = (const char *)(uintptr_t)args[arg_index++]; - if(str){ - for(uint32_t j = 0; str[j] && len < 255; j++) buf[len++] = str[j]; + if (str){ + for (uint32_t j = 0; str[j] && len < 255; j++) buf[len++] = str[j]; } break; } @@ -184,18 +170,18 @@ kstring kstring_format_args(const char *fmt, const uint64_t *args, uint32_t arg_ char temp[21]; uint32_t temp_len = 0; bool negative = false; - if((int64_t)val < 0){ + if ((int64_t)val < 0){ negative = true; val = (uint64_t)(-(int64_t)val); } - do{ + do { temp[temp_len++] = '0' + (val % 10); val /= 10; - }while(val && temp_len < sizeof(temp)-1); - if(negative && temp_len < sizeof(temp)-1){ + } while (val && temp_len < sizeof(temp)-1); + if (negative && temp_len < sizeof(temp)-1){ temp[temp_len++] = '-'; } - for(int j = temp_len - 1; j >= 0 && len < 255; j--){ + for (int j = temp_len - 1; j >= 0 && len < 255; j--){ buf[len++] = temp[j]; } break; @@ -204,7 +190,7 @@ kstring kstring_format_args(const char *fmt, const uint64_t *args, uint32_t arg_ buf[len++] = '%'; buf[len++] = fmt[i]; } - }else{ + } else { buf[len++] = fmt[i]; } } From c0e6bd8efb3f351a22e8c5e9430f5fcf7737c9d8 Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:01:45 +0200 Subject: [PATCH 15/33] Update chunked_list.cpp format --- shared/data_struct/chunked_list.cpp | 121 +++++++++++++++------------- 1 file changed, 65 insertions(+), 56 deletions(-) diff --git a/shared/data_struct/chunked_list.cpp b/shared/data_struct/chunked_list.cpp index 96ceaa02..af95e3d7 100644 --- a/shared/data_struct/chunked_list.cpp +++ b/shared/data_struct/chunked_list.cpp @@ -4,110 +4,119 @@ extern "C" { cchunked_list_t* cchunked_list_create(uint64_t chunkSize){ - uintptr_t raw=malloc(sizeof(cchunked_node_t)+chunkSize*sizeof(void*)); - if(!raw) return NULL; - cchunked_list_t* list=(cchunked_list_t*)malloc(sizeof(cchunked_list_t)); - if(!list){ + uintptr_t raw = malloc(sizeof(cchunked_node_t)+chunkSize*sizeof(void*)); + if (!raw) return NULL; + + cchunked_list_t* list = (cchunked_list_t*)malloc(sizeof(cchunked_list_t)); + if (!list){ free((void*)raw,sizeof(cchunked_node_t)+chunkSize*sizeof(void*)); - return NULL;} + return NULL; + } + list->chunkSize=chunkSize; list->length=0; list->head=(cchunked_node_t*)raw; list->head->count=0; list->head->next=NULL; list->tail=list->head; + return list; } void cchunked_list_destroy(cchunked_list_t* list){ - if(!list) return; + if (!list) return; + cchunked_node_t* node=list->head; - while(node){ + while (node){ cchunked_node_t* next=node->next; free(node,sizeof(cchunked_node_t)+list->chunkSize*sizeof(void*)); node=next; } + free(list,sizeof(cchunked_list_t)); } cchunked_list_t* cchunked_list_clone(const cchunked_list_t* list){ - if(!list) return NULL; + if (!list) return NULL; + cchunked_list_t* clone=cchunked_list_create(list->chunkSize); - if(!clone) return NULL; - for(cchunked_node_t* it=list->head;it;it=it->next){ - for(uint64_t i=0;icount;i++) + if (!clone) return NULL; + + for (cchunked_node_t* it = list->head; it; it = it->next){ + for (uint64_t i = 0; icount; i++) cchunked_list_push_back(clone,it->data[i]); } return clone; } void cchunked_list_push_back(cchunked_list_t* list, void* data){ - if(!list) return; + if (!list) return; //first mnode - if(list->tail == NULL){ + if (list->tail == NULL){ uintptr_t m = malloc(sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); - if(!m) return; + if (!m) return; cchunked_node_t* node = (cchunked_node_t*)m; node->count = 0; - node->next = NULL; + node->next = NULL; list->head=list->tail = node; } - if(list->tail->count == list->chunkSize){ //last chunk - uintptr_t m = malloc(sizeof(cchunked_node_t)+list->chunkSize * sizeof(void*)); - if(!m) return; - cchunked_node_t* node=(cchunked_node_t*)m; + + if (list->tail->count == list->chunkSize){ //last chunk + uintptr_t m = malloc(sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); + if (!m) return; + cchunked_node_t* node = (cchunked_node_t*)m; node->count = 0; - node->next = NULL; + node->next = NULL; list->tail->next = node; list->tail = node; } - list->tail->data[list->tail->count++]=data; + list->tail->data[list->tail->count++] = data; list->length++; } cchunked_node_t* cchunked_list_insert_after(cchunked_list_t* list,cchunked_node_t* node, void* data){ - if(!list) - return NULL; - if(!node){ + if (!list) return NULL; + + if (!node){ cchunked_list_push_back(list, data); return list->tail; } + if(node->count < list->chunkSize){//chunk isnt completrly full - node->data[node->count++]=data; + node->data[node->count++] = data; list->length++; return node; } + //new node - uintptr_t m = malloc(sizeof(cchunked_node_t) + list->chunkSize*sizeof(void*)); - if(!m) return NULL; + uintptr_t m = malloc(sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); + if (!m) return NULL; cchunked_node_t* new_node = (cchunked_node_t*)m; - new_node->count =1; + new_node->count = 1; new_node->next = node->next; new_node->data[0] = data; //linking new node node->next = new_node; - if (list->tail == node) - list->tail = new_node; + if (list->tail == node) list->tail = new_node; + list->length++; return new_node; } void* cchunked_list_pop_front(cchunked_list_t* list){ - if(!list || list->length == 0 || list->head == NULL) - return NULL; + if (!list || list->length == 0 || list->head == NULL) return NULL; + void* data = list->head->data[0]; - if(list->head->count > 1){ - for(uint64_t i = 1; i < list->head->count; ++i) - list->head->data[i-1] = list->head->data[i]; + if (list->head->count > 1){ + for (uint64_t i = 1; i < list->head->count; ++i) list->head->data[i-1] = list->head->data[i]; list->head->count--; - }else{ + } else { //uniq element cchunked_node_t* old =list->head; list->head = old->next; - if(list->head == NULL){ - list->tail = NULL; - } + if(list->head == NULL) list->tail = NULL; free(old, sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); } + list->length--; return data; } @@ -115,22 +124,21 @@ void* cchunked_list_pop_front(cchunked_list_t* list){ void* cchunked_list_remove_node(cchunked_list_t* list, cchunked_node_t* node){ - if(!list||!node|| !list->head) return NULL; + if (!list || !node || !list->head) return NULL; //delegate to pop_front - if(node == list->head) - return cchunked_list_pop_front(list); + if (node == list->head) return cchunked_list_pop_front(list); cchunked_node_t* prev = list->head; - while(prev->next && prev->next != node) prev= prev->next; - if(prev->next != node) return NULL; + while (prev->next && prev->next != node) prev= prev->next; + if (prev->next != node) return NULL; void* data = node->data[0]; - for(uint64_t i = 1; i < node->count; ++i) - node->data[i-1] = node->data[i]; + for (uint64_t i = 1; i < node->count; ++i) node->data[i-1] = node->data[i]; + node->count--; list->length--; - if(node->count == 0){ + if (node->count == 0){ prev->next = node->next; if(list->tail == node) list->tail = prev; free(node, sizeof(cchunked_node_t)+list->chunkSize * sizeof(void*)); @@ -141,7 +149,7 @@ void* cchunked_list_remove_node(cchunked_list_t* list, cchunked_node_t* node){ void cchunked_list_update(cchunked_list_t* list, cchunked_node_t* node, void* new_data){ (void)list; - if(node&&node->count) node->data[0] = new_data; + if (node&&node->count) node->data[0] = new_data; } uint64_t cchunked_list_length(const cchunked_list_t* list){ @@ -149,25 +157,26 @@ uint64_t cchunked_list_length(const cchunked_list_t* list){ } uint64_t cchunked_list_size_bytes(const cchunked_list_t* list){ - if(!list) return 0; + if (!list) return 0; uint64_t nodes = 0; - for(cchunked_node_t* it = list->head; it; it = it->next) nodes++; + for (cchunked_node_t* it = list->head; it; it = it->next) nodes++; + return nodes * (sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); } void cchunked_list_for_each(const cchunked_list_t* list, void (*func)(void*)){ - if(!list||!func) return; - for(cchunked_node_t* it = list->head; it; it = it->next) - for(uint64_t i = 0; i < it->count; ++i) + if (!list || !func) return; + for (cchunked_node_t* it = list->head; it; it = it->next) + for (uint64_t i = 0; i < it->count; ++i) func(it->data[i]); } void cchunked_list_update_at(cchunked_list_t* list, cchunked_node_t* node,uint64_t offset,void* new_data){ - if(!list || !node || offset >= node->count)//{ - return; + if(!list || !node || offset >= node->count)return; + node->data[offset] = new_data; } int cchunked_list_is_empty(const cchunked_list_t* list){ - return (!list ||list->length == 0) ? 1 : 0; + return (!list || list->length == 0) ? 1 : 0; } } From 6d5119ba83285a5c9989c3af22511ffe5b391bcc Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:05:11 +0200 Subject: [PATCH 16/33] Update chunked_list.hpp format --- shared/data_struct/chunked_list.hpp | 57 +++++++++++++++-------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/shared/data_struct/chunked_list.hpp b/shared/data_struct/chunked_list.hpp index d20c3142..41069f58 100644 --- a/shared/data_struct/chunked_list.hpp +++ b/shared/data_struct/chunked_list.hpp @@ -49,7 +49,7 @@ class ChunkedList{ explicit ChunkedList(uint64_t cs): head(nullptr), tail(nullptr), length_(0), chunkSize(cs){} ~ChunkedList(){ - while(head){ + while (head){ Node* nx = head->next; ::free(head, sizeof(Node) + chunkSize * sizeof(T)); head = nx; @@ -57,52 +57,51 @@ class ChunkedList{ } ChunkedList(const ChunkedList& other): head(nullptr), tail(nullptr), length_(0), chunkSize(other.chunkSize){ - for(Node* c = other.head; c; c = c->next) - for(uint64_t i = 0; i < c->count; ++i) + for (Node* c = other.head; c; c = c->next) + for (uint64_t i = 0; i < c->count; ++i) push_back(c->data[i]); } ChunkedList& operator=(const ChunkedList& other){ - if(this != &other){ + if (this != &other){ clear(); chunkSize = other.chunkSize; - for(Node* c = other.head; c; c = c->next) - for(uint64_t i = 0; i < c->count; ++i) + for (Node* c = other.head; c; c = c->next) + for (uint64_t i = 0; i < c->count; ++i) push_back(c->data[i]); } return *this; } void push_back(const T& value){ - if(!tail) allocFirst(); - if(tail->count == chunkSize) allocChunk(); + if (!tail) allocFirst(); + if (tail->count == chunkSize) allocChunk(); tail->data[tail->count++] = value; ++length_; } T pop_front(){ - if(!head) return T(); + if (!head) return T(); T val = head->data[0]; - if(head->count > 1){ - for(uint64_t i = 1; i < head->count; ++i) - head->data[i-1] = head->data[i]; + if (head->count > 1){ + for (uint64_t i = 1; i < head->count; ++i) head->data[i-1] = head->data[i]; --head->count; - }else{ + } else { Node* old = head; head = head->next; ::free(old, sizeof(Node) + chunkSize * sizeof(T)); - if(!head) tail = nullptr; + if (!head) tail = nullptr; } --length_; return val; } Node* insert_after(Node* node, const T& value){ - if(!node){ + if (!node){ push_back(value); return tail; } - if(node->count < chunkSize){ + if (node->count < chunkSize){ node->data[node->count++] = value; ++length_; return node; @@ -110,14 +109,14 @@ class ChunkedList{ Node* n=allocNode(value); n->next = node->next; node->next = n; - if(tail ==node) tail = n; + if (tail ==node) tail = n; ++length_; return n; } T remove_node(Node* node){ - if(!node|| !head) return T(); - if(node == head){ + if (!node|| !head) return T(); + if (node == head){ T val = head->data[0]; uint64_t removed = head->count; Node* nxt = head->next; @@ -128,12 +127,14 @@ T remove_node(Node* node){ return val; } Node* prev = head; - while(prev->next && prev->next != node) prev = prev->next; - if(prev->next != node) return T(); + while (prev->next && prev->next != node) prev = prev->next; + if (prev->next != node) return T(); + T val = node->data[0]; uint64_t removed = node->count; - prev->next= node->next; - if(tail==node) tail = prev; + prev->next = node->next; + if (tail==node) tail = prev; + ::free(node, sizeof(Node) + chunkSize * sizeof(T)); length_ -= removed; return val; @@ -159,8 +160,8 @@ T remove_node(Node* node){ template void for_each(Func f) const{ - for(Node* it = head; it; it = it->next) - for(uint64_t i = 0; i < it->count; ++i) + for (Node* it = head; it; it = it->next) + for (uint64_t i = 0; i < it->count; ++i) f(it->data[i]); } @@ -181,7 +182,7 @@ T remove_node(Node* node){ void allocFirst(){ uintptr_t raw = ::malloc(sizeof(Node) +chunkSize*sizeof(T)); - head = tail =reinterpret_cast(raw); + head = tail = reinterpret_cast(raw); head->count = 0; head->next = nullptr; } @@ -196,7 +197,7 @@ T remove_node(Node* node){ } void clear(){ - while(head) pop_front(); + while (head) pop_front(); } }; -#endif \ No newline at end of file +#endif From 18617b976c7650fb0b8f000f9634bf9801cb0170 Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:08:38 +0200 Subject: [PATCH 17/33] Update doubly_linked_list.cpp format --- shared/data_struct/doubly_linked_list.cpp | 116 ++++++++++++---------- 1 file changed, 65 insertions(+), 51 deletions(-) diff --git a/shared/data_struct/doubly_linked_list.cpp b/shared/data_struct/doubly_linked_list.cpp index 8623c6bd..2b01d051 100644 --- a/shared/data_struct/doubly_linked_list.cpp +++ b/shared/data_struct/doubly_linked_list.cpp @@ -1,18 +1,20 @@ #include "doubly_linked_list.hpp" cdouble_linked_list_t* cdouble_linked_list_create(void){ - uintptr_t raw= malloc((uint64_t)sizeof(cdouble_linked_list_t));//i believe that casting sizeof is better until size_t is defined correctly - if(raw == 0) return NULL; - cdouble_linked_list_t *list=(cdouble_linked_list_t*)raw; - list->head = list->tail =NULL; + uintptr_t raw = malloc((uint64_t)sizeof(cdouble_linked_list_t));//i believe that casting sizeof is better until size_t is defined correctly + if (raw == 0) return NULL; + + cdouble_linked_list_t *list = (cdouble_linked_list_t*)raw; + list->head = list->tail = NULL; list->length = 0; return list; } void cdouble_linked_list_destroy(cdouble_linked_list_t *list){ - if(!list) return; + if (!list) return; + cdouble_linked_list_node_t *node=list->head; - for(uint64_t i = 0; i < list->length; ++i){ + for (uint64_t i = 0; i < list->length; ++i){ cdouble_linked_list_node_t *next = node->next; free(node,(uint64_t)sizeof(cdouble_linked_list_node_t)); node = next; @@ -21,19 +23,22 @@ void cdouble_linked_list_destroy(cdouble_linked_list_t *list){ } cdouble_linked_list_t* cdouble_linked_list_clone(const cdouble_linked_list_t *list){ //could create data aliasing. be careful. TODO - if(!list) return NULL; + if (!list) return NULL; + cdouble_linked_list_t *clone=cdouble_linked_list_create(); - if(!clone) return NULL; + if (!clone) return NULL; + cdouble_linked_list_node_t *it =list->head; - for(uint64_t i = 0; ilength; ++i){ + for (uint64_t i = 0; ilength; ++i){ uintptr_t raw =malloc((uint64_t)sizeof(cdouble_linked_list_node_t)); - if(raw) { + if (raw) { cdouble_linked_list_node_t *node =(cdouble_linked_list_node_t*)raw; node->data = it->data; - if(clone->length == 0){ + + if (clone->length == 0){ node->next=node->prev = node; clone->head=clone->tail = node; - }else{ + } else { node->prev=clone->tail; node->next=clone->head; clone->tail->next=node; @@ -48,15 +53,16 @@ cdouble_linked_list_t* cdouble_linked_list_clone(const cdouble_linked_list_t *li } void cdouble_linked_list_push_front(cdouble_linked_list_t *list, void *data){ - if(!list) return; + if (!list) return; uintptr_t raw= malloc((uint64_t)sizeof(cdouble_linked_list_node_t)); - if(raw == 0) return; + if (raw == 0) return; cdouble_linked_list_node_t *node=(cdouble_linked_list_node_t*)raw; node->data = data; - if(list->length == 0){ + + if (list->length == 0){ node->next=node->prev = node; list->head=list->tail = node; - }else{ + } else { node->next=list->head; node->prev=list->tail; list->head->prev= node; @@ -67,15 +73,16 @@ void cdouble_linked_list_push_front(cdouble_linked_list_t *list, void *data){ } void cdouble_linked_list_push_back(cdouble_linked_list_t* list, void* data){ - if(!list) return; + if (!list) return; uintptr_t raw=malloc((uint64_t)sizeof(cdouble_linked_list_node_t)); - if(raw ==0) return; + if (raw ==0) return; cdouble_linked_list_node_t* node=(cdouble_linked_list_node_t*)raw; - node->data=data; - if(list->length==0){ + node->data = data; + + if (list->length==0){ node->next=node->prev=node; list->head=list->tail = node; - }else{ + } else { node->prev= list->tail; node->next=list->head; list->tail->next=node; @@ -86,12 +93,13 @@ void cdouble_linked_list_push_back(cdouble_linked_list_t* list, void* data){ } void* cdouble_linked_list_pop_front(cdouble_linked_list_t* list){ - if(!list||list->length==0) return NULL; + if (!list||list->length==0) return NULL; + cdouble_linked_list_node_t* node=list->head; void* data=node->data; - if(list->length == 1){ + if (list->length == 1){ list->head=list->tail=NULL; - }else{ + } else { list->head=node->next; list->head->prev =list->tail; list->tail->next=list->head; @@ -102,12 +110,13 @@ void* cdouble_linked_list_pop_front(cdouble_linked_list_t* list){ } void* cdouble_linked_list_pop_back(cdouble_linked_list_t* list){ - if(!list||list->length==0) return NULL; + if (!list||list->length==0) return NULL; + cdouble_linked_list_node_t* node=list->tail; void* data=node->data; - if(list->length==1){ + if (list->length==1){ list->head = list->tail=NULL; - }else{ + } else { list->tail=node->prev; list->tail->next=list->head; list->head->prev=list->tail; @@ -118,27 +127,30 @@ void* cdouble_linked_list_pop_back(cdouble_linked_list_t* list){ } cdouble_linked_list_node_t* cdouble_linked_list_insert_after(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* data){ - if(!list) return NULL; - if(!node){ + if (!list) return NULL; + if (!node){ cdouble_linked_list_push_front(list, data); return list->head; } + uintptr_t raw=malloc((uint64_t)sizeof(cdouble_linked_list_node_t)); - if(raw==0) return NULL; - cdouble_linked_list_node_t* new_node=(cdouble_linked_list_node_t*)raw; - new_node->data=data; - new_node->next= node->next; - new_node->prev=node; - node->next->prev= new_node; - node->next= new_node; - if(list->tail == node) list->tail=new_node; + if (raw==0) return NULL; + + cdouble_linked_list_node_t* new_node = (cdouble_linked_list_node_t*)raw; + new_node->data = data; + new_node->next = node->next; + new_node->prev = node; + node->next->prev = new_node; + node->next = new_node; + if (list->tail == node) list->tail = new_node; ++list->length; return new_node; } cdouble_linked_list_node_t* cdouble_linked_list_insert_before(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* data){ - if(!list) return NULL; - if(!node){ + if (!list) return NULL; + + if (!node){ cdouble_linked_list_push_back(list, data); return list->tail; } @@ -146,12 +158,13 @@ cdouble_linked_list_node_t* cdouble_linked_list_insert_before(cdouble_linked_lis } void* cdouble_linked_list_remove(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node){ - if(!list|| !node|| list->length==0) return NULL; - if(node==list->head) return cdouble_linked_list_pop_front(list); - if(node==list->tail) return cdouble_linked_list_pop_back(list); - node->prev->next=node->next; - node->next->prev=node->prev; - void* data=node->data; + if (!list|| !node|| list->length==0) return NULL; + + if (node==list->head) return cdouble_linked_list_pop_front(list); + if (node==list->tail) return cdouble_linked_list_pop_back(list); + node->prev->next = node->next; + node->next->prev = node->prev; + void* data = node->data; --list->length; free(node,(uint64_t)sizeof(cdouble_linked_list_node_t)); return data; @@ -159,7 +172,7 @@ void* cdouble_linked_list_remove(cdouble_linked_list_t* list, cdouble_linked_lis void cdouble_linked_list_update(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* new_data){ (void)list; - if(node) node->data=new_data; + if (node) node->data=new_data; } uint64_t cdouble_linked_list_length(const cdouble_linked_list_t* list){ @@ -167,15 +180,16 @@ uint64_t cdouble_linked_list_length(const cdouble_linked_list_t* list){ } uint64_t cdouble_linked_list_size_bytes(const cdouble_linked_list_t* list){ - return list?list-> length*sizeof(cdouble_linked_list_node_t):0; + return list?list-> length * sizeof(cdouble_linked_list_node_t):0; } cdouble_linked_list_node_t* cdouble_linked_list_find(cdouble_linked_list_t* list, void* key, int (*cmp)(void*,void*)){ - if(!list||!cmp|| list->length==0) return NULL; + if (!list||!cmp|| list->length==0) return NULL; + cdouble_linked_list_node_t* it=list->head; - for(uint64_t i=0; ilength; ++i){ - if(cmp(it->data,key)==0) return it; - it=it->next; + for (uint64_t i=0; ilength; ++i){ + if (cmp(it->data,key)==0) return it; + it = it->next; } return NULL; } From b4c7d891deeb1bf5a61437ccd424d03a4d272e4e Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:12:05 +0200 Subject: [PATCH 18/33] Update doubly_linked_list.hpp format --- shared/data_struct/doubly_linked_list.hpp | 136 ++++++++++++---------- 1 file changed, 75 insertions(+), 61 deletions(-) diff --git a/shared/data_struct/doubly_linked_list.hpp b/shared/data_struct/doubly_linked_list.hpp index 1c6d045e..0bc71f34 100644 --- a/shared/data_struct/doubly_linked_list.hpp +++ b/shared/data_struct/doubly_linked_list.hpp @@ -17,9 +17,11 @@ typedef struct cdouble_linked_list{ uint64_t length; }cdouble_linked_list_t; + extern uintptr_t malloc(uint64_t size); extern void free(void *ptr, uint64_t size); + cdouble_linked_list_t *cdouble_linked_list_create(void); void cdouble_linked_list_destroy(cdouble_linked_list_t *list); cdouble_linked_list_t *cdouble_linked_list_clone(const cdouble_linked_list_t *list); @@ -34,6 +36,7 @@ void cdouble_linked_list_update(cdouble_linked_list_t *list, cdouble_linked_list uint64_t cdouble_linked_list_length(const cdouble_linked_list_t *list); uint64_t cdouble_linked_list_size_bytes(const cdouble_linked_list_t *list); cdouble_linked_list_node_t* cdouble_linked_list_find(cdouble_linked_list_t *list, void *key, int (*cmp)(void *, void *)); + #ifdef __cplusplus } @@ -52,48 +55,50 @@ class LinkedList{ Node* alloc_node(const T& value){ uintptr_t raw= malloc((uint64_t)sizeof(Node)); - if(raw == 0) return nullptr; - Node* n =(Node*)raw; - n->data=value; - n->next=n->prev=nullptr; + if (raw == 0) return nullptr; + + Node* n = (Node*)raw; + n->data = value; + n->next = n->prev = nullptr; return n; } + void free_node(Node* n){ - if(!n) return; + if (!n) return; free(n,(uint64_t)sizeof(Node)); } static void swap(LinkedList& a, LinkedList& b) noexcept{ Node* tmp_head = a.head; - a.head= b.head; - b.head= tmp_head; - Node* tmp_tail =a.tail; - a.tail= b.tail; - b.tail= tmp_tail; + a.head = b.head; + b.head = tmp_head; + Node* tmp_tail = a.tail; + a.tail = b.tail; + b.tail = tmp_tail; uint64_t tmp_length = a.length; a.length = b.length; b.length = tmp_length; } public: - LinkedList():head(nullptr), tail(nullptr), length(0){} + LinkedList() : head(nullptr), tail(nullptr), length(0){} LinkedList(const LinkedList& other):head(nullptr), tail(nullptr), length(0){ - if(other.head){ + if (other.head){ Node* it = other.head; - do{ + do { push_back(it->data); it = it->next; - }while(it != other.head); + } while (it != other.head); } } ~LinkedList(){ - while(!empty()) pop_front(); + while (!empty()) pop_front(); } LinkedList& operator=(const LinkedList& other){ - if(this != &other){ + if (this != &other) { LinkedList tmp(other); swap(*this, tmp); } @@ -102,43 +107,46 @@ class LinkedList{ void push_front(const T& value){ Node* n = alloc_node(value); - if(!n) return; - if(!head){ + if( !n) return; + + if (!head){ head = tail = n; n->next = n->prev = n; - }else{ - n->next= head; - n->prev= tail; - head->prev=n; - tail->next= n; - head= n; + } else { + n->next = head; + n->prev = tail; + head->prev = n; + tail->next = n; + head = n; } ++length; } void push_back(const T& value){ - Node* n =alloc_node(value); - if(!n) return; - if(!tail){ + Node* n = alloc_node(value); + if (!n) return; + + if (!tail){ head = tail = n; n->next = n->prev = n; - }else{ - n->prev = tail; - n->next = head; - tail->next = n; - head->prev = n; - tail = n; + } else { + n->prev = tail; + n->next = head; + tail->next = n; + head->prev = n; + tail = n; } ++length; } T pop_front(){ - if(!head) return T(); + if !head) return T(); + Node* n = head; T val = n->data; - if(head == tail){ - head =tail=nullptr; - }else{ + if (head == tail){ + head = tail=nullptr; + } else { head = head->next; head->prev = tail; tail->next = head; @@ -149,12 +157,13 @@ class LinkedList{ } T pop_back(){ - if(!tail) return T(); + if (!tail) return T(); + Node* n = tail; T val = n->data; - if(head == tail){ + if (head == tail){ head = tail = nullptr; - }else{ + } else { tail = tail->prev; tail->next = head; head->prev = tail; @@ -165,23 +174,25 @@ class LinkedList{ } Node* insert_after(Node* node, const T& value){ - if(!node){ + if (!node){ push_front(value); return head; } - Node* n= alloc_node(value); - if(!n) return nullptr; + + Node* n = alloc_node(value); + if (!n) return nullptr; + n->next=node->next; n->prev=node; node->next->prev = n; node->next= n; - if(tail==node) tail = n; + if (tail == node) tail = n; ++length; return n; } Node* insert_before(Node* node, const T& value){ - if(!node){ + if (!node){ push_back(value); return tail; } @@ -189,19 +200,20 @@ class LinkedList{ } T remove(Node* node){ - if(!node) return T(); - if(node == head) return pop_front(); - if(node == tail) return pop_back(); - node->prev->next=node->next; - node->next->prev=node->prev; - T val=node->data; + if (!node) return T(); + if (node == head) return pop_front(); + if (node == tail) return pop_back(); + + node->prev->next = node->next; + node->next->prev = node->prev; + T val = node->data; --length; free_node(node); return val; } void update(Node* node, const T& value){ - if(!node) return; + if (!node) return; node->data = value; } @@ -212,23 +224,25 @@ class LinkedList{ template Node* find(Predicate pred) const{ - if(!head) return nullptr; - Node* it=head; - do{ - if(pred(it->data)) return it; - it=it->next; - }while(it != head); + if (!head) return nullptr; + + Node* it = head; + do { + if (pred(it->data)) return it; + it = it->next; + } while (it != head); + return nullptr; } template void for_each(Func func) const{ - if(!head) return; + if (!head) return; Node* it = head; - do{ + do { func(it->data); it = it->next; - }while(it != head); + } while (it != head); } }; #endif From 3e0badc1274c248c37715b6d80f19afee96b295b Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:14:38 +0200 Subject: [PATCH 19/33] Update linked_list.cpp format --- shared/data_struct/linked_list.cpp | 65 ++++++++++++++++++------------ 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/shared/data_struct/linked_list.cpp b/shared/data_struct/linked_list.cpp index 83c6b307..8012bdc2 100644 --- a/shared/data_struct/linked_list.cpp +++ b/shared/data_struct/linked_list.cpp @@ -7,7 +7,8 @@ extern "C" { clinkedlist_t *clinkedlist_create(void){ uintptr_t mem = malloc(sizeof(clinkedlist_t)); - if((void *)mem == NULL) return NULL; + if ((void *)mem == NULL) return NULL; + clinkedlist_t *list = (clinkedlist_t *)mem; list->head = NULL; list->tail = NULL; @@ -16,9 +17,10 @@ clinkedlist_t *clinkedlist_create(void){ } void clinkedlist_destroy(clinkedlist_t *list){ - if(list == NULL) return; + if (list == NULL) return; + clinkedlist_node_t *node = list->head; - while(node){ + while (node){ clinkedlist_node_t *next = node->next; free(node, sizeof(clinkedlist_node_t)); node = next; @@ -27,19 +29,21 @@ void clinkedlist_destroy(clinkedlist_t *list){ } clinkedlist_t *clinkedlist_clone(const clinkedlist_t *list){ - if(list == NULL) return NULL; + if (list == NULL) return NULL; + clinkedlist_t *clone = clinkedlist_create(); - if(clone == NULL) return NULL; + if (clone == NULL) return NULL; + clinkedlist_node_t *it = list->head; - while(it){ - if(clone->tail){ + while (it){ + if (clone->tail){ clinkedlist_node_t *new_node = (clinkedlist_node_t *)malloc(sizeof(clinkedlist_node_t)); new_node->data = it->data; new_node->next = NULL; clone->tail->next = new_node; clone->tail = new_node; clone->length++; - }else{ + } else { clinkedlist_push_front(clone, it->data); } it = it->next; @@ -48,29 +52,33 @@ clinkedlist_t *clinkedlist_clone(const clinkedlist_t *list){ } void clinkedlist_push_front(clinkedlist_t *list, void *data){ - if(list == NULL) return; + if (list == NULL) return; + clinkedlist_node_t *node = (clinkedlist_node_t *)malloc(sizeof(clinkedlist_node_t)); node->data = data; node->next = list->head; list->head = node; - if(list->tail == NULL) list->tail = node; + if (list->tail == NULL) list->tail = node; list->length++; } void *clinkedlist_pop_front(clinkedlist_t *list){ - if(list == NULL || list->head== NULL) return NULL; + if (list == NULL || list->head== NULL) return NULL; + clinkedlist_node_t *node = list->head; void *data = node->data; list->head = node->next; if (list->head == NULL) list->tail = NULL; + list->length--; free(node, sizeof(clinkedlist_node_t)); return data; } clinkedlist_node_t *clinkedlist_insert_after(clinkedlist_t *list, clinkedlist_node_t *node, void *data) { - if(list == NULL) return NULL; - if(node == NULL){ + if (list == NULL) return NULL; + + if (node == NULL){ clinkedlist_push_front(list, data); return list->head; } @@ -78,23 +86,26 @@ clinkedlist_node_t *clinkedlist_insert_after(clinkedlist_t *list, clinkedlist_no new_node->data = data; new_node->next = node->next; node->next = new_node; - if(list->tail == node) list->tail = new_node; + if (list->tail == node) list->tail = new_node; + list->length++; return new_node; } void *clinkedlist_remove(clinkedlist_t *list, clinkedlist_node_t *node){ - if(list == NULL || node == NULL || list->head == NULL) return NULL; - if(node == list->head){ + if (list == NULL || node == NULL || list->head == NULL) return NULL; + + if (node == list->head){ return clinkedlist_pop_front(list); } clinkedlist_node_t *prev = list->head; - while(prev->next && prev->next != node){ + while (prev->next && prev->next != node){ prev = prev->next; } - if(prev->next != node) return NULL; + if (prev->next != node) return NULL; + prev->next = node->next; - if(node == list->tail) list->tail = prev; + if (node == list->tail) list->tail = prev; void *data = node->data; list->length--; free(node, sizeof(clinkedlist_node_t)); @@ -103,7 +114,7 @@ void *clinkedlist_remove(clinkedlist_t *list, clinkedlist_node_t *node){ void clinkedlist_update(clinkedlist_t *list, clinkedlist_node_t *node, void *new_data){ (void)list; - if(node) node->data = new_data; + if (node) node->data = new_data; } uint64_t clinkedlist_length(const clinkedlist_t *list){ @@ -115,19 +126,21 @@ uint64_t clinkedlist_size_bytes(const clinkedlist_t *list){ } clinkedlist_node_t *clinkedlist_find(clinkedlist_t *list, void *key, int (*cmp)(void *, void *)){ - if(list == NULL || cmp == NULL) return NULL; + if (list == NULL || cmp == NULL) return NULL; + clinkedlist_node_t *it = list->head; - while(it){ - if(cmp(it->data, key) == 0) return it; + while (it){ + if (cmp(it->data, key) == 0) return it; it = it->next; } return NULL; } void clinkedlist_for_each(const clinkedlist_t *list, void (*func)(void *)){ - if(list == NULL || func == NULL) return; + if (list == NULL || func == NULL) return; + clinkedlist_node_t *it = list->head; - while(it){ + while (it){ func(it->data); it = it->next; } @@ -135,4 +148,4 @@ void clinkedlist_for_each(const clinkedlist_t *list, void (*func)(void *)){ #ifdef __cplusplus } -#endif \ No newline at end of file +#endif From 31aa64439f814bb0bc6714e38e4967509c9b286b Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:16:30 +0200 Subject: [PATCH 20/33] Update linked_list.hpp format --- shared/data_struct/linked_list.hpp | 44 ++++++++++++++++++------------ 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/shared/data_struct/linked_list.hpp b/shared/data_struct/linked_list.hpp index c865d37a..2167ddfd 100644 --- a/shared/data_struct/linked_list.hpp +++ b/shared/data_struct/linked_list.hpp @@ -4,6 +4,7 @@ #ifdef __cplusplus extern "C" { #endif + typedef struct clinkedlist_node{ void *data; struct clinkedlist_node *next; @@ -15,9 +16,11 @@ typedef struct clinkedlist{ uint64_t length; }clinkedlist_t; + extern uintptr_t malloc(uint64_t size); extern void free(void *ptr, uint64_t size); + clinkedlist_t *clinkedlist_create(void); void clinkedlist_destroy(clinkedlist_t *list); clinkedlist_t *clinkedlist_clone(const clinkedlist_t *list); @@ -40,6 +43,7 @@ template class LinkedList{ T data; Node *next; }; + Node *head = nullptr; Node *tail = nullptr; uint64_t length = 0; @@ -51,6 +55,7 @@ template class LinkedList{ n->next = nullptr; return n; } + void free_node(Node *n){ free(n, sizeof(Node)); } @@ -59,28 +64,29 @@ template class LinkedList{ LinkedList() = default; LinkedList(const LinkedList &other){ - for(Node *it = other.head; it; it = it->next){ + for (Node *it = other.head; it; it = it->next){ push_front(it->data); } LinkedList tmp; - while(!empty()){ + while (!empty()){ tmp.push_front(pop_front()); } *this = tmp; } ~LinkedList(){ - while(!empty()) pop_front(); + while (!empty()) pop_front(); } LinkedList &operator=(const LinkedList &other){ - if(this != &other){ - while(!empty()) pop_front(); - for(Node *it = other.head; it; it = it->next){ + if (this != &other){ + while (!empty()) pop_front(); + for (Node *it = other.head; it; it = it->next){ push_front(it->data); } + LinkedList tmp; - while(!empty()){ + while (!empty()){ tmp.push_front(pop_front()); } head = tmp.head; @@ -104,6 +110,7 @@ template class LinkedList{ Node *n = head; head = head->next; if (head == nullptr) tail = nullptr; + T val = n->data; free_node(n); --length; @@ -111,26 +118,28 @@ template class LinkedList{ } Node *insert_after(Node *node, const T &value){ - if(node == nullptr){ + if (node == nullptr){ push_front(value); return head; } + Node *n = alloc_node(value); n->next = node->next; node->next = n; - if(tail == node) tail = n; + if (tail == node) tail = n; ++length; return n; } T remove(Node *node){ - if(node == nullptr) return T(); - if(node == head) return pop_front(); + if (node == nullptr) return T(); + if (node == head) return pop_front(); + Node *prev = head; - while(prev && prev->next != node) prev = prev->next; - if(prev == nullptr) return T(); + while (prev && prev->next != node) prev = prev->next; + if (prev == nullptr) return T(); prev->next = node->next; - if(node == tail) tail = prev; + if (node == tail) tail = prev; T val = node->data; free_node(node); --length; @@ -138,7 +147,7 @@ template class LinkedList{ } void update(Node *node, const T &value){ - if(node) node->data = value; + if (node) node->data = value; } uint64_t size() const{return length;} @@ -146,9 +155,10 @@ template class LinkedList{ Node *begin() const{ return head; } Node *end() const{ return nullptr;} template + Node *find(Predicate pred) const{ - for(Node *it = head; it; it = it->next){ - if(pred(it->data)) return it; + for (Node *it = head; it; it = it->next){ + if (pred(it->data)) return it; } return nullptr; } From 871110205a8ad297ebb01268742478deeec545d8 Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:18:29 +0200 Subject: [PATCH 21/33] Update queue.cpp format --- shared/data_struct/queue.cpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/shared/data_struct/queue.cpp b/shared/data_struct/queue.cpp index d5a9fa58..16374b06 100644 --- a/shared/data_struct/queue.cpp +++ b/shared/data_struct/queue.cpp @@ -1,3 +1,4 @@ + #include "queue.hpp" #include "std/memfunctions.h" @@ -7,44 +8,45 @@ void cqueue_init(CQueue* q,uint64_t max_capacity,uint64_t elem_size){ q->max_capacity = max_capacity; q->elem_size = elem_size; q->head = q->tail = q->length = 0; - if(max_capacity>0){ + if (max_capacity > 0){ uintptr_t b = malloc(max_capacity*elem_size); - if(b) q->buffer = (void*)b; + if (b) q->buffer = (void*)b; } } int32_t cqueue_enqueue(CQueue* q,const void* item){ - if(!q) return 0; - if(q->max_capacity>0){ - if(q->length==q->capacity) return 0; + if (!q) return 0; + if (q->max_capacity>0){ + if (q->length == q->capacity) return 0; } else { - if(q->length==q->capacity){ + if (q->length == q->capacity){ uint64_t nc = q->capacity>0 ? q->capacity*2 : 4; uintptr_t nb = malloc(nc*q->elem_size); - if(!nb) return 0; + if (!nb) return 0; + void* newb = (void*)nb; - for(uint64_t i=0;ilength;++i){ + for (uint64_t i=0; ilength; ++i){ uint64_t idx = (q->tail + i)%q->capacity; memcpy((uint8_t*)newb + i*q->elem_size, (uint8_t*)q->buffer + idx*q->elem_size, q->elem_size); } - if(q->buffer) free(q->buffer,q->capacity*q->elem_size); + if (q->buffer) free(q->buffer,q->capacity*q->elem_size); q->buffer=newb; q->capacity=nc; q->head=q->length; q->tail=0; } } - memcpy((uint8_t*)q->buffer + q->head*q->elem_size, - item,q->elem_size); + memcpy((uint8_t*)q->buffer + q->head*q->elem_size, item,q->elem_size); q->head = (q->head+1)%q->capacity; q->length++; return 1; } int32_t cqueue_dequeue(CQueue* q,void* out){ - if(!q||q->length==0) return 0; + if (!q || q->length == 0) return 0; + memcpy(out,(uint8_t*)q->buffer + q->tail*q->elem_size,q->elem_size); q->tail = (q->tail+1)%q->capacity; q->length--; @@ -60,11 +62,11 @@ uint64_t cqueue_size(const CQueue* q){ } void cqueue_clear(CQueue* q){ - if(!q) return; + if (!q) return; q->head=q->tail=q->length=0; } void cqueue_destroy(CQueue* q){ - if(!q) return; - if(q->buffer) free(q->buffer,q->capacity*q->elem_size); + if (!q) return; + if (q->buffer) free(q->buffer,q->capacity*q->elem_size); } From 6aaca80fcc0f370e8828eacc6a3a5ca0463c36ca Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:19:15 +0200 Subject: [PATCH 22/33] Update queue.hpp format --- shared/data_struct/queue.hpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/shared/data_struct/queue.hpp b/shared/data_struct/queue.hpp index d31dcda8..7f0bbe8f 100644 --- a/shared/data_struct/queue.hpp +++ b/shared/data_struct/queue.hpp @@ -5,7 +5,7 @@ extern "C" { #endif -typedef struct CQueue{ +typedef struct CQueue { void* buffer; uint64_t capacity; //curremt queue size uint64_t max_capacity; //0 is infinite @@ -15,9 +15,11 @@ typedef struct CQueue{ uint64_t length; }CQueue; + extern uintptr_t malloc(uint64_t); extern void free(void*,uint64_t); + void cqueue_init(CQueue* q,uint64_t max_capacity,uint64_t elem_size); int32_t cqueue_enqueue(CQueue* q,const void* item); int32_t cqueue_dequeue(CQueue* q,void* out); @@ -30,8 +32,9 @@ void cqueue_destroy(CQueue* q); } template -class Queue{ +class Queue { CQueue q_; + public: explicit Queue(uint64_t cap=0){cqueue_init(&q_,cap,sizeof(T));} ~Queue(){ cqueue_destroy(&q_); } From 065b9d1af3669ed2bcea44cdaed8ff8d525ca257 Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:19:53 +0200 Subject: [PATCH 23/33] Update ring_buffer.cpp format --- shared/data_struct/ring_buffer.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/shared/data_struct/ring_buffer.cpp b/shared/data_struct/ring_buffer.cpp index 281117f6..a1e7e946 100644 --- a/shared/data_struct/ring_buffer.cpp +++ b/shared/data_struct/ring_buffer.cpp @@ -10,16 +10,18 @@ void cring_init(struct CRingBuffer* rb, void* storage, uint64_t capacity, uint64 rb->tail = 0; rb->full = 0; } + uint64_t cring_capacity(const struct CRingBuffer* rb){ return rb->capacity; } + int32_t cring_push(struct CRingBuffer* rb, const void* item){ if (rb->full) return 0; uint8_t* base = (uint8_t*)rb->buffer; void* dest = base + (rb->head * rb->element_size); - for(uint32_t i = 0; i < rb->element_size; ++i){ + for (uint32_t i = 0; i < rb->element_size; ++i){ ((uint8_t*)dest)[i] =((const uint8_t*)item)[i]; } @@ -29,17 +31,17 @@ int32_t cring_push(struct CRingBuffer* rb, const void* item){ } int32_t cring_pop(struct CRingBuffer* rb, void* out){ - if(!rb->full && (rb->head == rb->tail)) return 0; + if (!rb->full && (rb->head == rb->tail)) return 0; uint8_t* base = (uint8_t*)rb->buffer; void* src = base + (rb->tail * rb->element_size); - for(uint64_t i = 0; i < rb->element_size; ++i){ + for (uint64_t i = 0; i < rb->element_size; ++i){ ((uint8_t*)out)[i] = ((uint8_t*)src)[i]; } - rb->tail =(rb->tail + 1) % rb->capacity; - rb->full =0; + rb->tail = (rb->tail + 1) % rb->capacity; + rb->full = 0; return 1; } From 1967c8f124831d2871e9efab08f14a8a159fb41d Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:21:10 +0200 Subject: [PATCH 24/33] Update ring_buffer.hpp format --- shared/data_struct/ring_buffer.hpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/shared/data_struct/ring_buffer.hpp b/shared/data_struct/ring_buffer.hpp index 73b09d5b..e7e8c983 100644 --- a/shared/data_struct/ring_buffer.hpp +++ b/shared/data_struct/ring_buffer.hpp @@ -2,7 +2,7 @@ #include "types.h" extern "C" { -struct CRingBuffer{ +struct CRingBuffer { void* buffer; uint64_t capacity; //TODO: define size_t as the same size of os architecture uint64_t element_size; @@ -18,9 +18,12 @@ int32_t cring_is_empty(const struct CRingBuffer* rb); int32_t cring_is_full(const struct CRingBuffer* rb); void cring_clear(struct CRingBuffer* rb); uint64_t cring_capacity(const struct CRingBuffer* rb); + } -template class RingBuffer{ +template +class RingBuffer{ + private: T data[Capacity]; uint64_t head = 0; @@ -29,15 +32,17 @@ template class RingBuffer{ public: int32_t push(const T& item){ - if(full) return 0; + if (full) return 0; + data[head] = item; head = (head + 1) % Capacity; - full =(head == tail); + full = (head == tail); return 1; } int32_t pop(T& out){ - if(is_empty()) return 0; + if (is_empty()) return 0; + out = data[tail]; tail = (tail + 1)% Capacity; full = 0; @@ -53,13 +58,15 @@ template class RingBuffer{ } void clear(){ - head = tail= 0; + head = tail = 0; full = 0; } uint64_t size() const{ - if(full) return Capacity; - if(head >= tail) return head - tail; + if (full) return Capacity; + + if (head >= tail) return head - tail; + return Capacity + head - tail; } @@ -74,5 +81,6 @@ template class RingBuffer{ T& at(uint32_t index){ return data[(tail + index)% Capacity]; } - static constexpr uint64_t capacity(){return Capacity;} + + static constexpr uint64_t capacity(){ return Capacity; } }; From 6cf11c569ac19cbee67254a7b6963a78e65111c4 Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:27:23 +0200 Subject: [PATCH 25/33] Update string.c format --- shared/std/string.c | 153 +++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 79 deletions(-) diff --git a/shared/std/string.c b/shared/std/string.c index d906302e..f67fbe0a 100644 --- a/shared/std/string.c +++ b/shared/std/string.c @@ -4,29 +4,23 @@ #include "std/memfunctions.h" static uint32_t compute_length(char *s, uint32_t max_length){ - if (s == NULL){ - return 0; - } + if (s == NULL) return 0; + uint32_t len = 0; - while ((max_length == 0 || len < max_length) && s[len] != '\0'){ - len++; - } + while ((max_length == 0 || len < max_length) && s[len] != '\0') len++; + return len; } string string_l(char *literal){ - if (literal == NULL){ - return (string){ .data = NULL, .length = 0, .mem_length = 0}; - } + if (literal == NULL) return (string){ .data = NULL, .length = 0, .mem_length = 0}; + uint32_t len = compute_length(literal, 0); char *buf = (char*)malloc(len + 1); - if (!buf) { - //manage malloc - return (string){ .data = NULL, .length = 0, .mem_length = 0 }; - } - for (uint32_t i = 0; i < len; i++){ - buf[i] = literal[i]; - } + if (!buf) return (string){ .data = NULL, .length = 0, .mem_length = 0 }; + + for (uint32_t i = 0; i < len; i++) buf[i] = literal[i]; + buf[len] = '\0'; return (string){ .data = buf, .length = len, .mem_length = len + 1 }; } @@ -39,38 +33,32 @@ string string_repeat(char symbol, uint32_t amount){ } string string_tail(char *array, uint32_t max_length){ - if (array == NULL){ - return (string){ .data = NULL, .length = 0, .mem_length = 0 }; - } + + if (array == NULL) return (string){ .data = NULL, .length = 0, .mem_length = 0 }; + uint32_t len = compute_length(array, 0); int offset = (int)len - (int)max_length; - if(offset < 0){ - offset = 0; - } + if (offset < 0) offset = 0; + uint32_t adjusted_len = len - offset; char *buf = (char*)malloc(adjusted_len + 1); - if(!buf){ - return (string){ .data = NULL, .length = 0, .mem_length = 0 }; - } - for (uint32_t i = 0; i < adjusted_len; i++){ - buf[i] = array[offset + i]; - } + if (!buf) return (string){ .data = NULL, .length = 0, .mem_length = 0 }; + + for (uint32_t i = 0; i < adjusted_len; i++) buf[i] = array[offset + i]; + buf[adjusted_len] = '\0'; return (string){.data = buf, .length = adjusted_len, .mem_length = adjusted_len + 1 }; } string string_ca_max(char *array, uint32_t max_length){ - if(array == NULL){ - return (string){.data = NULL, .length = 0, .mem_length= 0 }; - } + if (array == NULL) return (string){.data = NULL, .length = 0, .mem_length= 0 }; + uint32_t len = compute_length(array, max_length); char *buf = (char*)malloc(len + 1); - if(!buf){ - return (string){ .data = NULL, .length = 0, .mem_length=0 }; - } - for(uint32_t i = 0; i < len; i++){ - buf[i] = array[i]; - } + if(!buf) return (string){ .data = NULL, .length = 0, .mem_length=0 }; + + for (uint32_t i = 0; i < len; i++) buf[i] = array[i]; + buf[len] = '\0'; return (string){ .data = buf, .length = len, .mem_length = len+1}; } @@ -94,6 +82,7 @@ uint32_t parse_hex(uint64_t value, char* buf){ started = true; buf[len++] = curr_char; } + if (i == 0) break; } @@ -118,6 +107,7 @@ uint32_t parse_bin(uint64_t value, char* buf){ started = true; buf[len++] = bit; } + if (i == 0) break; } @@ -136,9 +126,8 @@ bool string_equals(string a, string b){ } string string_format(char *fmt, ...){ - if(fmt == NULL){ - return (string){ .data = NULL, .length = 0, .mem_length = 0}; - } + if (fmt == NULL) return (string){ .data = NULL, .length = 0, .mem_length = 0}; + va_list args; va_start(args, fmt); string result = string_format_va(fmt, args); @@ -150,45 +139,50 @@ string string_format_va(char *fmt, va_list args){ char *buf = (char*)malloc(256); uint32_t len = 0; uint32_t arg_index = 0; - for(uint32_t i = 0; fmt[i] && len < 255; i++){ - if(fmt[i] == '%' && fmt[i+1]){ + for (uint32_t i = 0; fmt[i] && len < 255; i++){ + if (fmt[i] == '%' && fmt[i+1]){ i++; - if(fmt[i] == 'x'){ + if (fmt[i] == 'x'){ uint64_t val = va_arg(args, uint64_t); len += parse_hex(val,(char*)(buf + len)); - }else if(fmt[i] == 'b') { + + } else if (fmt[i] == 'b') { uint64_t val = va_arg(args, uint64_t); string bin = string_from_bin(val); for(uint32_t j = 0; j < bin.length && len < 255; j++) buf[len++] = bin.data[j]; free(bin.data,bin.mem_length); - }else if(fmt[i] == 'c') { + + } else if (fmt[i] == 'c') { uint64_t val = va_arg(args, uint64_t); buf[len++] = (char)val; - }else if(fmt[i] == 's') { + + } else if (fmt[i] == 's') { char *str = ( char *)va_arg(args, uintptr_t); for (uint32_t j = 0; str[j] && len < 255; j++) buf[len++] = str[j]; - }else if(fmt[i] == 'i') { + + } else if (fmt[i] == 'i') { uint64_t val = va_arg(args, long int); char temp[21]; uint32_t temp_len = 0; bool negative = false; - if((int)val < 0) { + if ((int)val < 0) { negative = true; buf[len++] = '-'; val = (uint64_t)(-(int)val); } - do{ + + do { temp[temp_len++] = '0' + (val % 10); val /= 10; - }while(val && temp_len < 20); + } while (val && temp_len < 20); - for(int j = temp_len - 1; j >= 0 && len < 255; j--){ + for (int j = temp_len - 1; j >= 0 && len < 255; j--){ buf[len++] = temp[j]; } - }else if(fmt[i] == 'f' || fmt[i] == 'd') { + } else if (fmt[i] == 'f' || fmt[i] == 'd') { double val = va_arg(args, double); - if(val < 0){ + if (val < 0){ buf[len++] = '-'; val = -val; } @@ -197,26 +191,27 @@ string string_format_va(char *fmt, va_list args){ char temp[21]; uint32_t temp_len = 0; - do{ + do { temp[temp_len++] = '0' +(whole % 10); whole /= 10; - }while(whole && temp_len < 20); + } while(whole && temp_len < 20); - for(int j = temp_len - 1; j >= 0 && len < 255; j--){ + for (int j = temp_len - 1; j >= 0 && len < 255; j--){ buf[len++] = temp[j]; } - if(len < 255) buf[len++] = '.'; + if (len < 255) buf[len++] = '.'; + for (int d = 0; d < 6 && len < 255; d++) { frac *= 10; int digit = (int)frac; buf[len++] = '0' + digit; frac -= digit; } - }else{ + } else { buf[len++] = '%'; buf[len++] = fmt[i]; } - }else{ + } else { buf[len++] = fmt[i]; } } @@ -226,33 +221,33 @@ string string_format_va(char *fmt, va_list args){ } char tolower(char c){ - if(c >= 'A' && c <= 'Z') return c + 'a' - 'A'; + if (c >= 'A' && c <= 'Z') return c + 'a' - 'A'; return c; } int strcmp(char *a, char *b, bool case_insensitive){ - if(a == NULL && b == NULL)return 0; //i guess - if(a == NULL) return -1; - if(b == NULL) return 1; + if (a == NULL && b == NULL)return 0; //i guess + if (a == NULL) return -1; + if (b == NULL) return 1; - while(*a && *b){ + while (*a && *b){ char ca = *a; char cb = *b; - if(case_insensitive){ + if (case_insensitive){ ca = tolower((unsigned char)ca); cb = tolower((unsigned char)cb); } - if(ca != cb) return ca - cb; + if (ca != cb) return ca - cb; a++; b++; } - if(case_insensitive) - return tolower((unsigned char)*a) - tolower((unsigned char)*b); + if (case_insensitive) return tolower((unsigned char)*a) - tolower((unsigned char)*b); + return (unsigned char)*a - (unsigned char)*b; } int strstart(char *a, char *b, bool case_insensitive){ int index = 0; - while(*a && *b){ + while (*a && *b){ char ca = *a; char cb = *b; @@ -261,14 +256,14 @@ int strstart(char *a, char *b, bool case_insensitive){ cb = tolower(cb); } - if(ca != cb) return index; + if (ca != cb) return index; a++; b++; index++; } return 0; } int strindex( char *a, char *b){ - for(int i = 0; a[i]; i++){ + for (int i = 0; a[i]; i++){ int j = 0; while (b[j] && a[i + j] == b[j]) j++; if (!b[j]) return i; @@ -277,13 +272,13 @@ int strindex( char *a, char *b){ } int strend(char *a, char *b, bool case_insensitive){ - while(*a && *b){ + while (*a && *b){ char ca = case_insensitive ? tolower((unsigned char)*a) : *a; char cb = case_insensitive ? tolower((unsigned char)*b) : *b; - if(ca == cb){ + if (ca == cb){ char *pa = a, *pb = b; - while(1){ + while (1){ char cpa = case_insensitive ? tolower((unsigned char)*pa) : *pa; char cpb = case_insensitive ? tolower((unsigned char)*pb) : *pb; @@ -299,12 +294,12 @@ int strend(char *a, char *b, bool case_insensitive){ } bool strcont( char *a, char *b){ - while(*a){ + while (*a){ char *p = a, *q = b; - while(*p && *q && *p == *q){ + while (*p && *q && *p == *q){ p++; q++; } - if(*q == 0) return 1; + if (*q == 0) return 1; a++; } return 0; @@ -312,7 +307,7 @@ bool strcont( char *a, char *b){ bool utf16tochar(uint16_t* str_in, char* out_str, size_t max_len){ size_t out_i = 0; - for(int i = 0; i < max_len && str_in[i]; i++){ + for (int i = 0; i < max_len && str_in[i]; i++){ uint16_t wc = str_in[i]; out_str[out_i++] = (wc <= 0x7F) ? (char)(wc & 0xFF) : '?'; } @@ -322,7 +317,7 @@ bool utf16tochar(uint16_t* str_in, char* out_str, size_t max_len){ uint64_t parse_hex_u64(char* str, size_t size){ uint64_t result = 0; - for(uint32_t i = 0; i < size; i++){ + for (uint32_t i = 0; i < size; i++){ char c = str[i]; uint8_t digit = 0; if (c >= '0' && c <= '9') digit = c - '0'; From 1292655f9d02c89bb339e12ee5ed84b7dc6a0346 Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:28:16 +0200 Subject: [PATCH 26/33] Update doubly_linked_list.hpp format --- shared/data_struct/doubly_linked_list.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/data_struct/doubly_linked_list.hpp b/shared/data_struct/doubly_linked_list.hpp index 0bc71f34..15791ca1 100644 --- a/shared/data_struct/doubly_linked_list.hpp +++ b/shared/data_struct/doubly_linked_list.hpp @@ -140,7 +140,7 @@ class LinkedList{ } T pop_front(){ - if !head) return T(); + if (!head) return T(); Node* n = head; T val = n->data; From 38b796ca576c2273d7a8f52132aca15139c35e73 Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 19:38:57 +0200 Subject: [PATCH 27/33] Update Makefile --- kernel/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/Makefile b/kernel/Makefile index c16f18a0..ea3f3a96 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -8,6 +8,7 @@ endif LDFLAGS := $(LDFLAGS_BASE) -T $(shell ls *.ld) --defsym=LOAD_ADDR=$(LOAD_ADDR) +CLEAN_OBJS := $(shell find . -name '*.o') C_SRC := $(shell find . -name '*.c') ASM_SRC := $(shell find . -name '*.S') CPP_SRC := $(shell find . -name '*.cpp') @@ -31,4 +32,4 @@ $(TARGET): ../shared/libshared.a $(OBJ) $(CC) $(CFLAGS) -fno-rtti -c $< -o $@ clean: - rm -f $(OBJ) $(ELF) $(TARGET) \ No newline at end of file + rm -f $(CLEAN_OBJS) $(ELF) $(TARGET) From 4ac1ef8c475f3b0f21fce86d7423e9887059410e Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 19:49:34 +0200 Subject: [PATCH 28/33] Update Makefile --- shared/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shared/Makefile b/shared/Makefile index 8fe0d3d0..4c9fe9cd 100644 --- a/shared/Makefile +++ b/shared/Makefile @@ -1,6 +1,7 @@ #shared CFLAGS := $(CFLAGS_BASE) -I. -I../kernel -Wno-unused-parameter +CLEAN_OBJS := $(shell find . -name '*.o') C_SRC := $(shell find . -name '*.c') CPP_SRC := $(shell find . -name '*.cpp') ASM_SRC := $(shell find . -name '*.S') @@ -25,4 +26,4 @@ $(TARGET): $(OBJ) $(CC) $(CFLAGS) -fno-rtti -c $< -o $@ clean: - rm -f $(OBJ) $(TARGET) + rm -f $(CLEAN_OBJS) $(TARGET) From 4ad766f7dcec8fb160d264fc97a83e628138fd6c Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 19:50:37 +0200 Subject: [PATCH 29/33] Update Makefile --- user/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/user/Makefile b/user/Makefile index e85c133e..a9799437 100644 --- a/user/Makefile +++ b/user/Makefile @@ -2,6 +2,7 @@ CFLAGS := $(CFLAGS_BASE) -I. -I../shared -Wno-unused-parameter LDFLAGS := -T $(shell ls *.ld) +CLEAN_OBJS := $(shell find . -name '*.o') C_SRC := $(shell find . -name '*.c') CPP_SRC := $(shell find . -name '*.cpp') OBJ := $(C_SRC:.c=.o) $(CPP_SRC:.cpp=.o) @@ -29,4 +30,4 @@ $(LOCATION)$(TARGET): $(OBJ) $(CC) $(CFLAGS) -fno-rtti -c $< -o $@ clean: - rm -f $(OBJ) $(TARGET) + rm -f $(CLEAN_OBJS) $(TARGET) From 5bd3dfeb15b3276c44bda163a28fb64301a0e10e Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 20:47:43 +0200 Subject: [PATCH 30/33] Update kconsole.cpp --- kernel/console/kconsole/kconsole.cpp | 78 ++++++++++++++-------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/kernel/console/kconsole/kconsole.cpp b/kernel/console/kconsole/kconsole.cpp index 3581472e..aaefe82d 100644 --- a/kernel/console/kconsole/kconsole.cpp +++ b/kernel/console/kconsole/kconsole.cpp @@ -17,9 +17,9 @@ bool KernelConsole::check_ready(){ } void KernelConsole::resize(){ - gpu_size s = gpu_get_screen_size(); - columns = s.width / char_width; - rows = s.height / char_height; + gpu_size screen_size = gpu_get_screen_size(); + columns = screen_size.width / char_width; + rows = screen_size.height / char_height; if (row_data) temp_free(row_data, buffer_data_size); buffer_data_size = rows * columns; @@ -31,7 +31,7 @@ void KernelConsole::resize(){ } row_ring.clear(); - for (uint32_t i=0;i= columns) newline(); - uint32_t ri; - if (row_ring.pop(ri)){ - row_ring.push(ri); - char* line = row_data + ri * columns; + uint32_t row_index; + if (row_ring.pop(row_index)){ + row_ring.push(row_index); + char* line = row_data + row_index * columns; line[cursor_x] = c; - gpu_draw_char({cursor_x * char_width,cursor_y*char_height}, c, 1, 0xFFFFFFFF); + gpu_draw_char({cursor_x * char_width, cursor_y * char_height}, c, 1, 0xFFFFFFFF); cursor_x++; } } -void KernelConsole::put_string(const char* s){ +void KernelConsole::put_string(const char* str){ if (!check_ready()) return; - for (uint32_t i=0; s[i]; i++) put_char(s[i]); + for (uint32_t i = 0; str[i]; i++) put_char(str[i]); gpu_flush(); } -void KernelConsole::put_hex(uint64_t v){ +void KernelConsole::put_hex(uint64_t value){ if (!check_ready()) return; put_char('0'); put_char('x'); bool started = false; - for(uint32_t i =60 ;; i-=4){ - uint8_t n = (v>>i)&0xF; - char c = n < 10 ? '0' + n : 'A' + (n - 10); - if (started || c!='0' || i==0){ - started = true; - put_char(c); + for (uint32_t i = 60 ;; i -= 4){ + uint8_t nibble = (value >> i) & 0xF; + char current_char = nibble < 10 ? '0' + nibble : 'A' + (nibble - 10); + if (started || current_char != '0' || i == 0){ + started = true; + put_char(current_char); } if (i == 0) break; } @@ -77,27 +77,27 @@ void KernelConsole::put_hex(uint64_t v){ void KernelConsole::newline(){ if (!check_ready()) return; - uint32_t ri; - if (row_ring.pop(ri)){ - char* line=row_data+ri*columns; - for (uint32_t x = cursor_x; x < columns; x++) line[x]=0; - row_ring.push(ri); + uint32_t row_index; + if (row_ring.pop(row_index)){ + char* line = row_data + row_index * columns; + for (uint32_t x = cursor_x; x < columns; x++) line[x] = 0; + row_ring.push(row_index); } cursor_x = 0; cursor_y++; if (cursor_y >= rows){ scroll(); - cursor_y = rows -1; + cursor_y = rows - 1; } } void KernelConsole::scroll(){ if (!check_ready()) return; - uint32_t ri; - if (row_ring.pop(ri)){ - char* line=row_data+ri*columns; - for(uint32_t x = 0; x < columns; x++) line[x]=0; - row_ring.push(ri); + uint32_t row_index; + if (row_ring.pop(row_index)){ + char* line = row_data + row_index * columns; + for (uint32_t x = 0; x < columns; x++) line[x] = 0; + row_ring.push(row_index); } redraw(); } @@ -105,10 +105,10 @@ void KernelConsole::scroll(){ void KernelConsole::redraw(){ screen_clear(); for (uint32_t y = 0; y < rows; y++){ - uint32_t ri; - if (row_ring.pop(ri)){ - row_ring.push(ri); - char* line = row_data + ri * columns; + uint32_t row_index; + if (row_ring.pop(row_index)){ + row_ring.push(row_index); + char* line = row_data + row_index * columns; for (uint32_t x = 0; x < columns; x++){ gpu_draw_char({x * char_width, y * char_height}, line[x], 1, 0xFFFFFFFF); } @@ -123,12 +123,12 @@ void KernelConsole::screen_clear(){ void KernelConsole::clear(){ screen_clear(); for (uint32_t i = 0; i < rows; i++){ - uint32_t ri; - if (row_ring.pop(ri)){ - char* line=row_data+ri*columns; - for (uint32_t x = 0; x < columns; x++) line[x]=0; - row_ring.push(ri); + uint32_t row_index; + if (row_ring.pop(row_index)){ + char* line = row_data + row_index * columns; + for (uint32_t x = 0; x < columns; x++) line[x] = 0; + row_ring.push(row_index); } } - cursor_x=cursor_y = 0; + cursor_x = cursor_y = 0; } From 067282e2072106e0069515e885a693fed928d3b5 Mon Sep 17 00:00:00 2001 From: CodeAnarchist <144830359+CodeAnarchist@users.noreply.github.com> Date: Fri, 25 Jul 2025 20:52:23 +0200 Subject: [PATCH 31/33] Update Makefile --- kernel/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/Makefile b/kernel/Makefile index ea3f3a96..c6b00990 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -15,8 +15,8 @@ CPP_SRC := $(shell find . -name '*.cpp') OBJ := $(C_SRC:.c=.o) $(ASM_SRC:.S=.o) $(CPP_SRC:.cpp=.o) OBJL := $(filter-out ./boot.o,$(OBJ)) -ELF := kernel.elf -TARGET := kernel.img +ELF := ../kernel.elf +TARGET := ../kernel.img all: $(TARGET) From 0fb02c583c8c3e1068fba100e62dbc251e3d51ca Mon Sep 17 00:00:00 2001 From: CodeAnarchist Date: Fri, 25 Jul 2025 22:00:03 +0200 Subject: [PATCH 32/33] split data structures in .c/.h and .hpp --- kernel/input/xhci_types.h | 378 ++++++++++++++++++ .../{chunked_list.cpp => chunked_list.c} | 132 +++--- shared/data_struct/chunked_list.h | 40 ++ shared/data_struct/chunked_list.hpp | 180 ++++----- shared/data_struct/doubly_linked_list.c | 181 +++++++++ shared/data_struct/doubly_linked_list.cpp | 195 --------- shared/data_struct/doubly_linked_list.h | 40 ++ shared/data_struct/doubly_linked_list.hpp | 159 +++----- .../{linked_list.cpp => linked_list.c} | 74 ++-- shared/data_struct/linked_list.h | 37 ++ shared/data_struct/linked_list.hpp | 130 +++--- shared/data_struct/queue.c | 69 ++++ shared/data_struct/queue.cpp | 72 ---- shared/data_struct/queue.h | 31 ++ shared/data_struct/queue.hpp | 109 +++-- .../{ring_buffer.cpp => ring_buffer.c} | 29 +- shared/data_struct/ring_buffer.h | 27 ++ shared/data_struct/ring_buffer.hpp | 60 +-- 18 files changed, 1187 insertions(+), 756 deletions(-) create mode 100644 kernel/input/xhci_types.h rename shared/data_struct/{chunked_list.cpp => chunked_list.c} (61%) create mode 100644 shared/data_struct/chunked_list.h create mode 100644 shared/data_struct/doubly_linked_list.c delete mode 100644 shared/data_struct/doubly_linked_list.cpp create mode 100644 shared/data_struct/doubly_linked_list.h rename shared/data_struct/{linked_list.cpp => linked_list.c} (72%) create mode 100644 shared/data_struct/linked_list.h create mode 100644 shared/data_struct/queue.c delete mode 100644 shared/data_struct/queue.cpp create mode 100644 shared/data_struct/queue.h rename shared/data_struct/{ring_buffer.cpp => ring_buffer.c} (53%) create mode 100644 shared/data_struct/ring_buffer.h diff --git a/kernel/input/xhci_types.h b/kernel/input/xhci_types.h new file mode 100644 index 00000000..34484fac --- /dev/null +++ b/kernel/input/xhci_types.h @@ -0,0 +1,378 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include "types.h" + +#define TRB_TYPE_NORMAL 1 +#define TRB_TYPE_LINK 6 +#define TRB_TYPE_EVENT_DATA 3 +#define TRB_TYPE_ENABLE_SLOT 9 +#define TRB_TYPE_ADDRESS_DEV 11 +#define TRB_TYPE_CONFIG_EP 12 +#define TRB_TYPE_SETUP 2 +#define TRB_TYPE_STATUS 4 +#define TRB_TYPE_INPUT 8 + +#define MAX_TRB_AMOUNT 256 +#define MAX_ERST_AMOUNT 1 + +#define TRB_TYPE_MASK 0xFC00 +#define TRB_ENDPOINT_MASK 0xF0000 +#define TRB_SLOT_MASK 0xF000000 + +#define TRB_TYPE_TRANSFER 0x20 +#define TRB_TYPE_COMMAND_COMPLETION 0x21 +#define TRB_TYPE_PORT_STATUS_CHANGE 0x22 + +#define TRB_TYPE_SETUP_STAGE 0x2 +#define TRB_TYPE_DATA_STAGE 0x3 +#define TRB_TYPE_STATUS_STAGE 0x4 + +#define XHCI_USBSTS_HSE (1 << 2) +#define XHCI_USBSTS_CE (1 << 12) + +#define XHCI_IRQ 31 + +typedef struct { + uint64_t parameter; + uint32_t status; + uint32_t control; +}__attribute__((packed)) trb; + +typedef struct { + uint8_t caplength; + uint8_t reserved; + uint16_t hciversion; + uint32_t hcsparams1; + uint32_t hcsparams2; + uint32_t hcsparams3; + uint32_t hccparams1; + uint32_t dboff; + uint32_t rtsoff; + uint32_t hccparams2; +}__attribute__((packed)) xhci_cap_regs; + +typedef struct { + uint32_t usbcmd; + uint32_t usbsts; + uint32_t pagesize; + uint64_t reserved0; + uint32_t dnctrl; + uint64_t crcr; + uint32_t reserved1[4]; + uint64_t dcbaap; + uint32_t config; +}__attribute__((packed)) xhci_op_regs; + +typedef union { + struct { + uint32_t ccs : 1; + uint32_t ped : 1; + uint32_t rsvd0 : 1; + uint32_t oca : 1; + uint32_t pr : 1; + uint32_t pls : 4; + uint32_t pp : 1; + uint32_t port_speed : 4; + uint32_t pic : 2; + uint32_t lws : 1; + uint32_t csc : 1; + uint32_t pec : 1; + uint32_t wrc : 1; + uint32_t occ : 1; + uint32_t prc : 1; + uint32_t plc : 1; + uint32_t cec : 1; + uint32_t cas : 1; + uint32_t wce : 1; + uint32_t wde : 1; + uint32_t woe : 1; + uint32_t rsvd1 : 2; + uint32_t dr : 1; + uint32_t wpr : 1; + }; + uint32_t value; +} portstatuscontrol; + +typedef struct { + portstatuscontrol portsc; + uint32_t portpmsc; + uint32_t portli; + uint32_t rsvd; +}__attribute__((packed, aligned(4))) xhci_port_regs; + +typedef struct { + uint32_t iman; + uint32_t imod; + uint32_t erstsz; + uint32_t reserved; + uint64_t erstba; + uint64_t erdp; +}__attribute__((packed)) xhci_interrupter; + +typedef struct { + uint64_t mmio; + uint64_t mmio_size; + xhci_cap_regs* cap; + xhci_op_regs* op; + xhci_port_regs* ports; + uint64_t db_base; + uint64_t rt_base; + trb* cmd_ring; + uint32_t cmd_index; + uint32_t event_index; + bool command_cycle_bit; + bool event_cycle_bit; + trb* event_ring; + uint8_t* key_buffer; + xhci_interrupter* interrupter; + uint64_t* dcbaa; + uint16_t max_device_slots; + uint16_t max_ports; +} xhci_device; + +typedef struct { + uint64_t ring_base; + uint32_t ring_size; + uint32_t reserved; +}__attribute__((packed)) erst_entry; + +typedef struct { + uint32_t drop_flags; + uint32_t add_flags; + uint64_t reserved[3]; +}__attribute__((packed)) xhci_input_control_context; + +typedef union +{ + struct + { + uint32_t route_string: 20; + uint32_t speed: 4; + uint32_t rsvd : 1; + uint32_t mtt : 1; + uint32_t hub : 1; + uint32_t context_entries : 5; + }; + uint32_t value; +} slot_field0; + +typedef union +{ + struct + { + uint16_t max_exit_latency; + uint8_t root_hub_port_num; + uint8_t port_count; + }; + uint32_t value; +} slot_field1; + +typedef union +{ + struct + { + uint32_t parent_hub_slot_id : 8; + uint32_t parent_port_number : 8; + uint32_t think_time : 2; + uint32_t rsvd : 4; + uint32_t interrupt_target : 10; + }; + uint32_t value; +} slot_field2; + +typedef union +{ + struct + { + uint32_t device_address : 8; + uint32_t rsvd : 19; + uint32_t state : 5;//0 disabled, 1 default, 2 addressed, 3 configured + }; + uint32_t value; +} slot_field3; + +typedef union +{ + struct { + uint32_t endpoint_state : 3; + uint32_t rsvd0 : 5; + uint32_t mult : 2; + uint32_t max_primary_streams : 5; + uint32_t linear_stream_array : 1; + uint32_t interval : 8; + uint32_t max_esit_payload_hi : 8; + }; + uint32_t value; +} endpoint_field0; + +typedef union +{ + struct { + uint32_t rsvd1 : 1; + uint32_t error_count : 2; + uint32_t endpoint_type : 3; + uint32_t rsvd2 : 1; + uint32_t host_initiate_disable : 1; + uint32_t max_burst_size : 8; + uint32_t max_packet_size : 16; + }; + uint32_t value; +} endpoint_field1; + +typedef union { + struct { + uint64_t dcs : 1; + uint64_t rsvd0 : 3; + uint64_t ring_ptr : 60; + }; + uint64_t value; +} endpoint_field23; + +typedef union +{ + struct { + uint16_t average_trb_length; + uint16_t max_esit_payload_lo; + }; + uint32_t value; +} endpoint_field4; + +typedef struct { + slot_field0 slot_f0; + slot_field1 slot_f1; + slot_field2 slot_f2; + slot_field3 slot_f3; + uint32_t slot_rsvd[4]; + struct { + endpoint_field0 endpoint_f0; + endpoint_field1 endpoint_f1; + endpoint_field23 endpoint_f23; + endpoint_field4 endpoint_f4; + uint32_t ep_rsvd[3]; + } endpoints[31]; +} xhci_device_context; + +typedef struct { + xhci_input_control_context control_context; + xhci_device_context device_context; +} xhci_input_context; + +typedef struct __attribute__((packed)) { + uint8_t bmRequestType; + uint8_t bRequest; + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; +} usb_setup_packet; + +typedef struct __attribute__((packed)) { + uint8_t bLength; + uint8_t bDescriptorType; +} usb_descriptor_header ; + +typedef struct __attribute__((packed)) { + usb_descriptor_header header; + uint16_t bcdUSB; + uint8_t bDeviceClass; + uint8_t bDeviceSubClass; + uint8_t bDeviceProtocol; + uint8_t bMaxPacketSize0; + uint16_t idVendor; + uint16_t idProduct; + uint16_t bcdDevice; + uint8_t iManufacturer; + uint8_t iProduct; + uint8_t iSerialNumber; + uint8_t bNumConfigurations; +} usb_device_descriptor; + +typedef struct __attribute__((packed)) { + usb_descriptor_header header; + uint16_t wTotalLength; + uint8_t bNumInterfaces; + uint8_t bConfigurationValue; + uint8_t iConfiguration; + uint8_t bmAttributes; + uint8_t bMaxPower; + uint8_t data[255]; +} usb_configuration_descriptor; + +typedef struct __attribute__((packed)) { + usb_descriptor_header header; + uint8_t bInterfaceNumber; + uint8_t bAlternateSetting; + uint8_t bNumEndpoints; + uint8_t bInterfaceClass; + uint8_t bInterfaceSubClass; + uint8_t bInterfaceProtocol; + uint8_t iInterface; +} usb_interface_descriptor; + +typedef struct __attribute__((packed)) { + usb_descriptor_header header; + uint16_t bcdHID; + uint8_t bCountryCode; + uint8_t bNumDescriptors; + struct { + uint8_t bDescriptorType; + uint8_t wDescriptorLength; + }__attribute__((packed)) descriptors[1]; +//TODO: wDescriptorLength is supposed to be 16, but for some reason the descriptor from usb-kbd is 8. Will need to fix this once we do a real device +} usb_hid_descriptor; + +typedef struct __attribute__((packed)) { + usb_descriptor_header header; + uint8_t bEndpointAddress; + uint8_t bmAttributes; + uint8_t wMaxPacketSize; + uint8_t bInterval; +//TODO: wMaxPacketSize is supposed to be 16, but for some reason the descriptor from usb-kbd is 8. Will need to fix this once we do a real device +} usb_endpoint_descriptor; + +typedef struct __attribute__((packed)) { + usb_descriptor_header header; + uint16_t lang_ids[126]; +} usb_string_language_descriptor; + +typedef struct __attribute__((packed)){ + usb_descriptor_header header; + uint16_t unicode_string[126]; +} usb_string_descriptor; + +typedef enum { + NONE, + KEYBOARD, + MOUSE +} xhci_device_types; + +typedef struct { + uint8_t transfer_cycle_bit; + uint32_t transfer_index; + trb* transfer_ring; + uint32_t slot_id; + xhci_input_context* ctx; +} xhci_usb_device; + +typedef struct { + xhci_device_types type; + trb* endpoint_transfer_ring; + uint32_t endpoint_transfer_index; + uint8_t endpoint_transfer_cycle_bit; + uint8_t poll_packetSize; + uint8_t *input_buffer; + uint8_t poll_endpoint; + uint16_t report_length; + uint8_t *report_descriptor; + } xhci_usb_device_endpoint; + +#define USB_DEVICE_DESCRIPTOR 1 +#define USB_CONFIGURATION_DESCRIPTOR 2 +#define USB_STRING_DESCRIPTOR 3 + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/shared/data_struct/chunked_list.cpp b/shared/data_struct/chunked_list.c similarity index 61% rename from shared/data_struct/chunked_list.cpp rename to shared/data_struct/chunked_list.c index af95e3d7..376feca5 100644 --- a/shared/data_struct/chunked_list.cpp +++ b/shared/data_struct/chunked_list.c @@ -1,66 +1,61 @@ -#include "chunked_list.hpp" -#include "console/kio.h" - -extern "C" { +#include "chunked_list.h" +#include "types.h" cchunked_list_t* cchunked_list_create(uint64_t chunkSize){ - uintptr_t raw = malloc(sizeof(cchunked_node_t)+chunkSize*sizeof(void*)); + uintptr_t raw = malloc(sizeof(cchunked_node_t) + chunkSize * sizeof(void*)); if (!raw) return NULL; - + cchunked_list_t* list = (cchunked_list_t*)malloc(sizeof(cchunked_list_t)); - if (!list){ - free((void*)raw,sizeof(cchunked_node_t)+chunkSize*sizeof(void*)); + if (!list) { + free((void*)raw, sizeof(cchunked_node_t) + chunkSize * sizeof(void*)); return NULL; } - - list->chunkSize=chunkSize; - list->length=0; - list->head=(cchunked_node_t*)raw; - list->head->count=0; - list->head->next=NULL; - list->tail=list->head; - + + list->chunkSize = chunkSize; + list->length = 0; + list->head = (cchunked_node_t*)raw; + list->head->count = 0; + list->head->next = NULL; + list->tail = list->head; return list; } void cchunked_list_destroy(cchunked_list_t* list){ if (!list) return; - - cchunked_node_t* node=list->head; - while (node){ - cchunked_node_t* next=node->next; - free(node,sizeof(cchunked_node_t)+list->chunkSize*sizeof(void*)); - node=next; + cchunked_node_t* node = list->head; + while (node) { + cchunked_node_t* next = node->next; + free(node, sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); + node = next; } - - free(list,sizeof(cchunked_list_t)); + free(list, sizeof(cchunked_list_t)); } + cchunked_list_t* cchunked_list_clone(const cchunked_list_t* list){ if (!list) return NULL; - - cchunked_list_t* clone=cchunked_list_create(list->chunkSize); + cchunked_list_t* clone = cchunked_list_create(list->chunkSize); if (!clone) return NULL; - - for (cchunked_node_t* it = list->head; it; it = it->next){ - for (uint64_t i = 0; icount; i++) - cchunked_list_push_back(clone,it->data[i]); + + for (cchunked_node_t* it = list->head; it; it = it->next) { + for (uint64_t i = 0; i < it->count; i++) + cchunked_list_push_back(clone, it->data[i]); } return clone; } void cchunked_list_push_back(cchunked_list_t* list, void* data){ if (!list) return; - //first mnode - if (list->tail == NULL){ + + if (!list->tail) { uintptr_t m = malloc(sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); if (!m) return; cchunked_node_t* node = (cchunked_node_t*)m; node->count = 0; node->next = NULL; - list->head=list->tail = node; + list->head = list->tail = node; } - - if (list->tail->count == list->chunkSize){ //last chunk + + if (list->tail->count == list->chunkSize) { uintptr_t m = malloc(sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); if (!m) return; cchunked_node_t* node = (cchunked_node_t*)m; @@ -69,25 +64,24 @@ void cchunked_list_push_back(cchunked_list_t* list, void* data){ list->tail->next = node; list->tail = node; } + list->tail->data[list->tail->count++] = data; list->length++; } -cchunked_node_t* cchunked_list_insert_after(cchunked_list_t* list,cchunked_node_t* node, void* data){ +cchunked_node_t* cchunked_list_insert_after(cchunked_list_t* list, cchunked_node_t* node, void* data){ if (!list) return NULL; - - if (!node){ + if (!node) { cchunked_list_push_back(list, data); return list->tail; } - - if(node->count < list->chunkSize){//chunk isnt completrly full + + if (node->count < list->chunkSize) { node->data[node->count++] = data; list->length++; return node; } - - //new node + uintptr_t m = malloc(sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); if (!m) return NULL; cchunked_node_t* new_node = (cchunked_node_t*)m; @@ -95,53 +89,51 @@ cchunked_node_t* cchunked_list_insert_after(cchunked_list_t* list,cchunked_node_ new_node->next = node->next; new_node->data[0] = data; - //linking new node node->next = new_node; if (list->tail == node) list->tail = new_node; - list->length++; return new_node; } + void* cchunked_list_pop_front(cchunked_list_t* list){ - if (!list || list->length == 0 || list->head == NULL) return NULL; - + if (!list || !list->head || list->length == 0) return NULL; + void* data = list->head->data[0]; - if (list->head->count > 1){ - for (uint64_t i = 1; i < list->head->count; ++i) list->head->data[i-1] = list->head->data[i]; + if (list->head->count > 1) { + for (uint64_t i = 1; i < list->head->count; ++i) + list->head->data[i-1] = list->head->data[i]; list->head->count--; } else { - //uniq element - cchunked_node_t* old =list->head; + cchunked_node_t* old = list->head; list->head = old->next; - if(list->head == NULL) list->tail = NULL; + if (!list->head) list->tail = NULL; free(old, sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); } - + list->length--; return data; } - - void* cchunked_list_remove_node(cchunked_list_t* list, cchunked_node_t* node){ if (!list || !node || !list->head) return NULL; - //delegate to pop_front - if (node == list->head) return cchunked_list_pop_front(list); + if (node == list->head) + return cchunked_list_pop_front(list); cchunked_node_t* prev = list->head; - while (prev->next && prev->next != node) prev= prev->next; + while (prev->next && prev->next != node) prev = prev->next; if (prev->next != node) return NULL; void* data = node->data[0]; - for (uint64_t i = 1; i < node->count; ++i) node->data[i-1] = node->data[i]; - + for (uint64_t i = 1; i < node->count; ++i) + node->data[i - 1] = node->data[i]; node->count--; list->length--; - if (node->count == 0){ + + if (node->count == 0) { prev->next = node->next; - if(list->tail == node) list->tail = prev; - free(node, sizeof(cchunked_node_t)+list->chunkSize * sizeof(void*)); + if (list->tail == node) list->tail = prev; + free(node, sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); } return data; @@ -149,7 +141,14 @@ void* cchunked_list_remove_node(cchunked_list_t* list, cchunked_node_t* node){ void cchunked_list_update(cchunked_list_t* list, cchunked_node_t* node, void* new_data){ (void)list; - if (node&&node->count) node->data[0] = new_data; + if (node && node->count) + node->data[0] = new_data; +} + +void cchunked_list_update_at(cchunked_list_t* list, cchunked_node_t* node, uint64_t offset, void* new_data){ + if (!list || !node || offset >= node->count) + return; + node->data[offset] = new_data; } uint64_t cchunked_list_length(const cchunked_list_t* list){ @@ -160,7 +159,6 @@ uint64_t cchunked_list_size_bytes(const cchunked_list_t* list){ if (!list) return 0; uint64_t nodes = 0; for (cchunked_node_t* it = list->head; it; it = it->next) nodes++; - return nodes * (sizeof(cchunked_node_t) + list->chunkSize * sizeof(void*)); } @@ -170,13 +168,7 @@ void cchunked_list_for_each(const cchunked_list_t* list, void (*func)(void*)){ for (uint64_t i = 0; i < it->count; ++i) func(it->data[i]); } -void cchunked_list_update_at(cchunked_list_t* list, cchunked_node_t* node,uint64_t offset,void* new_data){ - if(!list || !node || offset >= node->count)return; - - node->data[offset] = new_data; -} int cchunked_list_is_empty(const cchunked_list_t* list){ return (!list || list->length == 0) ? 1 : 0; } -} diff --git a/shared/data_struct/chunked_list.h b/shared/data_struct/chunked_list.h new file mode 100644 index 00000000..3d341c96 --- /dev/null +++ b/shared/data_struct/chunked_list.h @@ -0,0 +1,40 @@ +#pragma once +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct cchunked_node { + uint64_t count; + struct cchunked_node* next; + void* data[]; +} cchunked_node_t; + +typedef struct cchunked_list { + uint64_t chunkSize; + uint64_t length; + cchunked_node_t* head; + cchunked_node_t* tail; +} cchunked_list_t; + +uintptr_t malloc(uint64_t size); +void free(void* ptr, uint64_t size); + +cchunked_list_t* cchunked_list_create(uint64_t chunkSize); +void cchunked_list_destroy(cchunked_list_t* list); +cchunked_list_t* cchunked_list_clone(const cchunked_list_t* list); +void cchunked_list_push_back(cchunked_list_t* list, void* data); +void* cchunked_list_pop_front(cchunked_list_t* list); +cchunked_node_t* cchunked_list_insert_after(cchunked_list_t* list, cchunked_node_t* node, void* data); +void* cchunked_list_remove_node(cchunked_list_t* list, cchunked_node_t* node); +void cchunked_list_update(cchunked_list_t* list, cchunked_node_t* node, void* new_data); +void cchunked_list_update_at(cchunked_list_t* list, cchunked_node_t* node, uint64_t offset, void* new_data); +uint64_t cchunked_list_length(const cchunked_list_t* list); +uint64_t cchunked_list_size_bytes(const cchunked_list_t* list); +void cchunked_list_for_each(const cchunked_list_t* list, void (*func)(void*)); +int cchunked_list_is_empty(const cchunked_list_t* list); + +#ifdef __cplusplus +} +#endif diff --git a/shared/data_struct/chunked_list.hpp b/shared/data_struct/chunked_list.hpp index 41069f58..df5f04c1 100644 --- a/shared/data_struct/chunked_list.hpp +++ b/shared/data_struct/chunked_list.hpp @@ -1,90 +1,64 @@ #pragma once - #include "types.h" - -#ifdef __cplusplus extern "C" { -#endif - -extern uintptr_t malloc(uint64_t size); -extern void free(void *ptr, uint64_t size); - -typedef struct cchunked_node{ - uint64_t count; - struct cchunked_node* next; - void* data[]; -}cchunked_node_t; - -typedef struct cchunked_list{ - uint64_t chunkSize; - uint64_t length; - cchunked_node_t* head; - cchunked_node_t* tail; -}cchunked_list_t; - -cchunked_list_t* cchunked_list_create(uint64_t chunkSize); -void cchunked_list_destroy(cchunked_list_t* list); -cchunked_list_t* cchunked_list_clone(const cchunked_list_t* list); -void cchunked_list_push_back(cchunked_list_t* list, void* data); -void* cchunked_list_pop_front(cchunked_list_t* list); -cchunked_node_t* cchunked_list_insert_after(cchunked_list_t* list, cchunked_node_t* node, void* data); -void* cchunked_list_remove_node(cchunked_list_t* list, cchunked_node_t* node); -void cchunked_list_update(cchunked_list_t* list, cchunked_node_t* node, void* new_data); -void cchunked_list_update_at(cchunked_list_t* list, cchunked_node_t* node,uint64_t offset, void* new_data); -uint64_t cchunked_list_length(const cchunked_list_t* list); -uint64_t cchunked_list_size_bytes(const cchunked_list_t* list); -void cchunked_list_for_each(const cchunked_list_t* list, void (*func)(void*)); -int cchunked_list_is_empty(const cchunked_list_t* list); - -#ifdef __cplusplus +#include "chunked_list.h" } + template -class ChunkedList{ +class ChunkedList { public: - struct Node{ + struct Node { uint64_t count; Node* next; T data[]; }; - explicit ChunkedList(uint64_t cs): head(nullptr), tail(nullptr), length_(0), chunkSize(cs){} - ~ChunkedList(){ - while (head){ - Node* nx = head->next; + explicit ChunkedList(uint64_t cs) + : head(nullptr), tail(nullptr), length_(0), chunkSize(cs) {} + + ~ChunkedList() { + while (head) { + Node* next = head->next; ::free(head, sizeof(Node) + chunkSize * sizeof(T)); - head = nx; + head = next; } } - ChunkedList(const ChunkedList& other): head(nullptr), tail(nullptr), length_(0), chunkSize(other.chunkSize){ - for (Node* c = other.head; c; c = c->next) - for (uint64_t i = 0; i < c->count; ++i) + ChunkedList(const ChunkedList& other) + : head(nullptr), tail(nullptr), length_(0), chunkSize(other.chunkSize) { + for (Node* c = other.head; c; c = c->next) { + for (uint64_t i = 0; i < c->count; ++i) { push_back(c->data[i]); + } + } } - ChunkedList& operator=(const ChunkedList& other){ - if (this != &other){ + ChunkedList& operator=(const ChunkedList& other) { + if (this != &other) { clear(); chunkSize = other.chunkSize; - for (Node* c = other.head; c; c = c->next) - for (uint64_t i = 0; i < c->count; ++i) + for (Node* c = other.head; c; c = c->next) { + for (uint64_t i = 0; i < c->count; ++i) { push_back(c->data[i]); + } + } } return *this; } - void push_back(const T& value){ + void push_back(const T& value) { if (!tail) allocFirst(); if (tail->count == chunkSize) allocChunk(); tail->data[tail->count++] = value; ++length_; } - T pop_front(){ + T pop_front() { if (!head) return T(); T val = head->data[0]; - if (head->count > 1){ - for (uint64_t i = 1; i < head->count; ++i) head->data[i-1] = head->data[i]; + if (head->count > 1) { + for (uint64_t i = 1; i < head->count; ++i) + head->data[i - 1] = head->data[i]; --head->count; } else { Node* old = head; @@ -96,83 +70,87 @@ class ChunkedList{ return val; } - Node* insert_after(Node* node, const T& value){ - if (!node){ + Node* insert_after(Node* node, const T& value) { + if (!node) { push_back(value); return tail; } - if (node->count < chunkSize){ + if (node->count < chunkSize) { node->data[node->count++] = value; ++length_; return node; } - Node* n=allocNode(value); + Node* n = allocNode(value); n->next = node->next; node->next = n; - if (tail ==node) tail = n; + if (tail == node) tail = n; ++length_; return n; } -T remove_node(Node* node){ - if (!node|| !head) return T(); - if (node == head){ - T val = head->data[0]; - uint64_t removed = head->count; - Node* nxt = head->next; - ::free(head, sizeof(Node) + chunkSize * sizeof(T)); - head = nxt; - if(!head) tail = nullptr; + T remove_node(Node* node) { + if (!node || !head) return T(); + if (node == head) { + T val = head->data[0]; + uint64_t removed = head->count; + Node* nxt = head->next; + ::free(head, sizeof(Node) + chunkSize * sizeof(T)); + head = nxt; + if (!head) tail = nullptr; + length_ -= removed; + return val; + } + Node* prev = head; + while (prev->next && prev->next != node) prev = prev->next; + if (prev->next != node) return T(); + T val = node->data[0]; + uint64_t removed = node->count; + prev->next = node->next; + if (tail == node) tail = prev; + ::free(node, sizeof(Node) + chunkSize * sizeof(T)); length_ -= removed; return val; } - Node* prev = head; - while (prev->next && prev->next != node) prev = prev->next; - if (prev->next != node) return T(); - - T val = node->data[0]; - uint64_t removed = node->count; - prev->next = node->next; - if (tail==node) tail = prev; - - ::free(node, sizeof(Node) + chunkSize * sizeof(T)); - length_ -= removed; - return val; -} - void update(Node* node, const T& value){ + void update(Node* node, const T& value) { if (node && node->count) node->data[0] = value; } - void update_at(Node* node, uint64_t offset, const T& value){ + void update_at(Node* node, uint64_t offset, const T& value) { if (node && offset < node->count) node->data[offset] = value; } - uint64_t size() const noexcept{return length_;} + uint64_t size() const noexcept { + return length_; + } - uint64_t size_bytes() const noexcept{ + uint64_t size_bytes() const noexcept { uint64_t nodes = 0; for (Node* it = head; it; it = it->next) ++nodes; return nodes * (sizeof(Node) + chunkSize * sizeof(T)); } - bool empty() const noexcept{return length_ == 0; } + bool empty() const noexcept { + return length_ == 0; + } template - void for_each(Func f) const{ - for (Node* it = head; it; it = it->next) - for (uint64_t i = 0; i < it->count; ++i) + void for_each(Func f) const { + for (Node* it = head; it; it = it->next) { + for (uint64_t i = 0; i < it->count; ++i) { f(it->data[i]); + } + } } private: - Node* head = nullptr; - Node* tail = nullptr; - uint64_t length_ = 0; + Node* head; + Node* tail; + uint64_t length_; uint64_t chunkSize; - Node* allocNode(const T& value){ - uintptr_t raw= ::malloc(sizeof(Node) + chunkSize * sizeof(T)); + Node* allocNode(const T& value) { + uintptr_t raw = ::malloc(sizeof(Node) + chunkSize * sizeof(T)); Node* n = reinterpret_cast(raw); n->count = 1; n->next = nullptr; @@ -180,24 +158,24 @@ T remove_node(Node* node){ return n; } - void allocFirst(){ - uintptr_t raw = ::malloc(sizeof(Node) +chunkSize*sizeof(T)); + void allocFirst() { + uintptr_t raw = ::malloc(sizeof(Node) + chunkSize * sizeof(T)); head = tail = reinterpret_cast(raw); head->count = 0; head->next = nullptr; } - void allocChunk(){ - uintptr_t raw = ::malloc(sizeof(Node)+ chunkSize * sizeof(T)); - Node* n=reinterpret_cast(raw); + void allocChunk() { + uintptr_t raw = ::malloc(sizeof(Node) + chunkSize * sizeof(T)); + Node* n = reinterpret_cast(raw); n->count = 0; n->next = nullptr; tail->next = n; tail = n; } - void clear(){ + void clear() { while (head) pop_front(); } }; -#endif + diff --git a/shared/data_struct/doubly_linked_list.c b/shared/data_struct/doubly_linked_list.c new file mode 100644 index 00000000..d3707a85 --- /dev/null +++ b/shared/data_struct/doubly_linked_list.c @@ -0,0 +1,181 @@ +#include "doubly_linked_list.h" + +cdouble_linked_list_t* cdouble_linked_list_create(void) { + uintptr_t raw = malloc((uint64_t)sizeof(cdouble_linked_list_t)); + if (!raw) return NULL; + cdouble_linked_list_t* list = (cdouble_linked_list_t*)raw; + list->head = list->tail = NULL; + list->length = 0; + return list; +} + +void cdouble_linked_list_destroy(cdouble_linked_list_t* list) { + if (!list) return; + cdouble_linked_list_node_t* node = list->head; + for (uint64_t i = 0; i < list->length; ++i) { + cdouble_linked_list_node_t* next = node->next; + free(node, sizeof(cdouble_linked_list_node_t)); + node = next; + } + free(list, sizeof(cdouble_linked_list_t)); +} + +cdouble_linked_list_t* cdouble_linked_list_clone(const cdouble_linked_list_t* list) { + if (!list) return NULL; + cdouble_linked_list_t* clone = cdouble_linked_list_create(); + if (!clone) return NULL; + cdouble_linked_list_node_t* it = list->head; + for (uint64_t i = 0; i < list->length; ++i) { + uintptr_t raw = malloc(sizeof(cdouble_linked_list_node_t)); + if (raw) { + cdouble_linked_list_node_t* node = (cdouble_linked_list_node_t*)raw; + node->data = it->data; + if (clone->length == 0) { + node->next = node->prev = node; + clone->head = clone->tail = node; + } else { + node->prev = clone->tail; + node->next = clone->head; + clone->tail->next = node; + clone->head->prev = node; + clone->tail = node; + } + ++clone->length; + } + it = it->next; + } + return clone; +} + +void cdouble_linked_list_push_front(cdouble_linked_list_t* list, void* data) { + if (!list) return; + uintptr_t raw = malloc(sizeof(cdouble_linked_list_node_t)); + if (!raw) return; + cdouble_linked_list_node_t* node = (cdouble_linked_list_node_t*)raw; + node->data = data; + if (list->length == 0) { + node->next = node->prev = node; + list->head = list->tail = node; + } else { + node->next = list->head; + node->prev = list->tail; + list->head->prev = node; + list->tail->next = node; + list->head = node; + } + ++list->length; +} + +void cdouble_linked_list_push_back(cdouble_linked_list_t* list, void* data) { + if (!list) return; + uintptr_t raw = malloc(sizeof(cdouble_linked_list_node_t)); + if (!raw) return; + cdouble_linked_list_node_t* node = (cdouble_linked_list_node_t*)raw; + node->data = data; + if (list->length == 0) { + node->next = node->prev = node; + list->head = list->tail = node; + } else { + node->prev = list->tail; + node->next = list->head; + list->tail->next = node; + list->head->prev = node; + list->tail = node; + } + ++list->length; +} + +void* cdouble_linked_list_pop_front(cdouble_linked_list_t* list) { + if (!list || list->length == 0) return NULL; + cdouble_linked_list_node_t* node = list->head; + void* data = node->data; + if (list->length == 1) { + list->head = list->tail = NULL; + } else { + list->head = node->next; + list->head->prev = list->tail; + list->tail->next = list->head; + } + --list->length; + free(node, sizeof(cdouble_linked_list_node_t)); + return data; +} + +void* cdouble_linked_list_pop_back(cdouble_linked_list_t* list) { + if (!list || list->length == 0) return NULL; + cdouble_linked_list_node_t* node = list->tail; + void* data = node->data; + if (list->length == 1) { + list->head = list->tail = NULL; + } else { + list->tail = node->prev; + list->tail->next = list->head; + list->head->prev = list->tail; + } + --list->length; + free(node, sizeof(cdouble_linked_list_node_t)); + return data; +} + +cdouble_linked_list_node_t* cdouble_linked_list_insert_after(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* data) { + if (!list) return NULL; + if (!node) { + cdouble_linked_list_push_front(list, data); + return list->head; + } + uintptr_t raw = malloc(sizeof(cdouble_linked_list_node_t)); + if (!raw) return NULL; + cdouble_linked_list_node_t* new_node = (cdouble_linked_list_node_t*)raw; + new_node->data = data; + new_node->next = node->next; + new_node->prev = node; + node->next->prev = new_node; + node->next = new_node; + if (list->tail == node) list->tail = new_node; + ++list->length; + return new_node; +} + +cdouble_linked_list_node_t* cdouble_linked_list_insert_before(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* data) { + if (!list) return NULL; + if (!node) { + cdouble_linked_list_push_back(list, data); + return list->tail; + } + return cdouble_linked_list_insert_after(list, node->prev, data); +} + +void* cdouble_linked_list_remove(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node) { + if (!list || !node || list->length == 0) return NULL; + if (node == list->head) return cdouble_linked_list_pop_front(list); + if (node == list->tail) return cdouble_linked_list_pop_back(list); + node->prev->next = node->next; + node->next->prev = node->prev; + void* data = node->data; + --list->length; + free(node, sizeof(cdouble_linked_list_node_t)); + return data; +} + +void cdouble_linked_list_update(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* new_data) { + (void)list; + if (node) node->data = new_data; +} + +uint64_t cdouble_linked_list_length(const cdouble_linked_list_t* list) { + return list ? list->length : 0; +} + +uint64_t cdouble_linked_list_size_bytes(const cdouble_linked_list_t* list) { + return list ? list->length * sizeof(cdouble_linked_list_node_t) : 0; +} + +cdouble_linked_list_node_t* cdouble_linked_list_find(cdouble_linked_list_t* list, void* key, int (*cmp)(void*, void*)) { + if (!list || !cmp || list->length == 0) return NULL; + cdouble_linked_list_node_t* it = list->head; + for (uint64_t i = 0; i < list->length; ++i) { + if (cmp(it->data, key) == 0) return it; + it = it->next; + } + return NULL; +} diff --git a/shared/data_struct/doubly_linked_list.cpp b/shared/data_struct/doubly_linked_list.cpp deleted file mode 100644 index 2b01d051..00000000 --- a/shared/data_struct/doubly_linked_list.cpp +++ /dev/null @@ -1,195 +0,0 @@ -#include "doubly_linked_list.hpp" - -cdouble_linked_list_t* cdouble_linked_list_create(void){ - uintptr_t raw = malloc((uint64_t)sizeof(cdouble_linked_list_t));//i believe that casting sizeof is better until size_t is defined correctly - if (raw == 0) return NULL; - - cdouble_linked_list_t *list = (cdouble_linked_list_t*)raw; - list->head = list->tail = NULL; - list->length = 0; - return list; -} - -void cdouble_linked_list_destroy(cdouble_linked_list_t *list){ - if (!list) return; - - cdouble_linked_list_node_t *node=list->head; - for (uint64_t i = 0; i < list->length; ++i){ - cdouble_linked_list_node_t *next = node->next; - free(node,(uint64_t)sizeof(cdouble_linked_list_node_t)); - node = next; - } - free(list, (uint64_t)sizeof(cdouble_linked_list_t)); -} - -cdouble_linked_list_t* cdouble_linked_list_clone(const cdouble_linked_list_t *list){ //could create data aliasing. be careful. TODO - if (!list) return NULL; - - cdouble_linked_list_t *clone=cdouble_linked_list_create(); - if (!clone) return NULL; - - cdouble_linked_list_node_t *it =list->head; - for (uint64_t i = 0; ilength; ++i){ - uintptr_t raw =malloc((uint64_t)sizeof(cdouble_linked_list_node_t)); - if (raw) { - cdouble_linked_list_node_t *node =(cdouble_linked_list_node_t*)raw; - node->data = it->data; - - if (clone->length == 0){ - node->next=node->prev = node; - clone->head=clone->tail = node; - } else { - node->prev=clone->tail; - node->next=clone->head; - clone->tail->next=node; - clone->head->prev= node; - clone->tail =node; - } - ++clone->length; - } - it = it->next; - } - return clone; -} - -void cdouble_linked_list_push_front(cdouble_linked_list_t *list, void *data){ - if (!list) return; - uintptr_t raw= malloc((uint64_t)sizeof(cdouble_linked_list_node_t)); - if (raw == 0) return; - cdouble_linked_list_node_t *node=(cdouble_linked_list_node_t*)raw; - node->data = data; - - if (list->length == 0){ - node->next=node->prev = node; - list->head=list->tail = node; - } else { - node->next=list->head; - node->prev=list->tail; - list->head->prev= node; - list->tail->next= node; - list->head= node; - } - ++list->length; -} - -void cdouble_linked_list_push_back(cdouble_linked_list_t* list, void* data){ - if (!list) return; - uintptr_t raw=malloc((uint64_t)sizeof(cdouble_linked_list_node_t)); - if (raw ==0) return; - cdouble_linked_list_node_t* node=(cdouble_linked_list_node_t*)raw; - node->data = data; - - if (list->length==0){ - node->next=node->prev=node; - list->head=list->tail = node; - } else { - node->prev= list->tail; - node->next=list->head; - list->tail->next=node; - list->head->prev=node; - list->tail =node; - } - ++list->length; -} - -void* cdouble_linked_list_pop_front(cdouble_linked_list_t* list){ - if (!list||list->length==0) return NULL; - - cdouble_linked_list_node_t* node=list->head; - void* data=node->data; - if (list->length == 1){ - list->head=list->tail=NULL; - } else { - list->head=node->next; - list->head->prev =list->tail; - list->tail->next=list->head; - } - --list->length; - free(node,(uint64_t)sizeof(cdouble_linked_list_node_t)); - return data; -} - -void* cdouble_linked_list_pop_back(cdouble_linked_list_t* list){ - if (!list||list->length==0) return NULL; - - cdouble_linked_list_node_t* node=list->tail; - void* data=node->data; - if (list->length==1){ - list->head = list->tail=NULL; - } else { - list->tail=node->prev; - list->tail->next=list->head; - list->head->prev=list->tail; - } - --list->length; - free(node, (uint64_t)sizeof(cdouble_linked_list_node_t)); - return data; -} - -cdouble_linked_list_node_t* cdouble_linked_list_insert_after(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* data){ - if (!list) return NULL; - if (!node){ - cdouble_linked_list_push_front(list, data); - return list->head; - } - - uintptr_t raw=malloc((uint64_t)sizeof(cdouble_linked_list_node_t)); - if (raw==0) return NULL; - - cdouble_linked_list_node_t* new_node = (cdouble_linked_list_node_t*)raw; - new_node->data = data; - new_node->next = node->next; - new_node->prev = node; - node->next->prev = new_node; - node->next = new_node; - if (list->tail == node) list->tail = new_node; - ++list->length; - return new_node; -} - -cdouble_linked_list_node_t* cdouble_linked_list_insert_before(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* data){ - if (!list) return NULL; - - if (!node){ - cdouble_linked_list_push_back(list, data); - return list->tail; - } - return cdouble_linked_list_insert_after(list, node->prev, data); -} - -void* cdouble_linked_list_remove(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node){ - if (!list|| !node|| list->length==0) return NULL; - - if (node==list->head) return cdouble_linked_list_pop_front(list); - if (node==list->tail) return cdouble_linked_list_pop_back(list); - node->prev->next = node->next; - node->next->prev = node->prev; - void* data = node->data; - --list->length; - free(node,(uint64_t)sizeof(cdouble_linked_list_node_t)); - return data; -} - -void cdouble_linked_list_update(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* new_data){ - (void)list; - if (node) node->data=new_data; -} - -uint64_t cdouble_linked_list_length(const cdouble_linked_list_t* list){ - return list?list-> length:0; -} - -uint64_t cdouble_linked_list_size_bytes(const cdouble_linked_list_t* list){ - return list?list-> length * sizeof(cdouble_linked_list_node_t):0; -} - -cdouble_linked_list_node_t* cdouble_linked_list_find(cdouble_linked_list_t* list, void* key, int (*cmp)(void*,void*)){ - if (!list||!cmp|| list->length==0) return NULL; - - cdouble_linked_list_node_t* it=list->head; - for (uint64_t i=0; ilength; ++i){ - if (cmp(it->data,key)==0) return it; - it = it->next; - } - return NULL; -} diff --git a/shared/data_struct/doubly_linked_list.h b/shared/data_struct/doubly_linked_list.h new file mode 100644 index 00000000..aee049f5 --- /dev/null +++ b/shared/data_struct/doubly_linked_list.h @@ -0,0 +1,40 @@ +#pragma once +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct cdouble_linked_list_node { + void* data; + struct cdouble_linked_list_node* next; + struct cdouble_linked_list_node* prev; +} cdouble_linked_list_node_t; + +typedef struct cdouble_linked_list { + cdouble_linked_list_node_t* head; + cdouble_linked_list_node_t* tail; + uint64_t length; +} cdouble_linked_list_t; + +extern uintptr_t malloc(uint64_t size); +extern void free(void* ptr, uint64_t size); + +cdouble_linked_list_t* cdouble_linked_list_create(void); +void cdouble_linked_list_destroy(cdouble_linked_list_t* list); +cdouble_linked_list_t* cdouble_linked_list_clone(const cdouble_linked_list_t* list); +void cdouble_linked_list_push_front(cdouble_linked_list_t* list, void* data); +void cdouble_linked_list_push_back(cdouble_linked_list_t* list, void* data); +void* cdouble_linked_list_pop_front(cdouble_linked_list_t* list); +void* cdouble_linked_list_pop_back(cdouble_linked_list_t* list); +cdouble_linked_list_node_t* cdouble_linked_list_insert_after(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* data); +cdouble_linked_list_node_t* cdouble_linked_list_insert_before(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* data); +void* cdouble_linked_list_remove(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node); +void cdouble_linked_list_update(cdouble_linked_list_t* list, cdouble_linked_list_node_t* node, void* new_data); +uint64_t cdouble_linked_list_length(const cdouble_linked_list_t* list); +uint64_t cdouble_linked_list_size_bytes(const cdouble_linked_list_t* list); +cdouble_linked_list_node_t* cdouble_linked_list_find(cdouble_linked_list_t* list, void* key, int (*cmp)(void*, void*)); + +#ifdef __cplusplus +} +#endif diff --git a/shared/data_struct/doubly_linked_list.hpp b/shared/data_struct/doubly_linked_list.hpp index 15791ca1..2438e01d 100644 --- a/shared/data_struct/doubly_linked_list.hpp +++ b/shared/data_struct/doubly_linked_list.hpp @@ -1,90 +1,48 @@ #pragma once #include "types.h" -#ifdef __cplusplus extern "C" { -#endif - -typedef struct cdouble_linked_list_node{ - void *data; - struct cdouble_linked_list_node *next; - struct cdouble_linked_list_node *prev; -}cdouble_linked_list_node_t; - -typedef struct cdouble_linked_list{ - cdouble_linked_list_node_t *head; - cdouble_linked_list_node_t *tail; - uint64_t length; -}cdouble_linked_list_t; - - -extern uintptr_t malloc(uint64_t size); -extern void free(void *ptr, uint64_t size); - - -cdouble_linked_list_t *cdouble_linked_list_create(void); -void cdouble_linked_list_destroy(cdouble_linked_list_t *list); -cdouble_linked_list_t *cdouble_linked_list_clone(const cdouble_linked_list_t *list); -void cdouble_linked_list_push_front(cdouble_linked_list_t *list, void *data); -void cdouble_linked_list_push_back(cdouble_linked_list_t *list, void *data); -void *cdouble_linked_list_pop_front(cdouble_linked_list_t *list); -void *cdouble_linked_list_pop_back(cdouble_linked_list_t *list); -cdouble_linked_list_node_t *cdouble_linked_list_insert_after(cdouble_linked_list_t *list, cdouble_linked_list_node_t *node, void *data); -cdouble_linked_list_node_t *cdouble_linked_list_insert_before(cdouble_linked_list_t *list, cdouble_linked_list_node_t *node, void *data); -void *cdouble_linked_list_remove(cdouble_linked_list_t *list, cdouble_linked_list_node_t *node); -void cdouble_linked_list_update(cdouble_linked_list_t *list, cdouble_linked_list_node_t *node, void *new_data); -uint64_t cdouble_linked_list_length(const cdouble_linked_list_t *list); -uint64_t cdouble_linked_list_size_bytes(const cdouble_linked_list_t *list); -cdouble_linked_list_node_t* cdouble_linked_list_find(cdouble_linked_list_t *list, void *key, int (*cmp)(void *, void *)); - -#ifdef __cplusplus +#include "doubly_linked_list.h" } template -class LinkedList{ +class LinkedList { private: - struct Node{ + struct Node { T data; - Node *next; - Node *prev; + Node* next; + Node* prev; }; - Node *head; - Node *tail; + Node* head; + Node* tail; uint64_t length; - Node* alloc_node(const T& value){ - uintptr_t raw= malloc((uint64_t)sizeof(Node)); + Node* alloc_node(const T& value) { + uintptr_t raw = malloc(sizeof(Node)); if (raw == 0) return nullptr; - - Node* n = (Node*)raw; + Node* n = reinterpret_cast(raw); n->data = value; n->next = n->prev = nullptr; return n; } - void free_node(Node* n){ + void free_node(Node* n) { if (!n) return; - free(n,(uint64_t)sizeof(Node)); + free(n, sizeof(Node)); } - static void swap(LinkedList& a, LinkedList& b) noexcept{ - Node* tmp_head = a.head; - a.head = b.head; - b.head = tmp_head; - Node* tmp_tail = a.tail; - a.tail = b.tail; - b.tail = tmp_tail; - uint64_t tmp_length = a.length; - a.length = b.length; - b.length = tmp_length; + static void swap(LinkedList& a, LinkedList& b) noexcept { + std::swap(a.head, b.head); + std::swap(a.tail, b.tail); + std::swap(a.length, b.length); } public: - LinkedList() : head(nullptr), tail(nullptr), length(0){} + LinkedList() : head(nullptr), tail(nullptr), length(0) {} - LinkedList(const LinkedList& other):head(nullptr), tail(nullptr), length(0){ - if (other.head){ + LinkedList(const LinkedList& other) : head(nullptr), tail(nullptr), length(0) { + if (other.head) { Node* it = other.head; do { push_back(it->data); @@ -93,11 +51,11 @@ class LinkedList{ } } - ~LinkedList(){ + ~LinkedList() { while (!empty()) pop_front(); } - LinkedList& operator=(const LinkedList& other){ + LinkedList& operator=(const LinkedList& other) { if (this != &other) { LinkedList tmp(other); swap(*this, tmp); @@ -105,11 +63,10 @@ class LinkedList{ return *this; } - void push_front(const T& value){ + void push_front(const T& value) { Node* n = alloc_node(value); - if( !n) return; - - if (!head){ + if (!n) return; + if (!head) { head = tail = n; n->next = n->prev = n; } else { @@ -122,11 +79,10 @@ class LinkedList{ ++length; } - void push_back(const T& value){ + void push_back(const T& value) { Node* n = alloc_node(value); if (!n) return; - - if (!tail){ + if (!tail) { head = tail = n; n->next = n->prev = n; } else { @@ -139,13 +95,12 @@ class LinkedList{ ++length; } - T pop_front(){ + T pop_front() { if (!head) return T(); - Node* n = head; T val = n->data; - if (head == tail){ - head = tail=nullptr; + if (head == tail) { + head = tail = nullptr; } else { head = head->next; head->prev = tail; @@ -156,12 +111,11 @@ class LinkedList{ return val; } - T pop_back(){ + T pop_back() { if (!tail) return T(); - Node* n = tail; T val = n->data; - if (head == tail){ + if (head == tail) { head = tail = nullptr; } else { tail = tail->prev; @@ -173,37 +127,34 @@ class LinkedList{ return val; } - Node* insert_after(Node* node, const T& value){ - if (!node){ + Node* insert_after(Node* node, const T& value) { + if (!node) { push_front(value); return head; } - Node* n = alloc_node(value); if (!n) return nullptr; - - n->next=node->next; - n->prev=node; + n->next = node->next; + n->prev = node; node->next->prev = n; - node->next= n; + node->next = n; if (tail == node) tail = n; ++length; return n; } - Node* insert_before(Node* node, const T& value){ - if (!node){ + Node* insert_before(Node* node, const T& value) { + if (!node) { push_back(value); return tail; } return insert_after(node->prev, value); } - T remove(Node* node){ + T remove(Node* node) { if (!node) return T(); if (node == head) return pop_front(); if (node == tail) return pop_back(); - node->prev->next = node->next; node->next->prev = node->prev; T val = node->data; @@ -212,31 +163,39 @@ class LinkedList{ return val; } - void update(Node* node, const T& value){ - if (!node) return; - node->data = value; + void update(Node* node, const T& value) { + if (node) node->data = value; } - uint64_t size()const{return length;} - bool empty() const{return length==0;} - Node* begin() const{return head;} - Node* end() const{return nullptr;} + uint64_t size() const { + return length; + } + + bool empty() const { + return length == 0; + } + + Node* begin() const { + return head; + } + + Node* end() const { + return nullptr; + } template - Node* find(Predicate pred) const{ + Node* find(Predicate pred) const { if (!head) return nullptr; - Node* it = head; do { if (pred(it->data)) return it; it = it->next; } while (it != head); - return nullptr; } template - void for_each(Func func) const{ + void for_each(Func func) const { if (!head) return; Node* it = head; do { @@ -245,4 +204,4 @@ class LinkedList{ } while (it != head); } }; -#endif + diff --git a/shared/data_struct/linked_list.cpp b/shared/data_struct/linked_list.c similarity index 72% rename from shared/data_struct/linked_list.cpp rename to shared/data_struct/linked_list.c index 8012bdc2..0ffdd600 100644 --- a/shared/data_struct/linked_list.cpp +++ b/shared/data_struct/linked_list.c @@ -1,14 +1,8 @@ -#include "linked_list.hpp" -#include "types.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "linked_list.h" clinkedlist_t *clinkedlist_create(void){ uintptr_t mem = malloc(sizeof(clinkedlist_t)); - if ((void *)mem == NULL) return NULL; - + if((void *)mem == NULL) return NULL; clinkedlist_t *list = (clinkedlist_t *)mem; list->head = NULL; list->tail = NULL; @@ -17,10 +11,9 @@ clinkedlist_t *clinkedlist_create(void){ } void clinkedlist_destroy(clinkedlist_t *list){ - if (list == NULL) return; - + if(list == NULL) return; clinkedlist_node_t *node = list->head; - while (node){ + while(node){ clinkedlist_node_t *next = node->next; free(node, sizeof(clinkedlist_node_t)); node = next; @@ -29,14 +22,12 @@ void clinkedlist_destroy(clinkedlist_t *list){ } clinkedlist_t *clinkedlist_clone(const clinkedlist_t *list){ - if (list == NULL) return NULL; - + if(list == NULL) return NULL; clinkedlist_t *clone = clinkedlist_create(); - if (clone == NULL) return NULL; - + if(clone == NULL) return NULL; clinkedlist_node_t *it = list->head; - while (it){ - if (clone->tail){ + while(it){ + if(clone->tail){ clinkedlist_node_t *new_node = (clinkedlist_node_t *)malloc(sizeof(clinkedlist_node_t)); new_node->data = it->data; new_node->next = NULL; @@ -52,33 +43,29 @@ clinkedlist_t *clinkedlist_clone(const clinkedlist_t *list){ } void clinkedlist_push_front(clinkedlist_t *list, void *data){ - if (list == NULL) return; - + if(list == NULL) return; clinkedlist_node_t *node = (clinkedlist_node_t *)malloc(sizeof(clinkedlist_node_t)); node->data = data; node->next = list->head; list->head = node; - if (list->tail == NULL) list->tail = node; + if(list->tail == NULL) list->tail = node; list->length++; } void *clinkedlist_pop_front(clinkedlist_t *list){ - if (list == NULL || list->head== NULL) return NULL; - + if(list == NULL || list->head == NULL) return NULL; clinkedlist_node_t *node = list->head; void *data = node->data; list->head = node->next; if (list->head == NULL) list->tail = NULL; - list->length--; free(node, sizeof(clinkedlist_node_t)); return data; } -clinkedlist_node_t *clinkedlist_insert_after(clinkedlist_t *list, clinkedlist_node_t *node, void *data) { - if (list == NULL) return NULL; - - if (node == NULL){ +clinkedlist_node_t *clinkedlist_insert_after(clinkedlist_t *list, clinkedlist_node_t *node, void *data){ + if(list == NULL) return NULL; + if(node == NULL){ clinkedlist_push_front(list, data); return list->head; } @@ -86,26 +73,23 @@ clinkedlist_node_t *clinkedlist_insert_after(clinkedlist_t *list, clinkedlist_no new_node->data = data; new_node->next = node->next; node->next = new_node; - if (list->tail == node) list->tail = new_node; - + if(list->tail == node) list->tail = new_node; list->length++; return new_node; } void *clinkedlist_remove(clinkedlist_t *list, clinkedlist_node_t *node){ - if (list == NULL || node == NULL || list->head == NULL) return NULL; - - if (node == list->head){ + if(list == NULL || node == NULL || list->head == NULL) return NULL; + if(node == list->head){ return clinkedlist_pop_front(list); } clinkedlist_node_t *prev = list->head; - while (prev->next && prev->next != node){ + while(prev->next && prev->next != node){ prev = prev->next; } - if (prev->next != node) return NULL; - + if(prev->next != node) return NULL; prev->next = node->next; - if (node == list->tail) list->tail = prev; + if(node == list->tail) list->tail = prev; void *data = node->data; list->length--; free(node, sizeof(clinkedlist_node_t)); @@ -114,7 +98,7 @@ void *clinkedlist_remove(clinkedlist_t *list, clinkedlist_node_t *node){ void clinkedlist_update(clinkedlist_t *list, clinkedlist_node_t *node, void *new_data){ (void)list; - if (node) node->data = new_data; + if(node) node->data = new_data; } uint64_t clinkedlist_length(const clinkedlist_t *list){ @@ -126,26 +110,20 @@ uint64_t clinkedlist_size_bytes(const clinkedlist_t *list){ } clinkedlist_node_t *clinkedlist_find(clinkedlist_t *list, void *key, int (*cmp)(void *, void *)){ - if (list == NULL || cmp == NULL) return NULL; - + if(list == NULL || cmp == NULL) return NULL; clinkedlist_node_t *it = list->head; - while (it){ - if (cmp(it->data, key) == 0) return it; + while(it){ + if(cmp(it->data, key) == 0) return it; it = it->next; } return NULL; } void clinkedlist_for_each(const clinkedlist_t *list, void (*func)(void *)){ - if (list == NULL || func == NULL) return; - + if(list == NULL || func == NULL) return; clinkedlist_node_t *it = list->head; - while (it){ + while(it){ func(it->data); it = it->next; } } - -#ifdef __cplusplus -} -#endif diff --git a/shared/data_struct/linked_list.h b/shared/data_struct/linked_list.h new file mode 100644 index 00000000..4e27a8eb --- /dev/null +++ b/shared/data_struct/linked_list.h @@ -0,0 +1,37 @@ +#pragma once +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct clinkedlist_node { + void *data; + struct clinkedlist_node *next; +} clinkedlist_node_t; + +typedef struct clinkedlist { + clinkedlist_node_t *head; + clinkedlist_node_t *tail; + uint64_t length; +} clinkedlist_t; + +extern uintptr_t malloc(uint64_t size); +extern void free(void *ptr, uint64_t size); + +clinkedlist_t *clinkedlist_create(void); +void clinkedlist_destroy(clinkedlist_t *list); +clinkedlist_t *clinkedlist_clone(const clinkedlist_t *list); +void clinkedlist_push_front(clinkedlist_t *list, void *data); +void *clinkedlist_pop_front(clinkedlist_t *list); +clinkedlist_node_t *clinkedlist_insert_after(clinkedlist_t *list, clinkedlist_node_t *node, void *data); +void *clinkedlist_remove(clinkedlist_t *list, clinkedlist_node_t *node); +void clinkedlist_update(clinkedlist_t *list, clinkedlist_node_t *node, void *new_data); +uint64_t clinkedlist_length(const clinkedlist_t *list); +uint64_t clinkedlist_size_bytes(const clinkedlist_t *list); +clinkedlist_node_t *clinkedlist_find(clinkedlist_t *list, void *key, int (*cmp)(void *, void *)); +void clinkedlist_for_each(const clinkedlist_t *list, void (*func)(void *)); + +#ifdef __cplusplus +} +#endif diff --git a/shared/data_struct/linked_list.hpp b/shared/data_struct/linked_list.hpp index 2167ddfd..b4781fb1 100644 --- a/shared/data_struct/linked_list.hpp +++ b/shared/data_struct/linked_list.hpp @@ -1,94 +1,58 @@ #pragma once #include "types.h" -#ifdef __cplusplus extern "C" { -#endif - -typedef struct clinkedlist_node{ - void *data; - struct clinkedlist_node *next; -}clinkedlist_node_t; - -typedef struct clinkedlist{ - clinkedlist_node_t *head; - clinkedlist_node_t *tail; - uint64_t length; -}clinkedlist_t; - - -extern uintptr_t malloc(uint64_t size); -extern void free(void *ptr, uint64_t size); - - -clinkedlist_t *clinkedlist_create(void); -void clinkedlist_destroy(clinkedlist_t *list); -clinkedlist_t *clinkedlist_clone(const clinkedlist_t *list); -void clinkedlist_push_front(clinkedlist_t *list,void *data); -void *clinkedlist_pop_front(clinkedlist_t *list); -clinkedlist_node_t *clinkedlist_insert_after(clinkedlist_t *list, clinkedlist_node_t *node, void *data); -void *clinkedlist_remove(clinkedlist_t *list, clinkedlist_node_t *node); -void clinkedlist_update(clinkedlist_t *list, clinkedlist_node_t *node, void *new_data); -uint64_t clinkedlist_length(const clinkedlist_t *list); -uint64_t clinkedlist_size_bytes(const clinkedlist_t *list); -clinkedlist_node_t *clinkedlist_find(clinkedlist_t *list, void *key,int (*cmp)(void *, void *));//so not const function doesnt rise an annoyng warning -void clinkedlist_for_each(const clinkedlist_t *list, void (*func)(void *)); - -#ifdef __cplusplus +#include "linked_list.h" } -template class LinkedList{ +template +class LinkedList { private: - struct Node{ + struct Node { T data; - Node *next; + Node* next; }; - Node *head = nullptr; - Node *tail = nullptr; + Node* head = nullptr; + Node* tail = nullptr; uint64_t length = 0; - Node *alloc_node(const T &value){ + Node* alloc_node(const T& value) { uintptr_t mem = malloc(sizeof(Node)); - Node *n = reinterpret_cast(mem); + Node* n = reinterpret_cast(mem); n->data = value; n->next = nullptr; return n; } - void free_node(Node *n){ + void free_node(Node* n) { free(n, sizeof(Node)); } public: LinkedList() = default; - LinkedList(const LinkedList &other){ - for (Node *it = other.head; it; it = it->next){ + LinkedList(const LinkedList& other) { + for (Node* it = other.head; it; it = it->next) { push_front(it->data); } LinkedList tmp; - while (!empty()){ - tmp.push_front(pop_front()); - } + while (!empty()) tmp.push_front(pop_front()); *this = tmp; } - ~LinkedList(){ + ~LinkedList() { while (!empty()) pop_front(); } - LinkedList &operator=(const LinkedList &other){ - if (this != &other){ + LinkedList& operator=(const LinkedList& other) { + if (this != &other) { while (!empty()) pop_front(); - for (Node *it = other.head; it; it = it->next){ + for (Node* it = other.head; it; it = it->next) { push_front(it->data); } - LinkedList tmp; - while (!empty()){ - tmp.push_front(pop_front()); - } + while (!empty()) tmp.push_front(pop_front()); head = tmp.head; tail = tmp.tail; length = tmp.length; @@ -98,32 +62,31 @@ template class LinkedList{ return *this; } - void push_front(const T &value){ - Node *n = alloc_node(value); + void push_front(const T& value) { + Node* n = alloc_node(value); n->next = head; head = n; - if (tail == nullptr) tail = n; + if (!tail) tail = n; ++length; } - T pop_front(){ - Node *n = head; + T pop_front() { + if (!head) return T(); + Node* n = head; head = head->next; - if (head == nullptr) tail = nullptr; - + if (!head) tail = nullptr; T val = n->data; free_node(n); --length; return val; } - Node *insert_after(Node *node, const T &value){ - if (node == nullptr){ + Node* insert_after(Node* node, const T& value) { + if (!node) { push_front(value); return head; } - - Node *n = alloc_node(value); + Node* n = alloc_node(value); n->next = node->next; node->next = n; if (tail == node) tail = n; @@ -131,13 +94,12 @@ template class LinkedList{ return n; } - T remove(Node *node){ - if (node == nullptr) return T(); + T remove(Node* node) { + if (!node) return T(); if (node == head) return pop_front(); - - Node *prev = head; + Node* prev = head; while (prev && prev->next != node) prev = prev->next; - if (prev == nullptr) return T(); + if (!prev) return T(); prev->next = node->next; if (node == tail) tail = prev; T val = node->data; @@ -146,22 +108,32 @@ template class LinkedList{ return val; } - void update(Node *node, const T &value){ + void update(Node* node, const T& value) { if (node) node->data = value; } - uint64_t size() const{return length;} - bool empty() const{return length == 0; } - Node *begin() const{ return head; } - Node *end() const{ return nullptr;} - template + uint64_t size() const { + return length; + } + + bool empty() const { + return length == 0; + } - Node *find(Predicate pred) const{ - for (Node *it = head; it; it = it->next){ + Node* begin() const { + return head; + } + + Node* end() const { + return nullptr; + } + + template + Node* find(Predicate pred) const { + for (Node* it = head; it; it = it->next) { if (pred(it->data)) return it; } return nullptr; } }; -#endif diff --git a/shared/data_struct/queue.c b/shared/data_struct/queue.c new file mode 100644 index 00000000..957ea22d --- /dev/null +++ b/shared/data_struct/queue.c @@ -0,0 +1,69 @@ +#include "queue.h" +#include "std/memfunctions.h" + +void cqueue_init(CQueue* q, uint64_t max_capacity, uint64_t elem_size) { + q->buffer = NULL; + q->capacity = max_capacity; + q->max_capacity = max_capacity; + q->elem_size = elem_size; + q->head = q->tail = q->length = 0; + if (max_capacity > 0) { + uintptr_t b = malloc(max_capacity * elem_size); + if (b) q->buffer = (void*)b; + } +} + +int32_t cqueue_enqueue(CQueue* q, const void* item) { + if (!q) return 0; + if (q->max_capacity > 0) { + if (q->length == q->capacity) return 0; + } else { + if (q->length == q->capacity) { + uint64_t nc = q->capacity > 0 ? q->capacity * 2 : 4; + uintptr_t nb = malloc(nc * q->elem_size); + if (!nb) return 0; + void* newb = (void*)nb; + for (uint64_t i = 0; i < q->length; ++i) { + uint64_t idx = (q->tail + i) % q->capacity; + memcpy((uint8_t*)newb + i * q->elem_size, + (uint8_t*)q->buffer + idx * q->elem_size, + q->elem_size); + } + if (q->buffer) free(q->buffer, q->capacity * q->elem_size); + q->buffer = newb; + q->capacity = nc; + q->head = q->length; + q->tail = 0; + } + } + memcpy((uint8_t*)q->buffer + q->head * q->elem_size, item, q->elem_size); + q->head = (q->head + 1) % q->capacity; + q->length++; + return 1; +} + +int32_t cqueue_dequeue(CQueue* q, void* out) { + if (!q || q->length == 0) return 0; + memcpy(out, (uint8_t*)q->buffer + q->tail * q->elem_size, q->elem_size); + q->tail = (q->tail + 1) % q->capacity; + q->length--; + return 1; +} + +int32_t cqueue_is_empty(const CQueue* q) { + return (!q || q->length == 0) ? 1 : 0; +} + +uint64_t cqueue_size(const CQueue* q) { + return q ? q->length : 0; +} + +void cqueue_clear(CQueue* q) { + if (!q) return; + q->head = q->tail = q->length = 0; +} + +void cqueue_destroy(CQueue* q) { + if (!q) return; + if (q->buffer) free(q->buffer, q->capacity * q->elem_size); +} diff --git a/shared/data_struct/queue.cpp b/shared/data_struct/queue.cpp deleted file mode 100644 index 16374b06..00000000 --- a/shared/data_struct/queue.cpp +++ /dev/null @@ -1,72 +0,0 @@ - -#include "queue.hpp" -#include "std/memfunctions.h" - -void cqueue_init(CQueue* q,uint64_t max_capacity,uint64_t elem_size){ - q->buffer = NULL; - q->capacity = max_capacity; - q->max_capacity = max_capacity; - q->elem_size = elem_size; - q->head = q->tail = q->length = 0; - if (max_capacity > 0){ - uintptr_t b = malloc(max_capacity*elem_size); - if (b) q->buffer = (void*)b; - } -} - -int32_t cqueue_enqueue(CQueue* q,const void* item){ - if (!q) return 0; - if (q->max_capacity>0){ - if (q->length == q->capacity) return 0; - } else { - if (q->length == q->capacity){ - uint64_t nc = q->capacity>0 ? q->capacity*2 : 4; - uintptr_t nb = malloc(nc*q->elem_size); - if (!nb) return 0; - - void* newb = (void*)nb; - for (uint64_t i=0; ilength; ++i){ - uint64_t idx = (q->tail + i)%q->capacity; - memcpy((uint8_t*)newb + i*q->elem_size, - (uint8_t*)q->buffer + idx*q->elem_size, - q->elem_size); - } - if (q->buffer) free(q->buffer,q->capacity*q->elem_size); - q->buffer=newb; - q->capacity=nc; - q->head=q->length; - q->tail=0; - } - } - memcpy((uint8_t*)q->buffer + q->head*q->elem_size, item,q->elem_size); - q->head = (q->head+1)%q->capacity; - q->length++; - return 1; -} - -int32_t cqueue_dequeue(CQueue* q,void* out){ - if (!q || q->length == 0) return 0; - - memcpy(out,(uint8_t*)q->buffer + q->tail*q->elem_size,q->elem_size); - q->tail = (q->tail+1)%q->capacity; - q->length--; - return 1; -} - -int32_t cqueue_is_empty(const CQueue* q){ - return (!q || q->length==0) ? 1 : 0; -} - -uint64_t cqueue_size(const CQueue* q){ - return q ? q->length : 0; -} - -void cqueue_clear(CQueue* q){ - if (!q) return; - q->head=q->tail=q->length=0; -} - -void cqueue_destroy(CQueue* q){ - if (!q) return; - if (q->buffer) free(q->buffer,q->capacity*q->elem_size); -} diff --git a/shared/data_struct/queue.h b/shared/data_struct/queue.h new file mode 100644 index 00000000..531d3089 --- /dev/null +++ b/shared/data_struct/queue.h @@ -0,0 +1,31 @@ +#pragma once +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct CQueue { + void* buffer; + uint64_t capacity; // current queue size + uint64_t max_capacity; // 0 = infinite + uint64_t elem_size; + uint64_t head; + uint64_t tail; + uint64_t length; +} CQueue; + +extern uintptr_t malloc(uint64_t); +extern void free(void*, uint64_t); + +void cqueue_init(CQueue* q, uint64_t max_capacity, uint64_t elem_size); +int32_t cqueue_enqueue(CQueue* q, const void* item); +int32_t cqueue_dequeue(CQueue* q, void* out); +int32_t cqueue_is_empty(const CQueue* q); +uint64_t cqueue_size(const CQueue* q); +void cqueue_clear(CQueue* q); +void cqueue_destroy(CQueue* q); + +#ifdef __cplusplus +} +#endif diff --git a/shared/data_struct/queue.hpp b/shared/data_struct/queue.hpp index 7f0bbe8f..fe02fb9c 100644 --- a/shared/data_struct/queue.hpp +++ b/shared/data_struct/queue.hpp @@ -1,48 +1,87 @@ #pragma once #include "types.h" +#include "std/string.h" +#include "std/memfunctions.h" +#include "queue.h" -#ifdef __cplusplus -extern "C" { -#endif +template +class Queue { +public: + explicit Queue(uint64_t capacity = 0) { + max_capacity = capacity; + this->capacity = capacity; + if (capacity > 0) { + uintptr_t mem = malloc(capacity * sizeof(T)); + if (mem) { + buffer = reinterpret_cast(mem); + } + } + } -typedef struct CQueue { - void* buffer; - uint64_t capacity; //curremt queue size - uint64_t max_capacity; //0 is infinite - uint64_t elem_size; - uint64_t head; - uint64_t tail; - uint64_t length; -}CQueue; + ~Queue() { + if (buffer) { + free(buffer, capacity * sizeof(T)); + } + } + bool enqueue(const T& value) { + if (max_capacity > 0 && length == capacity) return false; + if (max_capacity == 0 && length == capacity) grow_if_needed(); + if (!buffer) return false; -extern uintptr_t malloc(uint64_t); -extern void free(void*,uint64_t); + buffer[head] = value; + head = (head + 1) % capacity; + ++length; + return true; + } + bool dequeue(T& out) { + if (length == 0 || !buffer) return false; + out = buffer[tail]; + tail = (tail + 1) % capacity; + --length; + return true; + } -void cqueue_init(CQueue* q,uint64_t max_capacity,uint64_t elem_size); -int32_t cqueue_enqueue(CQueue* q,const void* item); -int32_t cqueue_dequeue(CQueue* q,void* out); -int32_t cqueue_is_empty(const CQueue* q); -uint64_t cqueue_size(const CQueue* q); -void cqueue_clear(CQueue* q); -void cqueue_destroy(CQueue* q); + bool is_empty() const { + return length == 0; + } -#ifdef __cplusplus -} + uint64_t size() const { + return length; + } -template -class Queue { - CQueue q_; + void clear() { + head = tail = length = 0; + } -public: - explicit Queue(uint64_t cap=0){cqueue_init(&q_,cap,sizeof(T));} - ~Queue(){ cqueue_destroy(&q_); } - bool enqueue(const T& v){return cqueue_enqueue(&q_,&v);} - bool dequeue(T& out){return cqueue_dequeue(&q_,&out);} - bool isEmpty() const{return cqueue_is_empty(&q_);} - uint64_t size() const{return cqueue_size(&q_);} - void clear(){cqueue_clear(&q_);} +private: + T* buffer = nullptr; + uint64_t capacity = 0; + uint64_t max_capacity = 0; // 0 infinite + uint64_t head = 0; + uint64_t tail = 0; + uint64_t length = 0; + + void grow_if_needed() { + uint64_t new_cap = (capacity > 0) ? capacity * 2 : 4; + uintptr_t new_mem = malloc(new_cap * sizeof(T)); + if (!new_mem) return; + + T* new_buf = reinterpret_cast(new_mem); + for (uint64_t i = 0; i < length; ++i) { + uint64_t idx = (tail + i) % capacity; + new_buf[i] = buffer[idx]; + } + + if (buffer) { + free(buffer, capacity * sizeof(T)); + } + + buffer = new_buf; + capacity = new_cap; + tail = 0; + head = length; + } }; -#endif diff --git a/shared/data_struct/ring_buffer.cpp b/shared/data_struct/ring_buffer.c similarity index 53% rename from shared/data_struct/ring_buffer.cpp rename to shared/data_struct/ring_buffer.c index a1e7e946..67429799 100644 --- a/shared/data_struct/ring_buffer.cpp +++ b/shared/data_struct/ring_buffer.c @@ -1,28 +1,26 @@ -#include "types.h" -#include "ring_buffer.hpp" +#include "ring_buffer.h" #include "std/memfunctions.h" -void cring_init(struct CRingBuffer* rb, void* storage, uint64_t capacity, uint64_t elem_size){ +void cring_init(CRingBuffer* rb, void* storage, uint64_t capacity, uint64_t elem_size) { rb->buffer = storage; rb->capacity = capacity; rb->element_size = elem_size; - rb->head = 0; - rb->tail = 0; + rb->head = rb->tail = 0; rb->full = 0; } -uint64_t cring_capacity(const struct CRingBuffer* rb){ +uint64_t cring_capacity(const CRingBuffer* rb) { return rb->capacity; } -int32_t cring_push(struct CRingBuffer* rb, const void* item){ +int32_t cring_push(CRingBuffer* rb, const void* item) { if (rb->full) return 0; uint8_t* base = (uint8_t*)rb->buffer; void* dest = base + (rb->head * rb->element_size); - for (uint32_t i = 0; i < rb->element_size; ++i){ - ((uint8_t*)dest)[i] =((const uint8_t*)item)[i]; + for (uint64_t i = 0; i < rb->element_size; ++i) { + ((uint8_t*)dest)[i] = ((const uint8_t*)item)[i]; } rb->head = (rb->head + 1) % rb->capacity; @@ -30,13 +28,13 @@ int32_t cring_push(struct CRingBuffer* rb, const void* item){ return 1; } -int32_t cring_pop(struct CRingBuffer* rb, void* out){ +int32_t cring_pop(CRingBuffer* rb, void* out) { if (!rb->full && (rb->head == rb->tail)) return 0; uint8_t* base = (uint8_t*)rb->buffer; void* src = base + (rb->tail * rb->element_size); - for (uint64_t i = 0; i < rb->element_size; ++i){ + for (uint64_t i = 0; i < rb->element_size; ++i) { ((uint8_t*)out)[i] = ((uint8_t*)src)[i]; } @@ -45,16 +43,15 @@ int32_t cring_pop(struct CRingBuffer* rb, void* out){ return 1; } -int32_t cring_is_empty(const struct CRingBuffer* rb){ +int32_t cring_is_empty(const CRingBuffer* rb) { return (!rb->full && (rb->head == rb->tail)); } -int32_t cring_is_full(const struct CRingBuffer* rb){ +int32_t cring_is_full(const CRingBuffer* rb) { return rb->full; } -void cring_clear(struct CRingBuffer* rb){ - rb->head = 0; - rb->tail = 0; +void cring_clear(CRingBuffer* rb) { + rb->head = rb->tail = 0; rb->full = 0; } diff --git a/shared/data_struct/ring_buffer.h b/shared/data_struct/ring_buffer.h new file mode 100644 index 00000000..958b9da5 --- /dev/null +++ b/shared/data_struct/ring_buffer.h @@ -0,0 +1,27 @@ +#pragma once +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct CRingBuffer { + void* buffer; + uint64_t capacity; + uint64_t element_size; + uint64_t head; + uint64_t tail; + int32_t full; +} CRingBuffer; + +void cring_init(CRingBuffer* rb, void* storage, uint64_t capacity, uint64_t elem_size); +int32_t cring_push(CRingBuffer* rb, const void* item); +int32_t cring_pop(CRingBuffer* rb, void* out); +int32_t cring_is_empty(const CRingBuffer* rb); +int32_t cring_is_full(const CRingBuffer* rb); +void cring_clear(CRingBuffer* rb); +uint64_t cring_capacity(const CRingBuffer* rb); + +#ifdef __cplusplus +} +#endif diff --git a/shared/data_struct/ring_buffer.hpp b/shared/data_struct/ring_buffer.hpp index e7e8c983..c798f23f 100644 --- a/shared/data_struct/ring_buffer.hpp +++ b/shared/data_struct/ring_buffer.hpp @@ -1,29 +1,8 @@ #pragma once #include "types.h" -extern "C" { -struct CRingBuffer { - void* buffer; - uint64_t capacity; //TODO: define size_t as the same size of os architecture - uint64_t element_size; - uint64_t head; - uint64_t tail; - int32_t full; -}; - -void cring_init(struct CRingBuffer* rb, void* storage, uint64_t capacity, uint64_t elem_size); -int32_t cring_push(struct CRingBuffer* rb, const void* item); -int32_t cring_pop(struct CRingBuffer* rb, void* out); -int32_t cring_is_empty(const struct CRingBuffer* rb); -int32_t cring_is_full(const struct CRingBuffer* rb); -void cring_clear(struct CRingBuffer* rb); -uint64_t cring_capacity(const struct CRingBuffer* rb); - -} - template -class RingBuffer{ - +class RingBuffer { private: T data[Capacity]; uint64_t head = 0; @@ -31,56 +10,57 @@ class RingBuffer{ int32_t full = 0; public: - int32_t push(const T& item){ + RingBuffer() : head(0), tail(0), full(0) {} + + int32_t push(const T& item) { if (full) return 0; - data[head] = item; head = (head + 1) % Capacity; full = (head == tail); return 1; } - int32_t pop(T& out){ + int32_t pop(T& out) { if (is_empty()) return 0; - out = data[tail]; - tail = (tail + 1)% Capacity; + tail = (tail + 1) % Capacity; full = 0; return 1; } - int32_t is_empty() const{ - return (!full && (head == tail)); + int32_t is_empty() const { + return (!full && head == tail); } - int32_t is_full() const{ + int32_t is_full() const { return full; } - void clear(){ + void clear() { head = tail = 0; full = 0; } - uint64_t size() const{ + uint64_t size() const { if (full) return Capacity; - if (head >= tail) return head - tail; - return Capacity + head - tail; } - const T& peek() const{ + static constexpr uint64_t capacity() { + return Capacity; + } + + const T& peek() const { return data[tail]; } - const T& at(uint32_t index) const{ + const T& at(uint32_t index) const { return data[(tail + index) % Capacity]; } - T& at(uint32_t index){ - return data[(tail + index)% Capacity]; + T& at(uint32_t index) { + return data[(tail + index) % Capacity]; } - - static constexpr uint64_t capacity(){ return Capacity; } }; + From 0d2d55f73d8fb89406a4586bb062c4134985740d Mon Sep 17 00:00:00 2001 From: CodeAnarchist Date: Sat, 26 Jul 2025 00:20:31 +0200 Subject: [PATCH 33/33] rem xhci types h --- kernel/input/xhci_types.h | 378 -------------------------------------- 1 file changed, 378 deletions(-) delete mode 100644 kernel/input/xhci_types.h diff --git a/kernel/input/xhci_types.h b/kernel/input/xhci_types.h deleted file mode 100644 index 34484fac..00000000 --- a/kernel/input/xhci_types.h +++ /dev/null @@ -1,378 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include "types.h" - -#define TRB_TYPE_NORMAL 1 -#define TRB_TYPE_LINK 6 -#define TRB_TYPE_EVENT_DATA 3 -#define TRB_TYPE_ENABLE_SLOT 9 -#define TRB_TYPE_ADDRESS_DEV 11 -#define TRB_TYPE_CONFIG_EP 12 -#define TRB_TYPE_SETUP 2 -#define TRB_TYPE_STATUS 4 -#define TRB_TYPE_INPUT 8 - -#define MAX_TRB_AMOUNT 256 -#define MAX_ERST_AMOUNT 1 - -#define TRB_TYPE_MASK 0xFC00 -#define TRB_ENDPOINT_MASK 0xF0000 -#define TRB_SLOT_MASK 0xF000000 - -#define TRB_TYPE_TRANSFER 0x20 -#define TRB_TYPE_COMMAND_COMPLETION 0x21 -#define TRB_TYPE_PORT_STATUS_CHANGE 0x22 - -#define TRB_TYPE_SETUP_STAGE 0x2 -#define TRB_TYPE_DATA_STAGE 0x3 -#define TRB_TYPE_STATUS_STAGE 0x4 - -#define XHCI_USBSTS_HSE (1 << 2) -#define XHCI_USBSTS_CE (1 << 12) - -#define XHCI_IRQ 31 - -typedef struct { - uint64_t parameter; - uint32_t status; - uint32_t control; -}__attribute__((packed)) trb; - -typedef struct { - uint8_t caplength; - uint8_t reserved; - uint16_t hciversion; - uint32_t hcsparams1; - uint32_t hcsparams2; - uint32_t hcsparams3; - uint32_t hccparams1; - uint32_t dboff; - uint32_t rtsoff; - uint32_t hccparams2; -}__attribute__((packed)) xhci_cap_regs; - -typedef struct { - uint32_t usbcmd; - uint32_t usbsts; - uint32_t pagesize; - uint64_t reserved0; - uint32_t dnctrl; - uint64_t crcr; - uint32_t reserved1[4]; - uint64_t dcbaap; - uint32_t config; -}__attribute__((packed)) xhci_op_regs; - -typedef union { - struct { - uint32_t ccs : 1; - uint32_t ped : 1; - uint32_t rsvd0 : 1; - uint32_t oca : 1; - uint32_t pr : 1; - uint32_t pls : 4; - uint32_t pp : 1; - uint32_t port_speed : 4; - uint32_t pic : 2; - uint32_t lws : 1; - uint32_t csc : 1; - uint32_t pec : 1; - uint32_t wrc : 1; - uint32_t occ : 1; - uint32_t prc : 1; - uint32_t plc : 1; - uint32_t cec : 1; - uint32_t cas : 1; - uint32_t wce : 1; - uint32_t wde : 1; - uint32_t woe : 1; - uint32_t rsvd1 : 2; - uint32_t dr : 1; - uint32_t wpr : 1; - }; - uint32_t value; -} portstatuscontrol; - -typedef struct { - portstatuscontrol portsc; - uint32_t portpmsc; - uint32_t portli; - uint32_t rsvd; -}__attribute__((packed, aligned(4))) xhci_port_regs; - -typedef struct { - uint32_t iman; - uint32_t imod; - uint32_t erstsz; - uint32_t reserved; - uint64_t erstba; - uint64_t erdp; -}__attribute__((packed)) xhci_interrupter; - -typedef struct { - uint64_t mmio; - uint64_t mmio_size; - xhci_cap_regs* cap; - xhci_op_regs* op; - xhci_port_regs* ports; - uint64_t db_base; - uint64_t rt_base; - trb* cmd_ring; - uint32_t cmd_index; - uint32_t event_index; - bool command_cycle_bit; - bool event_cycle_bit; - trb* event_ring; - uint8_t* key_buffer; - xhci_interrupter* interrupter; - uint64_t* dcbaa; - uint16_t max_device_slots; - uint16_t max_ports; -} xhci_device; - -typedef struct { - uint64_t ring_base; - uint32_t ring_size; - uint32_t reserved; -}__attribute__((packed)) erst_entry; - -typedef struct { - uint32_t drop_flags; - uint32_t add_flags; - uint64_t reserved[3]; -}__attribute__((packed)) xhci_input_control_context; - -typedef union -{ - struct - { - uint32_t route_string: 20; - uint32_t speed: 4; - uint32_t rsvd : 1; - uint32_t mtt : 1; - uint32_t hub : 1; - uint32_t context_entries : 5; - }; - uint32_t value; -} slot_field0; - -typedef union -{ - struct - { - uint16_t max_exit_latency; - uint8_t root_hub_port_num; - uint8_t port_count; - }; - uint32_t value; -} slot_field1; - -typedef union -{ - struct - { - uint32_t parent_hub_slot_id : 8; - uint32_t parent_port_number : 8; - uint32_t think_time : 2; - uint32_t rsvd : 4; - uint32_t interrupt_target : 10; - }; - uint32_t value; -} slot_field2; - -typedef union -{ - struct - { - uint32_t device_address : 8; - uint32_t rsvd : 19; - uint32_t state : 5;//0 disabled, 1 default, 2 addressed, 3 configured - }; - uint32_t value; -} slot_field3; - -typedef union -{ - struct { - uint32_t endpoint_state : 3; - uint32_t rsvd0 : 5; - uint32_t mult : 2; - uint32_t max_primary_streams : 5; - uint32_t linear_stream_array : 1; - uint32_t interval : 8; - uint32_t max_esit_payload_hi : 8; - }; - uint32_t value; -} endpoint_field0; - -typedef union -{ - struct { - uint32_t rsvd1 : 1; - uint32_t error_count : 2; - uint32_t endpoint_type : 3; - uint32_t rsvd2 : 1; - uint32_t host_initiate_disable : 1; - uint32_t max_burst_size : 8; - uint32_t max_packet_size : 16; - }; - uint32_t value; -} endpoint_field1; - -typedef union { - struct { - uint64_t dcs : 1; - uint64_t rsvd0 : 3; - uint64_t ring_ptr : 60; - }; - uint64_t value; -} endpoint_field23; - -typedef union -{ - struct { - uint16_t average_trb_length; - uint16_t max_esit_payload_lo; - }; - uint32_t value; -} endpoint_field4; - -typedef struct { - slot_field0 slot_f0; - slot_field1 slot_f1; - slot_field2 slot_f2; - slot_field3 slot_f3; - uint32_t slot_rsvd[4]; - struct { - endpoint_field0 endpoint_f0; - endpoint_field1 endpoint_f1; - endpoint_field23 endpoint_f23; - endpoint_field4 endpoint_f4; - uint32_t ep_rsvd[3]; - } endpoints[31]; -} xhci_device_context; - -typedef struct { - xhci_input_control_context control_context; - xhci_device_context device_context; -} xhci_input_context; - -typedef struct __attribute__((packed)) { - uint8_t bmRequestType; - uint8_t bRequest; - uint16_t wValue; - uint16_t wIndex; - uint16_t wLength; -} usb_setup_packet; - -typedef struct __attribute__((packed)) { - uint8_t bLength; - uint8_t bDescriptorType; -} usb_descriptor_header ; - -typedef struct __attribute__((packed)) { - usb_descriptor_header header; - uint16_t bcdUSB; - uint8_t bDeviceClass; - uint8_t bDeviceSubClass; - uint8_t bDeviceProtocol; - uint8_t bMaxPacketSize0; - uint16_t idVendor; - uint16_t idProduct; - uint16_t bcdDevice; - uint8_t iManufacturer; - uint8_t iProduct; - uint8_t iSerialNumber; - uint8_t bNumConfigurations; -} usb_device_descriptor; - -typedef struct __attribute__((packed)) { - usb_descriptor_header header; - uint16_t wTotalLength; - uint8_t bNumInterfaces; - uint8_t bConfigurationValue; - uint8_t iConfiguration; - uint8_t bmAttributes; - uint8_t bMaxPower; - uint8_t data[255]; -} usb_configuration_descriptor; - -typedef struct __attribute__((packed)) { - usb_descriptor_header header; - uint8_t bInterfaceNumber; - uint8_t bAlternateSetting; - uint8_t bNumEndpoints; - uint8_t bInterfaceClass; - uint8_t bInterfaceSubClass; - uint8_t bInterfaceProtocol; - uint8_t iInterface; -} usb_interface_descriptor; - -typedef struct __attribute__((packed)) { - usb_descriptor_header header; - uint16_t bcdHID; - uint8_t bCountryCode; - uint8_t bNumDescriptors; - struct { - uint8_t bDescriptorType; - uint8_t wDescriptorLength; - }__attribute__((packed)) descriptors[1]; -//TODO: wDescriptorLength is supposed to be 16, but for some reason the descriptor from usb-kbd is 8. Will need to fix this once we do a real device -} usb_hid_descriptor; - -typedef struct __attribute__((packed)) { - usb_descriptor_header header; - uint8_t bEndpointAddress; - uint8_t bmAttributes; - uint8_t wMaxPacketSize; - uint8_t bInterval; -//TODO: wMaxPacketSize is supposed to be 16, but for some reason the descriptor from usb-kbd is 8. Will need to fix this once we do a real device -} usb_endpoint_descriptor; - -typedef struct __attribute__((packed)) { - usb_descriptor_header header; - uint16_t lang_ids[126]; -} usb_string_language_descriptor; - -typedef struct __attribute__((packed)){ - usb_descriptor_header header; - uint16_t unicode_string[126]; -} usb_string_descriptor; - -typedef enum { - NONE, - KEYBOARD, - MOUSE -} xhci_device_types; - -typedef struct { - uint8_t transfer_cycle_bit; - uint32_t transfer_index; - trb* transfer_ring; - uint32_t slot_id; - xhci_input_context* ctx; -} xhci_usb_device; - -typedef struct { - xhci_device_types type; - trb* endpoint_transfer_ring; - uint32_t endpoint_transfer_index; - uint8_t endpoint_transfer_cycle_bit; - uint8_t poll_packetSize; - uint8_t *input_buffer; - uint8_t poll_endpoint; - uint16_t report_length; - uint8_t *report_descriptor; - } xhci_usb_device_endpoint; - -#define USB_DEVICE_DESCRIPTOR 1 -#define USB_CONFIGURATION_DESCRIPTOR 2 -#define USB_STRING_DESCRIPTOR 3 - -#ifdef __cplusplus -} -#endif \ No newline at end of file