Move string functions/utils from HAL to std/string
This commit is contained in:
@ -9,17 +9,6 @@ __attribute__((noreturn)) void hal_hang(void);
|
||||
void hal_init(void);
|
||||
void hal_intr_disable(void);
|
||||
void hal_intr_enable(void);
|
||||
void *hal_memset(void *p, int c, size_t n);
|
||||
void *hal_memcpy(void *dst, const void *src, size_t n);
|
||||
size_t hal_strlen(char *s);
|
||||
int hal_memcmp(const void *s1, const void *s2, int len);
|
||||
int hal_strcmp(const char *a, const char *b);
|
||||
size_t hal_strcspn(const char *s, const char *reject);
|
||||
size_t hal_strspn(const char *s, const char *accept);
|
||||
char *hal_strcpy(char *dest, const char *src);
|
||||
char *hal_strchr(const char *s, int c);
|
||||
char *hal_strstr(const char *str, const char *substring);
|
||||
char *hal_string_combine(char *dest, const char *src);
|
||||
void hal_wait(uint32_t ms);
|
||||
int32_t hal_randnum(void);
|
||||
|
||||
|
||||
@ -1,152 +0,0 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "hal.h"
|
||||
|
||||
void *hal_memset(void *p, int c, size_t n) {
|
||||
char *cp = p;
|
||||
for (size_t i = 0; i < n; i++) cp[i] = c;
|
||||
return p;
|
||||
}
|
||||
|
||||
void *hal_memcpy(void *dst, const void *src, size_t n) {
|
||||
char *a = dst;
|
||||
const char *b = src;
|
||||
for (size_t i = 0; i < n; i++) a[i] = b[i];
|
||||
return dst;
|
||||
}
|
||||
|
||||
size_t hal_strlen(char *s) {
|
||||
char *s2;
|
||||
for (s2 = s; *s2; ++s2);
|
||||
return (s2 - s);
|
||||
}
|
||||
|
||||
// https://aticleworld.com/memcmp-in-c/
|
||||
int hal_memcmp(const void *s1, const void *s2, int len)
|
||||
{
|
||||
unsigned char *p = (unsigned char *)s1;
|
||||
unsigned char *q = (unsigned char *)s2;
|
||||
int charCompareStatus = 0;
|
||||
//If both pointer pointing same memory block
|
||||
if (s1 == s2)
|
||||
{
|
||||
return charCompareStatus;
|
||||
}
|
||||
while (len > 0)
|
||||
{
|
||||
if (*p != *q)
|
||||
{ //compare the mismatching character
|
||||
charCompareStatus = (*p >*q)?1:-1;
|
||||
break;
|
||||
}
|
||||
len--;
|
||||
p++;
|
||||
q++;
|
||||
}
|
||||
return charCompareStatus;
|
||||
}
|
||||
|
||||
int hal_strcmp(const char *a, const char *b) {
|
||||
while (*a && (*a == *b)) {
|
||||
a++, b++;
|
||||
}
|
||||
return (unsigned char)*a - (unsigned char)*b;
|
||||
}
|
||||
|
||||
size_t hal_strcspn(const char *s, const char *reject) {
|
||||
size_t count = 0;
|
||||
for (; *s != '\0'; ++s) {
|
||||
const char *r = reject;
|
||||
while (*r != '\0') {
|
||||
if (*s == *r) {
|
||||
return count;
|
||||
}
|
||||
r++;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
size_t hal_strspn(const char *s, const char *accept) {
|
||||
size_t count = 0;
|
||||
for (; *s != '\0'; ++s) {
|
||||
const char *a = accept;
|
||||
int matched = 0;
|
||||
while (*a != '\0') {
|
||||
if (*s == *a) {
|
||||
matched = 1;
|
||||
break;
|
||||
}
|
||||
a++;
|
||||
}
|
||||
if (!matched) {
|
||||
return count;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
char *hal_strcpy(char *dest, const char *src) {
|
||||
char *d = dest;
|
||||
while ((*d++ = *src++) != '\0') {
|
||||
;
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
char *hal_strchr(const char *s, int c) {
|
||||
char ch = (char)c;
|
||||
while (*s != '\0') {
|
||||
if (*s == ch) {
|
||||
return (char *)s;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
if (ch == '\0') {
|
||||
return (char *)s;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *hal_strstr(const char *str, const char *substring)
|
||||
{
|
||||
const char *a;
|
||||
const char *b;
|
||||
|
||||
b = substring;
|
||||
|
||||
if (*b == 0) {
|
||||
return (char *) str;
|
||||
}
|
||||
|
||||
for ( ; *str != 0; str += 1) {
|
||||
if (*str != *b) {
|
||||
continue;
|
||||
}
|
||||
|
||||
a = str;
|
||||
while (1) {
|
||||
if (*b == 0) {
|
||||
return (char *) str;
|
||||
}
|
||||
if (*a++ != *b++) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
b = substring;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *hal_string_combine(char *dest, const char *src) {
|
||||
size_t i, j;
|
||||
for(i = 0; dest[i] != '\0'; i++);
|
||||
for(j = 0; src[j] != '\0'; j++) {
|
||||
dest[i+j] = src[j];
|
||||
}
|
||||
dest[i+j] = '\0';
|
||||
return dest;
|
||||
}
|
||||
@ -3,6 +3,7 @@
|
||||
#include "compiler/attr.h"
|
||||
#include "hal/hal.h"
|
||||
#include "gdt.h"
|
||||
#include "std/string.h"
|
||||
|
||||
#define GDT_PRESENT 0x80
|
||||
#define GDT_TSS 0x89
|
||||
@ -43,7 +44,7 @@ void gdt_setentry(GdtEntry *ent, uint32_t base, uint32_t limit, uint8_t acc, uin
|
||||
}
|
||||
|
||||
void gdt_init(void) {
|
||||
hal_memset(&tss, 0, sizeof(tss));
|
||||
memset(&tss, 0, sizeof(tss));
|
||||
tss.iopb_off = sizeof(tss);
|
||||
|
||||
tss.rsp0 = (uint64_t)(kernelstack + sizeof(kernelstack));
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
#include "proc/proc.h"
|
||||
#include "kprintf.h"
|
||||
#include "spinlock/spinlock.h"
|
||||
#include "std/string.h"
|
||||
|
||||
uint64_t KERNEL_CR3 = 0;
|
||||
SpinLock spinlock;
|
||||
@ -41,7 +42,7 @@ uint64_t *hal_vmm_nexttable(uint64_t *table, uint64_t ent, bool alloc) {
|
||||
|
||||
uint8_t *newphys = pmm_alloc(1);
|
||||
phys = (uint64_t)newphys;
|
||||
hal_memset(VIRT(phys), 0, HAL_PAGE_SIZE);
|
||||
memset(VIRT(phys), 0, HAL_PAGE_SIZE);
|
||||
table[ent] = phys | HAL_PG_USER | HAL_PG_RW | HAL_PG_PRESENT;
|
||||
}
|
||||
return (uint64_t *)((uint8_t *)VIRT(phys));
|
||||
@ -112,7 +113,7 @@ void hal_vmm_map_kern(uint64_t targetcr3) {
|
||||
uint64_t hal_vmm_userproc_pml4_phys(void) {
|
||||
uint8_t *cr3phys = pmm_alloc(1);
|
||||
uint64_t phys = (uint64_t)cr3phys;
|
||||
hal_memset(VIRT(phys), 0, HAL_PAGE_SIZE);
|
||||
memset(VIRT(phys), 0, HAL_PAGE_SIZE);
|
||||
|
||||
uint64_t *kcr3 = (uint64_t *)VIRT(KERNEL_CR3);
|
||||
uint64_t *pml4 = (uint64_t *)VIRT(phys);
|
||||
|
||||
Reference in New Issue
Block a user