switched to llvm
This commit is contained in:
parent
0460c68628
commit
3fb0c6e5a6
64
Makefile
64
Makefile
@ -1,38 +1,35 @@
|
||||
CROSS_PATH = ~/opt/cross/bin
|
||||
AS = nasm
|
||||
CC = $(CROSS_PATH)/x86_64-elf-gcc
|
||||
LD = $(CROSS_PATH)/x86_64-elf-ld
|
||||
DAS = $(CROSS_PATH)/x86_64-elf-objdump
|
||||
CC = clang
|
||||
LD = clang
|
||||
DAS = llvm-objdump
|
||||
|
||||
INCLUDE_DIR = include
|
||||
MK = mk
|
||||
OUT = out
|
||||
|
||||
C_WARNINGS = -Wall \
|
||||
-Werror \
|
||||
-Wextra \
|
||||
-Wpedantic \
|
||||
-Winit-self \
|
||||
-Wunused-parameter \
|
||||
-Wuninitialized \
|
||||
-Wfloat-equal \
|
||||
-Wshadow \
|
||||
-Wcast-qual \
|
||||
-Wcast-align \
|
||||
-Wstrict-prototypes \
|
||||
-Wpointer-arith \
|
||||
-Wno-comment
|
||||
C_IGNORED_WARNINGS = -Wno-cast-align \
|
||||
-Wno-padded
|
||||
|
||||
C_FLAGS = -std=c11 \
|
||||
-g \
|
||||
-c \
|
||||
-O2 \
|
||||
C_FLAGS = -xc\
|
||||
-g \
|
||||
-c \
|
||||
-O2 \
|
||||
-std=c11 \
|
||||
-Weverything \
|
||||
-Werror \
|
||||
$(C_IGNORED_WARNINGS) \
|
||||
-ffreestanding \
|
||||
-fno-pic \
|
||||
-mcmodel=kernel \
|
||||
-fno-stack-protector \
|
||||
-ffreestanding \
|
||||
-mno-red-zone \
|
||||
$(C_WARNINGS) \
|
||||
$(addprefix -I, $(INCLUDE_DIR))
|
||||
-mno-mmx \
|
||||
-mno-sse \
|
||||
-mno-sse2 \
|
||||
-mno-sse3 \
|
||||
-mno-3dnow \
|
||||
-target x86_64-elf \
|
||||
-I$(INCLUDE_DIR)
|
||||
|
||||
AS_FLAGS = -w+all \
|
||||
-w+error \
|
||||
@ -41,14 +38,19 @@ AS_FLAGS = -w+all \
|
||||
-g \
|
||||
$(addprefix -I, $(INCLUDE_DIR)/)
|
||||
|
||||
LD_FLAGS = -nostdlib \
|
||||
--fatal-warnings
|
||||
LD_FLAGS = -fuse-ld=lld \
|
||||
-nostdlib \
|
||||
-Wl,-T,$(LD_SCRIPT) \
|
||||
-Wl,--fatal-warnings
|
||||
|
||||
DUMP_FLAGS = -M intel \
|
||||
-d
|
||||
DUMP_FLAGS = -x86-asm-syntax=intel \
|
||||
-disassemble \
|
||||
-r \
|
||||
-t \
|
||||
-triple=x86_64-elf
|
||||
|
||||
PREP_FLAGS = -E \
|
||||
-x c \
|
||||
-xc\
|
||||
-P \
|
||||
$(C_FLAGS)
|
||||
|
||||
@ -59,7 +61,7 @@ GDEP_FLAGS = $(PREP_FLAGS) \
|
||||
MKDIR = mkdir -p $(dir $@)
|
||||
COMP = $(CC) $(C_FLAGS) -o $@ $<
|
||||
COMPAS = $(AS) $(AS_FLAGS) -o $@ $<
|
||||
LINK = $(LD) $(LD_FLAGS) -o $@ $^ $(shell $(CC) $(C_FLAGS) -print-libgcc-file-name)
|
||||
LINK = $(LD) $(LD_FLAGS) -o $@ $^
|
||||
DUMP = $(DAS) $(DUMP_FLAGS) $< > $@
|
||||
PREP = $(CC) $(PREP_FLAGS) $< > $@
|
||||
GDEP = $(CC) $(GDEP_FLAGS) -MF $(addsuffix .d, $@) $< > /dev/null
|
||||
|
@ -20,7 +20,7 @@ TGT := $(OUT)/secxkrnl.elf
|
||||
DMP := $(OUT)/secxkrnl.dmp
|
||||
|
||||
$(TGT): $(OBJ) $(LD_SCRIPT)
|
||||
$(LINK) -T $(LD_SCRIPT)
|
||||
$(LINK)
|
||||
|
||||
$(DMP): $(TGT)
|
||||
$(DUMP)
|
||||
|
16
hal/boot.c
16
hal/boot.c
@ -6,23 +6,23 @@
|
||||
#include "hal/boot.h"
|
||||
#include "status.h"
|
||||
|
||||
static void SXAPI halp_obtain_cpu_info(boot_info_t *hal_info)
|
||||
static void SXAPI halp_obtain_cpu_info(struct boot_info *hal_info)
|
||||
{
|
||||
if (hal_info == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
|
||||
uint32 eax = 0, ebx = 0, ecx = 0, edx = 0;
|
||||
hal_cpuid(&eax, &ebx, &ecx, &edx);
|
||||
lb_mem_copy(&ebx, &hal_info->cpu_vd_str[0], sizeof(uint32_t));
|
||||
lb_mem_copy(&edx, &hal_info->cpu_vd_str[4], sizeof(uint32_t));
|
||||
lb_mem_copy(&ecx, &hal_info->cpu_vd_str[8], sizeof(uint32_t));
|
||||
lb_mem_copy(&ebx, &hal_info->cpu_vd_str[0], sizeof(uint32));
|
||||
lb_mem_copy(&edx, &hal_info->cpu_vd_str[4], sizeof(uint32));
|
||||
lb_mem_copy(&ecx, &hal_info->cpu_vd_str[8], sizeof(uint32));
|
||||
hal_info->cpu_vd_str[12] = 0;
|
||||
}
|
||||
|
||||
status_t SXAPI hal_init(void *m_info)
|
||||
sx_status SXAPI hal_init(void *m_info)
|
||||
{
|
||||
if (m_info == NULL || (uint64_t) m_info & lb_bit_field_mask(0, 2))
|
||||
if (m_info == NULL || (uint64) m_info & lb_bit_field_mask(0, 2))
|
||||
{
|
||||
return STATUS_FAIL;
|
||||
}
|
||||
@ -32,7 +32,7 @@ status_t SXAPI hal_init(void *m_info)
|
||||
hal_mem_init();
|
||||
|
||||
|
||||
boot_info_t *boot_info = halloc(sizeof(boot_info_t));
|
||||
struct boot_info *boot_info = halloc(sizeof(struct boot_info));
|
||||
|
||||
// obtain cpu info
|
||||
halp_obtain_cpu_info(boot_info);
|
||||
|
16
hal/cpu.asm
16
hal/cpu.asm
@ -76,7 +76,7 @@ mov cr8,rdi
|
||||
ret
|
||||
|
||||
; ============================
|
||||
; int32_t KAPI hal_interlocked_exchange_32(int32_t *target, int32_t val)
|
||||
; int32 KAPI hal_interlocked_exchange_32(int32 *target, int32 val)
|
||||
global hal_interlocked_exchange_32
|
||||
hal_interlocked_exchange_32:
|
||||
lock xchg dword [rdi], esi
|
||||
@ -85,7 +85,7 @@ mov eax, esi
|
||||
ret
|
||||
|
||||
; ============================
|
||||
; int32_t KAPI hal_interlocked_compare_exchange_32(int32_t *dst, int32_t compare, int32_t val);
|
||||
; int32 KAPI hal_interlocked_compare_exchange_32(int32 *dst, int32 compare, int32 val);
|
||||
global hal_interlocked_compare_exchange_32
|
||||
hal_interlocked_compare_exchange_32:
|
||||
mov eax, esi; eax = compare
|
||||
@ -93,7 +93,7 @@ lock cmpxchg dword [rdi], edx ; edx = val, rdi = ptr to dst
|
||||
ret
|
||||
|
||||
; ============================
|
||||
; int32_t KAPI hal_interlocked_increment_32(int32_t *target, int32_t increment);
|
||||
; int32 KAPI hal_interlocked_increment_32(int32 *target, int32 increment);
|
||||
global hal_interlocked_increment_32
|
||||
hal_interlocked_increment_32:
|
||||
lock xadd dword [rdi], esi ; [rdi] = [rdi] + esi, esi = old [rdi]
|
||||
@ -102,7 +102,7 @@ mov eax, esi
|
||||
ret
|
||||
|
||||
; ============================
|
||||
; extern void KAPI hal_cpuid(uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t* edx);
|
||||
; extern void KAPI hal_cpuid(uint32* eax, uint32* ebx, uint32* ecx, uint32* edx);
|
||||
global hal_cpuid
|
||||
hal_cpuid:
|
||||
push rbp
|
||||
@ -196,14 +196,14 @@ ret
|
||||
|
||||
;====================
|
||||
global hal_write_mem_32
|
||||
; (void* target, uint32_t* data)
|
||||
; (void* target, uint32* data)
|
||||
hal_write_mem_32:
|
||||
mov dword [rdi], esi
|
||||
ret
|
||||
|
||||
;====================
|
||||
global hal_write_mem_64
|
||||
; (void* target, uint64_t data)
|
||||
; (void* target, u64 data)
|
||||
hal_write_mem_64:
|
||||
mov qword [rdi], rsi
|
||||
ret
|
||||
@ -228,7 +228,7 @@ hlt
|
||||
jmp .loop
|
||||
|
||||
;====================
|
||||
;(uint32_t *ecx, uint32_t* edx, uint32_t* eax)
|
||||
;(uint32 *ecx, uint32* edx, uint32* eax)
|
||||
global hal_read_msr
|
||||
hal_read_msr:
|
||||
; preserve rdx
|
||||
@ -242,7 +242,7 @@ mov dword [r11], eax
|
||||
ret
|
||||
|
||||
;====================
|
||||
;(uint32_t *ecx, uint32_t* edx, uint32_t* eax)
|
||||
;(uint32 *ecx, uint32* edx, uint32* eax)
|
||||
global hal_write_msr
|
||||
hal_write_msr:
|
||||
mov ecx, dword [rdi]
|
||||
|
115
hal/intr.c
115
hal/intr.c
@ -1,114 +1,108 @@
|
||||
|
||||
#include "type.h"
|
||||
#include "kernel/hal/intr.h"
|
||||
#include "hal/cpu.h"
|
||||
#include "hal/intr.h"
|
||||
#include "hal/print.h"
|
||||
#include "hal/mem.h"
|
||||
#include "lib/sxtdlib.h"
|
||||
|
||||
static uint8_t _idts[HAL_CORE_COUNT][IDT_ENTRY_NUM * IDT_ENTRY_SIZE];
|
||||
static hal_idt_ptr_t _idt_ptrs[HAL_CORE_COUNT];
|
||||
static intr_handler_t _intr_handler_table[HAL_CORE_COUNT][IDT_ENTRY_NUM];
|
||||
static uint8 _idts[HAL_CORE_COUNT][IDT_ENTRY_NUM * IDT_ENTRY_SIZE];
|
||||
static struct hal_idt_ptr _idt_ptrs[HAL_CORE_COUNT];
|
||||
static intr_handler _intr_handler_table[HAL_CORE_COUNT][IDT_ENTRY_NUM];
|
||||
static void *_intr_handler_context_table[HAL_CORE_COUNT][IDT_ENTRY_NUM];
|
||||
static exc_handler_t _exc_handler_table[HAL_CORE_COUNT][IDT_ENTRY_NUM];
|
||||
static exc_handler _exc_handler_table[HAL_CORE_COUNT][IDT_ENTRY_NUM];
|
||||
|
||||
irql_t SXAPI hal_set_irql(irql_t irql)
|
||||
k_irql SXAPI hal_set_irql(k_irql irql)
|
||||
{
|
||||
UNREFERENCED(irql)
|
||||
hal_assert(false, "Unimplemented function called.");
|
||||
hal_assert(FALSE, "Unimplemented function called.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
irql_t SXAPI hal_get_irql(void)
|
||||
k_irql SXAPI hal_get_irql(void)
|
||||
{
|
||||
hal_assert(false, "Unimplemented function called.");
|
||||
hal_assert(FALSE, "Unimplemented function called.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void SXAPI hal_write_gate(void *const gate,
|
||||
uint64_t const offset,
|
||||
uint32_t const selector,
|
||||
uint32_t const attr)
|
||||
uint64 const offset,
|
||||
uint32 const selector,
|
||||
uint32 const attr)
|
||||
{
|
||||
((uint8_t *) gate)[0] = (uint8_t) (offset & 0xFF);
|
||||
((uint8_t *) gate)[1] = (uint8_t) ((offset >> 8) & 0xFF);
|
||||
((uint8_t *) gate)[2] = (uint8_t) (selector & 0xFF);
|
||||
((uint8_t *) gate)[3] = (uint8_t) ((selector >> 8) & 0xFF);
|
||||
((uint8_t *) gate)[4] = (uint8_t) (attr & 0xFF);
|
||||
((uint8_t *) gate)[5] = (uint8_t) ((attr >> 8) & 0xFF);
|
||||
((uint8_t *) gate)[6] = (uint8_t) ((offset >> 16) & 0xFF);
|
||||
((uint8_t *) gate)[7] = (uint8_t) ((offset >> 24) & 0xFF);
|
||||
((uint8_t *) gate)[8] = (uint8_t) ((offset >> 32) & 0xFF);
|
||||
((uint8_t *) gate)[9] = (uint8_t) ((offset >> 40) & 0xFF);
|
||||
((uint8_t *) gate)[10] = (uint8_t) ((offset >> 48) & 0xFF);
|
||||
((uint8_t *) gate)[11] = (uint8_t) ((offset >> 56) & 0xFF);
|
||||
((uint8_t *) gate)[12] = 0;
|
||||
((uint8_t *) gate)[13] = 0;
|
||||
((uint8_t *) gate)[14] = 0;
|
||||
((uint8_t *) gate)[15] = 0;
|
||||
return;
|
||||
((uint8 *) gate)[0] = (uint8) (offset & 0xFF);
|
||||
((uint8 *) gate)[1] = (uint8) ((offset >> 8) & 0xFF);
|
||||
((uint8 *) gate)[2] = (uint8) (selector & 0xFF);
|
||||
((uint8 *) gate)[3] = (uint8) ((selector >> 8) & 0xFF);
|
||||
((uint8 *) gate)[4] = (uint8) (attr & 0xFF);
|
||||
((uint8 *) gate)[5] = (uint8) ((attr >> 8) & 0xFF);
|
||||
((uint8 *) gate)[6] = (uint8) ((offset >> 16) & 0xFF);
|
||||
((uint8 *) gate)[7] = (uint8) ((offset >> 24) & 0xFF);
|
||||
((uint8 *) gate)[8] = (uint8) ((offset >> 32) & 0xFF);
|
||||
((uint8 *) gate)[9] = (uint8) ((offset >> 40) & 0xFF);
|
||||
((uint8 *) gate)[10] = (uint8) ((offset >> 48) & 0xFF);
|
||||
((uint8 *) gate)[11] = (uint8) ((offset >> 56) & 0xFF);
|
||||
((uint8 *) gate)[12] = 0;
|
||||
((uint8 *) gate)[13] = 0;
|
||||
((uint8 *) gate)[14] = 0;
|
||||
((uint8 *) gate)[15] = 0;
|
||||
}
|
||||
|
||||
void SXAPI hal_set_interrupt_handler(uint64_t index,
|
||||
void SXAPI hal_set_interrupt_handler(uint64 index,
|
||||
void (*handler)(void))
|
||||
{
|
||||
if (index < IDT_ENTRY_NUM)
|
||||
{
|
||||
hal_write_gate(_idts[hal_get_core_id()] + 16 * index, (uint64_t) handler, seg_selector(1, 0),
|
||||
hal_write_gate(_idts[hal_get_core_id()] + 16 * index, (uintptr) handler, seg_selector(1, 0),
|
||||
GATE_DPL_0 | GATE_PRESENT | GATE_TYPE_INTERRUPT);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void SXAPI hal_issue_interrupt(uint32_t target_core, uint32_t vector)
|
||||
void SXAPI hal_issue_interrupt(uint32 target_core, uint32 vector)
|
||||
{
|
||||
UNREFERENCED(target_core);
|
||||
UNREFERENCED(vector);
|
||||
hal_assert(false, "Unimplemented function called.");
|
||||
return;
|
||||
hal_assert(FALSE, "Unimplemented function called.");
|
||||
}
|
||||
|
||||
void SXAPI hal_register_interrupt_handler(uint32_t coreid, uint32_t index, intr_handler_t handler, void *context)
|
||||
void SXAPI hal_register_interrupt_handler(uint32 coreid, uint32 index, intr_handler handler, void *context)
|
||||
{
|
||||
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
|
||||
{
|
||||
_intr_handler_table[coreid][index] = handler;
|
||||
_intr_handler_context_table[coreid][index] = context;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void SXAPI hal_deregister_interrupt_handler(uint32_t coreid, uint32_t index)
|
||||
void SXAPI hal_deregister_interrupt_handler(uint32 coreid, uint32 index)
|
||||
{
|
||||
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
|
||||
{
|
||||
_intr_handler_table[coreid][index] = NULL;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void SXAPI hal_register_exception_handler(uint32_t coreid, uint32_t index, exc_handler_t handler)
|
||||
void SXAPI hal_register_exception_handler(uint32 coreid, uint32 index, exc_handler handler)
|
||||
{
|
||||
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
|
||||
{
|
||||
_exc_handler_table[coreid][index] = handler;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void SXAPI hal_deregister_exception_handler(uint32_t coreid, uint32_t index)
|
||||
void SXAPI hal_deregister_exception_handler(uint32 coreid, uint32 index)
|
||||
{
|
||||
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
|
||||
{
|
||||
_exc_handler_table[coreid][index] = NULL;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void SXAPI hal_interrupt_dispatcher(uint64_t int_vec, hal_interrupt_context_t *context)
|
||||
void SXAPI hal_interrupt_dispatcher(uint64 int_vec, hal_interrupt_context_t *context)
|
||||
{
|
||||
uint32_t coreid = hal_get_core_id();
|
||||
uint32 coreid = hal_get_core_id();
|
||||
if (_intr_handler_table[int_vec] == NULL)
|
||||
{
|
||||
hal_printf("Unhandled interrupt %d at 0x%X.\n", int_vec, context->rip);
|
||||
@ -117,12 +111,11 @@ void SXAPI hal_interrupt_dispatcher(uint64_t int_vec, hal_interrupt_context_t *c
|
||||
{
|
||||
_intr_handler_table[coreid][int_vec](context, _intr_handler_context_table[coreid][int_vec]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void SXAPI hal_exception_dispatcher(uint64_t exc_vec, hal_interrupt_context_t *context, uint64_t errorcode)
|
||||
void SXAPI hal_exception_dispatcher(uint64 exc_vec, hal_interrupt_context_t *context, uint64 errorcode)
|
||||
{
|
||||
uint32_t coreid = hal_get_core_id();
|
||||
uint32 coreid = hal_get_core_id();
|
||||
if (_exc_handler_table[exc_vec] == NULL)
|
||||
{
|
||||
hal_printf("Unhandled exception %d at 0x%X.\n", exc_vec, context->rip);
|
||||
@ -131,7 +124,6 @@ void SXAPI hal_exception_dispatcher(uint64_t exc_vec, hal_interrupt_context_t *c
|
||||
{
|
||||
_exc_handler_table[coreid][exc_vec](context->rip, context->rsp, errorcode);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
static void SXAPI halp_populate_idt(void)
|
||||
@ -392,19 +384,18 @@ static void SXAPI halp_populate_idt(void)
|
||||
hal_set_interrupt_handler(253, hal_interrupt_handler_253);
|
||||
hal_set_interrupt_handler(254, hal_interrupt_handler_254);
|
||||
hal_set_interrupt_handler(255, hal_interrupt_handler_255);
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t SXAPI hal_get_core_id(void)
|
||||
uint32 SXAPI hal_get_core_id(void)
|
||||
{
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t SXAPI hal_interrupt_init(void)
|
||||
int32 SXAPI hal_interrupt_init(void)
|
||||
{
|
||||
uint32_t coreid = hal_get_core_id();
|
||||
uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
|
||||
uint32 coreid = hal_get_core_id();
|
||||
uint32 eax = 0, ebx = 0, ecx = 0, edx = 0;
|
||||
eax = 1;
|
||||
hal_cpuid(&eax, &ebx, &ecx, &edx);
|
||||
if (!(edx & lb_bit_mask(9)))
|
||||
@ -414,11 +405,11 @@ int32_t SXAPI hal_interrupt_init(void)
|
||||
}
|
||||
|
||||
// get idt ptr ready
|
||||
_idt_ptrs[coreid].base = (uint64_t) &_idts[coreid];
|
||||
_idt_ptrs[coreid].base = (uint64) &_idts[coreid];
|
||||
_idt_ptrs[coreid].limit = IDT_ENTRY_NUM * IDT_ENTRY_SIZE - 1;
|
||||
|
||||
// clear dispatch table
|
||||
for (uint64_t i = 0; i < IDT_ENTRY_NUM; i++)
|
||||
for (uint64 i = 0; i < IDT_ENTRY_NUM; i++)
|
||||
{
|
||||
_intr_handler_table[coreid][i] = NULL;
|
||||
_exc_handler_table[coreid][i] = NULL;
|
||||
@ -434,29 +425,29 @@ int32_t SXAPI hal_interrupt_init(void)
|
||||
hal_write_port_8(0xa1, 0xff);
|
||||
hal_write_port_8(0x21, 0xff);
|
||||
|
||||
uint64_t apic_base_reg = 0;
|
||||
uint64_t apic_base = 0;
|
||||
uint64 apic_base_reg = 0;
|
||||
uint64 apic_base = 0;
|
||||
ecx = MSR_IA32_APIC_BASE;
|
||||
hal_read_msr(&ecx, &edx, &eax);
|
||||
apic_base_reg = ((uint64_t) edx << 32) + (uint64_t) eax;
|
||||
apic_base_reg = ((uint64) edx << 32) + (uint64) eax;
|
||||
apic_base = apic_base_reg & lb_bit_field_mask(12, 35);
|
||||
UNREFERENCED(apic_base);
|
||||
|
||||
//hal_printf("APIC Base: 0x%X\n", apic_base);
|
||||
//hal_printf("APIC Enabled: %s\n", apic_base_reg & bit_mask_64(11) ? "Yes" : "No");
|
||||
//hal_printf("BSP: %s\n", apic_base_reg & bit_mask_64(8) ? "Yes" : "No");
|
||||
//hal_printf("APIC Spour: 0x%X\n", *(uint32_t *) ((char *) apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET));
|
||||
//hal_printf("APIC Spour: 0x%X\n", *(uint32 *) ((char *) apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET));
|
||||
|
||||
// hardware enable APIC
|
||||
ecx = MSR_IA32_APIC_BASE;
|
||||
eax = (uint32_t) ((apic_base_reg & lb_bit_field_mask(0, 31)) | lb_bit_mask(11));
|
||||
eax = (uint32) ((apic_base_reg & lb_bit_field_mask(0, 31)) | lb_bit_mask(11));
|
||||
hal_write_msr(&ecx, &edx, &eax);
|
||||
|
||||
// software enable APIC
|
||||
// hal_write_mem_32((char *) apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET, *(uint32_t *) (apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET) | (uint32_t)lb_bit_mask(8));
|
||||
// hal_write_mem_32((char *) apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET, *(uint32 *) (apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET) | (uint32)lb_bit_mask(8));
|
||||
|
||||
// hal_issue_interrupt(1, 255);
|
||||
// hal_enable_interrupt();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
153
hal/mem.c
153
hal/mem.c
@ -6,46 +6,38 @@
|
||||
#include "hal/intr.h"
|
||||
#include "status.h"
|
||||
|
||||
static uint8_t _gdts[HAL_CORE_COUNT][GDT_ENTRY_NUM * GDT_ENTRY_SIZE];
|
||||
static hal_gdt_ptr_t _gdt_ptrs[HAL_CORE_COUNT];
|
||||
static uint8 _gdts[HAL_CORE_COUNT][GDT_ENTRY_NUM * GDT_ENTRY_SIZE];
|
||||
static struct hal_gdt_ptr _gdt_ptrs[HAL_CORE_COUNT];
|
||||
|
||||
#define KERNEL_HEAP_SIZE 8192
|
||||
|
||||
char kernel_heap[KERNEL_HEAP_SIZE];
|
||||
static char kernel_heap[KERNEL_HEAP_SIZE];
|
||||
|
||||
/**
|
||||
* helper for boot.asm, not open to C headers
|
||||
* @param k_start kernel start paddr
|
||||
* @param k_end kernel end paddr
|
||||
* @param multiboot_info multibootinfo paddr
|
||||
* @param pt_base page table base paddr
|
||||
* @param pt_end page table entry paddr
|
||||
*/
|
||||
status_t SXAPI hal_write_initial_page_table(void *multiboot_info)
|
||||
sx_status SXAPI hal_write_initial_page_table(void *multiboot_info)
|
||||
{
|
||||
UNREFERENCED(multiboot_info);
|
||||
|
||||
/*
|
||||
// still identity mapping
|
||||
uint32_t pt_num = 0;
|
||||
uint32_t pd_num = 0;
|
||||
uint32_t pdpt_num = 0;
|
||||
uint32_t pml4_num = 0;
|
||||
uint32 pt_num = 0;
|
||||
uint32 pd_num = 0;
|
||||
uint32 pdpt_num = 0;
|
||||
uint32 pml4_num = 0;
|
||||
|
||||
// calculate the number of page tables required:
|
||||
uint64_t k_size = (uintptr_t)KERNEL_IMAGE_END_VADDR - (uintptr_t)KERNEL_IMAGE_VADDR;
|
||||
u64 k_size = (uintptr)KERNEL_IMAGE_END_VADDR - (uintptr)KERNEL_IMAGE_VADDR;
|
||||
// see multiboot boot info header
|
||||
uint32_t m_size = *(uint32_t *)multiboot_info;
|
||||
uint32 m_size = *(uint32 *)multiboot_info;
|
||||
|
||||
// how many pages do we need to hold the entries
|
||||
// 512 page table entries per 4k page
|
||||
pt_num = (1 + (uint32_t)((k_size + m_size - 1) / KERNEL_PAGE_SIZE)) / 512;
|
||||
pt_num = (1 + (uint32)((k_size + m_size - 1) / KERNEL_PAGE_SIZE)) / 512;
|
||||
pd_num = 1 + (pt_num - 1) / 512;
|
||||
pdpt_num = 1 + (pd_num - 1) / 512;
|
||||
pml4_num = 1 + (pdpt_num - 1) / 512;
|
||||
|
||||
// calculate the # of page tables
|
||||
if ((((uintptr_t)(pt_end) - (uintptr_t)(pt_base)) / KERNEL_PAGE_SIZE) < (pt_num + pd_num + pdpt_num + pml4_num))
|
||||
if ((((uintptr)(pt_end) - (uintptr)(pt_base)) / KERNEL_PAGE_SIZE) < (pt_num + pd_num + pdpt_num + pml4_num))
|
||||
{
|
||||
return STATUS_FAIL;
|
||||
}
|
||||
@ -57,107 +49,102 @@ status_t SXAPI hal_write_initial_page_table(void *multiboot_info)
|
||||
KERNEL_DYNAMIC_SIZE = ;
|
||||
|
||||
// map recursive page tables
|
||||
hal_write_pml4(pt_base, (uintptr_t)pt_base, PML4_PRESENT | PML4_WRITE);
|
||||
hal_write_pml4(pt_base, (uintptr)pt_base, PML4_PRESENT | PML4_WRITE);
|
||||
*/
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void SXAPI hal_write_pt(void *const base, uintptr_t const p_addr, uint64_t const attr)
|
||||
void SXAPI hal_write_pt(void *const base, uintptr const p_addr, uint64 const attr)
|
||||
{
|
||||
if (base == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
uint64_t entry = (p_addr & 0xFFFFFFFFFF000) | attr;
|
||||
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
|
||||
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
|
||||
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
|
||||
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
|
||||
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
|
||||
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
|
||||
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
|
||||
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
|
||||
return;
|
||||
uint64 entry = (p_addr & 0xFFFFFFFFFF000) | attr;
|
||||
((uint8 *) base)[0] = (uint8) (entry & 0xFF);
|
||||
((uint8 *) base)[1] = (uint8) ((entry >> 8) & 0xFF);
|
||||
((uint8 *) base)[2] = (uint8) ((entry >> 16) & 0xFF);
|
||||
((uint8 *) base)[3] = (uint8) ((entry >> 24) & 0xFF);
|
||||
((uint8 *) base)[4] = (uint8) ((entry >> 32) & 0xFF);
|
||||
((uint8 *) base)[5] = (uint8) ((entry >> 40) & 0xFF);
|
||||
((uint8 *) base)[6] = (uint8) ((entry >> 48) & 0xFF);
|
||||
((uint8 *) base)[7] = (uint8) ((entry >> 56) & 0xFF);
|
||||
}
|
||||
|
||||
void SXAPI hal_write_pd(void *const base, uintptr_t const pt_addr, uint64_t const attr)
|
||||
void SXAPI hal_write_pd(void *const base, uintptr const pt_addr, uint64 const attr)
|
||||
{
|
||||
if (base == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
uint64_t entry = (pt_addr & 0xFFFFFFFFFF000) | attr;
|
||||
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
|
||||
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
|
||||
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
|
||||
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
|
||||
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
|
||||
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
|
||||
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
|
||||
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
|
||||
return;
|
||||
uint64 entry = (pt_addr & 0xFFFFFFFFFF000) | attr;
|
||||
((uint8 *) base)[0] = (uint8) (entry & 0xFF);
|
||||
((uint8 *) base)[1] = (uint8) ((entry >> 8) & 0xFF);
|
||||
((uint8 *) base)[2] = (uint8) ((entry >> 16) & 0xFF);
|
||||
((uint8 *) base)[3] = (uint8) ((entry >> 24) & 0xFF);
|
||||
((uint8 *) base)[4] = (uint8) ((entry >> 32) & 0xFF);
|
||||
((uint8 *) base)[5] = (uint8) ((entry >> 40) & 0xFF);
|
||||
((uint8 *) base)[6] = (uint8) ((entry >> 48) & 0xFF);
|
||||
((uint8 *) base)[7] = (uint8) ((entry >> 56) & 0xFF);
|
||||
}
|
||||
|
||||
void SXAPI hal_write_pdpt(void *const base, uintptr_t const pd_addr, uint64_t const attr)
|
||||
void SXAPI hal_write_pdpt(void *const base, uintptr const pd_addr, uint64 const attr)
|
||||
{
|
||||
if (base == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
uint64_t entry = (pd_addr & 0xFFFFFFFFFF000) | attr;
|
||||
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
|
||||
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
|
||||
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
|
||||
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
|
||||
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
|
||||
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
|
||||
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
|
||||
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
|
||||
return;
|
||||
uint64 entry = (pd_addr & 0xFFFFFFFFFF000) | attr;
|
||||
((uint8 *) base)[0] = (uint8) (entry & 0xFF);
|
||||
((uint8 *) base)[1] = (uint8) ((entry >> 8) & 0xFF);
|
||||
((uint8 *) base)[2] = (uint8) ((entry >> 16) & 0xFF);
|
||||
((uint8 *) base)[3] = (uint8) ((entry >> 24) & 0xFF);
|
||||
((uint8 *) base)[4] = (uint8) ((entry >> 32) & 0xFF);
|
||||
((uint8 *) base)[5] = (uint8) ((entry >> 40) & 0xFF);
|
||||
((uint8 *) base)[6] = (uint8) ((entry >> 48) & 0xFF);
|
||||
((uint8 *) base)[7] = (uint8) ((entry >> 56) & 0xFF);
|
||||
}
|
||||
|
||||
void SXAPI hal_write_pml4(void *const base, uintptr_t const pdpt_addr, uint64_t const attr)
|
||||
void SXAPI hal_write_pml4(void *const base, uintptr const pdpt_addr, uint64 const attr)
|
||||
{
|
||||
if (base == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
uint64_t const entry = (pdpt_addr & 0xFFFFFFFFFF000) | attr;
|
||||
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
|
||||
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
|
||||
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
|
||||
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
|
||||
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
|
||||
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
|
||||
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
|
||||
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
|
||||
return;
|
||||
uint64 const entry = (pdpt_addr & 0xFFFFFFFFFF000) | attr;
|
||||
((uint8 *) base)[0] = (uint8) (entry & 0xFF);
|
||||
((uint8 *) base)[1] = (uint8) ((entry >> 8) & 0xFF);
|
||||
((uint8 *) base)[2] = (uint8) ((entry >> 16) & 0xFF);
|
||||
((uint8 *) base)[3] = (uint8) ((entry >> 24) & 0xFF);
|
||||
((uint8 *) base)[4] = (uint8) ((entry >> 32) & 0xFF);
|
||||
((uint8 *) base)[5] = (uint8) ((entry >> 40) & 0xFF);
|
||||
((uint8 *) base)[6] = (uint8) ((entry >> 48) & 0xFF);
|
||||
((uint8 *) base)[7] = (uint8) ((entry >> 56) & 0xFF);
|
||||
}
|
||||
|
||||
void SXAPI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uint32_t const limit,
|
||||
uint64_t const attr)
|
||||
void SXAPI hal_write_segment_descriptor(void *const gdt, uint32 const base, uint32 const limit,
|
||||
uint64 const attr)
|
||||
{
|
||||
if (gdt == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
uint64_t const seg_desc = (((uint64_t) base & 0xFFFF) << 16) | ((((uint64_t) base >> 16) & 0xFF) << 32) |
|
||||
((((uint64_t) base >> 24) & 0xFF) << 56) | ((uint64_t) limit & 0xFFFF) |
|
||||
((((uint64_t) limit >> 16) & 0xF) << 48) | attr;
|
||||
((uint8_t *) gdt)[0] = (uint8_t) (seg_desc & 0xFF);
|
||||
((uint8_t *) gdt)[1] = (uint8_t) ((seg_desc >> 8) & 0xFF);
|
||||
((uint8_t *) gdt)[2] = (uint8_t) ((seg_desc >> 16) & 0xFF);
|
||||
((uint8_t *) gdt)[3] = (uint8_t) ((seg_desc >> 24) & 0xFF);
|
||||
((uint8_t *) gdt)[4] = (uint8_t) ((seg_desc >> 32) & 0xFF);
|
||||
((uint8_t *) gdt)[5] = (uint8_t) ((seg_desc >> 40) & 0xFF);
|
||||
((uint8_t *) gdt)[6] = (uint8_t) ((seg_desc >> 48) & 0xFF);
|
||||
((uint8_t *) gdt)[7] = (uint8_t) ((seg_desc >> 56) & 0xFF);
|
||||
return;
|
||||
uint64 const seg_desc = (((uint64) base & 0xFFFF) << 16) | ((((uint64) base >> 16) & 0xFF) << 32) |
|
||||
((((uint64) base >> 24) & 0xFF) << 56) | ((uint64) limit & 0xFFFF) |
|
||||
((((uint64) limit >> 16) & 0xF) << 48) | attr;
|
||||
((uint8 *) gdt)[0] = (uint8) (seg_desc & 0xFF);
|
||||
((uint8 *) gdt)[1] = (uint8) ((seg_desc >> 8) & 0xFF);
|
||||
((uint8 *) gdt)[2] = (uint8) ((seg_desc >> 16) & 0xFF);
|
||||
((uint8 *) gdt)[3] = (uint8) ((seg_desc >> 24) & 0xFF);
|
||||
((uint8 *) gdt)[4] = (uint8) ((seg_desc >> 32) & 0xFF);
|
||||
((uint8 *) gdt)[5] = (uint8) ((seg_desc >> 40) & 0xFF);
|
||||
((uint8 *) gdt)[6] = (uint8) ((seg_desc >> 48) & 0xFF);
|
||||
((uint8 *) gdt)[7] = (uint8) ((seg_desc >> 56) & 0xFF);
|
||||
}
|
||||
|
||||
void *SXAPI halloc(uint32_t size)
|
||||
void *SXAPI halloc(uint32 size)
|
||||
{
|
||||
return lb_salloc(kernel_heap, size);
|
||||
}
|
||||
@ -165,12 +152,11 @@ void *SXAPI halloc(uint32_t size)
|
||||
void SXAPI hfree(void *ptr)
|
||||
{
|
||||
lb_sfree(kernel_heap, ptr);
|
||||
return;
|
||||
}
|
||||
|
||||
static void SXAPI _hal_init_gdt(void)
|
||||
{
|
||||
uint32_t coreid = hal_get_core_id();
|
||||
uint32 coreid = hal_get_core_id();
|
||||
// get gdt ready
|
||||
hal_write_segment_descriptor((void *) &_gdts[coreid][0], 0, 0, 0);
|
||||
hal_write_segment_descriptor((void *) &_gdts[coreid][8], 0, 0,
|
||||
@ -194,7 +180,7 @@ static void SXAPI _hal_init_gdt(void)
|
||||
hal_write_segment_descriptor((void *) &_gdts[coreid][64], 0, 0xFFFFF,
|
||||
SEG_DPL_3 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
|
||||
SEG_TYPE_DATA_RW);
|
||||
_gdt_ptrs[coreid].base = (uint64_t) &_gdts[coreid];
|
||||
_gdt_ptrs[coreid].base = (uint64) &_gdts[coreid];
|
||||
_gdt_ptrs[coreid].limit = GDT_ENTRY_NUM * GDT_ENTRY_SIZE - 1;
|
||||
hal_flush_gdt(&_gdt_ptrs[coreid], seg_selector(1, 0), seg_selector(2, 0));
|
||||
}
|
||||
@ -203,5 +189,4 @@ void SXAPI hal_mem_init()
|
||||
{
|
||||
_hal_init_gdt();
|
||||
lb_salloc_init(kernel_heap, KERNEL_HEAP_SIZE);
|
||||
return;
|
||||
}
|
||||
|
61
hal/print.c
61
hal/print.c
@ -3,11 +3,11 @@
|
||||
#include "hal/print.h"
|
||||
#include "hal/cpu.h"
|
||||
|
||||
#define get_column(pos) ((pos) % 80)
|
||||
// #define get_column(pos) ((pos) % 80)
|
||||
#define get_row(pos) ((pos) / 80)
|
||||
#define get_pos(row, col) ((row) * 80 + (col))
|
||||
|
||||
static uint64_t text_pos;
|
||||
static uint64 text_pos;
|
||||
|
||||
void SXAPI hal_print_init(void)
|
||||
{
|
||||
@ -17,7 +17,6 @@ void SXAPI hal_print_init(void)
|
||||
static void SXAPI halp_print_scroll(void)
|
||||
{
|
||||
lb_mem_move((void *) (0xb8000 + get_pos(1, 0) * 2), (void *) (0xb8000 + get_pos(0, 0) * 2), (80 * 24) * 2);
|
||||
return;
|
||||
}
|
||||
|
||||
static void SXAPI halp_print_str(char const *str)
|
||||
@ -54,19 +53,18 @@ static void SXAPI halp_print_str(char const *str)
|
||||
text_pos++;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
static void SXAPI halp_print_uint(uint64_t number)
|
||||
static void SXAPI halp_print_uint(uint64 number)
|
||||
{
|
||||
char arr[21]; // do not need to initialize
|
||||
arr[20] = 0; //zero-terminated
|
||||
uint32_t index = 19;
|
||||
uint32_t const div = 10;
|
||||
uint32 index = 19;
|
||||
uint32 const div = 10;
|
||||
while (1)
|
||||
{
|
||||
uint64_t quo = number / div;
|
||||
uint64_t rmd = number % div;
|
||||
uint64 quo = number / div;
|
||||
uint64 rmd = number % div;
|
||||
number = quo;
|
||||
arr[index--] = (char) ('0' + rmd);
|
||||
if (number == 0)
|
||||
@ -75,16 +73,15 @@ static void SXAPI halp_print_uint(uint64_t number)
|
||||
}
|
||||
}
|
||||
halp_print_str(&(arr[index + 1]));
|
||||
return;
|
||||
}
|
||||
|
||||
static void SXAPI halp_print_int(int64_t number)
|
||||
static void SXAPI halp_print_int(int64 number)
|
||||
{
|
||||
char arr[21]; // do not need to initialize
|
||||
arr[20] = 0; //zero-terminated
|
||||
uint32_t index = 19;
|
||||
uint32_t isNegative = 0;
|
||||
uint32_t const div = 10;
|
||||
uint32 index = 19;
|
||||
uint32 isNegative = 0;
|
||||
uint32 const div = 10;
|
||||
if (number < 0)
|
||||
{
|
||||
isNegative = 1;
|
||||
@ -92,8 +89,8 @@ static void SXAPI halp_print_int(int64_t number)
|
||||
}
|
||||
while (1)
|
||||
{
|
||||
int64_t quo = number / div;
|
||||
int64_t rmd = number % div;
|
||||
int64 quo = number / div;
|
||||
int64 rmd = number % div;
|
||||
number = quo;
|
||||
arr[index--] = (char) ('0' + rmd);
|
||||
if (number == 0)
|
||||
@ -106,22 +103,21 @@ static void SXAPI halp_print_int(int64_t number)
|
||||
arr[index--] = '-';
|
||||
}
|
||||
halp_print_str(&(arr[index + 1]));
|
||||
return;
|
||||
}
|
||||
|
||||
static void SXAPI halp_print_hex(uint64_t number, uint64_t capital)
|
||||
static void SXAPI halp_print_hex(uint64 number, uint64 capital)
|
||||
{
|
||||
char const lookup_table_cap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
||||
char const lookup_table[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
||||
char const *const look_up = capital == 1 ? &lookup_table_cap[0] : &lookup_table[0];
|
||||
char arr[17];
|
||||
arr[16] = 0; //zero-terminated
|
||||
uint32_t index = 15;
|
||||
uint32_t const div = 16;
|
||||
uint32 index = 15;
|
||||
uint32 const div = 16;
|
||||
while (1)
|
||||
{
|
||||
uint64_t quo = number / div;
|
||||
uint64_t rmd = number % div;
|
||||
uint64 quo = number / div;
|
||||
uint64 rmd = number % div;
|
||||
number = quo;
|
||||
arr[index--] = look_up[rmd];
|
||||
if (number == 0)
|
||||
@ -130,21 +126,19 @@ static void SXAPI halp_print_hex(uint64_t number, uint64_t capital)
|
||||
}
|
||||
}
|
||||
halp_print_str(&(arr[index + 1]));
|
||||
return;
|
||||
}
|
||||
|
||||
void SXAPI hal_clear_screen(void)
|
||||
{
|
||||
text_pos = 0; // reset text_pos
|
||||
lb_mem_set((void *) 0xb8000, 0, 25 * 80 * 2);
|
||||
return;
|
||||
}
|
||||
|
||||
void SXAPI hal_vprintf(char const *format, va_list args)
|
||||
{
|
||||
char buf[2] = {0, 0};
|
||||
int64_t d;
|
||||
uint64_t u;
|
||||
int64 d;
|
||||
uint64 u;
|
||||
char *s;
|
||||
char c;
|
||||
for (; *format != '\0'; format++)
|
||||
@ -159,11 +153,11 @@ void SXAPI hal_vprintf(char const *format, va_list args)
|
||||
switch (*format)
|
||||
{
|
||||
case 'd':
|
||||
d = va_arg(args, int64_t);
|
||||
d = va_arg(args, int64);
|
||||
halp_print_int(d);
|
||||
break;
|
||||
case 'u':
|
||||
u = va_arg(args, uint64_t);
|
||||
u = va_arg(args, uint64);
|
||||
halp_print_uint(u);
|
||||
break;
|
||||
case 's':
|
||||
@ -171,16 +165,16 @@ void SXAPI hal_vprintf(char const *format, va_list args)
|
||||
halp_print_str(s);
|
||||
break;
|
||||
case 'c':
|
||||
c = (char) va_arg(args, int64_t);
|
||||
c = (char) va_arg(args, int64);
|
||||
buf[0] = c;
|
||||
halp_print_str(buf);
|
||||
break;
|
||||
case 'x':
|
||||
u = va_arg(args, uint64_t);
|
||||
u = va_arg(args, uint64);
|
||||
halp_print_hex(u, 0);
|
||||
break;
|
||||
case 'X':
|
||||
u = va_arg(args, uint64_t);
|
||||
u = va_arg(args, uint64);
|
||||
halp_print_hex(u, 1);
|
||||
break;
|
||||
case '%':
|
||||
@ -204,12 +198,11 @@ void SXAPI hal_printf(char const *format, ...)
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void SXAPI hal_assert(uint32_t expression, char *message)
|
||||
void SXAPI hal_assert(uint32 expression, char *message)
|
||||
{
|
||||
if (!expression)
|
||||
{
|
||||
hal_printf("HAL: Assertion failed. Detail: %s", message == NULL ? "NULL" : message);
|
||||
hal_halt_cpu();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
#ifndef _HAL_BOOT_H_
|
||||
#define _HAL_BOOT_H_
|
||||
#ifndef HAL_BOOT_H
|
||||
#define HAL_BOOT_H
|
||||
|
||||
#include "kernel/hal/boot.h"
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef _HAL_CPU_H_
|
||||
#define _HAL_CPU_H_
|
||||
#ifndef HAL_CPU_H
|
||||
#define HAL_CPU_H
|
||||
|
||||
#include "type.h"
|
||||
#include "kernel/hal/atomic.h"
|
||||
@ -7,23 +7,23 @@
|
||||
#define HAL_CORE_COUNT 1
|
||||
|
||||
|
||||
typedef struct
|
||||
struct STRUCT_PACKED hal_gdt_ptr
|
||||
{
|
||||
uint16_t limit;
|
||||
uint64_t base;
|
||||
} STRUCT_PACKED hal_gdt_ptr_t;
|
||||
uint16 limit;
|
||||
uint64 base;
|
||||
};
|
||||
|
||||
typedef struct
|
||||
struct STRUCT_PACKED hal_idt_ptr
|
||||
{
|
||||
uint16_t limit;
|
||||
uint64_t base;
|
||||
} STRUCT_PACKED hal_idt_ptr_t;
|
||||
uint16 limit;
|
||||
uint64 base;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* CPU Instructions
|
||||
*/
|
||||
extern void SXAPI hal_cpuid(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx);
|
||||
extern void SXAPI hal_cpuid(uint32 *eax, uint32 *ebx, uint32 *ecx, uint32 *edx);
|
||||
|
||||
extern void SXAPI hal_halt_cpu(void);
|
||||
|
||||
@ -36,47 +36,47 @@ extern void SXAPI hal_disable_interrupt(void);
|
||||
* IO Port Operations
|
||||
*/
|
||||
|
||||
extern int8_t SXAPI hal_read_port_8(uint16_t port);
|
||||
extern int8 SXAPI hal_read_port_8(uint16 port);
|
||||
|
||||
extern int16_t SXAPI hal_read_port_16(uint16_t port);
|
||||
extern int16 SXAPI hal_read_port_16(uint16 port);
|
||||
|
||||
extern int32_t SXAPI hal_read_port_32(uint16_t port);
|
||||
extern int32 SXAPI hal_read_port_32(uint16 port);
|
||||
|
||||
extern void SXAPI hal_write_port_8(uint16_t port, uint8_t data);
|
||||
extern void SXAPI hal_write_port_8(uint16 port, uint8 data);
|
||||
|
||||
extern void SXAPI hal_write_port_16(uint16_t port, uint16_t data);
|
||||
extern void SXAPI hal_write_port_16(uint16 port, uint16 data);
|
||||
|
||||
extern void SXAPI hal_write_port_32(uint16_t port, uint32_t data);
|
||||
extern void SXAPI hal_write_port_32(uint16 port, uint32 data);
|
||||
|
||||
|
||||
/**
|
||||
* CPU Structure Operations
|
||||
*/
|
||||
|
||||
extern void SXAPI hal_flush_gdt(hal_gdt_ptr_t *gdt_ptr, uint64_t code_slct, uint64_t data_slct);
|
||||
extern void SXAPI hal_flush_gdt(struct hal_gdt_ptr *gdt_ptr, uint64 code_slct, uint64 data_slct);
|
||||
|
||||
extern void SXAPI hal_flush_tlb(void);
|
||||
|
||||
extern void SXAPI hal_flush_idt(hal_idt_ptr_t *idt_ptr);
|
||||
extern void SXAPI hal_flush_idt(struct hal_idt_ptr *idt_ptr);
|
||||
|
||||
extern void SXAPI hal_read_idt(hal_idt_ptr_t **idt_ptr);
|
||||
extern void SXAPI hal_read_idt(struct hal_idt_ptr **idt_ptr);
|
||||
|
||||
/**
|
||||
* Control Register Operations
|
||||
*/
|
||||
#define MSR_IA32_APIC_BASE 0x1B
|
||||
|
||||
extern void SXAPI hal_read_msr(uint32_t *ecx, uint32_t *edx, uint32_t *eax);
|
||||
extern void SXAPI hal_read_msr(uint32 *ecx, uint32 *edx, uint32 *eax);
|
||||
|
||||
extern void SXAPI hal_write_msr(uint32_t *ecx, uint32_t *edx, uint32_t *eax);
|
||||
extern void SXAPI hal_write_msr(uint32 *ecx, uint32 *edx, uint32 *eax);
|
||||
|
||||
extern void SXAPI hal_write_cr3(uint64_t base);
|
||||
extern void SXAPI hal_write_cr3(uint64 base);
|
||||
|
||||
extern uint64_t SXAPI hal_read_cr3(void);
|
||||
extern uint64 SXAPI hal_read_cr3(void);
|
||||
|
||||
extern void SXAPI hal_write_cr8(uint64_t pri);
|
||||
extern void SXAPI hal_write_cr8(uint64 pri);
|
||||
|
||||
extern uint64_t SXAPI hal_read_cr8(void);
|
||||
extern uint64 SXAPI hal_read_cr8(void);
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef _HAL_INTR_H_
|
||||
#define _HAL_INTR_H_
|
||||
#ifndef HAL_INTR_H
|
||||
#define HAL_INTR_H
|
||||
|
||||
#include "type.h"
|
||||
#include "kernel/hal/intr.h"
|
||||
@ -9,11 +9,11 @@
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
const uint64_t rip;
|
||||
const uint64_t cs;
|
||||
const uint64_t rflags;
|
||||
const uint64_t rsp;
|
||||
const uint64_t ss;
|
||||
const uint64 rip;
|
||||
const uint64 cs;
|
||||
const uint64 rflags;
|
||||
const uint64 rsp;
|
||||
const uint64 ss;
|
||||
} hal_interrupt_context_t;
|
||||
|
||||
|
||||
@ -36,21 +36,36 @@ typedef struct
|
||||
* intr.h
|
||||
*/
|
||||
|
||||
int32_t SXAPI hal_interrupt_init(void);
|
||||
int32 SXAPI hal_interrupt_init(void);
|
||||
|
||||
void SXAPI hal_write_gate(void *const gate,
|
||||
uint64 const offset,
|
||||
uint32 const selector,
|
||||
uint32 const attr);
|
||||
|
||||
void SXAPI hal_set_interrupt_handler(uint64 index, void (*handler)(void));
|
||||
|
||||
|
||||
/**
|
||||
* Dispatchers for asm code
|
||||
*/
|
||||
void SXAPI hal_interrupt_dispatcher(uint64 int_vec, hal_interrupt_context_t *context);
|
||||
|
||||
void SXAPI hal_exception_dispatcher(uint64 exc_vec, hal_interrupt_context_t *context, uint64 errorcode);
|
||||
|
||||
|
||||
/**
|
||||
* System exception Handlers
|
||||
*/
|
||||
extern void SXAPI hal_interrupt_handler_0(void);
|
||||
extern SXAPI void hal_interrupt_handler_0(void);
|
||||
|
||||
extern void SXAPI hal_interrupt_handler_1(void);
|
||||
extern SXAPI void hal_interrupt_handler_1(void);
|
||||
|
||||
extern void SXAPI hal_interrupt_handler_2(void);
|
||||
extern SXAPI void hal_interrupt_handler_2(void);
|
||||
|
||||
extern void SXAPI hal_interrupt_handler_3(void);
|
||||
extern SXAPI void hal_interrupt_handler_3(void);
|
||||
|
||||
extern void SXAPI hal_interrupt_handler_4(void);
|
||||
extern SXAPI void hal_interrupt_handler_4(void);
|
||||
|
||||
extern void SXAPI hal_interrupt_handler_5(void);
|
||||
|
||||
@ -558,4 +573,4 @@ extern void SXAPI hal_interrupt_handler_254(void);
|
||||
extern void SXAPI hal_interrupt_handler_255(void);
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,8 +1,9 @@
|
||||
#ifndef _HAL_MEM_H_
|
||||
#define _HAL_MEM_H_
|
||||
#ifndef HAL_MEM_H
|
||||
#define HAL_MEM_H
|
||||
|
||||
#include "type.h"
|
||||
#include "kernel/hal/mem.h"
|
||||
#include "status.h"
|
||||
|
||||
/**
|
||||
Global Descriptors Table Definitions
|
||||
@ -73,30 +74,31 @@
|
||||
#define PD_ENTRY_NUM(vaddr) (((vaddr) >> 21) & 0x1FF)
|
||||
#define PT_ENTRY_NUM(vaddr) (((vaddr) >> 12) & 0x1FF)
|
||||
|
||||
static inline uint32_t SXAPI seg_selector(uint32_t index, uint32_t rpl)
|
||||
static inline uint32 SXAPI seg_selector(uint32 index, uint32 rpl)
|
||||
{
|
||||
return (index << 3) + rpl;
|
||||
}
|
||||
|
||||
void SXAPI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uint32_t const limit, uint64_t const attr);
|
||||
void SXAPI hal_write_segment_descriptor(void *const gdt, uint32 const base, uint32 const limit, uint64 const attr);
|
||||
|
||||
void SXAPI hal_write_pml4(void *const base, uintptr_t const pdpt_addr, uint64_t const attr);
|
||||
void SXAPI hal_write_pml4(void *const base, uintptr const pdpt_addr, uint64 const attr);
|
||||
|
||||
void SXAPI hal_write_pdpt(void *const base, uintptr_t const pd_addr, uint64_t const attr);
|
||||
void SXAPI hal_write_pdpt(void *const base, uintptr const pd_addr, uint64 const attr);
|
||||
|
||||
void SXAPI hal_write_pd(void *const base, uintptr_t const pt_addr, uint64_t const attr);
|
||||
void SXAPI hal_write_pd(void *const base, uintptr const pt_addr, uint64 const attr);
|
||||
|
||||
void SXAPI hal_write_pt(void *const base, uintptr_t const p_addr, uint64_t const attr);
|
||||
void SXAPI hal_write_pt(void *const base, uintptr const p_addr, uint64 const attr);
|
||||
|
||||
sx_status SXAPI hal_write_initial_page_table(void *multiboot_info);
|
||||
|
||||
/**
|
||||
Function Defn
|
||||
**/
|
||||
|
||||
void *SXAPI halloc(uint32_t size);
|
||||
void *SXAPI halloc(uint32 size);
|
||||
|
||||
void SXAPI hfree(void *ptr);
|
||||
|
||||
void SXAPI hal_mem_init(void);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,10 +1,10 @@
|
||||
#ifndef _HAL_PRINT_H_
|
||||
#define _HAL_PRINT_H_
|
||||
#ifndef HAL_PRINT_H
|
||||
#define HAL_PRINT_H
|
||||
|
||||
#include "type.h"
|
||||
#include "kernel/hal/print.h"
|
||||
|
||||
void SXAPI hal_assert(uint32_t expression, char *message);
|
||||
void SXAPI hal_assert(uint32 expression, char *message);
|
||||
|
||||
void SXAPI hal_printf(const char *str, ...);
|
||||
|
||||
@ -12,4 +12,4 @@ void SXAPI hal_clear_screen(void);
|
||||
|
||||
void SXAPI hal_print_init(void);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef _KERNEL_HAL_ATOMIC_H_
|
||||
#define _KERNEL_HAL_ATOMIC_H_
|
||||
#ifndef KERNEL_HAL_ATOMIC_H
|
||||
#define KERNEL_HAL_ATOMIC_H
|
||||
|
||||
#include "type.h"
|
||||
|
||||
@ -7,10 +7,10 @@
|
||||
* Atomic operations
|
||||
*/
|
||||
|
||||
extern int32_t SXAPI hal_interlocked_exchange_32(int32_t *target, int32_t val);
|
||||
extern int32 SXAPI hal_interlocked_exchange_32(int32 *target, int32 val);
|
||||
|
||||
extern int32_t SXAPI hal_interlocked_increment_32(int32_t *target, int32_t increment);
|
||||
extern int32 SXAPI hal_interlocked_increment_32(int32 *target, int32 increment);
|
||||
|
||||
extern int32_t SXAPI hal_interlocked_compare_exchange_32(int32_t *target, int32_t compare, int32_t val);
|
||||
extern int32 SXAPI hal_interlocked_compare_exchange_32(int32 *target, int32 compare, int32 val);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef _KERNEL_HAL_BOOT_H_
|
||||
#define _KERNEL_HAL_BOOT_H_
|
||||
#ifndef KERNEL_HAL_BOOT_H
|
||||
#define KERNEL_HAL_BOOT_H
|
||||
|
||||
#include "type.h"
|
||||
#include "kernel/hal/intr.h"
|
||||
@ -10,13 +10,13 @@
|
||||
* Required OS boot info
|
||||
*/
|
||||
|
||||
typedef struct
|
||||
struct boot_info
|
||||
{
|
||||
void *krnl_end;
|
||||
intr_info_t intr_info;
|
||||
struct intr_info intr_info;
|
||||
char cpu_vd_str[13];
|
||||
} boot_info_t;
|
||||
};
|
||||
|
||||
status_t SXAPI hal_init(void *m_info);
|
||||
sx_status SXAPI hal_init(void *m_info);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,38 +1,40 @@
|
||||
#ifndef _KERNEL_HAL_INTR_H_
|
||||
#define _KERNEL_HAL_INTR_H_
|
||||
#ifndef KERNEL_HAL_INTR_H
|
||||
#define KERNEL_HAL_INTR_H
|
||||
|
||||
#include "type.h"
|
||||
|
||||
/**
|
||||
* IRQL Definitions
|
||||
*/
|
||||
typedef uint32_t irql_t;
|
||||
typedef uint32 k_irql;
|
||||
#define IRQL_DISABLED_LEVEL (1 << 3)
|
||||
#define IRQL_DPC_LEVEL (1 << 2)
|
||||
#define IRQL_APC_LEVEL (1 << 1)
|
||||
#define IRQL_PASSIVE_LEVEL (1 << 0)
|
||||
|
||||
irql_t SXAPI hal_set_irql(irql_t irql);
|
||||
k_irql SXAPI hal_set_irql(k_irql irql);
|
||||
|
||||
irql_t SXAPI hal_get_irql(void);
|
||||
k_irql SXAPI hal_get_irql(void);
|
||||
|
||||
uint32_t SXAPI hal_get_core_id(void);
|
||||
uint32 SXAPI hal_get_core_id(void);
|
||||
|
||||
void SXAPI hal_issue_interrupt(uint32 target_core, uint32 vector);
|
||||
|
||||
/**
|
||||
* Interrupt Handler Registration
|
||||
*/
|
||||
typedef struct
|
||||
struct intr_info
|
||||
{
|
||||
uint32_t timer_intr_vec;
|
||||
uint32_t apc_intr_vec;
|
||||
uint32_t dpc_intr_vec;
|
||||
} intr_info_t;
|
||||
uint32 timer_intr_vec;
|
||||
uint32 apc_intr_vec;
|
||||
uint32 dpc_intr_vec;
|
||||
};
|
||||
|
||||
typedef void (SXAPI *intr_handler_t)(void *context, void *intr_stack);
|
||||
typedef void (SXAPI *intr_handler)(void *context, void *intr_stack);
|
||||
|
||||
void SXAPI hal_register_interrupt_handler(uint32_t coreid, uint32_t index, intr_handler_t handler, void *context);
|
||||
void SXAPI hal_register_interrupt_handler(uint32 coreid, uint32 index, intr_handler handler, void *context);
|
||||
|
||||
void SXAPI hal_deregister_interrupt_handler(uint32_t coreid, uint32_t index);
|
||||
void SXAPI hal_deregister_interrupt_handler(uint32 coreid, uint32 index);
|
||||
|
||||
/**
|
||||
* Exception Handler Registration
|
||||
@ -48,10 +50,11 @@ typedef enum
|
||||
debug_exc
|
||||
} exc_type_t;
|
||||
|
||||
typedef void (SXAPI *exc_handler_t)(uint64_t exc_addr, uint64_t exc_stack, uint64_t error_code);
|
||||
typedef void (SXAPI *exc_handler)(uint64 exc_addr, uint64 exc_stack, uint64 error_code);
|
||||
|
||||
void SXAPI hal_register_exception_handler(uint32_t coreid, uint32_t index, exc_handler_t handler);
|
||||
void SXAPI hal_register_exception_handler(uint32 coreid, uint32 index, exc_handler handler);
|
||||
|
||||
void SXAPI hal_deregister_exception_handler(uint32_t coreid, uint32_t index);
|
||||
void SXAPI hal_deregister_exception_handler(uint32 coreid, uint32 index);
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef _KERNEL_HAL_MEM_H_
|
||||
#define _KERNEL_HAL_MEM_H_
|
||||
#ifndef KERNEL_HAL_MEM_H
|
||||
#define KERNEL_HAL_MEM_H
|
||||
|
||||
/**
|
||||
* Kernel Memory Layout
|
||||
@ -41,17 +41,18 @@
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uintptr_t base;
|
||||
uint64_t size;
|
||||
uint32_t attr;
|
||||
uintptr base;
|
||||
uint64 size;
|
||||
uint32 attr;
|
||||
} pmm_node_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t num_of_nodes;
|
||||
uint32 num_of_nodes;
|
||||
pmm_node_t nodes[];
|
||||
} pmm_info_t;
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
#ifndef _KERNEL_HAL_PRINT_H_
|
||||
#define _KERNEL_HAL_PRINT_H_
|
||||
#ifndef KERNEL_HAL_PRINT_H
|
||||
#define KERNEL_HAL_PRINT_H
|
||||
|
||||
#include "type.h"
|
||||
|
||||
void SXAPI hal_vprintf(const char *str, va_list args);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,12 +1,12 @@
|
||||
#ifndef _KERNEL_KE_ALLOC_H_
|
||||
#define _KERNEL_KE_ALLOC_H_
|
||||
#ifndef KERNEL_KE_ALLOC_H
|
||||
#define KERNEL_KE_ALLOC_H
|
||||
|
||||
#include "type.h"
|
||||
|
||||
void SXAPI ke_alloc_init(void);
|
||||
|
||||
void *SXAPI ke_alloc(uint32_t size);
|
||||
void *SXAPI ke_alloc(uint32 size);
|
||||
|
||||
void SXAPI ke_free(void *ptr);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,10 +1,10 @@
|
||||
#ifndef _KERNEL_KE_ASSERT_H_
|
||||
#define _KERNEL_KE_ASSERT_H_
|
||||
#ifndef KERNEL_KE_ASSERT_H
|
||||
#define KERNEL_KE_ASSERT_H
|
||||
|
||||
#include "type.h"
|
||||
|
||||
void SXAPI ke_assert_ex(const char *expr_str, const char *file, int32_t line, int32_t expr);
|
||||
void SXAPI ke_assert_ex(const char *expr_str, const char *file, int32 line, int32 expr);
|
||||
|
||||
#define ke_assert(expr) ke_assert_ex(#expr, __FILE__, __LINE__, expr)
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,13 +1,13 @@
|
||||
#ifndef _KERNEL_KE_ATOMIC_H_
|
||||
#define _KERNEL_KE_ATOMIC_H_
|
||||
#ifndef KERNEL_KE_ATOMIC_H
|
||||
#define KERNEL_KE_ATOMIC_H
|
||||
|
||||
#include "type.h"
|
||||
#include "kernel/hal/atomic.h"
|
||||
|
||||
int32_t SXAPI ke_interlocked_exchange_32(int32_t *target, int32_t val);
|
||||
int32 SXAPI ke_interlocked_exchange_32(int32 *target, int32 val);
|
||||
|
||||
int32_t SXAPI ke_interlocked_increment_32(int32_t *target, int32_t increment);
|
||||
int32 SXAPI ke_interlocked_increment_32(int32 *target, int32 increment);
|
||||
|
||||
int32_t SXAPI ke_interlocked_compare_exchange_32(int32_t *target, int32_t compare, int32_t val);
|
||||
int32 SXAPI ke_interlocked_compare_exchange_32(int32 *target, int32 compare, int32 val);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,6 +1,8 @@
|
||||
#ifndef _KERNEL_KE_BOOT_H_
|
||||
#define _KERNEL_KE_BOOT_H_
|
||||
#ifndef KERNEL_KE_BOOT_H
|
||||
#define KERNEL_KE_BOOT_H
|
||||
|
||||
#include "kernel/hal/boot.h"
|
||||
|
||||
#endif
|
||||
void SXAPI ke_main(struct boot_info *boot_info);
|
||||
|
||||
#endif
|
||||
|
@ -1,13 +1,13 @@
|
||||
#ifndef _KERNEL_KE_BUG_CHECK_H_
|
||||
#define _KERNEL_KE_BUG_CHECK_H_
|
||||
#ifndef KERNEL_KE_BUG_CHECK_H
|
||||
#define KERNEL_KE_BUG_CHECK_H
|
||||
|
||||
#include "type.h"
|
||||
|
||||
#define BUG_CHECK_IRQL_MISMATCH 0
|
||||
#define BUG_CHECK_PMM_UNALIGNED 1
|
||||
|
||||
void SXAPI ke_panic(uint64_t reason);
|
||||
void SXAPI ke_panic(uint64 reason);
|
||||
|
||||
void SXAPI ke_trap(void);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,15 +1,15 @@
|
||||
#ifndef _KERNEL_KE_INTR_H_
|
||||
#define _KERNEL_KE_INTR_H_
|
||||
#ifndef KERNEL_KE_INTR_H
|
||||
#define KERNEL_KE_INTR_H
|
||||
|
||||
#include "kernel/hal/intr.h"
|
||||
#include "type.h"
|
||||
|
||||
irql_t SXAPI ke_raise_irql(irql_t irql);
|
||||
k_irql SXAPI ke_raise_irql(k_irql irql);
|
||||
|
||||
irql_t SXAPI ke_lower_irql(irql_t irql);
|
||||
k_irql SXAPI ke_lower_irql(k_irql irql);
|
||||
|
||||
int SXAPI ke_get_current_core(void);
|
||||
uint32 SXAPI ke_get_current_core(void);
|
||||
|
||||
irql_t SXAPI ke_get_irql(void);
|
||||
k_irql SXAPI ke_get_irql(void);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef _KERNEL_KE_PRINT_H_
|
||||
#define _KERNEL_KE_PRINT_H_
|
||||
#ifndef KERNEL_KE_PRINT_H
|
||||
#define KERNEL_KE_PRINT_H
|
||||
|
||||
#include "type.h"
|
||||
#include "kernel/hal/print.h"
|
||||
@ -8,4 +8,4 @@ void SXAPI ke_printf(const char *str, ...);
|
||||
|
||||
void SXAPI ke_vprintf(const char *str, va_list args);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef _KERNEL_KE_RWLOCK_H_
|
||||
#define _KERNEL_KE_RWLOCK_H_
|
||||
#ifndef KERNEL_KE_RWLOCK_H
|
||||
#define KERNEL_KE_RWLOCK_H
|
||||
|
||||
#include "kernel/ke/spin_lock.h"
|
||||
#include "type.h"
|
||||
@ -10,8 +10,8 @@ typedef struct
|
||||
k_spin_lock_t r_mutex;
|
||||
k_spin_lock_t res_lock;
|
||||
k_spin_lock_t r_try;
|
||||
uint32_t reader_ct;
|
||||
uint32_t writer_ct;
|
||||
uint32 reader_ct;
|
||||
uint32 writer_ct;
|
||||
} k_rwwlock_t;
|
||||
|
||||
void SXAPI ke_rwwlock_init(k_rwwlock_t *lock);
|
||||
@ -24,12 +24,13 @@ void SXAPI ke_rwwlock_writer_lock(k_rwwlock_t *lock);
|
||||
|
||||
void SXAPI ke_rwwlock_writer_unlock(k_rwwlock_t *lock);
|
||||
|
||||
irql_t SXAPI ke_rwwlock_reader_lock_raise_irql(k_rwwlock_t *lock, irql_t irql);
|
||||
k_irql SXAPI ke_rwwlock_reader_lock_raise_irql(k_rwwlock_t *lock, k_irql irql);
|
||||
|
||||
void SXAPI ke_rwwlock_reader_unlock_lower_irql(k_rwwlock_t *lock, irql_t irql);
|
||||
void SXAPI ke_rwwlock_reader_unlock_lower_irql(k_rwwlock_t *lock, k_irql irql);
|
||||
|
||||
irql_t SXAPI ke_rwwlock_writer_lock_raise_irql(k_rwwlock_t *lock, irql_t irql);
|
||||
k_irql SXAPI ke_rwwlock_writer_lock_raise_irql(k_rwwlock_t *lock, k_irql irql);
|
||||
|
||||
void SXAPI ke_rwwlock_writer_unlock_lower_irql(k_rwwlock_t *lock, irql_t irql);
|
||||
void SXAPI ke_rwwlock_writer_unlock_lower_irql(k_rwwlock_t *lock, k_irql irql);
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
#ifndef _KERNEL_KE_SPIN_LOCK_H_
|
||||
#define _KERNEL_KE_SPIN_LOCK_H_
|
||||
#ifndef KERNEL_KE_SPIN_LOCK_H
|
||||
#define KERNEL_KE_SPIN_LOCK_H
|
||||
|
||||
#include "type.h"
|
||||
#include "kernel/ke/intr.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int32_t val;
|
||||
int32 val;
|
||||
} k_spin_lock_t;
|
||||
|
||||
void SXAPI ke_spin_lock_init(k_spin_lock_t *lock);
|
||||
@ -15,8 +15,9 @@ void SXAPI ke_spin_lock(k_spin_lock_t *lock);
|
||||
|
||||
void SXAPI ke_spin_unlock(k_spin_lock_t *lock);
|
||||
|
||||
irql_t SXAPI ke_spin_lock_raise_irql(k_spin_lock_t *lock, irql_t irql);
|
||||
k_irql SXAPI ke_spin_lock_raise_irql(k_spin_lock_t *lock, k_irql irql);
|
||||
|
||||
void SXAPI ke_spin_unlock_lower_irql(k_spin_lock_t *lock, irql_t irql);
|
||||
void SXAPI ke_spin_unlock_lower_irql(k_spin_lock_t *lock, k_irql irql);
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#ifndef _KERNEL_MM_MEM_H_
|
||||
#define _KERNEL_MM_MEM_H_
|
||||
#ifndef KERNEL_MM_MEM_H
|
||||
#define KERNEL_MM_MEM_H
|
||||
|
||||
#include "kernel/hal/mem.h"
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef _KERNEL_MM_PMM_H_
|
||||
#define _KERNEL_MM_PMM_H_
|
||||
#ifndef KERNEL_MM_PMM_H
|
||||
#define KERNEL_MM_PMM_H
|
||||
|
||||
#include "type.h"
|
||||
#include "lib/avl_tree.h"
|
||||
@ -13,24 +13,24 @@
|
||||
//
|
||||
//typedef struct
|
||||
//{
|
||||
// uint32_t attr;
|
||||
// uint32 attr;
|
||||
//} k_physical_page_attr_t;
|
||||
|
||||
|
||||
status_t SXAPI sx_pmm_init(pmm_info_t *info);
|
||||
sx_status SXAPI sx_pmm_init(pmm_info_t *info);
|
||||
|
||||
status_t SXAPI mm_alloc_page(uintptr_t *out);
|
||||
sx_status SXAPI mm_alloc_page(uintptr *out);
|
||||
|
||||
status_t SXAPI mm_free_page(uintptr_t base);
|
||||
sx_status SXAPI mm_free_page(uintptr base);
|
||||
|
||||
status_t SXAPI mm_query_page_attr(uintptr_t base,
|
||||
int32_t *out);
|
||||
sx_status SXAPI mm_query_page_attr(uintptr base,
|
||||
int32 *out);
|
||||
|
||||
// TODO: implement these somehow, i might just reserve the first 16MB for these
|
||||
int32_t SXAPI mm_alloc_contiguous_pages(uint64_t num_of_page,
|
||||
uintptr_t highest_p_addr,
|
||||
uintptr_t *out);
|
||||
int32 SXAPI mm_alloc_contiguous_pages(uint64 num_of_page,
|
||||
uintptr highest_p_addr,
|
||||
uintptr *out);
|
||||
|
||||
int32_t SXAPI mm_free_contiguous_pages(uintptr_t base);
|
||||
int32 SXAPI mm_free_contiguous_pages(uintptr base);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,38 +1,38 @@
|
||||
#ifndef _KERNEL_RF_REF_H_
|
||||
#define _KERNEL_RF_REF_H_
|
||||
#ifndef KERNEL_RF_REF_H
|
||||
#define KERNEL_RF_REF_H
|
||||
|
||||
#include "type.h"
|
||||
#include "status.h"
|
||||
|
||||
typedef uint32_t handle_t;
|
||||
typedef uint32 handle_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int32_t ref_count;
|
||||
callback_func_t free_routine;
|
||||
int32 ref_count;
|
||||
callback_func free_routine;
|
||||
} ref_node_t;
|
||||
|
||||
#define K_HANDLE_BASE 0x80000000
|
||||
#define K_HANDLE_BASE (0x80000000ul)
|
||||
|
||||
//
|
||||
// All functions are sx since users or kernel devs should not be
|
||||
// specifying where the allocations take place
|
||||
//
|
||||
|
||||
status_t SXAPI rf_reference_setup(void);
|
||||
sx_status SXAPI rf_reference_setup(void);
|
||||
|
||||
status_t SXAPI rf_reference_create(ref_node_t *ref,
|
||||
callback_func_t free_func);
|
||||
sx_status SXAPI rf_reference_create(ref_node_t *ref,
|
||||
callback_func free_func);
|
||||
|
||||
status_t SXAPI rf_reference_obj(ref_node_t *ref);
|
||||
sx_status SXAPI rf_reference_obj(ref_node_t *ref);
|
||||
|
||||
status_t SXAPI rf_dereference_obj(ref_node_t *ref);
|
||||
sx_status SXAPI rf_dereference_obj(ref_node_t *ref);
|
||||
|
||||
// HANDLES
|
||||
status_t SXAPI sx_open_obj_by_handle(handle_t handle, ref_node_t **out);
|
||||
sx_status SXAPI sx_open_obj_by_handle(handle_t handle, ref_node_t **out);
|
||||
|
||||
status_t SXAPI sx_create_handle(ref_node_t *ref, handle_t *out);
|
||||
sx_status SXAPI sx_create_handle(ref_node_t *ref, handle_t *out);
|
||||
|
||||
status_t SXAPI sx_close_handle(handle_t handle);
|
||||
sx_status SXAPI sx_close_handle(handle_t handle);
|
||||
|
||||
#endif
|
||||
|
@ -1,17 +1,17 @@
|
||||
#ifndef _LIB_AVL_TREE_H_
|
||||
#define _LIB_AVL_TREE_H_
|
||||
#ifndef LIB_AVL_TREE_H
|
||||
#define LIB_AVL_TREE_H
|
||||
|
||||
#include "type.h"
|
||||
#include "lib/sxtdlib.h"
|
||||
|
||||
typedef struct _k_avl_tree_node_t
|
||||
struct avl_tree_node
|
||||
{
|
||||
struct _k_avl_tree_node_t *left;
|
||||
struct _k_avl_tree_node_t *right;
|
||||
struct _k_avl_tree_node_t *parent;
|
||||
struct avl_tree_node *left;
|
||||
struct avl_tree_node *right;
|
||||
struct avl_tree_node *parent;
|
||||
|
||||
int32_t height;
|
||||
} avl_tree_node_t;
|
||||
int32 height;
|
||||
};
|
||||
|
||||
/*
|
||||
* A comparison function between tree_node and your_node
|
||||
@ -20,30 +20,30 @@ typedef struct _k_avl_tree_node_t
|
||||
* = 0 if tree_node == your_node
|
||||
* > 0 if tree_node > your_node
|
||||
*/
|
||||
typedef struct _k_avl_tree_t
|
||||
struct avl_tree
|
||||
{
|
||||
callback_func_t compare;
|
||||
avl_tree_node_t *root;
|
||||
} avl_tree_t;
|
||||
callback_func compare;
|
||||
struct avl_tree_node *root;
|
||||
};
|
||||
|
||||
avl_tree_node_t *SXAPI lb_avl_tree_search(avl_tree_t *tree, avl_tree_node_t *entry);
|
||||
struct avl_tree_node *SXAPI lb_avl_tree_search(struct avl_tree *tree, struct avl_tree_node *entry);
|
||||
|
||||
void SXAPI lb_avl_tree_insert(avl_tree_t *tree, avl_tree_node_t *entry);
|
||||
void SXAPI lb_avl_tree_insert(struct avl_tree *tree, struct avl_tree_node *entry);
|
||||
|
||||
avl_tree_node_t *SXAPI lb_avl_tree_delete(avl_tree_t *tree, avl_tree_node_t *entry);
|
||||
struct avl_tree_node *SXAPI lb_avl_tree_delete(struct avl_tree *tree, struct avl_tree_node *entry);
|
||||
|
||||
void SXAPI lb_avl_tree_init(avl_tree_t *tree, callback_func_t compare);
|
||||
void SXAPI lb_avl_tree_init(struct avl_tree *tree, callback_func compare);
|
||||
|
||||
avl_tree_node_t *SXAPI lb_avl_tree_largest(avl_tree_t *tree);
|
||||
struct avl_tree_node *SXAPI lb_avl_tree_largest(struct avl_tree *tree);
|
||||
|
||||
avl_tree_node_t *SXAPI lb_avl_tree_smallest(avl_tree_t *tree);
|
||||
struct avl_tree_node *SXAPI lb_avl_tree_smallest(struct avl_tree *tree);
|
||||
|
||||
avl_tree_node_t *SXAPI lb_avl_tree_larger(avl_tree_node_t *entry);
|
||||
struct avl_tree_node *SXAPI lb_avl_tree_larger(struct avl_tree_node *entry);
|
||||
|
||||
avl_tree_node_t *SXAPI lb_avl_tree_smaller(avl_tree_node_t *entry);
|
||||
struct avl_tree_node *SXAPI lb_avl_tree_smaller(struct avl_tree_node *entry);
|
||||
|
||||
bool SXAPI lb_avl_tree_validate(avl_tree_t *tree);
|
||||
bool SXAPI lb_avl_tree_validate(struct avl_tree *tree);
|
||||
|
||||
int32_t SXAPI lb_avl_tree_size(avl_tree_t *tree);
|
||||
int32 SXAPI lb_avl_tree_size(struct avl_tree *tree);
|
||||
|
||||
#endif
|
||||
|
@ -1,56 +1,56 @@
|
||||
#ifndef _LIB_LINKED_LIST_H_
|
||||
#define _LIB_LINKED_LIST_H_
|
||||
#ifndef LIB_LINKED_LIST_H
|
||||
#define LIB_LINKED_LIST_H
|
||||
|
||||
#include "type.h"
|
||||
|
||||
typedef struct _linked_list_node_t
|
||||
struct linked_list_node
|
||||
{
|
||||
struct _linked_list_node_t *prev;
|
||||
struct _linked_list_node_t *next;
|
||||
} linked_list_node_t;
|
||||
struct linked_list_node *prev;
|
||||
struct linked_list_node *next;
|
||||
};
|
||||
|
||||
typedef struct _linked_list_t
|
||||
struct linked_list
|
||||
{
|
||||
linked_list_node_t *head;
|
||||
linked_list_node_t *tail;
|
||||
} linked_list_t;
|
||||
struct linked_list_node *head;
|
||||
struct linked_list_node *tail;
|
||||
};
|
||||
|
||||
/*
|
||||
* Returns true if current list node == your node
|
||||
* false otherwise
|
||||
*/
|
||||
|
||||
void SXAPI lb_linked_list_init(linked_list_t *list);
|
||||
void SXAPI lb_linked_list_init(struct linked_list *list);
|
||||
|
||||
int32_t SXAPI lb_linked_list_size(linked_list_t *list);
|
||||
int32 SXAPI lb_linked_list_size(struct linked_list *list);
|
||||
|
||||
void SXAPI lb_linked_list_push_front(linked_list_t *list, linked_list_node_t *node);
|
||||
void SXAPI lb_linked_list_push_front(struct linked_list *list, struct linked_list_node *node);
|
||||
|
||||
void SXAPI lb_linked_list_push_back(linked_list_t *list, linked_list_node_t *node);
|
||||
void SXAPI lb_linked_list_push_back(struct linked_list *list, struct linked_list_node *node);
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_pop_front(linked_list_t *list);
|
||||
struct linked_list_node *SXAPI lb_linked_list_pop_front(struct linked_list *list);
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_pop_back(linked_list_t *list);
|
||||
struct linked_list_node *SXAPI lb_linked_list_pop_back(struct linked_list *list);
|
||||
|
||||
void SXAPI lb_linked_list_insert(linked_list_t *list, int32_t index, linked_list_node_t *node);
|
||||
void SXAPI lb_linked_list_insert(struct linked_list *list, int32 index, struct linked_list_node *node);
|
||||
|
||||
void SXAPI lb_linked_list_insert_ref(linked_list_t *list, linked_list_node_t *prev_node, linked_list_node_t *node);
|
||||
void SXAPI lb_linked_list_insert_ref(struct linked_list *list, struct linked_list_node *prev_node, struct linked_list_node *node);
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_remove(linked_list_t *list, int32_t index);
|
||||
struct linked_list_node *SXAPI lb_linked_list_remove(struct linked_list *list, int32 index);
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_remove_ref(linked_list_t *list, linked_list_node_t *node);
|
||||
struct linked_list_node *SXAPI lb_linked_list_remove_ref(struct linked_list *list, struct linked_list_node *node);
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_get(linked_list_t *list, int32_t index);
|
||||
struct linked_list_node *SXAPI lb_linked_list_get(struct linked_list *list, int32 index);
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_next(linked_list_node_t *node);
|
||||
struct linked_list_node *SXAPI lb_linked_list_next(struct linked_list_node *node);
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_prev(linked_list_node_t *node);
|
||||
struct linked_list_node *SXAPI lb_linked_list_prev(struct linked_list_node *node);
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_first(linked_list_t *list);
|
||||
struct linked_list_node *SXAPI lb_linked_list_first(struct linked_list *list);
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_last(linked_list_t *list);
|
||||
struct linked_list_node *SXAPI lb_linked_list_last(struct linked_list *list);
|
||||
|
||||
int32_t SXAPI lb_linked_list_search(linked_list_t *list, linked_list_node_t *target,
|
||||
callback_func_t equals);
|
||||
int32 SXAPI lb_linked_list_search(struct linked_list *list, struct linked_list_node *target,
|
||||
callback_func equals);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,15 +1,15 @@
|
||||
#ifndef _LIB_SALLOC_H_
|
||||
#define _LIB_SALLOC_H_
|
||||
#ifndef LIB_SALLOC_H
|
||||
#define LIB_SALLOC_H
|
||||
|
||||
#include "type.h"
|
||||
|
||||
void SXAPI lb_salloc_init(void *base, uint32_t size);
|
||||
void SXAPI lb_salloc_init(void *base, uint32 size);
|
||||
|
||||
void *SXAPI lb_salloc(void *base, uint32_t size);
|
||||
void *SXAPI lb_salloc(void *base, uint32 size);
|
||||
|
||||
void SXAPI lb_sfree(void *base, void *ptr);
|
||||
|
||||
bool SXAPI lb_salloc_assert(void *base, uint32_t *blk_size, bool *blk_free, uint32_t size);
|
||||
bool SXAPI lb_salloc_assert(void *base, const uint32 *blk_size, const bool *blk_free, uint32 size);
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -1,63 +1,63 @@
|
||||
#ifndef _LIB_SXTDLIB_H_
|
||||
#define _LIB_SXTDLIB_H_
|
||||
#ifndef LIB_SXTDLIB_H
|
||||
#define LIB_SXTDLIB_H
|
||||
|
||||
#include "type.h"
|
||||
|
||||
uint32_t SXAPI lb_rand(void);
|
||||
uint32 SXAPI lb_rand(void);
|
||||
|
||||
void SXAPI lb_srand(uint32_t _seed);
|
||||
void SXAPI lb_srand(uint32 _seed);
|
||||
|
||||
void SXAPI lb_mrand(uint32_t max);
|
||||
void SXAPI lb_mrand(uint32 max);
|
||||
|
||||
uint64_t SXAPI lb_str_len(char const *str);
|
||||
uint64 SXAPI lb_str_len(char const *str);
|
||||
|
||||
uint64_t SXAPI lb_str_cmp(char const *str1, char const *str2);
|
||||
uint64 SXAPI lb_str_cmp(char const *str1, char const *str2);
|
||||
|
||||
void SXAPI lb_mem_copy(void *src, void *dst, uint64_t size);
|
||||
void SXAPI lb_mem_copy(void *src, void *dst, uint64 size);
|
||||
|
||||
void SXAPI lb_mem_move(void *src, void *dst, uint64_t size);
|
||||
void SXAPI lb_mem_move(void *src, void *dst, uint64 size);
|
||||
|
||||
void SXAPI lb_mem_set(void *src, uint8_t const val, uint64_t size);
|
||||
void SXAPI lb_mem_set(void *src, uint8 const val, uint64 size);
|
||||
|
||||
static inline uint64_t SXAPI lb_align_down(uint64_t val, uint64_t alignment)
|
||||
static inline uint64 SXAPI lb_align_down(uint64 val, uint64 alignment)
|
||||
{
|
||||
return (val / alignment) * alignment;
|
||||
}
|
||||
|
||||
static inline uint64_t SXAPI lb_align_up(uint64_t val, uint64_t alignment)
|
||||
static inline uint64 SXAPI lb_align_up(uint64 val, uint64 alignment)
|
||||
{
|
||||
return ((((val) % (alignment)) == 0) ? (((val) / (alignment)) * (alignment)) : (
|
||||
(((val) / (alignment)) * (alignment)) + 1));
|
||||
}
|
||||
|
||||
static inline uint64_t SXAPI lb_is_overlap(uint64_t x1, uint64_t x2, uint64_t y1, uint64_t y2)
|
||||
static inline uint64 SXAPI lb_is_overlap(uint64 x1, uint64 x2, uint64 y1, uint64 y2)
|
||||
{
|
||||
return ((x1 <= y2) && (y1 <= x2)) ? 1 : 0;
|
||||
}
|
||||
|
||||
|
||||
static inline int64_t SXAPI lb_max_64(int64_t a, int64_t b)
|
||||
static inline int64 SXAPI lb_max_64(int64 a, int64 b)
|
||||
{
|
||||
return (a) > (b) ? a : b;
|
||||
}
|
||||
|
||||
static inline int64_t SXAPI lb_min_64(int64_t a, int64_t b)
|
||||
static inline int64 SXAPI lb_min_64(int64 a, int64 b)
|
||||
{
|
||||
return (a) < (b) ? a : b;
|
||||
}
|
||||
|
||||
static inline int32_t SXAPI lb_max_32(int32_t a, int32_t b)
|
||||
static inline int32 SXAPI lb_max_32(int32 a, int32 b)
|
||||
{
|
||||
return (a) > (b) ? a : b;
|
||||
}
|
||||
|
||||
static inline int32_t SXAPI lb_min_32(int32_t a, int32_t b)
|
||||
static inline int32 SXAPI lb_min_32(int32 a, int32 b)
|
||||
{
|
||||
return (a) < (b) ? a : b;
|
||||
}
|
||||
|
||||
/*
|
||||
static inline uint64_t KAPI round_up_power_of_2(uint64_t num)
|
||||
static inline u64 KAPI round_up_power_of_2(u64 num)
|
||||
{
|
||||
num--;
|
||||
num |= num >> 1;
|
||||
@ -67,12 +67,12 @@ static inline uint64_t KAPI round_up_power_of_2(uint64_t num)
|
||||
num |= num >> 16;
|
||||
num |= num >> 32;
|
||||
num++;
|
||||
return (uint64_t)num;
|
||||
return (u64)num;
|
||||
}
|
||||
|
||||
static inline uint32_t KAPI log_base_2(uint64_t num)
|
||||
static inline uint32 KAPI log_base_2(u64 num)
|
||||
{
|
||||
uint32_t result = 0;
|
||||
uint32 result = 0;
|
||||
|
||||
while (num >>= 1)
|
||||
{
|
||||
@ -83,50 +83,50 @@ static inline uint32_t KAPI log_base_2(uint64_t num)
|
||||
}
|
||||
*/
|
||||
|
||||
#define OBTAIN_STRUCT_ADDR(member_addr, struct_name, member_name) ((struct_name*)((uintptr_t)(member_addr) - (uintptr_t)(&(((struct_name*)0)->member_name))))
|
||||
#define OBTAIN_STRUCT_ADDR(member_addr, struct_name, member_name) ((struct_name*)((uintptr)(member_addr) - (uintptr)(&(((struct_name*)0)->member_name))))
|
||||
|
||||
static inline uint64_t SXAPI lb_bit_mask(uint32_t bit)
|
||||
static inline uint64 SXAPI lb_bit_mask(uint32 bit)
|
||||
{
|
||||
return (uint64_t) 1 << bit;
|
||||
return (uint64) 1 << bit;
|
||||
}
|
||||
|
||||
static inline uint64_t SXAPI lb_bit_field_mask(uint32_t low, uint32_t high)
|
||||
static inline uint64 SXAPI lb_bit_field_mask(uint32 low, uint32 high)
|
||||
{
|
||||
return ~(~(uint64_t) 0 << high << 1) << low;
|
||||
return ~(~(uint64) 0 << high << 1) << low;
|
||||
}
|
||||
|
||||
static inline void SXAPI lb_bit_map_set(void *bit_map, uint64_t bit)
|
||||
static inline void SXAPI lb_bit_map_set(void *bit_map, uint64 bit)
|
||||
{
|
||||
if (bit_map != NULL)
|
||||
{
|
||||
uint64_t quot = bit >> 3;
|
||||
uint32_t rmd = (uint32_t) (bit & lb_bit_field_mask(0, 2));
|
||||
uint64 quot = bit >> 3;
|
||||
uint32 rmd = (uint32) (bit & lb_bit_field_mask(0, 2));
|
||||
|
||||
*((uint8_t *) (bit_map) + quot) |= (uint8_t) lb_bit_mask(rmd);
|
||||
*((uint8 *) (bit_map) + quot) |= (uint8) lb_bit_mask(rmd);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void SXAPI lb_bit_map_clear(void *bit_map, uint64_t bit)
|
||||
static inline void SXAPI lb_bit_map_clear(void *bit_map, uint64 bit)
|
||||
{
|
||||
if (bit_map != NULL)
|
||||
{
|
||||
uint64_t quot = bit >> 3;
|
||||
uint32_t rmd = (uint32_t) (bit & lb_bit_field_mask(0, 2));
|
||||
uint64 quot = bit >> 3;
|
||||
uint32 rmd = (uint32) (bit & lb_bit_field_mask(0, 2));
|
||||
|
||||
*((uint8_t *) (bit_map) + quot) &= ~(uint8_t) lb_bit_mask(rmd);
|
||||
*((uint8 *) (bit_map) + quot) &= ~(uint8) lb_bit_mask(rmd);
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint32_t SXAPI lb_bit_map_read(void *bit_map, uint64_t bit)
|
||||
static inline uint32 SXAPI lb_bit_map_read(void *bit_map, uint64 bit)
|
||||
{
|
||||
if (bit_map != NULL)
|
||||
{
|
||||
uint64_t quot = bit >> 3;
|
||||
uint32_t rmd = (uint32_t) (bit & lb_bit_field_mask(0, 2));
|
||||
uint64 quot = bit >> 3;
|
||||
uint32 rmd = (uint32) (bit & lb_bit_field_mask(0, 2));
|
||||
|
||||
return (*((uint8_t *) (bit_map) + quot) & (uint8_t) lb_bit_mask(rmd)) == 0 ? 0 : 1;
|
||||
return (*((uint8 *) (bit_map) + quot) & (uint8) lb_bit_mask(rmd)) == 0 ? 0 : 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,10 +1,10 @@
|
||||
#ifndef _STATUS_H_
|
||||
#define _STATUS_H_
|
||||
#ifndef STATUS_H
|
||||
#define STATUS_H
|
||||
|
||||
#include "type.h"
|
||||
#include "lib/sxtdlib.h"
|
||||
|
||||
typedef uint32_t status_t;
|
||||
typedef uint32 sx_status;
|
||||
|
||||
//
|
||||
// 32 bit ints
|
||||
@ -17,19 +17,19 @@ typedef uint32_t status_t;
|
||||
// bits 15-29 - Facility 32768 in total
|
||||
//
|
||||
|
||||
#define SX_MAKE_STATUS(Severity, Facility, Return) ((status_t)(((Severity) << 30) | ((Facility) << 16) | (Return)))
|
||||
#define SX_MAKE_STATUS(Severity, Facility, Return) ((sx_status)(((Severity) << 30) | ((Facility) << 16) | (Return)))
|
||||
|
||||
#define SEVERITY_ERROR 0x3
|
||||
#define SEVERITY_SUCCESS 0x0
|
||||
#define SEVERITY_INFO 0x1
|
||||
#define SEVERITY_ERROR 0x3ul
|
||||
#define SEVERITY_SUCCESS 0x0ul
|
||||
#define SEVERITY_INFO 0x1ul
|
||||
|
||||
#define FACILITY_GENERIC 0
|
||||
#define FACILITY_RF 1
|
||||
#define FACILITY_MM 2
|
||||
#define FACILITY_GENERIC 0ul
|
||||
#define FACILITY_RF 1ul
|
||||
#define FACILITY_MM 2ul
|
||||
|
||||
static inline bool sx_success(status_t status)
|
||||
static inline bool sx_success(sx_status status)
|
||||
{
|
||||
uint32_t severity = status >> 30;
|
||||
uint32 severity = status >> 30;
|
||||
return (severity == SEVERITY_INFO) || (severity == SEVERITY_SUCCESS);
|
||||
}
|
||||
|
||||
@ -47,4 +47,4 @@ static inline bool sx_success(status_t status)
|
||||
#define MM_UNINITIALIZED (SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_MM, 3))
|
||||
#define MM_NOT_ENOUGH_PAGE (SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_MM, 4))
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef _TEST_DRIVER_H_
|
||||
#define _TEST_DRIVER_H_
|
||||
#ifndef TEST_DRIVER_H
|
||||
#define TEST_DRIVER_H
|
||||
|
||||
#include "type.h"
|
||||
|
||||
@ -7,7 +7,7 @@ void SXAPI test_begin(char *name);
|
||||
|
||||
void SXAPI test_end(void);
|
||||
|
||||
void *SXAPI talloc(uint32_t size);
|
||||
void *SXAPI talloc(uint32 size);
|
||||
|
||||
void SXAPI run_case(char *name, bool result);
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef _TEST_TEST_H_
|
||||
#define _TEST_TEST_H_
|
||||
#ifndef TEST_TEST_H
|
||||
#define TEST_TEST_H
|
||||
|
||||
#include "type.h"
|
||||
|
||||
@ -9,4 +9,4 @@ void SXAPI avl_tree_test(void);
|
||||
|
||||
void SXAPI salloc_test(void);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,18 +1,32 @@
|
||||
#ifndef _TYPE_H_
|
||||
#define _TYPE_H_
|
||||
#ifndef TYPE_H
|
||||
#define TYPE_H
|
||||
|
||||
#define SXAPI __attribute__((sysv_abi))
|
||||
#define UAPI __attribute__((sysv_abi))
|
||||
#define SXTRAP _Noreturn
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef int32_t (*callback_func_t)(void *kernel_args, void *user_args);
|
||||
typedef uint32_t uint32;
|
||||
typedef int32_t int32;
|
||||
typedef uint64_t uint64;
|
||||
typedef int64_t int64;
|
||||
typedef uintptr_t uintptr;
|
||||
typedef uint16_t uint16;
|
||||
typedef int16_t int16;
|
||||
typedef uint8_t uint8;
|
||||
typedef int8_t int8;
|
||||
|
||||
typedef _Bool bool;
|
||||
#define TRUE (1)
|
||||
#define FALSE (0)
|
||||
|
||||
|
||||
typedef int32 (*callback_func)(void *kernel_args, void *user_args);
|
||||
|
||||
#define STRUCT_PACKED __attribute__((packed))
|
||||
|
||||
#define UNREFERENCED(x) {(x) = (x);}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -4,19 +4,19 @@
|
||||
|
||||
#define K_KERNEL_HEAP_SIZE 8192
|
||||
|
||||
static _Bool alloc_initialized;
|
||||
static uint8_t alloc_heap[K_KERNEL_HEAP_SIZE];
|
||||
static bool alloc_initialized;
|
||||
static uint8 alloc_heap[K_KERNEL_HEAP_SIZE];
|
||||
|
||||
void SXAPI ke_alloc_init(void)
|
||||
{
|
||||
if (!alloc_initialized)
|
||||
{
|
||||
lb_salloc_init(alloc_heap, K_KERNEL_HEAP_SIZE);
|
||||
alloc_initialized = true;
|
||||
alloc_initialized = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
void *SXAPI ke_alloc(uint32_t size)
|
||||
void *SXAPI ke_alloc(uint32 size)
|
||||
{
|
||||
return alloc_initialized ? lb_salloc(alloc_heap, size) : NULL;
|
||||
}
|
||||
@ -27,4 +27,4 @@ void SXAPI ke_free(void *ptr)
|
||||
{
|
||||
lb_sfree(alloc_heap, ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,10 @@
|
||||
#include "kernel/ke/assert.h"
|
||||
#include "kernel/ke/print.h"
|
||||
|
||||
void ke_assert_ex(const char *expr_str, const char *file, int32_t line, int32_t expr)
|
||||
void ke_assert_ex(const char *expr_str, const char *file, int32 line, int32 expr)
|
||||
{
|
||||
if (!expr)
|
||||
{
|
||||
ke_printf("Assertion \"%s\" failed at %s:%d.\n", expr_str, file, line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,18 @@
|
||||
#include "type.h"
|
||||
#include "kernel/ke/atomic.h"
|
||||
|
||||
int32_t SXAPI ke_interlocked_exchange_32(int32_t *target, int32_t val)
|
||||
int32 SXAPI ke_interlocked_exchange_32(int32 *target, int32 val)
|
||||
{
|
||||
return hal_interlocked_exchange_32(target, val);
|
||||
}
|
||||
|
||||
int32_t SXAPI ke_interlocked_increment_32(int32_t *target, int32_t increment)
|
||||
int32 SXAPI ke_interlocked_increment_32(int32 *target, int32 increment)
|
||||
{
|
||||
return hal_interlocked_increment_32(target, increment);
|
||||
}
|
||||
|
||||
int32_t SXAPI ke_interlocked_compare_exchange_32(int32_t *target, int32_t compare, int32_t val)
|
||||
int32 SXAPI ke_interlocked_compare_exchange_32(int32 *target, int32 compare, int32 val)
|
||||
{
|
||||
return hal_interlocked_compare_exchange_32(target, compare, val);
|
||||
}
|
||||
|
||||
|
@ -5,15 +5,14 @@
|
||||
* Kernel entry point
|
||||
* @param boot_info passed by the bootloader
|
||||
*/
|
||||
void SXAPI ke_main(boot_info_t *boot_info)
|
||||
void SXAPI ke_main(struct boot_info *boot_info)
|
||||
{
|
||||
status_t status = STATUS_SUCCESS;
|
||||
sx_status status = STATUS_SUCCESS;
|
||||
status = hal_init(boot_info);
|
||||
if (!sx_success(status))
|
||||
{
|
||||
ke_panic(status);
|
||||
return;
|
||||
}
|
||||
|
||||
ke_trap();
|
||||
}
|
||||
|
||||
|
@ -2,14 +2,14 @@
|
||||
#include "kernel/ke/print.h"
|
||||
#include "kernel/ke/bug_check.h"
|
||||
|
||||
void SXAPI ke_trap(void)
|
||||
void SXAPI SXTRAP ke_trap(void)
|
||||
{
|
||||
while (true)
|
||||
while (TRUE)
|
||||
{};
|
||||
}
|
||||
|
||||
void SXAPI ke_panic(uint64_t reason)
|
||||
void SXAPI SXTRAP ke_panic(uint64 reason)
|
||||
{
|
||||
ke_printf("BugCheck: Reason - %ul\n", reason);
|
||||
ke_trap();
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +1,25 @@
|
||||
#include "kernel/ke/assert.h"
|
||||
#include "kernel/ke/intr.h"
|
||||
|
||||
irql_t SXAPI ke_raise_irql(irql_t irql)
|
||||
k_irql SXAPI ke_raise_irql(k_irql irql)
|
||||
{
|
||||
ke_assert(ke_get_irql() <= irql);
|
||||
return hal_set_irql(irql);
|
||||
}
|
||||
|
||||
irql_t SXAPI ke_lower_irql(irql_t irql)
|
||||
k_irql SXAPI ke_lower_irql(k_irql irql)
|
||||
{
|
||||
irql_t old_irql = ke_get_irql();
|
||||
k_irql old_irql = ke_get_irql();
|
||||
ke_assert(old_irql >= irql);
|
||||
return hal_set_irql(irql);
|
||||
}
|
||||
|
||||
irql_t SXAPI ke_get_irql(void)
|
||||
k_irql SXAPI ke_get_irql(void)
|
||||
{
|
||||
return hal_get_irql();
|
||||
}
|
||||
|
||||
int SXAPI ke_get_current_core(void)
|
||||
uint32 SXAPI ke_get_current_core(void)
|
||||
{
|
||||
return hal_get_core_id();
|
||||
}
|
||||
}
|
||||
|
@ -13,4 +13,4 @@ void SXAPI ke_vprintf(const char *str, va_list args)
|
||||
{
|
||||
hal_vprintf(str, args);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -71,30 +71,30 @@ void ke_rwwlock_writer_unlock(k_rwwlock_t *lock)
|
||||
ke_spin_unlock(&lock->w_mutex);
|
||||
}
|
||||
|
||||
irql_t ke_rwwlock_reader_lock_raise_irql(k_rwwlock_t *lock, irql_t irql)
|
||||
k_irql ke_rwwlock_reader_lock_raise_irql(k_rwwlock_t *lock, k_irql irql)
|
||||
{
|
||||
irql_t old_irql = ke_raise_irql(irql);
|
||||
k_irql old_irql = ke_raise_irql(irql);
|
||||
ke_rwwlock_reader_lock(lock);
|
||||
return old_irql;
|
||||
}
|
||||
|
||||
void ke_rwwlock_reader_unlock_lower_irql(k_rwwlock_t *lock, irql_t irql)
|
||||
void ke_rwwlock_reader_unlock_lower_irql(k_rwwlock_t *lock, k_irql irql)
|
||||
{
|
||||
ke_rwwlock_reader_unlock(lock);
|
||||
ke_lower_irql(irql);
|
||||
return;
|
||||
}
|
||||
|
||||
irql_t ke_rwwlock_writer_lock_raise_irql(k_rwwlock_t *lock, irql_t irql)
|
||||
k_irql ke_rwwlock_writer_lock_raise_irql(k_rwwlock_t *lock, k_irql irql)
|
||||
{
|
||||
irql_t old_irql = ke_raise_irql(irql);
|
||||
k_irql old_irql = ke_raise_irql(irql);
|
||||
ke_rwwlock_writer_lock(lock);
|
||||
return old_irql;
|
||||
}
|
||||
|
||||
void ke_rwwlock_writer_unlock_lower_irql(k_rwwlock_t *lock, irql_t irql)
|
||||
void ke_rwwlock_writer_unlock_lower_irql(k_rwwlock_t *lock, k_irql irql)
|
||||
{
|
||||
ke_rwwlock_writer_unlock(lock);
|
||||
ke_lower_irql(irql);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -28,9 +28,9 @@ void SXAPI ke_spin_unlock(k_spin_lock_t *lock)
|
||||
return;
|
||||
}
|
||||
|
||||
irql_t SXAPI ke_spin_lock_raise_irql(k_spin_lock_t *lock, irql_t irql)
|
||||
k_irql SXAPI ke_spin_lock_raise_irql(k_spin_lock_t *lock, k_irql irql)
|
||||
{
|
||||
irql_t prev_irql = ke_get_irql();
|
||||
k_irql prev_irql = ke_get_irql();
|
||||
if (lock != NULL)
|
||||
{
|
||||
ke_raise_irql(irql);
|
||||
@ -39,7 +39,7 @@ irql_t SXAPI ke_spin_lock_raise_irql(k_spin_lock_t *lock, irql_t irql)
|
||||
return prev_irql;
|
||||
}
|
||||
|
||||
void SXAPI ke_spin_unlock_lower_irql(k_spin_lock_t *lock, irql_t irql)
|
||||
void SXAPI ke_spin_unlock_lower_irql(k_spin_lock_t *lock, k_irql irql)
|
||||
{
|
||||
if (lock != NULL)
|
||||
{
|
||||
@ -47,4 +47,4 @@ void SXAPI ke_spin_unlock_lower_irql(k_spin_lock_t *lock, irql_t irql)
|
||||
ke_lower_irql(irql);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -4,16 +4,16 @@
|
||||
#include "kernel/ke/alloc.h"
|
||||
#include "kernel/mm/pmm.h"
|
||||
|
||||
typedef struct
|
||||
struct phys_page_desc
|
||||
{
|
||||
linked_list_node_t free_list_node;
|
||||
avl_tree_node_t avl_tree_node;
|
||||
uintptr_t base;
|
||||
int32_t attr;
|
||||
} physical_page_descriptor_t;
|
||||
struct linked_list_node free_list_node;
|
||||
struct avl_tree_node tree_node;
|
||||
uintptr base;
|
||||
int32 attr;
|
||||
};
|
||||
|
||||
static avl_tree_t active_tree;
|
||||
static linked_list_t free_list;
|
||||
static struct avl_tree active_tree;
|
||||
static struct linked_list free_list;
|
||||
static k_rwwlock_t lock;
|
||||
static _Bool initialized;
|
||||
|
||||
@ -24,14 +24,14 @@ static _Bool initialized;
|
||||
* = 0 if tree_node == your_node
|
||||
* > 0 if tree_node > your_node
|
||||
*/
|
||||
static int32_t mmp_base_paddr_compare(void *tree_node, void *my_node)
|
||||
static int32 mmp_base_paddr_compare(void *tree_node, void *my_node)
|
||||
{
|
||||
uintptr_t tree_base = OBTAIN_STRUCT_ADDR(tree_node,
|
||||
physical_page_descriptor_t,
|
||||
avl_tree_node)->base;
|
||||
uintptr_t my_base = OBTAIN_STRUCT_ADDR(my_node,
|
||||
physical_page_descriptor_t,
|
||||
avl_tree_node)->base;
|
||||
uintptr tree_base = OBTAIN_STRUCT_ADDR(tree_node,
|
||||
struct phys_page_desc,
|
||||
tree_node)->base;
|
||||
uintptr my_base = OBTAIN_STRUCT_ADDR(my_node,
|
||||
struct phys_page_desc,
|
||||
tree_node)->base;
|
||||
if (tree_base > my_base)
|
||||
{
|
||||
return 1;
|
||||
@ -49,7 +49,7 @@ static int32_t mmp_base_paddr_compare(void *tree_node, void *my_node)
|
||||
}
|
||||
}
|
||||
|
||||
status_t SXAPI sx_pmm_init(pmm_info_t *info)
|
||||
sx_status SXAPI sx_pmm_init(pmm_info_t *info)
|
||||
{
|
||||
if (info == NULL)
|
||||
{
|
||||
@ -65,19 +65,19 @@ status_t SXAPI sx_pmm_init(pmm_info_t *info)
|
||||
lb_linked_list_init(&free_list);
|
||||
lb_avl_tree_init(&active_tree, mmp_base_paddr_compare);
|
||||
|
||||
for (uint32_t i = 0; i < info->num_of_nodes; i++)
|
||||
for (uint32 i = 0; i < info->num_of_nodes; i++)
|
||||
{
|
||||
pmm_node_t *each_node = &info->nodes[i];
|
||||
|
||||
ke_assert (each_node->base % KERNEL_PAGE_SIZE != 0);
|
||||
|
||||
for (uint64_t j = 0; j <= each_node->size; j++)
|
||||
for (uint64 j = 0; j <= each_node->size; j++)
|
||||
{
|
||||
// note that k_alloc function here might trigger page fault
|
||||
// however it's fine as long as we don't touch linked list just yet
|
||||
// it will use the pages that are already on file to enlarge the kernel heap
|
||||
// don't worry, be happy :)
|
||||
physical_page_descriptor_t *page_info = ke_alloc(sizeof(physical_page_descriptor_t));
|
||||
struct phys_page_desc *page_info = ke_alloc(sizeof(struct phys_page_desc));
|
||||
|
||||
if (page_info == NULL)
|
||||
{
|
||||
@ -88,7 +88,7 @@ status_t SXAPI sx_pmm_init(pmm_info_t *info)
|
||||
lb_linked_list_push_back(&free_list, &page_info->free_list_node);
|
||||
}
|
||||
}
|
||||
initialized = true;
|
||||
initialized = TRUE;
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
@ -97,7 +97,7 @@ status_t SXAPI sx_pmm_init(pmm_info_t *info)
|
||||
// potential callers of these, since timer/interrupts queue DPC, which might trigger
|
||||
// page fault (kernel heap), therefore, it must set IRQL to DISABLED
|
||||
|
||||
status_t SXAPI mm_alloc_page(uintptr_t *out)
|
||||
sx_status SXAPI mm_alloc_page(uintptr *out)
|
||||
{
|
||||
if (!initialized)
|
||||
{
|
||||
@ -109,17 +109,17 @@ status_t SXAPI mm_alloc_page(uintptr_t *out)
|
||||
return MM_INVALID_ARGUMENTS;
|
||||
}
|
||||
|
||||
irql_t irql = ke_rwwlock_writer_lock_raise_irql(&lock, IRQL_DISABLED_LEVEL);
|
||||
status_t result = STATUS_SUCCESS;
|
||||
linked_list_node_t *node = NULL;
|
||||
physical_page_descriptor_t *page_info = NULL;
|
||||
k_irql irql = ke_rwwlock_writer_lock_raise_irql(&lock, IRQL_DISABLED_LEVEL);
|
||||
sx_status result = STATUS_SUCCESS;
|
||||
struct linked_list_node *node = NULL;
|
||||
struct phys_page_desc *page_info = NULL;
|
||||
node = lb_linked_list_pop_front(&free_list);
|
||||
if (node != NULL)
|
||||
{
|
||||
page_info = OBTAIN_STRUCT_ADDR(node,
|
||||
physical_page_descriptor_t,
|
||||
struct phys_page_desc,
|
||||
free_list_node);
|
||||
lb_avl_tree_insert(&active_tree, &page_info->avl_tree_node);
|
||||
lb_avl_tree_insert(&active_tree, &page_info->tree_node);
|
||||
*out = page_info->base;
|
||||
}
|
||||
else
|
||||
@ -131,8 +131,8 @@ status_t SXAPI mm_alloc_page(uintptr_t *out)
|
||||
return result;
|
||||
}
|
||||
|
||||
status_t SXAPI mm_query_page_attr(uintptr_t base,
|
||||
int32_t *out)
|
||||
sx_status SXAPI mm_query_page_attr(uintptr base,
|
||||
int32 *out)
|
||||
{
|
||||
if (!initialized)
|
||||
{
|
||||
@ -144,18 +144,18 @@ status_t SXAPI mm_query_page_attr(uintptr_t base,
|
||||
return MM_INVALID_ARGUMENTS;
|
||||
}
|
||||
|
||||
irql_t irql = ke_rwwlock_reader_lock_raise_irql(&lock, IRQL_DISABLED_LEVEL);
|
||||
status_t result = STATUS_SUCCESS;
|
||||
avl_tree_node_t *node = NULL;
|
||||
k_irql irql = ke_rwwlock_reader_lock_raise_irql(&lock, IRQL_DISABLED_LEVEL);
|
||||
sx_status result = STATUS_SUCCESS;
|
||||
struct avl_tree_node *node = NULL;
|
||||
// search for dummy
|
||||
physical_page_descriptor_t dummy, *page_info = NULL;
|
||||
struct phys_page_desc dummy, *page_info = NULL;
|
||||
dummy.base = base;
|
||||
|
||||
node = lb_avl_tree_delete(&active_tree, &dummy.avl_tree_node);
|
||||
node = lb_avl_tree_delete(&active_tree, &dummy.tree_node);
|
||||
|
||||
if (node != NULL)
|
||||
{
|
||||
page_info = OBTAIN_STRUCT_ADDR(node, physical_page_descriptor_t, avl_tree_node);
|
||||
page_info = OBTAIN_STRUCT_ADDR(node, struct phys_page_desc, tree_node);
|
||||
*out = page_info->attr;
|
||||
}
|
||||
else
|
||||
@ -168,7 +168,7 @@ status_t SXAPI mm_query_page_attr(uintptr_t base,
|
||||
return result;
|
||||
}
|
||||
|
||||
status_t SXAPI mm_free_page(uintptr_t base)
|
||||
sx_status SXAPI mm_free_page(uintptr base)
|
||||
{
|
||||
if (!initialized)
|
||||
{
|
||||
@ -176,17 +176,17 @@ status_t SXAPI mm_free_page(uintptr_t base)
|
||||
}
|
||||
|
||||
// just lock since not sharing with anyone
|
||||
irql_t irql = ke_rwwlock_writer_lock_raise_irql(&lock, IRQL_DISABLED_LEVEL);
|
||||
status_t result = STATUS_SUCCESS;
|
||||
avl_tree_node_t *node = NULL;
|
||||
k_irql irql = ke_rwwlock_writer_lock_raise_irql(&lock, IRQL_DISABLED_LEVEL);
|
||||
sx_status result = STATUS_SUCCESS;
|
||||
struct avl_tree_node *node = NULL;
|
||||
// search for dummy
|
||||
physical_page_descriptor_t dummy, *page_info;
|
||||
struct phys_page_desc dummy, *page_info;
|
||||
dummy.base = base;
|
||||
|
||||
node = lb_avl_tree_delete(&active_tree, &dummy.avl_tree_node);
|
||||
node = lb_avl_tree_delete(&active_tree, &dummy.tree_node);
|
||||
if (node != NULL)
|
||||
{
|
||||
page_info = OBTAIN_STRUCT_ADDR(node, physical_page_descriptor_t, avl_tree_node);
|
||||
page_info = OBTAIN_STRUCT_ADDR(node, struct phys_page_desc, tree_node);
|
||||
lb_linked_list_push_back(&free_list, &page_info->free_list_node);
|
||||
}
|
||||
else
|
||||
@ -197,4 +197,4 @@ status_t SXAPI mm_free_page(uintptr_t base)
|
||||
ke_rwwlock_writer_unlock_lower_irql(&lock, irql);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -8,13 +8,13 @@
|
||||
|
||||
typedef struct
|
||||
{
|
||||
avl_tree_node_t tree_node;
|
||||
struct avl_tree_node tree_node;
|
||||
handle_t handle;
|
||||
ref_node_t *ref;
|
||||
callback_func_t free_routine;
|
||||
callback_func free_routine;
|
||||
} handle_node_t;
|
||||
|
||||
static int32_t rfp_handle_node_free(void *node, void *up)
|
||||
static int32 rfp_handle_node_free(void *node, void *up)
|
||||
{
|
||||
UNREFERENCED(up);
|
||||
ke_free(node);
|
||||
@ -25,23 +25,23 @@ static int32_t rfp_handle_node_free(void *node, void *up)
|
||||
// Ke Functions
|
||||
// ===========================
|
||||
|
||||
static avl_tree_t handle_tree;
|
||||
static struct avl_tree handle_tree;
|
||||
static bool initialized;
|
||||
static k_spin_lock_t handle_tree_lock;
|
||||
static int32_t handle_base;
|
||||
static uint32 handle_base;
|
||||
|
||||
static int32_t rfp_handle_compare(void *tree_node, void *my_node)
|
||||
static int32 rfp_handle_compare(void *tree_node, void *my_node)
|
||||
{
|
||||
handle_node_t *tcb = OBTAIN_STRUCT_ADDR(tree_node, handle_node_t, tree_node);
|
||||
handle_node_t *my_tcb = OBTAIN_STRUCT_ADDR(my_node, handle_node_t, tree_node);
|
||||
|
||||
if ((uintptr_t) tcb->handle > (uintptr_t) my_tcb->handle)
|
||||
if ((uintptr) tcb->handle > (uintptr) my_tcb->handle)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((uintptr_t) tcb->handle == (uintptr_t) my_tcb->handle)
|
||||
if ((uintptr) tcb->handle == (uintptr) my_tcb->handle)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@ -54,27 +54,27 @@ static int32_t rfp_handle_compare(void *tree_node, void *my_node)
|
||||
|
||||
static handle_node_t *rfp_search_handle_node(handle_t handle)
|
||||
{
|
||||
avl_tree_node_t *result;
|
||||
struct avl_tree_node *result;
|
||||
handle_node_t temp;
|
||||
temp.handle = handle;
|
||||
result = lb_avl_tree_search(&handle_tree, &temp.tree_node);
|
||||
return result == NULL ? NULL : OBTAIN_STRUCT_ADDR(result, handle_node_t, tree_node);
|
||||
}
|
||||
|
||||
status_t SXAPI rf_reference_setup(void)
|
||||
sx_status SXAPI rf_reference_setup(void)
|
||||
{
|
||||
if (!initialized)
|
||||
{
|
||||
lb_avl_tree_init(&handle_tree, rfp_handle_compare);
|
||||
ke_spin_lock_init(&handle_tree_lock);
|
||||
handle_base = K_HANDLE_BASE;
|
||||
initialized = true;
|
||||
initialized = TRUE;
|
||||
}
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
status_t SXAPI rf_reference_create(ref_node_t *ref,
|
||||
callback_func_t free_func)
|
||||
sx_status SXAPI rf_reference_create(ref_node_t *ref,
|
||||
callback_func free_func)
|
||||
{
|
||||
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
|
||||
|
||||
@ -89,7 +89,7 @@ status_t SXAPI rf_reference_create(ref_node_t *ref,
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
status_t SXAPI rf_reference_obj(ref_node_t *ref_node)
|
||||
sx_status SXAPI rf_reference_obj(ref_node_t *ref_node)
|
||||
{
|
||||
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
|
||||
|
||||
@ -98,14 +98,14 @@ status_t SXAPI rf_reference_obj(ref_node_t *ref_node)
|
||||
return RF_INVALID_ARGUMENTS;
|
||||
}
|
||||
|
||||
int32_t old_ref_count = ke_interlocked_increment_32(&ref_node->ref_count, 1);
|
||||
int32 old_ref_count = ke_interlocked_increment_32(&ref_node->ref_count, 1);
|
||||
|
||||
ke_assert(old_ref_count >= 1);
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
status_t SXAPI rf_dereference_obj(ref_node_t *ref_node)
|
||||
sx_status SXAPI rf_dereference_obj(ref_node_t *ref_node)
|
||||
{
|
||||
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
|
||||
|
||||
@ -114,9 +114,9 @@ status_t SXAPI rf_dereference_obj(ref_node_t *ref_node)
|
||||
return RF_INVALID_ARGUMENTS;
|
||||
}
|
||||
|
||||
status_t result = STATUS_SUCCESS;
|
||||
sx_status result = STATUS_SUCCESS;
|
||||
|
||||
int32_t old_ref_count = ke_interlocked_increment_32(&ref_node->ref_count, -1);
|
||||
int32 old_ref_count = ke_interlocked_increment_32(&ref_node->ref_count, -1);
|
||||
|
||||
ke_assert(old_ref_count >= 1);
|
||||
|
||||
@ -129,7 +129,7 @@ status_t SXAPI rf_dereference_obj(ref_node_t *ref_node)
|
||||
}
|
||||
|
||||
|
||||
static status_t SXAPI rf_open_obj_by_handle(handle_t handle, ref_node_t **out)
|
||||
static sx_status SXAPI rf_open_obj_by_handle(handle_t handle, ref_node_t **out)
|
||||
{
|
||||
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
|
||||
|
||||
@ -143,8 +143,8 @@ static status_t SXAPI rf_open_obj_by_handle(handle_t handle, ref_node_t **out)
|
||||
return RF_INVALID_ARGUMENTS;
|
||||
}
|
||||
|
||||
irql_t irql;
|
||||
status_t status = STATUS_SUCCESS;
|
||||
k_irql irql;
|
||||
sx_status status = STATUS_SUCCESS;
|
||||
ref_node_t *ref = NULL;
|
||||
|
||||
|
||||
@ -173,7 +173,7 @@ static status_t SXAPI rf_open_obj_by_handle(handle_t handle, ref_node_t **out)
|
||||
return status;
|
||||
}
|
||||
|
||||
static status_t SXAPI rf_create_handle(ref_node_t *ref,
|
||||
static sx_status SXAPI rf_create_handle(ref_node_t *ref,
|
||||
handle_node_t *node,
|
||||
handle_t *out)
|
||||
{
|
||||
@ -189,14 +189,14 @@ static status_t SXAPI rf_create_handle(ref_node_t *ref,
|
||||
return RF_INVALID_ARGUMENTS;
|
||||
}
|
||||
|
||||
status_t result = STATUS_SUCCESS;
|
||||
irql_t irql;
|
||||
sx_status result = STATUS_SUCCESS;
|
||||
k_irql irql;
|
||||
|
||||
|
||||
if (sx_success(result))
|
||||
{
|
||||
// TODO: CHECK OVERFLOW
|
||||
node->handle = (handle_t) ke_interlocked_increment_32(&handle_base, 1);
|
||||
node->handle = (handle_t) ke_interlocked_increment_32((int32*)&handle_base, 1);
|
||||
node->ref = ref;
|
||||
irql = ke_spin_lock_raise_irql(&handle_tree_lock, IRQL_DPC_LEVEL);
|
||||
handle_node_t *existing_node = rfp_search_handle_node(node->handle);
|
||||
@ -226,7 +226,7 @@ static status_t SXAPI rf_create_handle(ref_node_t *ref,
|
||||
return result;
|
||||
}
|
||||
|
||||
static status_t SXAPI rf_close_handle(handle_t handle)
|
||||
static sx_status SXAPI rf_close_handle(handle_t handle)
|
||||
{
|
||||
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
|
||||
|
||||
@ -235,10 +235,10 @@ static status_t SXAPI rf_close_handle(handle_t handle)
|
||||
return RF_UNINITIALIZED;
|
||||
}
|
||||
|
||||
irql_t irql;
|
||||
status_t status = STATUS_SUCCESS;
|
||||
k_irql irql;
|
||||
sx_status status = STATUS_SUCCESS;
|
||||
ref_node_t *ref = NULL;
|
||||
bool free = false;
|
||||
bool free = FALSE;
|
||||
|
||||
irql = ke_spin_lock_raise_irql(&handle_tree_lock, IRQL_DPC_LEVEL);
|
||||
handle_node_t *handle_node = rfp_search_handle_node(handle);
|
||||
@ -250,7 +250,7 @@ static status_t SXAPI rf_close_handle(handle_t handle)
|
||||
{
|
||||
ref = handle_node->ref;
|
||||
lb_avl_tree_delete(&handle_tree, &handle_node->tree_node);
|
||||
free = true;
|
||||
free = TRUE;
|
||||
}
|
||||
ke_spin_unlock_lower_irql(&handle_tree_lock, irql);
|
||||
|
||||
@ -273,7 +273,7 @@ static status_t SXAPI rf_close_handle(handle_t handle)
|
||||
// SX Functions
|
||||
// ===========================
|
||||
|
||||
status_t SXAPI sx_create_handle(ref_node_t *ref, handle_t *out)
|
||||
sx_status SXAPI sx_create_handle(ref_node_t *ref, handle_t *out)
|
||||
{
|
||||
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
|
||||
|
||||
@ -294,7 +294,7 @@ status_t SXAPI sx_create_handle(ref_node_t *ref, handle_t *out)
|
||||
return rf_create_handle(ref, node, out);
|
||||
}
|
||||
|
||||
status_t SXAPI sx_close_handle(handle_t handle)
|
||||
sx_status SXAPI sx_close_handle(handle_t handle)
|
||||
{
|
||||
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
|
||||
|
||||
@ -308,7 +308,7 @@ status_t SXAPI sx_close_handle(handle_t handle)
|
||||
return rf_close_handle(handle);
|
||||
}
|
||||
|
||||
status_t SXAPI sx_open_obj_by_handle(handle_t handle, ref_node_t **out)
|
||||
sx_status SXAPI sx_open_obj_by_handle(handle_t handle, ref_node_t **out)
|
||||
{
|
||||
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
|
||||
|
||||
|
117
lib/avl_tree.c
117
lib/avl_tree.c
@ -1,11 +1,11 @@
|
||||
#include "lib/avl_tree.h"
|
||||
|
||||
static inline int32_t SXAPI lbp_avl_tree_node_get_height(avl_tree_node_t *node)
|
||||
static inline int32 SXAPI lbp_avl_tree_node_get_height(struct avl_tree_node *node)
|
||||
{
|
||||
return node == NULL ? -1 : node->height;
|
||||
}
|
||||
|
||||
static inline int32_t SXAPI lbp_avl_tree_node_get_balance_factor(avl_tree_node_t *node)
|
||||
static inline int32 SXAPI lbp_avl_tree_node_get_balance_factor(struct avl_tree_node *node)
|
||||
{
|
||||
if (node == NULL)
|
||||
{
|
||||
@ -14,9 +14,9 @@ static inline int32_t SXAPI lbp_avl_tree_node_get_balance_factor(avl_tree_node_t
|
||||
return lbp_avl_tree_node_get_height(node->left) - lbp_avl_tree_node_get_height(node->right);
|
||||
}
|
||||
|
||||
static avl_tree_node_t *SXAPI lbp_avl_tree_node_right_rotate(avl_tree_node_t *root)
|
||||
static struct avl_tree_node *SXAPI lbp_avl_tree_node_right_rotate(struct avl_tree_node *root)
|
||||
{
|
||||
avl_tree_node_t *left_children = root->left;
|
||||
struct avl_tree_node *left_children = root->left;
|
||||
//adjust parents first
|
||||
left_children->parent = root->parent;
|
||||
root->parent = left_children;
|
||||
@ -35,9 +35,9 @@ static avl_tree_node_t *SXAPI lbp_avl_tree_node_right_rotate(avl_tree_node_t *ro
|
||||
return left_children;
|
||||
}
|
||||
|
||||
static avl_tree_node_t *SXAPI lbp_avl_tree_node_left_rotate(avl_tree_node_t *root)
|
||||
static struct avl_tree_node *SXAPI lbp_avl_tree_node_left_rotate(struct avl_tree_node *root)
|
||||
{
|
||||
avl_tree_node_t *right_children = root->right;
|
||||
struct avl_tree_node *right_children = root->right;
|
||||
//adjust parents
|
||||
right_children->parent = root->parent;
|
||||
root->parent = right_children;
|
||||
@ -57,13 +57,13 @@ static avl_tree_node_t *SXAPI lbp_avl_tree_node_left_rotate(avl_tree_node_t *roo
|
||||
return right_children;
|
||||
}
|
||||
|
||||
static avl_tree_node_t *SXAPI lbp_avl_tree_node_balance(avl_tree_node_t *node)
|
||||
static struct avl_tree_node *SXAPI lbp_avl_tree_node_balance(struct avl_tree_node *node)
|
||||
{
|
||||
const int32_t bf = lbp_avl_tree_node_get_balance_factor(node);
|
||||
const int32 bf = lbp_avl_tree_node_get_balance_factor(node);
|
||||
|
||||
if (bf > 1)
|
||||
{
|
||||
const int32_t left_bf = lbp_avl_tree_node_get_balance_factor(node->left);
|
||||
const int32 left_bf = lbp_avl_tree_node_get_balance_factor(node->left);
|
||||
if (left_bf >= 0)
|
||||
{
|
||||
//left left
|
||||
@ -80,7 +80,7 @@ static avl_tree_node_t *SXAPI lbp_avl_tree_node_balance(avl_tree_node_t *node)
|
||||
{
|
||||
if (bf < -1)
|
||||
{
|
||||
const int32_t right_bf = lbp_avl_tree_node_get_balance_factor(node->right);
|
||||
const int32 right_bf = lbp_avl_tree_node_get_balance_factor(node->right);
|
||||
if (right_bf <= 0)
|
||||
{
|
||||
// right right
|
||||
@ -101,9 +101,9 @@ static avl_tree_node_t *SXAPI lbp_avl_tree_node_balance(avl_tree_node_t *node)
|
||||
|
||||
}
|
||||
|
||||
static avl_tree_node_t *SXAPI lbp_avl_tree_node_insert(avl_tree_node_t *root, avl_tree_node_t *node,
|
||||
callback_func_t compare,
|
||||
avl_tree_node_t *parent)
|
||||
static struct avl_tree_node *SXAPI lbp_avl_tree_node_insert(struct avl_tree_node *root, struct avl_tree_node *node,
|
||||
callback_func compare,
|
||||
struct avl_tree_node *parent)
|
||||
{
|
||||
if (node == NULL || compare == NULL)
|
||||
{
|
||||
@ -115,7 +115,7 @@ static avl_tree_node_t *SXAPI lbp_avl_tree_node_insert(avl_tree_node_t *root, av
|
||||
return node;
|
||||
}
|
||||
|
||||
const int32_t comp = compare(root, node);
|
||||
const int32 comp = compare(root, node);
|
||||
if (comp < 0)
|
||||
{
|
||||
root->right = lbp_avl_tree_node_insert(root->right, node, compare, root);
|
||||
@ -137,16 +137,17 @@ static avl_tree_node_t *SXAPI lbp_avl_tree_node_insert(avl_tree_node_t *root, av
|
||||
return lbp_avl_tree_node_balance(root);
|
||||
}
|
||||
|
||||
static void lbp_avl_tree_swap_nodes(avl_tree_node_t *node1, avl_tree_node_t *node2)
|
||||
static void SXAPI lbp_avl_tree_swap_nodes(struct avl_tree_node *node1, struct avl_tree_node *node2)
|
||||
{
|
||||
if (node1 == NULL || node2 == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
avl_tree_node_t *parent = NULL;
|
||||
avl_tree_node_t *child = NULL;
|
||||
avl_tree_node_t *temp = NULL;
|
||||
struct avl_tree_node *parent = NULL;
|
||||
struct avl_tree_node *child = NULL;
|
||||
struct avl_tree_node *temp = NULL;
|
||||
//swap node but does not change anything else other than node1,node2
|
||||
// determine the parent/child relationship
|
||||
if (node1->parent != NULL && node1->parent == node2)
|
||||
{
|
||||
parent = node2;
|
||||
@ -282,22 +283,21 @@ static void lbp_avl_tree_swap_nodes(avl_tree_node_t *node1, avl_tree_node_t *nod
|
||||
}
|
||||
|
||||
//swap height
|
||||
int32_t height = node1->height;
|
||||
int32 height = node1->height;
|
||||
node1->height = node2->height;
|
||||
node2->height = height;
|
||||
return;
|
||||
}
|
||||
|
||||
static avl_tree_node_t *SXAPI lbp_avl_tree_node_delete(avl_tree_node_t *root,
|
||||
avl_tree_node_t *node,
|
||||
callback_func_t compare,
|
||||
avl_tree_node_t **deleted_node)
|
||||
static struct avl_tree_node *SXAPI lbp_avl_tree_node_delete(struct avl_tree_node *root,
|
||||
struct avl_tree_node *node,
|
||||
callback_func compare,
|
||||
struct avl_tree_node **deleted_node)
|
||||
{
|
||||
if (root == NULL || node == NULL || compare == NULL || deleted_node == NULL)
|
||||
{
|
||||
return root;
|
||||
}
|
||||
const int32_t comp = compare(root, node);
|
||||
const int32 comp = compare(root, node);
|
||||
if (comp < 0)
|
||||
{
|
||||
root->right = lbp_avl_tree_node_delete(root->right, node, compare, deleted_node);
|
||||
@ -314,7 +314,7 @@ static avl_tree_node_t *SXAPI lbp_avl_tree_node_delete(avl_tree_node_t *root,
|
||||
// node with only one child or no child
|
||||
if ((root->left == NULL) || (root->right == NULL))
|
||||
{
|
||||
avl_tree_node_t *child = root->left != NULL ? root->left : root->right;
|
||||
struct avl_tree_node *child = root->left != NULL ? root->left : root->right;
|
||||
|
||||
if (child == NULL)
|
||||
{ // 0 child
|
||||
@ -330,7 +330,7 @@ static avl_tree_node_t *SXAPI lbp_avl_tree_node_delete(avl_tree_node_t *root,
|
||||
{
|
||||
// node with two children: Get the inorder successor (smallest
|
||||
// in the right subtree)
|
||||
avl_tree_node_t *successor = lb_avl_tree_larger(root);
|
||||
struct avl_tree_node *successor = lb_avl_tree_larger(root);
|
||||
//swap fields
|
||||
lbp_avl_tree_swap_nodes(successor, root);
|
||||
|
||||
@ -350,14 +350,14 @@ static avl_tree_node_t *SXAPI lbp_avl_tree_node_delete(avl_tree_node_t *root,
|
||||
return root;
|
||||
}
|
||||
|
||||
static avl_tree_node_t *SXAPI lbp_avl_tree_node_search(avl_tree_node_t *root, avl_tree_node_t *node,
|
||||
callback_func_t compare)
|
||||
static struct avl_tree_node *SXAPI lbp_avl_tree_node_search(struct avl_tree_node *root, struct avl_tree_node *node,
|
||||
callback_func compare)
|
||||
{
|
||||
if (root == NULL || compare == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
const int32_t comp = compare(root, node);
|
||||
const int32 comp = compare(root, node);
|
||||
if (comp < 0)
|
||||
{
|
||||
return lbp_avl_tree_node_search(root->right, node, compare);
|
||||
@ -376,7 +376,7 @@ static avl_tree_node_t *SXAPI lbp_avl_tree_node_search(avl_tree_node_t *root, av
|
||||
}
|
||||
|
||||
|
||||
static void SXAPI lbp_avl_tree_node_init(avl_tree_node_t *it)
|
||||
static void SXAPI lbp_avl_tree_node_init(struct avl_tree_node *it)
|
||||
{
|
||||
if (it != NULL)
|
||||
{
|
||||
@ -385,17 +385,16 @@ static void SXAPI lbp_avl_tree_node_init(avl_tree_node_t *it)
|
||||
it->right = NULL;
|
||||
it->parent = NULL;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
avl_tree_node_t *SXAPI lb_avl_tree_smallest(avl_tree_t *tree)
|
||||
struct avl_tree_node *SXAPI lb_avl_tree_smallest(struct avl_tree *tree)
|
||||
{
|
||||
if (tree == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
avl_tree_node_t *entry = tree->root;
|
||||
struct avl_tree_node *entry = tree->root;
|
||||
if (entry == NULL)
|
||||
{
|
||||
return NULL;
|
||||
@ -407,13 +406,13 @@ avl_tree_node_t *SXAPI lb_avl_tree_smallest(avl_tree_t *tree)
|
||||
return entry;
|
||||
}
|
||||
|
||||
avl_tree_node_t *SXAPI lb_avl_tree_largest(avl_tree_t *tree)
|
||||
struct avl_tree_node *SXAPI lb_avl_tree_largest(struct avl_tree *tree)
|
||||
{
|
||||
if (tree == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
avl_tree_node_t *entry = tree->root;
|
||||
struct avl_tree_node *entry = tree->root;
|
||||
if (entry == NULL)
|
||||
{
|
||||
return NULL;
|
||||
@ -426,13 +425,13 @@ avl_tree_node_t *SXAPI lb_avl_tree_largest(avl_tree_t *tree)
|
||||
}
|
||||
|
||||
|
||||
avl_tree_node_t *SXAPI lb_avl_tree_larger(avl_tree_node_t *it)
|
||||
struct avl_tree_node *SXAPI lb_avl_tree_larger(struct avl_tree_node *it)
|
||||
{
|
||||
if (it == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
avl_tree_node_t *root = it;
|
||||
struct avl_tree_node *root = it;
|
||||
if (root->right != NULL)
|
||||
{
|
||||
root = root->right;
|
||||
@ -456,13 +455,13 @@ avl_tree_node_t *SXAPI lb_avl_tree_larger(avl_tree_node_t *it)
|
||||
}
|
||||
}
|
||||
|
||||
avl_tree_node_t *SXAPI lb_avl_tree_smaller(avl_tree_node_t *it)
|
||||
struct avl_tree_node *SXAPI lb_avl_tree_smaller(struct avl_tree_node *it)
|
||||
{
|
||||
if (it == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
avl_tree_node_t *root = it;
|
||||
struct avl_tree_node *root = it;
|
||||
if (root->left != NULL)
|
||||
{
|
||||
root = root->left;
|
||||
@ -486,25 +485,24 @@ avl_tree_node_t *SXAPI lb_avl_tree_smaller(avl_tree_node_t *it)
|
||||
}
|
||||
}
|
||||
|
||||
avl_tree_node_t *SXAPI lb_avl_tree_search(avl_tree_t *tree, avl_tree_node_t *node)
|
||||
struct avl_tree_node *SXAPI lb_avl_tree_search(struct avl_tree *tree, struct avl_tree_node *node)
|
||||
{
|
||||
return lbp_avl_tree_node_search(tree->root, node, tree->compare);
|
||||
}
|
||||
|
||||
|
||||
void SXAPI lb_avl_tree_insert(avl_tree_t *tree, avl_tree_node_t *data)
|
||||
void SXAPI lb_avl_tree_insert(struct avl_tree *tree, struct avl_tree_node *data)
|
||||
{
|
||||
if (tree != NULL && data != NULL)
|
||||
{
|
||||
lbp_avl_tree_node_init(data);
|
||||
tree->root = lbp_avl_tree_node_insert(tree->root, data, tree->compare, NULL);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
avl_tree_node_t *SXAPI lb_avl_tree_delete(avl_tree_t *tree, avl_tree_node_t *data)
|
||||
struct avl_tree_node *SXAPI lb_avl_tree_delete(struct avl_tree *tree, struct avl_tree_node *data)
|
||||
{
|
||||
avl_tree_node_t *node = NULL;
|
||||
struct avl_tree_node *node = NULL;
|
||||
if (tree != NULL && data != NULL)
|
||||
{
|
||||
tree->root = lbp_avl_tree_node_delete(tree->root, data, tree->compare, &node);
|
||||
@ -512,7 +510,7 @@ avl_tree_node_t *SXAPI lb_avl_tree_delete(avl_tree_t *tree, avl_tree_node_t *dat
|
||||
return node;
|
||||
}
|
||||
|
||||
int32_t SXAPI lb_avl_tree_size(avl_tree_t *tree)
|
||||
int32 SXAPI lb_avl_tree_size(struct avl_tree *tree)
|
||||
{
|
||||
if (tree == NULL)
|
||||
{
|
||||
@ -522,8 +520,8 @@ int32_t SXAPI lb_avl_tree_size(avl_tree_t *tree)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int32_t size = 0;
|
||||
avl_tree_node_t *entry = lb_avl_tree_smallest(tree);
|
||||
int32 size = 0;
|
||||
struct avl_tree_node *entry = lb_avl_tree_smallest(tree);
|
||||
while (entry != NULL)
|
||||
{
|
||||
size++;
|
||||
@ -532,21 +530,20 @@ int32_t SXAPI lb_avl_tree_size(avl_tree_t *tree)
|
||||
return size;
|
||||
}
|
||||
|
||||
void SXAPI lb_avl_tree_init(avl_tree_t *tree, callback_func_t compare)
|
||||
void SXAPI lb_avl_tree_init(struct avl_tree *tree, callback_func compare)
|
||||
{
|
||||
if (tree != NULL)
|
||||
{
|
||||
tree->compare = compare;
|
||||
tree->root = NULL;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// TESTING STUFF
|
||||
|
||||
static int32_t SXAPI lbp_avl_tree_node_calculate_height(avl_tree_node_t *tree)
|
||||
static int32 SXAPI lbp_avl_tree_node_calculate_height(struct avl_tree_node *tree)
|
||||
{
|
||||
if (tree == NULL)
|
||||
{
|
||||
@ -556,29 +553,29 @@ static int32_t SXAPI lbp_avl_tree_node_calculate_height(avl_tree_node_t *tree)
|
||||
1;
|
||||
}
|
||||
|
||||
static bool SXAPI lbp_avl_tree_node_test(avl_tree_node_t *tree, callback_func_t compare)
|
||||
static bool SXAPI lbp_avl_tree_node_test(struct avl_tree_node *tree, callback_func compare)
|
||||
{
|
||||
if (tree == NULL)
|
||||
{
|
||||
return true;
|
||||
return TRUE;
|
||||
}
|
||||
if (lbp_avl_tree_node_get_balance_factor(tree) < -1 || lbp_avl_tree_node_get_balance_factor(tree) > 1 ||
|
||||
lbp_avl_tree_node_calculate_height(tree) != tree->height)
|
||||
{
|
||||
return false;
|
||||
return FALSE;
|
||||
}
|
||||
if (tree->left != NULL)
|
||||
{
|
||||
if (tree->left->parent != tree)
|
||||
{
|
||||
return false;
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
if (tree->right != NULL)
|
||||
{
|
||||
if (tree->right->parent != tree)
|
||||
{
|
||||
return false;
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
if (compare != NULL)
|
||||
@ -586,17 +583,17 @@ static bool SXAPI lbp_avl_tree_node_test(avl_tree_node_t *tree, callback_func_t
|
||||
if ((tree->right != NULL && compare(tree, tree->right) > 0) ||
|
||||
(tree->left != NULL && compare(tree, tree->left) < 0))
|
||||
{
|
||||
return false;
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
return lbp_avl_tree_node_test(tree->left, compare) && lbp_avl_tree_node_test(tree->right, compare);
|
||||
}
|
||||
|
||||
bool SXAPI lb_avl_tree_validate(avl_tree_t *tree)
|
||||
bool SXAPI lb_avl_tree_validate(struct avl_tree *tree)
|
||||
{
|
||||
if (tree == NULL)
|
||||
{
|
||||
return true;
|
||||
return TRUE;
|
||||
}
|
||||
return lbp_avl_tree_node_test(tree->root, tree->compare);
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +1,22 @@
|
||||
#include "lib/linked_list.h"
|
||||
|
||||
static void SXAPI lbp_init_linked_list_node(linked_list_node_t *node)
|
||||
static void SXAPI lbp_init_linked_list_node(struct linked_list_node *node)
|
||||
{
|
||||
if (node != NULL)
|
||||
{
|
||||
node->next = NULL;
|
||||
node->prev = NULL;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
static void SXAPI lbp_append_node(linked_list_node_t *target, linked_list_node_t *node)
|
||||
static void SXAPI lbp_append_node(struct linked_list_node *target, struct linked_list_node *node)
|
||||
{
|
||||
if (target == NULL || node == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
linked_list_node_t *next = target->next;
|
||||
struct linked_list_node *next = target->next;
|
||||
// update the next node
|
||||
if (next != NULL)
|
||||
{
|
||||
@ -30,19 +29,17 @@ static void SXAPI lbp_append_node(linked_list_node_t *target, linked_list_node_t
|
||||
// update the node itself
|
||||
node->prev = target;
|
||||
node->next = next;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// link target with node, suppose target is in the current list
|
||||
static void SXAPI lbp_prepend_node(linked_list_node_t *target, linked_list_node_t *node)
|
||||
static void SXAPI lbp_prepend_node(struct linked_list_node *target, struct linked_list_node *node)
|
||||
{
|
||||
if (target == NULL || node == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
linked_list_node_t *prev = target->prev;
|
||||
struct linked_list_node *prev = target->prev;
|
||||
// update the prev node
|
||||
if (prev != NULL)
|
||||
{
|
||||
@ -55,11 +52,9 @@ static void SXAPI lbp_prepend_node(linked_list_node_t *target, linked_list_node_
|
||||
// update the node itself
|
||||
node->next = target;
|
||||
node->prev = prev;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static void SXAPI lbp_unlink_node(linked_list_node_t *node)
|
||||
static void SXAPI lbp_unlink_node(struct linked_list_node *node)
|
||||
{
|
||||
if (node == NULL)
|
||||
{
|
||||
@ -75,21 +70,18 @@ static void SXAPI lbp_unlink_node(linked_list_node_t *node)
|
||||
{
|
||||
node->next->prev = node->prev;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void SXAPI lb_linked_list_init(linked_list_t *list)
|
||||
void SXAPI lb_linked_list_init(struct linked_list *list)
|
||||
{
|
||||
if (list != NULL)
|
||||
{
|
||||
list->head = NULL;
|
||||
list->tail = NULL;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
int32_t SXAPI lb_linked_list_size(linked_list_t *list)
|
||||
int32 SXAPI lb_linked_list_size(struct linked_list *list)
|
||||
{
|
||||
if (list == NULL)
|
||||
{
|
||||
@ -100,9 +92,9 @@ int32_t SXAPI lb_linked_list_size(linked_list_t *list)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t size = 1;
|
||||
linked_list_node_t *cur_node = list->head;
|
||||
linked_list_node_t *tail = list->tail;
|
||||
int32 size = 1;
|
||||
struct linked_list_node *cur_node = list->head;
|
||||
struct linked_list_node *tail = list->tail;
|
||||
while ((cur_node != tail) && ((cur_node = cur_node->next) != NULL))
|
||||
{
|
||||
size++;
|
||||
@ -110,7 +102,7 @@ int32_t SXAPI lb_linked_list_size(linked_list_t *list)
|
||||
return size;
|
||||
}
|
||||
|
||||
void SXAPI lb_linked_list_push_front(linked_list_t *list, linked_list_node_t *node)
|
||||
void SXAPI lb_linked_list_push_front(struct linked_list *list, struct linked_list_node *node)
|
||||
{
|
||||
if (list == NULL || node == NULL)
|
||||
{
|
||||
@ -120,11 +112,9 @@ void SXAPI lb_linked_list_push_front(linked_list_t *list, linked_list_node_t *no
|
||||
lbp_init_linked_list_node(node);
|
||||
|
||||
lb_linked_list_insert_ref(list, NULL, node);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void SXAPI lb_linked_list_push_back(linked_list_t *list, linked_list_node_t *node)
|
||||
void SXAPI lb_linked_list_push_back(struct linked_list *list, struct linked_list_node *node)
|
||||
{
|
||||
if (list == NULL || node == NULL)
|
||||
{
|
||||
@ -134,11 +124,9 @@ void SXAPI lb_linked_list_push_back(linked_list_t *list, linked_list_node_t *nod
|
||||
lbp_init_linked_list_node(node);
|
||||
|
||||
lb_linked_list_insert_ref(list, list->tail, node);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_pop_front(linked_list_t *list)
|
||||
struct linked_list_node *SXAPI lb_linked_list_pop_front(struct linked_list *list)
|
||||
{
|
||||
if (list == NULL)
|
||||
{
|
||||
@ -147,7 +135,7 @@ linked_list_node_t *SXAPI lb_linked_list_pop_front(linked_list_t *list)
|
||||
return lb_linked_list_remove_ref(list, list->head);
|
||||
}
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_pop_back(linked_list_t *list)
|
||||
struct linked_list_node *SXAPI lb_linked_list_pop_back(struct linked_list *list)
|
||||
{
|
||||
if (list == NULL)
|
||||
{
|
||||
@ -158,7 +146,7 @@ linked_list_node_t *SXAPI lb_linked_list_pop_back(linked_list_t *list)
|
||||
}
|
||||
|
||||
|
||||
void SXAPI lb_linked_list_insert_ref(linked_list_t *list, linked_list_node_t *prev_node, linked_list_node_t *node)
|
||||
void SXAPI lb_linked_list_insert_ref(struct linked_list *list, struct linked_list_node *prev_node, struct linked_list_node *node)
|
||||
{
|
||||
if (list == NULL || node == NULL)
|
||||
{
|
||||
@ -195,13 +183,13 @@ void SXAPI lb_linked_list_insert_ref(linked_list_t *list, linked_list_node_t *pr
|
||||
}
|
||||
}
|
||||
|
||||
void SXAPI lb_linked_list_insert(linked_list_t *list, int32_t index, linked_list_node_t *node)
|
||||
void SXAPI lb_linked_list_insert(struct linked_list *list, int32 index, struct linked_list_node *node)
|
||||
{
|
||||
if (list == NULL || index < 0 || node == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
linked_list_node_t *prev_node = lb_linked_list_get(list, index - 1);
|
||||
struct linked_list_node *prev_node = lb_linked_list_get(list, index - 1);
|
||||
lbp_init_linked_list_node(node);
|
||||
|
||||
if (prev_node == NULL)
|
||||
@ -215,17 +203,15 @@ void SXAPI lb_linked_list_insert(linked_list_t *list, int32_t index, linked_list
|
||||
{
|
||||
lb_linked_list_insert_ref(list, prev_node, node);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_remove(linked_list_t *list, int32_t index)
|
||||
struct linked_list_node *SXAPI lb_linked_list_remove(struct linked_list *list, int32 index)
|
||||
{
|
||||
if (list == NULL || index < 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
linked_list_node_t *cur_node = lb_linked_list_get(list, index);
|
||||
struct linked_list_node *cur_node = lb_linked_list_get(list, index);
|
||||
|
||||
if (cur_node == NULL)
|
||||
{
|
||||
@ -235,7 +221,7 @@ linked_list_node_t *SXAPI lb_linked_list_remove(linked_list_t *list, int32_t ind
|
||||
return lb_linked_list_remove_ref(list, cur_node);
|
||||
}
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_remove_ref(linked_list_t *list, linked_list_node_t *node)
|
||||
struct linked_list_node *SXAPI lb_linked_list_remove_ref(struct linked_list *list, struct linked_list_node *node)
|
||||
{
|
||||
if (list == NULL || node == NULL)
|
||||
{
|
||||
@ -259,19 +245,19 @@ linked_list_node_t *SXAPI lb_linked_list_remove_ref(linked_list_t *list, linked_
|
||||
return node;
|
||||
}
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_get(linked_list_t *list, int32_t index)
|
||||
struct linked_list_node *SXAPI lb_linked_list_get(struct linked_list *list, int32 index)
|
||||
{
|
||||
if (list == NULL || index < 0 || list->head == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
linked_list_node_t *cur_node = list->head;
|
||||
struct linked_list_node *cur_node = list->head;
|
||||
while (index-- && (cur_node = cur_node->next) != NULL)
|
||||
{}
|
||||
return cur_node;
|
||||
}
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_next(linked_list_node_t *node)
|
||||
struct linked_list_node *SXAPI lb_linked_list_next(struct linked_list_node *node)
|
||||
{
|
||||
if (node != NULL)
|
||||
{
|
||||
@ -280,7 +266,7 @@ linked_list_node_t *SXAPI lb_linked_list_next(linked_list_node_t *node)
|
||||
return node;
|
||||
}
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_prev(linked_list_node_t *node)
|
||||
struct linked_list_node *SXAPI lb_linked_list_prev(struct linked_list_node *node)
|
||||
{
|
||||
if (node != NULL)
|
||||
{
|
||||
@ -289,9 +275,9 @@ linked_list_node_t *SXAPI lb_linked_list_prev(linked_list_node_t *node)
|
||||
return node;
|
||||
}
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_first(linked_list_t *list)
|
||||
struct linked_list_node *SXAPI lb_linked_list_first(struct linked_list *list)
|
||||
{
|
||||
linked_list_node_t *result = NULL;
|
||||
struct linked_list_node *result = NULL;
|
||||
if (list != NULL)
|
||||
{
|
||||
result = list->head;
|
||||
@ -299,9 +285,9 @@ linked_list_node_t *SXAPI lb_linked_list_first(linked_list_t *list)
|
||||
return result;
|
||||
}
|
||||
|
||||
linked_list_node_t *SXAPI lb_linked_list_last(linked_list_t *list)
|
||||
struct linked_list_node *SXAPI lb_linked_list_last(struct linked_list *list)
|
||||
{
|
||||
linked_list_node_t *result = NULL;
|
||||
struct linked_list_node *result = NULL;
|
||||
if (list != NULL)
|
||||
{
|
||||
result = list->tail;
|
||||
@ -309,15 +295,15 @@ linked_list_node_t *SXAPI lb_linked_list_last(linked_list_t *list)
|
||||
return result;
|
||||
}
|
||||
|
||||
int32_t SXAPI lb_linked_list_search(linked_list_t *list, linked_list_node_t *target,
|
||||
callback_func_t equals)
|
||||
int32 SXAPI lb_linked_list_search(struct linked_list *list, struct linked_list_node *target,
|
||||
callback_func equals)
|
||||
{
|
||||
if (list == NULL || target == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
int32_t result = 0;
|
||||
linked_list_node_t *node = lb_linked_list_first(list);
|
||||
int32 result = 0;
|
||||
struct linked_list_node *node = lb_linked_list_first(list);
|
||||
while (node != NULL)
|
||||
{
|
||||
if (equals != NULL)
|
||||
@ -341,17 +327,3 @@ int32_t SXAPI lb_linked_list_search(linked_list_t *list, linked_list_node_t *tar
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
108
lib/salloc.c
108
lib/salloc.c
@ -1,50 +1,52 @@
|
||||
#include "type.h"
|
||||
#include "lib/sxtdlib.h"
|
||||
#include "lib/salloc.h"
|
||||
|
||||
typedef union
|
||||
struct salloc_header
|
||||
{
|
||||
uint32_t size;
|
||||
uint32_t flags;
|
||||
} _salloc_header;
|
||||
union
|
||||
{
|
||||
uint32 size;
|
||||
uint32 flags;
|
||||
};
|
||||
};
|
||||
|
||||
#define ALLOC_FLAG_NUM 2
|
||||
#define ALLOC_HEADER_FLAG_FREE 0
|
||||
#define ALLOC_HEADER_FLAG_LAST 1
|
||||
|
||||
static void lbp_set_salloc_header_size(_salloc_header *header, uint32_t size)
|
||||
static void lbp_set_salloc_header_size(struct salloc_header *header, uint32 size)
|
||||
{
|
||||
// align the integer, ignoring overflowed bits
|
||||
size <<= ALLOC_FLAG_NUM;
|
||||
|
||||
// clear ALLOC_FLAG_NUM-th to 31-th bits
|
||||
header->size &= ~(uint32_t) lb_bit_field_mask(ALLOC_FLAG_NUM, 31);
|
||||
header->size &= ~(uint32) lb_bit_field_mask(ALLOC_FLAG_NUM, 31);
|
||||
// set bits
|
||||
header->size |= size;
|
||||
return;
|
||||
}
|
||||
|
||||
static uint32_t lbp_read_salloc_header_size(_salloc_header *header)
|
||||
static uint32 lbp_read_salloc_header_size(struct salloc_header *header)
|
||||
{
|
||||
return header->size >> ALLOC_FLAG_NUM;
|
||||
}
|
||||
|
||||
static uint32_t lbp_read_salloc_header_flag(_salloc_header *header, uint32_t bit)
|
||||
static uint32 lbp_read_salloc_header_flag(struct salloc_header *header, uint32 bit)
|
||||
{
|
||||
return (header->flags & (uint32_t) lb_bit_mask(bit)) == 0 ? 0 : 1;
|
||||
return (header->flags & (uint32) lb_bit_mask(bit)) == 0 ? 0 : 1;
|
||||
}
|
||||
|
||||
static void lbp_set_salloc_header_flag(_salloc_header *header, uint32_t bit, uint32_t value)
|
||||
static void lbp_set_salloc_header_flag(struct salloc_header *header, uint32 bit, uint32 value)
|
||||
{
|
||||
value &= (uint32_t) lb_bit_mask(0);
|
||||
value &= (uint32) lb_bit_mask(0);
|
||||
if (value == 1)
|
||||
{
|
||||
header->flags |= (uint32_t) lb_bit_mask(bit);
|
||||
header->flags |= (uint32) lb_bit_mask(bit);
|
||||
}
|
||||
else
|
||||
{
|
||||
header->flags &= ~(uint32_t) lb_bit_mask(bit);
|
||||
header->flags &= ~(uint32) lb_bit_mask(bit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
static void lbp_salloc_join(void *base)
|
||||
@ -54,22 +56,22 @@ static void lbp_salloc_join(void *base)
|
||||
char *c_ptr = (char *) base;
|
||||
while (1)
|
||||
{
|
||||
uint32_t c_blk_free = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
|
||||
uint32_t c_blk_last = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
|
||||
uint32_t c_blk_size = lbp_read_salloc_header_size((_salloc_header *) c_ptr);
|
||||
uint32 c_blk_free = lbp_read_salloc_header_flag((struct salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
|
||||
uint32 c_blk_last = lbp_read_salloc_header_flag((struct salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
|
||||
uint32 c_blk_size = lbp_read_salloc_header_size((struct salloc_header *) c_ptr);
|
||||
char *n_ptr = c_blk_last == 1 ? NULL : c_ptr + c_blk_size;
|
||||
if (n_ptr != NULL && c_blk_free == 1)
|
||||
{
|
||||
// if this is not the last block and the prev block is free
|
||||
uint32_t n_blk_free = lbp_read_salloc_header_flag((_salloc_header *) n_ptr, ALLOC_HEADER_FLAG_FREE);
|
||||
uint32_t n_blk_last = lbp_read_salloc_header_flag((_salloc_header *) n_ptr, ALLOC_HEADER_FLAG_LAST);
|
||||
uint32_t n_blk_size = lbp_read_salloc_header_size((_salloc_header *) n_ptr);
|
||||
uint32 n_blk_free = lbp_read_salloc_header_flag((struct salloc_header *) n_ptr, ALLOC_HEADER_FLAG_FREE);
|
||||
uint32 n_blk_last = lbp_read_salloc_header_flag((struct salloc_header *) n_ptr, ALLOC_HEADER_FLAG_LAST);
|
||||
uint32 n_blk_size = lbp_read_salloc_header_size((struct salloc_header *) n_ptr);
|
||||
|
||||
if (n_blk_free == 1)
|
||||
{
|
||||
// logically gone
|
||||
lbp_set_salloc_header_size((_salloc_header *) c_ptr, n_blk_size + c_blk_size);
|
||||
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST, n_blk_last);
|
||||
lbp_set_salloc_header_size((struct salloc_header *) c_ptr, n_blk_size + c_blk_size);
|
||||
lbp_set_salloc_header_flag((struct salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST, n_blk_last);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@ -86,22 +88,22 @@ static void lbp_salloc_join(void *base)
|
||||
}
|
||||
}
|
||||
|
||||
bool SXAPI lb_salloc_assert(void *base, uint32_t *blk_size, bool *blk_free, uint32_t size)
|
||||
bool SXAPI lb_salloc_assert(void *base, const uint32 *blk_size, const bool *blk_free, uint32 size)
|
||||
{
|
||||
if (base == NULL || blk_free == NULL || blk_size == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
uint32_t i = 0;
|
||||
uint32 i = 0;
|
||||
char *c_ptr = (char *) base;
|
||||
while (1)
|
||||
{
|
||||
uint32_t cur_blk_free = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
|
||||
uint32_t cur_blk_last = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
|
||||
uint32_t cur_blk_size = lbp_read_salloc_header_size((_salloc_header *) c_ptr);
|
||||
uint32 cur_blk_free = lbp_read_salloc_header_flag((struct salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
|
||||
uint32 cur_blk_last = lbp_read_salloc_header_flag((struct salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
|
||||
uint32 cur_blk_size = lbp_read_salloc_header_size((struct salloc_header *) c_ptr);
|
||||
if (cur_blk_free != blk_free[i] || cur_blk_size != blk_size[i])
|
||||
{
|
||||
return false;
|
||||
return FALSE;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -115,30 +117,29 @@ bool SXAPI lb_salloc_assert(void *base, uint32_t *blk_size, bool *blk_free, uint
|
||||
}
|
||||
}
|
||||
|
||||
void SXAPI lb_salloc_init(void *base, uint32_t size)
|
||||
void SXAPI lb_salloc_init(void *base, uint32 size)
|
||||
{
|
||||
if (base != NULL && size >= sizeof(_salloc_header))
|
||||
if (base != NULL && size >= sizeof(struct salloc_header))
|
||||
{
|
||||
_salloc_header *ptr = (_salloc_header *) base;
|
||||
struct salloc_header *ptr = (struct salloc_header *) base;
|
||||
lbp_set_salloc_header_size(ptr, size);
|
||||
lbp_set_salloc_header_flag(ptr, ALLOC_HEADER_FLAG_FREE, 1);
|
||||
lbp_set_salloc_header_flag(ptr, ALLOC_HEADER_FLAG_LAST, 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void *SXAPI lb_salloc(void *base, uint32_t size)
|
||||
void *SXAPI lb_salloc(void *base, uint32 size)
|
||||
{
|
||||
void *result = NULL;
|
||||
if (base != NULL && size != 0)
|
||||
{
|
||||
uint32_t total_size = size + sizeof(_salloc_header);
|
||||
uint32 total_size = size + sizeof(struct salloc_header);
|
||||
char *c_ptr = (char *) base;
|
||||
while (1)
|
||||
{
|
||||
uint32_t cur_blk_free = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
|
||||
uint32_t cur_blk_size = lbp_read_salloc_header_size((_salloc_header *) c_ptr);
|
||||
uint32_t cur_blk_last = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
|
||||
uint32 cur_blk_free = lbp_read_salloc_header_flag((struct salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
|
||||
uint32 cur_blk_size = lbp_read_salloc_header_size((struct salloc_header *) c_ptr);
|
||||
uint32 cur_blk_last = lbp_read_salloc_header_flag((struct salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
|
||||
if (cur_blk_free == 0 || cur_blk_size < total_size)
|
||||
{
|
||||
//if cur block not a free block
|
||||
@ -157,28 +158,28 @@ void *SXAPI lb_salloc(void *base, uint32_t size)
|
||||
{
|
||||
// we have a free block with enough size
|
||||
if (total_size == cur_blk_size ||
|
||||
cur_blk_size - total_size < sizeof(_salloc_header))
|
||||
cur_blk_size - total_size < sizeof(struct salloc_header))
|
||||
{
|
||||
// since the space left is not enough for salloc_header
|
||||
// we alloc the whole block
|
||||
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE, 0);
|
||||
lbp_set_salloc_header_flag((struct salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
// we split the block here
|
||||
// set properties for the first block
|
||||
lbp_set_salloc_header_size((_salloc_header *) c_ptr, total_size);
|
||||
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST, 0);
|
||||
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE, 0);
|
||||
lbp_set_salloc_header_size((struct salloc_header *) c_ptr, total_size);
|
||||
lbp_set_salloc_header_flag((struct salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST, 0);
|
||||
lbp_set_salloc_header_flag((struct salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE, 0);
|
||||
|
||||
// set properties for the second block
|
||||
lbp_set_salloc_header_size((_salloc_header *) (c_ptr + total_size), cur_blk_size - total_size);
|
||||
lbp_set_salloc_header_flag((_salloc_header *) (c_ptr + total_size), ALLOC_HEADER_FLAG_LAST,
|
||||
lbp_set_salloc_header_size((struct salloc_header *) (c_ptr + total_size), cur_blk_size - total_size);
|
||||
lbp_set_salloc_header_flag((struct salloc_header *) (c_ptr + total_size), ALLOC_HEADER_FLAG_LAST,
|
||||
cur_blk_last);
|
||||
lbp_set_salloc_header_flag((_salloc_header *) (c_ptr + total_size), ALLOC_HEADER_FLAG_FREE, 1);
|
||||
lbp_set_salloc_header_flag((struct salloc_header *) (c_ptr + total_size), ALLOC_HEADER_FLAG_FREE, 1);
|
||||
}
|
||||
// return the pointer, skip the alloc header
|
||||
result = c_ptr + sizeof(_salloc_header);
|
||||
result = c_ptr + sizeof(struct salloc_header);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -193,13 +194,13 @@ void SXAPI lb_sfree(void *base, void *ptr)
|
||||
char *c_ptr = (char *) base;
|
||||
while (1)
|
||||
{
|
||||
uint32_t cur_blk_free = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
|
||||
uint32_t cur_blk_last = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
|
||||
uint32_t cur_blk_size = lbp_read_salloc_header_size((_salloc_header *) c_ptr);
|
||||
if (cur_blk_free == 0 && ptr == c_ptr + sizeof(_salloc_header))
|
||||
uint32 cur_blk_free = lbp_read_salloc_header_flag((struct salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
|
||||
uint32 cur_blk_last = lbp_read_salloc_header_flag((struct salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
|
||||
uint32 cur_blk_size = lbp_read_salloc_header_size((struct salloc_header *) c_ptr);
|
||||
if (cur_blk_free == 0 && ptr == c_ptr + sizeof(struct salloc_header))
|
||||
{
|
||||
// we found the block, mark it as free
|
||||
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE, 1);
|
||||
lbp_set_salloc_header_flag((struct salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE, 1);
|
||||
// merge blocks
|
||||
lbp_salloc_join(base);
|
||||
break;
|
||||
@ -215,5 +216,4 @@ void SXAPI lb_sfree(void *base, void *ptr)
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include "type.h"
|
||||
#include "lib/sxtdlib.h"
|
||||
|
||||
void SXAPI lb_mem_copy(void *src, void *dst, uint64_t size)
|
||||
void SXAPI lb_mem_copy(void *src, void *dst, uint64 size)
|
||||
{
|
||||
if (src == NULL || dst == NULL)
|
||||
{
|
||||
@ -13,10 +13,9 @@ void SXAPI lb_mem_copy(void *src, void *dst, uint64_t size)
|
||||
{
|
||||
*(cDst++) = *(cSrc++);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void SXAPI lb_mem_set(void *src, uint8_t const val, uint64_t size)
|
||||
void SXAPI lb_mem_set(void *src, uint8 const val, uint64 size)
|
||||
{
|
||||
if (src == NULL)
|
||||
{
|
||||
@ -24,13 +23,12 @@ void SXAPI lb_mem_set(void *src, uint8_t const val, uint64_t size)
|
||||
}
|
||||
while (size--)
|
||||
{
|
||||
*(uint8_t *) src = val;
|
||||
src = (void *) ((uintptr_t) src + 1);
|
||||
*(uint8 *) src = val;
|
||||
src = (void *) ((uintptr) src + 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void SXAPI lb_mem_move(void *src, void *dst, uint64_t size)
|
||||
void SXAPI lb_mem_move(void *src, void *dst, uint64 size)
|
||||
{
|
||||
if (src == NULL || dst == NULL)
|
||||
{
|
||||
@ -41,35 +39,34 @@ void SXAPI lb_mem_move(void *src, void *dst, uint64_t size)
|
||||
lb_mem_copy(src, dst, size);
|
||||
return;
|
||||
}
|
||||
src = (void *) ((uintptr_t) src + size - 1);
|
||||
dst = (void *) ((uintptr_t) dst + size - 1);
|
||||
src = (void *) ((uintptr) src + size - 1);
|
||||
dst = (void *) ((uintptr) dst + size - 1);
|
||||
while (size--)
|
||||
{
|
||||
*(char *) dst = *(char *) src;
|
||||
dst = (void *) ((uintptr_t) dst - 1);
|
||||
src = (void *) ((uintptr_t) src - 1);
|
||||
dst = (void *) ((uintptr) dst - 1);
|
||||
src = (void *) ((uintptr) src - 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Random Generator
|
||||
//
|
||||
static uint32_t seed = 1;
|
||||
static uint32_t max = 16777215;
|
||||
static uint32 seed = 1;
|
||||
static uint32 max = 16777215;
|
||||
|
||||
uint32_t SXAPI lb_rand(void)
|
||||
uint32 SXAPI lb_rand(void)
|
||||
{
|
||||
seed = seed * 1103512986 + 29865;
|
||||
return (unsigned int) (seed / 65536) % (max + 1);
|
||||
}
|
||||
|
||||
void SXAPI lb_srand(uint32_t _seed)
|
||||
void SXAPI lb_srand(uint32 _seed)
|
||||
{
|
||||
seed = _seed;
|
||||
}
|
||||
|
||||
void SXAPI lb_mrand(uint32_t _max)
|
||||
void SXAPI lb_mrand(uint32 _max)
|
||||
{
|
||||
max = _max;
|
||||
}
|
||||
@ -78,9 +75,9 @@ void SXAPI lb_mrand(uint32_t _max)
|
||||
// String Library
|
||||
//
|
||||
|
||||
uint64_t SXAPI lb_str_len(char const *str)
|
||||
uint64 SXAPI lb_str_len(char const *str)
|
||||
{
|
||||
uint64_t length = 0;
|
||||
uint64 length = 0;
|
||||
if (str == NULL)
|
||||
{
|
||||
return 0;
|
||||
@ -93,13 +90,13 @@ uint64_t SXAPI lb_str_len(char const *str)
|
||||
return length;
|
||||
}
|
||||
|
||||
uint64_t SXAPI lb_str_cmp(char const *str1, char const *str2)
|
||||
uint64 SXAPI lb_str_cmp(char const *str1, char const *str2)
|
||||
{
|
||||
if (str1 == NULL || str2 == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
uint64_t length = lb_str_len(str1);
|
||||
uint64 length = lb_str_len(str1);
|
||||
if (length != lb_str_len(str2))
|
||||
{
|
||||
return 0;
|
||||
|
@ -1,10 +1,11 @@
|
||||
#include "test/driver.h"
|
||||
#include "test/test_case.h"
|
||||
#include "lib/avl_tree.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
avl_tree_node_t tree_entry;
|
||||
int val;
|
||||
struct avl_tree_node tree_entry;
|
||||
int32 val;
|
||||
} int_tree_node;
|
||||
|
||||
static int_tree_node *create_tree_node(int val)
|
||||
@ -14,16 +15,16 @@ static int_tree_node *create_tree_node(int val)
|
||||
return rs;
|
||||
}
|
||||
|
||||
static int32_t compare(void *root1, void *node1)
|
||||
static int32 compare(void *root1, void *node1)
|
||||
{
|
||||
avl_tree_node_t *root = (avl_tree_node_t *) node1;
|
||||
avl_tree_node_t *node = (avl_tree_node_t *) root1;
|
||||
struct avl_tree_node *root = (struct avl_tree_node *) node1;
|
||||
struct avl_tree_node *node = (struct avl_tree_node *) root1;
|
||||
int_tree_node *rooti = OBTAIN_STRUCT_ADDR(root, int_tree_node, tree_entry);
|
||||
int_tree_node *nodei = OBTAIN_STRUCT_ADDR(node, int_tree_node, tree_entry);
|
||||
return rooti->val - nodei->val;
|
||||
}
|
||||
|
||||
//static void _pre_order(avl_tree_node_t *node, bool root)
|
||||
//static void _pre_order(struct avl_tree_node *node, bool root)
|
||||
//{
|
||||
// if (node == NULL)
|
||||
// return;
|
||||
@ -35,29 +36,29 @@ static int32_t compare(void *root1, void *node1)
|
||||
// printf("\n");
|
||||
//}
|
||||
//
|
||||
//static void pre_order(avl_tree_node_t *node)
|
||||
//static void pre_order(struct avl_tree_node *node)
|
||||
//{
|
||||
// _pre_order(node, true);
|
||||
// _pre_order(node, TRUE);
|
||||
//}
|
||||
|
||||
static int counter = 0;
|
||||
|
||||
static bool _pre_order_assert(avl_tree_node_t *node, int order[], int size)
|
||||
static bool _pre_order_assert(struct avl_tree_node *node, int order[], int size)
|
||||
{
|
||||
if (node == NULL)
|
||||
{
|
||||
return true;
|
||||
return TRUE;
|
||||
}
|
||||
if (counter >= size)
|
||||
{
|
||||
return false;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
int_tree_node *my_node = OBTAIN_STRUCT_ADDR(node, int_tree_node, tree_entry);
|
||||
if (order[counter] != my_node->val)
|
||||
{
|
||||
result = false;
|
||||
result = FALSE;
|
||||
}
|
||||
counter++;
|
||||
result = result && _pre_order_assert(node->left, order, size);
|
||||
@ -65,7 +66,7 @@ static bool _pre_order_assert(avl_tree_node_t *node, int order[], int size)
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool pre_order_assert(avl_tree_t *node, int order[], int size)
|
||||
static bool pre_order_assert(struct avl_tree *node, int order[], int size)
|
||||
{
|
||||
counter = 0;
|
||||
return _pre_order_assert(node->root, order, size);
|
||||
@ -81,8 +82,8 @@ static bool insert_simple_l(void)
|
||||
// \
|
||||
// 3
|
||||
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
lb_avl_tree_insert(&tree, &create_tree_node(1)->tree_entry);
|
||||
@ -105,8 +106,8 @@ static bool insert_simple_r(void)
|
||||
// /
|
||||
//1
|
||||
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
lb_avl_tree_insert(&tree, &create_tree_node(3)->tree_entry);
|
||||
@ -128,8 +129,8 @@ static bool insert_simple_ll(void)
|
||||
// 4 == 2L ==> 2 4
|
||||
// /
|
||||
//3
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
lb_avl_tree_insert(&tree, &create_tree_node(2)->tree_entry);
|
||||
@ -151,8 +152,8 @@ static bool insert_simple_rr(void)
|
||||
//2 == 2R ==> 2 4
|
||||
// \
|
||||
// 3
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
lb_avl_tree_insert(&tree, &create_tree_node(4)->tree_entry);
|
||||
@ -176,8 +177,8 @@ static bool insert_complex_1(void)
|
||||
//3 9 3 9- 4+ 15 3 15 26
|
||||
// \ /
|
||||
// 15 3
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
lb_avl_tree_insert(&tree, &create_tree_node(20)->tree_entry);
|
||||
@ -204,8 +205,8 @@ static bool insert_complex_2(void)
|
||||
//3 9 3 9+ 4 3 8 26
|
||||
// / / \
|
||||
// 8 3 8
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
lb_avl_tree_insert(&tree, &create_tree_node(20)->tree_entry);
|
||||
@ -234,8 +235,8 @@ static bool insert_complex_3(void)
|
||||
//2 7 11 2 7 11- 3+ 7 15 2 15 21 30
|
||||
// \ /
|
||||
// 15 2
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
lb_avl_tree_insert(&tree, &create_tree_node(20)->tree_entry);
|
||||
@ -269,8 +270,8 @@ static bool insert_complex_4(void)
|
||||
//2 7 11 2 7- 11 3+ 7- 2 8 21 30
|
||||
// \ / \
|
||||
// 8 2 8
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
lb_avl_tree_insert(&tree, &create_tree_node(20)->tree_entry);
|
||||
@ -304,8 +305,8 @@ static bool insert_duplicate(void)
|
||||
//2 7 11 2 7- 11 3+ 7- 2 8 21 30
|
||||
// \ / \
|
||||
// 8 2 8
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
lb_avl_tree_insert(&tree, &create_tree_node(20)->tree_entry);
|
||||
@ -339,8 +340,8 @@ static bool delete_simple_l(void)
|
||||
// \
|
||||
// 4
|
||||
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
int_tree_node *deleted = create_tree_node(1);
|
||||
@ -367,8 +368,8 @@ static bool delete_simple_r(void)
|
||||
// /
|
||||
//1
|
||||
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
int_tree_node *deleted = create_tree_node(4);
|
||||
@ -394,8 +395,8 @@ static bool delete_simple_ll(void)
|
||||
//1 4 == 2L ==> 2 4
|
||||
// /
|
||||
// 3
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
int_tree_node *deleted = create_tree_node(1);
|
||||
@ -421,8 +422,8 @@ static bool delete_simple_rr(void)
|
||||
//2 4 == 2R ==> 1 3
|
||||
// \
|
||||
// 1
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
int_tree_node *deleted = create_tree_node(4);
|
||||
@ -452,8 +453,8 @@ static bool delete_complex_1(void)
|
||||
//
|
||||
// Result:
|
||||
// empty tree
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
int_tree_node *deleted = create_tree_node(10);
|
||||
@ -490,8 +491,8 @@ static bool delete_complex_2(void)
|
||||
// \
|
||||
// 35
|
||||
//
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
int_tree_node *deleted = create_tree_node(20);
|
||||
@ -529,8 +530,8 @@ static bool delete_complex_3(void)
|
||||
// 15 30
|
||||
// / /
|
||||
// 5 25
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
int_tree_node *deleted = create_tree_node(10);
|
||||
@ -568,8 +569,8 @@ static bool delete_complex_4(void)
|
||||
// Results:
|
||||
// 20
|
||||
//
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
int_tree_node *delete5 = create_tree_node(5);
|
||||
@ -627,8 +628,8 @@ static bool delete_complex_single_rotation(void)
|
||||
// 12 22 31 40
|
||||
//
|
||||
//
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
int_tree_node *deleted = create_tree_node(50);
|
||||
@ -682,8 +683,8 @@ static bool delete_complex_double_rotation(void)
|
||||
// 12 25 31 50
|
||||
//
|
||||
//
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
int_tree_node *deleted = create_tree_node(22);
|
||||
@ -737,8 +738,8 @@ static bool delete_complex_multiple_rotation(void)
|
||||
// 10 15 22 31
|
||||
//
|
||||
//
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
int_tree_node *deleted = create_tree_node(5);
|
||||
@ -785,8 +786,8 @@ static bool delete_DNE(void)
|
||||
// / \ /
|
||||
// 5 15 25
|
||||
//
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
int_tree_node *delete100 = create_tree_node(100);
|
||||
@ -814,17 +815,17 @@ static int_tree_node apocalypse[AVL_APOCALYPSE_NUM];
|
||||
|
||||
static bool test_apocalypse(void)
|
||||
{
|
||||
bool result = true;
|
||||
avl_tree_t tree;
|
||||
bool result = TRUE;
|
||||
struct avl_tree tree;
|
||||
lb_avl_tree_init(&tree, compare);
|
||||
|
||||
// insert test
|
||||
for (int i = 0; i < AVL_APOCALYPSE_NUM; i++)
|
||||
{
|
||||
apocalypse[i].val = lb_rand();
|
||||
apocalypse[i].val = (int32)lb_rand();
|
||||
while (lb_avl_tree_search(&tree, &apocalypse[i].tree_entry) != NULL)
|
||||
{
|
||||
apocalypse[i].val += lb_rand() % 32765;
|
||||
apocalypse[i].val += (int32)lb_rand() % 32765;
|
||||
}
|
||||
lb_avl_tree_insert(&tree, &apocalypse[i].tree_entry);
|
||||
}
|
||||
@ -834,15 +835,15 @@ static bool test_apocalypse(void)
|
||||
result = result && lb_avl_tree_size(&tree) == AVL_APOCALYPSE_NUM;
|
||||
|
||||
// smaller and bigger test
|
||||
avl_tree_node_t *entry = lb_avl_tree_smallest(&tree);
|
||||
uint32_t size = 0;
|
||||
int32_t prev = -1;
|
||||
int32_t cur = OBTAIN_STRUCT_ADDR(entry, int_tree_node, tree_entry)->val;
|
||||
struct avl_tree_node *entry = lb_avl_tree_smallest(&tree);
|
||||
uint32 size = 0;
|
||||
int32 prev = -1;
|
||||
int32 cur = OBTAIN_STRUCT_ADDR(entry, int_tree_node, tree_entry)->val;
|
||||
while (entry != NULL)
|
||||
{
|
||||
if (cur < prev)
|
||||
{
|
||||
result = false;
|
||||
result = FALSE;
|
||||
break;
|
||||
}
|
||||
size++;
|
||||
@ -865,7 +866,7 @@ static bool test_apocalypse(void)
|
||||
{
|
||||
if (cur > prev)
|
||||
{
|
||||
result = false;
|
||||
result = FALSE;
|
||||
break;
|
||||
}
|
||||
size++;
|
||||
|
@ -49,10 +49,10 @@ static bool gat_full(void)
|
||||
{
|
||||
if (gat[i] == NULL)
|
||||
{
|
||||
return false;
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void gat_free(void)
|
||||
@ -76,7 +76,7 @@ static void ginfo_push(char *case_name, bool success)
|
||||
{
|
||||
ginfo[i].case_name = r_case_name;
|
||||
ginfo[i].success = success;
|
||||
ginfo[i].used = true;
|
||||
ginfo[i].used = TRUE;
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -93,14 +93,14 @@ void SXAPI test_begin(char *name)
|
||||
}
|
||||
for (int i = 0; i < CASE_NUM; i++)
|
||||
{
|
||||
ginfo[i].used = false;
|
||||
ginfo[i].used = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
void SXAPI test_end(void)
|
||||
{
|
||||
gat_free();
|
||||
int32_t total = 0, failed = 0, success = 0;
|
||||
int32 total = 0, failed = 0, success = 0;
|
||||
for (int i = 0; i < CASE_NUM; i++)
|
||||
{
|
||||
if (ginfo[i].used)
|
||||
@ -131,11 +131,11 @@ void SXAPI test_end(void)
|
||||
}
|
||||
for (int i = 0; i < CASE_NUM; i++)
|
||||
{
|
||||
ginfo[i].used = false;
|
||||
ginfo[i].used = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
void SXAPI *talloc(uint32_t size)
|
||||
void SXAPI *talloc(uint32 size)
|
||||
{
|
||||
if (!gat_full())
|
||||
{
|
||||
|
@ -5,13 +5,13 @@
|
||||
|
||||
typedef struct
|
||||
{
|
||||
linked_list_node_t lnode;
|
||||
struct linked_list_node lnode;
|
||||
int val;
|
||||
} my_list_node;
|
||||
|
||||
static bool validate_list(linked_list_t *list)
|
||||
static bool validate_list(struct linked_list *list)
|
||||
{
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
// list_head_test
|
||||
if (list->head != NULL)
|
||||
{
|
||||
@ -35,14 +35,14 @@ static bool validate_list(linked_list_t *list)
|
||||
}
|
||||
|
||||
|
||||
static bool assert_list(linked_list_t *list, int val[], int size)
|
||||
static bool assert_list(struct linked_list *list, int val[], int size)
|
||||
{
|
||||
linked_list_node_t *node = lb_linked_list_first(list);
|
||||
struct linked_list_node *node = lb_linked_list_first(list);
|
||||
int i = 0;
|
||||
|
||||
if (!validate_list(list))
|
||||
{
|
||||
return false;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
while (node != NULL && i < size)
|
||||
@ -50,7 +50,7 @@ static bool assert_list(linked_list_t *list, int val[], int size)
|
||||
my_list_node *enode = OBTAIN_STRUCT_ADDR(node, my_list_node, lnode);
|
||||
if (enode->val != val[i])
|
||||
{
|
||||
return false;
|
||||
return FALSE;
|
||||
}
|
||||
i++;
|
||||
node = lb_linked_list_next(node);
|
||||
@ -58,7 +58,7 @@ static bool assert_list(linked_list_t *list, int val[], int size)
|
||||
|
||||
if (i != size)
|
||||
{
|
||||
return false;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
node = lb_linked_list_last(list);
|
||||
@ -67,7 +67,7 @@ static bool assert_list(linked_list_t *list, int val[], int size)
|
||||
my_list_node *enode = OBTAIN_STRUCT_ADDR(node, my_list_node, lnode);
|
||||
if (enode->val != val[i - 1])
|
||||
{
|
||||
return false;
|
||||
return FALSE;
|
||||
}
|
||||
i--;
|
||||
node = lb_linked_list_prev(node);
|
||||
@ -76,13 +76,13 @@ static bool assert_list(linked_list_t *list, int val[], int size)
|
||||
return i == 0;
|
||||
}
|
||||
|
||||
//void print_validate(linked_list_t *list)
|
||||
//void print_validate(struct linked_list *list)
|
||||
//{
|
||||
// my_list_node* node = (my_list_node*) linked_list_first(list);
|
||||
// while(node != NULL)
|
||||
// {
|
||||
// hal_printf("%d", node->val);
|
||||
// node = (my_list_node*) linked_list_next((linked_list_node_t *) node);
|
||||
// node = (my_list_node*) linked_list_next((struct linked_list_node *) node);
|
||||
// }
|
||||
//
|
||||
// hal_printf("======");
|
||||
@ -90,7 +90,7 @@ static bool assert_list(linked_list_t *list, int val[], int size)
|
||||
// while(node != NULL)
|
||||
// {
|
||||
// hal_printf("%d", node->val);
|
||||
// node = (my_list_node*) linked_list_prev((linked_list_node_t *) node);
|
||||
// node = (my_list_node*) linked_list_prev((struct linked_list_node *) node);
|
||||
// }
|
||||
//
|
||||
// validate_list(list);
|
||||
@ -98,21 +98,21 @@ static bool assert_list(linked_list_t *list, int val[], int size)
|
||||
// return;
|
||||
//}
|
||||
|
||||
static void insert_val(linked_list_t *list, int index, int val)
|
||||
static void insert_val(struct linked_list *list, int index, int val)
|
||||
{
|
||||
my_list_node *a = (my_list_node *) talloc(sizeof(my_list_node));
|
||||
a->val = val;
|
||||
lb_linked_list_insert(list, index, &a->lnode);
|
||||
}
|
||||
|
||||
static void push_back_val(linked_list_t *list, int val)
|
||||
static void push_back_val(struct linked_list *list, int val)
|
||||
{
|
||||
my_list_node *a = (my_list_node *) talloc(sizeof(my_list_node));
|
||||
a->val = val;
|
||||
lb_linked_list_push_back(list, &a->lnode);
|
||||
}
|
||||
|
||||
static void push_front_val(linked_list_t *list, int val)
|
||||
static void push_front_val(struct linked_list *list, int val)
|
||||
{
|
||||
my_list_node *a = (my_list_node *) talloc(sizeof(my_list_node));
|
||||
a->val = val;
|
||||
@ -122,7 +122,7 @@ static void push_front_val(linked_list_t *list, int val)
|
||||
|
||||
static bool insert_test_beginning(void)
|
||||
{
|
||||
linked_list_t list;
|
||||
struct linked_list list;
|
||||
lb_linked_list_init(&list);
|
||||
insert_val(&list, 0, 0);
|
||||
insert_val(&list, 0, 1);
|
||||
@ -136,7 +136,7 @@ static bool insert_test_beginning(void)
|
||||
|
||||
static bool insert_test_middle(void)
|
||||
{
|
||||
linked_list_t list;
|
||||
struct linked_list list;
|
||||
lb_linked_list_init(&list);
|
||||
|
||||
insert_val(&list, 0, 0);
|
||||
@ -153,7 +153,7 @@ static bool insert_test_middle(void)
|
||||
|
||||
static bool insert_test_end(void)
|
||||
{
|
||||
linked_list_t list;
|
||||
struct linked_list list;
|
||||
lb_linked_list_init(&list);
|
||||
|
||||
insert_val(&list, 0, 0);
|
||||
@ -167,7 +167,7 @@ static bool insert_test_end(void)
|
||||
|
||||
static bool insert_test_invalid(void)
|
||||
{
|
||||
linked_list_t list;
|
||||
struct linked_list list;
|
||||
lb_linked_list_init(&list);
|
||||
|
||||
insert_val(&list, 0, 3);
|
||||
@ -197,7 +197,7 @@ static bool insert_test_invalid(void)
|
||||
|
||||
static bool remove_test_beginning(void)
|
||||
{
|
||||
linked_list_t list;
|
||||
struct linked_list list;
|
||||
lb_linked_list_init(&list);
|
||||
insert_val(&list, 0, 0);
|
||||
insert_val(&list, 0, 1);
|
||||
@ -214,7 +214,7 @@ static bool remove_test_beginning(void)
|
||||
|
||||
static bool remove_test_middle(void)
|
||||
{
|
||||
linked_list_t list;
|
||||
struct linked_list list;
|
||||
lb_linked_list_init(&list);
|
||||
|
||||
insert_val(&list, 0, 0);
|
||||
@ -235,7 +235,7 @@ static bool remove_test_middle(void)
|
||||
|
||||
static bool remove_test_end(void)
|
||||
{
|
||||
linked_list_t list;
|
||||
struct linked_list list;
|
||||
lb_linked_list_init(&list);
|
||||
|
||||
insert_val(&list, 0, 0);
|
||||
@ -252,8 +252,8 @@ static bool remove_test_end(void)
|
||||
|
||||
static bool remove_test_all(void)
|
||||
{
|
||||
bool result = true;
|
||||
linked_list_t list;
|
||||
bool result = TRUE;
|
||||
struct linked_list list;
|
||||
lb_linked_list_init(&list);
|
||||
|
||||
insert_val(&list, 0, 0);
|
||||
@ -297,7 +297,7 @@ static bool remove_test_all(void)
|
||||
|
||||
static bool remove_test_invalid(void)
|
||||
{
|
||||
linked_list_t list;
|
||||
struct linked_list list;
|
||||
lb_linked_list_init(&list);
|
||||
|
||||
insert_val(&list, 0, 3);
|
||||
@ -327,10 +327,10 @@ static bool remove_test_invalid(void)
|
||||
|
||||
static bool size_test(void)
|
||||
{
|
||||
bool result = true;
|
||||
linked_list_t list;
|
||||
bool result = TRUE;
|
||||
struct linked_list list;
|
||||
lb_linked_list_init(&list);
|
||||
linked_list_t list2;
|
||||
struct linked_list list2;
|
||||
lb_linked_list_init(&list2);
|
||||
|
||||
insert_val(&list, 0, 0);
|
||||
@ -347,8 +347,8 @@ static bool size_test(void)
|
||||
|
||||
static bool push_pop_front_test(void)
|
||||
{
|
||||
bool result = true;
|
||||
linked_list_t list;
|
||||
bool result = TRUE;
|
||||
struct linked_list list;
|
||||
lb_linked_list_init(&list);
|
||||
|
||||
push_front_val(&list, 1);
|
||||
@ -375,8 +375,8 @@ static bool push_pop_front_test(void)
|
||||
|
||||
static bool push_pop_back_test(void)
|
||||
{
|
||||
bool result = true;
|
||||
linked_list_t list;
|
||||
bool result = TRUE;
|
||||
struct linked_list list;
|
||||
lb_linked_list_init(&list);
|
||||
|
||||
push_back_val(&list, 1);
|
||||
@ -401,16 +401,16 @@ static bool push_pop_back_test(void)
|
||||
return result;
|
||||
}
|
||||
|
||||
static int32_t equals(void *a, void *b)
|
||||
static int32 equals(void *a, void *b)
|
||||
{
|
||||
return ((int64_t) (linked_list_node_t *) b) ==
|
||||
OBTAIN_STRUCT_ADDR((linked_list_node_t *) a, my_list_node, lnode)->val;
|
||||
return ((int64) (struct linked_list_node *) b) ==
|
||||
OBTAIN_STRUCT_ADDR((struct linked_list_node *) a, my_list_node, lnode)->val;
|
||||
}
|
||||
|
||||
static bool search_test(void)
|
||||
{
|
||||
bool result = true;
|
||||
linked_list_t list;
|
||||
bool result = TRUE;
|
||||
struct linked_list list;
|
||||
lb_linked_list_init(&list);
|
||||
|
||||
push_back_val(&list, 1);
|
||||
@ -422,14 +422,14 @@ static bool search_test(void)
|
||||
result = result && assert_list(&list, val1, 4);
|
||||
|
||||
result = result && (lb_linked_list_search(&list, (void *) 4, equals) == 3);
|
||||
result = result && (lb_linked_list_search(&list, (linked_list_node_t *) 3, equals) == 2);
|
||||
result = result && (lb_linked_list_search(&list, (linked_list_node_t *) 2, equals) == 1);
|
||||
result = result && (lb_linked_list_search(&list, (linked_list_node_t *) 1, equals) == 0);
|
||||
result = result && (lb_linked_list_search(&list, (struct linked_list_node *) 3, equals) == 2);
|
||||
result = result && (lb_linked_list_search(&list, (struct linked_list_node *) 2, equals) == 1);
|
||||
result = result && (lb_linked_list_search(&list, (struct linked_list_node *) 1, equals) == 0);
|
||||
|
||||
result = result && (lb_linked_list_search(&list, NULL, equals) == -1);
|
||||
result = result && (lb_linked_list_search(NULL, (linked_list_node_t *) 1, equals) == -1);
|
||||
result = result && (lb_linked_list_search(NULL, (struct linked_list_node *) 1, equals) == -1);
|
||||
|
||||
linked_list_node_t *node = lb_linked_list_get(&list, 1);
|
||||
struct linked_list_node *node = lb_linked_list_get(&list, 1);
|
||||
result = result && (lb_linked_list_search(&list, node, NULL) == 1);
|
||||
|
||||
|
||||
|
@ -1,69 +1,70 @@
|
||||
#include "test/driver.h"
|
||||
#include "lib/salloc.h"
|
||||
#include "test/test_case.h"
|
||||
|
||||
typedef union
|
||||
{
|
||||
uint32_t size;
|
||||
uint32_t flags;
|
||||
} _salloc_header;
|
||||
uint32 size;
|
||||
uint32 flags;
|
||||
} salloc_header;
|
||||
|
||||
const uint32_t salloc_header_size = sizeof(_salloc_header);
|
||||
static const uint32 salloc_header_size = sizeof(salloc_header);
|
||||
|
||||
static char buffer[1024];
|
||||
|
||||
static bool salloc_init_test(void)
|
||||
{
|
||||
lb_salloc_init(buffer, 1024);
|
||||
uint32_t blk_size[] = {1024};
|
||||
bool blk_free[] = {true};
|
||||
uint32 blk_size[] = {1024};
|
||||
bool blk_free[] = {TRUE};
|
||||
return lb_salloc_assert(buffer, blk_size, blk_free, 1);
|
||||
}
|
||||
|
||||
static bool salloc_basic_alloc(void)
|
||||
{
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
lb_salloc_init(buffer, 1024);
|
||||
result = result && (lb_salloc(buffer, 10) != NULL);
|
||||
uint32_t blk_size[] = {10 + salloc_header_size, 1024 - 10 - salloc_header_size};
|
||||
bool blk_free[] = {false, true};
|
||||
uint32 blk_size[] = {10 + salloc_header_size, 1024 - 10 - salloc_header_size};
|
||||
bool blk_free[] = {FALSE, TRUE};
|
||||
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 2);
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool salloc_full_alloc(void)
|
||||
{
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
lb_salloc_init(buffer, 1024);
|
||||
result = result && (lb_salloc(buffer, 1024 - salloc_header_size) != NULL);
|
||||
uint32_t blk_size[] = {1024};
|
||||
bool blk_free[] = {false};
|
||||
uint32 blk_size[] = {1024};
|
||||
bool blk_free[] = {FALSE};
|
||||
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool salloc_overflow_alloc(void)
|
||||
{
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
lb_salloc_init(buffer, 1024);
|
||||
result = result && (lb_salloc(buffer, 1024 - salloc_header_size + 1) == NULL);
|
||||
uint32_t blk_size[] = {1024};
|
||||
bool blk_free[] = {true};
|
||||
uint32 blk_size[] = {1024};
|
||||
bool blk_free[] = {TRUE};
|
||||
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool salloc_multiple_alloc(void)
|
||||
{
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
lb_salloc_init(buffer, 1024);
|
||||
result = result && (lb_salloc(buffer, 10) != NULL);
|
||||
result = result && (lb_salloc(buffer, 10) != NULL);
|
||||
result = result && (lb_salloc(buffer, 10) != NULL);
|
||||
uint32_t blk_size[] = {10 + salloc_header_size,
|
||||
uint32 blk_size[] = {10 + salloc_header_size,
|
||||
10 + salloc_header_size,
|
||||
10 + salloc_header_size,
|
||||
1024 - 3 * (10 + salloc_header_size)};
|
||||
bool blk_free[] = {false, false, false, true};
|
||||
bool blk_free[] = {FALSE, FALSE, FALSE, TRUE};
|
||||
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4);
|
||||
return result;
|
||||
}
|
||||
@ -71,12 +72,12 @@ static bool salloc_multiple_alloc(void)
|
||||
static bool salloc_alloc_not_enough(void)
|
||||
{
|
||||
void *ptr;
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
lb_salloc_init(buffer, salloc_header_size + salloc_header_size + salloc_header_size - 1);
|
||||
ptr = lb_salloc(buffer, salloc_header_size);
|
||||
result = result && (ptr != NULL);
|
||||
uint32_t blk_size[] = {salloc_header_size + salloc_header_size + salloc_header_size - 1};
|
||||
bool blk_free[] = {false};
|
||||
uint32 blk_size[] = {salloc_header_size + salloc_header_size + salloc_header_size - 1};
|
||||
bool blk_free[] = {FALSE};
|
||||
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
|
||||
return result;
|
||||
}
|
||||
@ -85,14 +86,14 @@ static bool salloc_alloc_not_enough(void)
|
||||
static bool salloc_basic_free(void)
|
||||
{
|
||||
void *ptr;
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
lb_salloc_init(buffer, 1024);
|
||||
ptr = lb_salloc(buffer, 10);
|
||||
result = result && (ptr != NULL);
|
||||
|
||||
lb_sfree(buffer, ptr);
|
||||
uint32_t blk_size[] = {1024};
|
||||
bool blk_free[] = {true};
|
||||
uint32 blk_size[] = {1024};
|
||||
bool blk_free[] = {TRUE};
|
||||
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
|
||||
return result;
|
||||
}
|
||||
@ -100,14 +101,14 @@ static bool salloc_basic_free(void)
|
||||
static bool salloc_full_free(void)
|
||||
{
|
||||
void *ptr;
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
lb_salloc_init(buffer, 1024);
|
||||
ptr = lb_salloc(buffer, 1024 - salloc_header_size);
|
||||
result = result && (ptr != NULL);
|
||||
|
||||
lb_sfree(buffer, ptr);
|
||||
uint32_t blk_size[] = {1024};
|
||||
bool blk_free[] = {true};
|
||||
uint32 blk_size[] = {1024};
|
||||
bool blk_free[] = {TRUE};
|
||||
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
|
||||
return result;
|
||||
}
|
||||
@ -115,7 +116,7 @@ static bool salloc_full_free(void)
|
||||
static bool salloc_multiple_free(void)
|
||||
{
|
||||
void *ptr1, *ptr2, *ptr3, *ptr4;
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
lb_salloc_init(buffer, 1024);
|
||||
ptr1 = lb_salloc(buffer, 10);
|
||||
ptr2 = lb_salloc(buffer, 10);
|
||||
@ -125,12 +126,12 @@ static bool salloc_multiple_free(void)
|
||||
lb_sfree(buffer, ptr1);
|
||||
lb_sfree(buffer, ptr3);
|
||||
|
||||
uint32_t blk_size[] = {10 + salloc_header_size,
|
||||
uint32 blk_size[] = {10 + salloc_header_size,
|
||||
10 + salloc_header_size,
|
||||
10 + salloc_header_size,
|
||||
10 + salloc_header_size,
|
||||
1024 - 4 * (10 + salloc_header_size)};
|
||||
bool blk_free[] = {true, false, true, false, true};
|
||||
bool blk_free[] = {TRUE, FALSE, TRUE, FALSE, TRUE};
|
||||
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 5);
|
||||
return result;
|
||||
}
|
||||
@ -138,7 +139,7 @@ static bool salloc_multiple_free(void)
|
||||
static bool salloc_free_join_tail(void)
|
||||
{
|
||||
void *ptr1, *ptr2, *ptr3, *ptr4;
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
lb_salloc_init(buffer, 1024);
|
||||
ptr1 = lb_salloc(buffer, 10);
|
||||
ptr2 = lb_salloc(buffer, 10);
|
||||
@ -147,11 +148,11 @@ static bool salloc_free_join_tail(void)
|
||||
result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL);
|
||||
lb_sfree(buffer, ptr4);
|
||||
|
||||
uint32_t blk_size[] = {10 + salloc_header_size,
|
||||
uint32 blk_size[] = {10 + salloc_header_size,
|
||||
10 + salloc_header_size,
|
||||
10 + salloc_header_size,
|
||||
1024 - 3 * (10 + salloc_header_size)};
|
||||
bool blk_free[] = {false, false, false, true};
|
||||
bool blk_free[] = {FALSE, FALSE, FALSE, TRUE};
|
||||
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4);
|
||||
return result;
|
||||
}
|
||||
@ -159,7 +160,7 @@ static bool salloc_free_join_tail(void)
|
||||
static bool salloc_free_join_head(void)
|
||||
{
|
||||
void *ptr1, *ptr2, *ptr3, *ptr4;
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
lb_salloc_init(buffer, 1024);
|
||||
ptr1 = lb_salloc(buffer, 10);
|
||||
ptr2 = lb_salloc(buffer, 10);
|
||||
@ -169,11 +170,11 @@ static bool salloc_free_join_head(void)
|
||||
lb_sfree(buffer, ptr1);
|
||||
lb_sfree(buffer, ptr2);
|
||||
|
||||
uint32_t blk_size[] = {2 * (10 + salloc_header_size),
|
||||
uint32 blk_size[] = {2 * (10 + salloc_header_size),
|
||||
10 + salloc_header_size,
|
||||
10 + salloc_header_size,
|
||||
1024 - 4 * (10 + salloc_header_size)};
|
||||
bool blk_free[] = {true, false, false, true};
|
||||
bool blk_free[] = {TRUE, FALSE, FALSE, TRUE};
|
||||
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4);
|
||||
return result;
|
||||
}
|
||||
@ -181,7 +182,7 @@ static bool salloc_free_join_head(void)
|
||||
static bool salloc_free_join_mid(void)
|
||||
{
|
||||
void *ptr1, *ptr2, *ptr3, *ptr4;
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
lb_salloc_init(buffer, 1024);
|
||||
ptr1 = lb_salloc(buffer, 10);
|
||||
ptr2 = lb_salloc(buffer, 10);
|
||||
@ -191,11 +192,11 @@ static bool salloc_free_join_mid(void)
|
||||
lb_sfree(buffer, ptr2);
|
||||
lb_sfree(buffer, ptr3);
|
||||
|
||||
uint32_t blk_size[] = {10 + salloc_header_size,
|
||||
uint32 blk_size[] = {10 + salloc_header_size,
|
||||
2 * (10 + salloc_header_size),
|
||||
10 + salloc_header_size,
|
||||
1024 - 4 * (10 + salloc_header_size)};
|
||||
bool blk_free[] = {false, true, false, true};
|
||||
bool blk_free[] = {FALSE, TRUE, FALSE, TRUE};
|
||||
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4);
|
||||
return result;
|
||||
}
|
||||
@ -203,7 +204,7 @@ static bool salloc_free_join_mid(void)
|
||||
static bool salloc_free_join_consecutive(void)
|
||||
{
|
||||
void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
lb_salloc_init(buffer, 1024);
|
||||
ptr1 = lb_salloc(buffer, 10);
|
||||
ptr2 = lb_salloc(buffer, 10);
|
||||
@ -214,22 +215,22 @@ static bool salloc_free_join_consecutive(void)
|
||||
lb_sfree(buffer, ptr2);
|
||||
lb_sfree(buffer, ptr4);
|
||||
|
||||
uint32_t blk_size[] = {10 + salloc_header_size,
|
||||
uint32 blk_size[] = {10 + salloc_header_size,
|
||||
10 + salloc_header_size,
|
||||
10 + salloc_header_size,
|
||||
10 + salloc_header_size,
|
||||
10 + salloc_header_size,
|
||||
1024 - 5 * (10 + salloc_header_size)};
|
||||
bool blk_free[] = {false, true, false, true, false, true};
|
||||
bool blk_free[] = {FALSE, TRUE, FALSE, TRUE, FALSE, TRUE};
|
||||
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 6);
|
||||
|
||||
lb_sfree(buffer, ptr3);
|
||||
|
||||
uint32_t blk_size2[] = {10 + salloc_header_size,
|
||||
uint32 blk_size2[] = {10 + salloc_header_size,
|
||||
3 * (10 + salloc_header_size),
|
||||
10 + salloc_header_size,
|
||||
1024 - 5 * (10 + salloc_header_size)};
|
||||
bool blk_free2[] = {false, true, false, true};
|
||||
bool blk_free2[] = {FALSE, TRUE, FALSE, TRUE};
|
||||
result = result && lb_salloc_assert(buffer, blk_size2, blk_free2, 4);
|
||||
return result;
|
||||
}
|
||||
@ -237,7 +238,7 @@ static bool salloc_free_join_consecutive(void)
|
||||
static bool salloc_free_all(void)
|
||||
{
|
||||
void *ptr1, *ptr2, *ptr3, *ptr4;
|
||||
bool result = true;
|
||||
bool result = TRUE;
|
||||
lb_salloc_init(buffer, 1024);
|
||||
ptr1 = lb_salloc(buffer, 10);
|
||||
ptr2 = lb_salloc(buffer, 10);
|
||||
@ -249,8 +250,8 @@ static bool salloc_free_all(void)
|
||||
lb_sfree(buffer, ptr3);
|
||||
lb_sfree(buffer, ptr4);
|
||||
|
||||
uint32_t blk_size[] = {1024};
|
||||
bool blk_free[] = {true};
|
||||
uint32 blk_size[] = {1024};
|
||||
bool blk_free[] = {TRUE};
|
||||
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
|
||||
return result;
|
||||
}
|
||||
@ -278,4 +279,4 @@ void SXAPI salloc_test(void)
|
||||
run_case("salloc_free_all", salloc_free_all());
|
||||
|
||||
test_end();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user