Added mem_realloc and string functions
This commit is contained in:
parent
56c522d05a
commit
12c9b4fdcc
10 changed files with 278 additions and 15 deletions
23
boot/common/init.c
Normal file
23
boot/common/init.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
/*********************************************************************************/
|
||||
/* Module Name: init.c */
|
||||
/* Project: AurixOS */
|
||||
/* */
|
||||
/* Copyright (c) 2024-2025 Jozef Nagy */
|
||||
/* */
|
||||
/* This source is subject to the MIT License. */
|
||||
/* See License.txt in the root of this repository. */
|
||||
/* All other rights reserved. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
|
||||
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
|
||||
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
|
||||
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
|
||||
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
|
||||
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
|
||||
/* SOFTWARE. */
|
||||
/*********************************************************************************/
|
||||
|
||||
void axboot_init()
|
||||
{
|
||||
while (1);
|
||||
}
|
|
@ -18,6 +18,7 @@
|
|||
/*********************************************************************************/
|
||||
|
||||
#include <lib/string.h>
|
||||
#include <mm/mman.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
@ -49,6 +50,46 @@ size_t mbstowcs(wchar_t *dest, const char **src, size_t len)
|
|||
return len - count;
|
||||
}
|
||||
|
||||
size_t strspn(const char *s, const char *accept)
|
||||
{
|
||||
const char *p;
|
||||
size_t count = 0;
|
||||
|
||||
while (s != NULL) {
|
||||
for (p = accept; *p; p++) {
|
||||
if (*s == *p) {
|
||||
count++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!*p) {
|
||||
break;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
size_t strcspn(const char *s, const char *reject)
|
||||
{
|
||||
const char *p;
|
||||
size_t count = 0;
|
||||
|
||||
while (*s) {
|
||||
for (p = reject; *p; p++) {
|
||||
if (*s == *p) {
|
||||
return count;
|
||||
}
|
||||
}
|
||||
s++;
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
size_t strlen(const char *str)
|
||||
{
|
||||
size_t count = 0;
|
||||
|
@ -64,6 +105,26 @@ size_t strlen(const char *str)
|
|||
return count;
|
||||
}
|
||||
|
||||
int strcmp(const char *s1, const char *s2)
|
||||
{
|
||||
while (*s1 && (*s1 == *s2)) {
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
|
||||
return *(unsigned char *)s1 - *(unsigned char *)s2;
|
||||
}
|
||||
|
||||
int strncmp(const char *s1, const char *s2, size_t n)
|
||||
{
|
||||
while (n-- && *s1 && (*s1 == *s2)) {
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
|
||||
return n ? (*(unsigned char *)s1 - *(unsigned char *)s2) : 0;
|
||||
}
|
||||
|
||||
char *strcpy(char *dest, const char *src)
|
||||
{
|
||||
if (dest == NULL || src == NULL) {
|
||||
|
@ -82,6 +143,80 @@ char *strcpy(char *dest, const char *src)
|
|||
return pdest;
|
||||
}
|
||||
|
||||
// TODO: Get rid of this function
|
||||
char *strdup(const char *s)
|
||||
{
|
||||
size_t len = strlen(s);
|
||||
char *new = (char *)mem_alloc(len + 1);
|
||||
|
||||
if (new) {
|
||||
strcpy(new, s);
|
||||
}
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
char *strtok(char *str, const char *delim)
|
||||
{
|
||||
static char *last;
|
||||
char *end;
|
||||
|
||||
if (str == NULL)
|
||||
{
|
||||
str = last;
|
||||
}
|
||||
if (str == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
str += strspn(str, delim);
|
||||
if (*str == '\0')
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
end = str + strcspn(str, delim);
|
||||
if (*end)
|
||||
{
|
||||
*end++ = '\0';
|
||||
}
|
||||
|
||||
last = end;
|
||||
return str;
|
||||
}
|
||||
|
||||
char *strchr(char *s, int c)
|
||||
{
|
||||
if (s == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (*s != 0) {
|
||||
if (*s == c)
|
||||
return (char *)s;
|
||||
s++;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *strrchr(char *s, int c)
|
||||
{
|
||||
const char *last = NULL;
|
||||
|
||||
if (s == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (*s != 0) {
|
||||
if (*s == (char)c)
|
||||
last = s;
|
||||
s++;
|
||||
}
|
||||
return (char *)last;
|
||||
}
|
||||
|
||||
void *memset(void *dest, int val, size_t len)
|
||||
{
|
||||
unsigned char *ptr = dest;
|
||||
|
@ -91,7 +226,7 @@ void *memset(void *dest, int val, size_t len)
|
|||
return dest;
|
||||
}
|
||||
|
||||
void *memcpy(void *dest, void *src, size_t len)
|
||||
void *memcpy(void *dest, const void *src, size_t len)
|
||||
{
|
||||
char *d = (char *)dest;
|
||||
const char *s = (const char *)src;
|
||||
|
|
|
@ -17,10 +17,49 @@
|
|||
/* SOFTWARE. */
|
||||
/*********************************************************************************/
|
||||
|
||||
#include <mem/mman.h>
|
||||
#include <arch/mm/paging.h>
|
||||
#include <mm/mman.h>
|
||||
#include <lib/string.h>
|
||||
#include <print.h>
|
||||
#include <stddef.h>
|
||||
|
||||
struct alloc_header allocation_list[MAX_ALLOCATIONS] = {0};
|
||||
|
||||
int find_alloc(void *addr)
|
||||
{
|
||||
for (int i = 0; i < MAX_ALLOCATIONS; i++) {
|
||||
if (allocation_list[i].addr == addr) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int add_alloc(void *addr, size_t size)
|
||||
{
|
||||
for (int i = 0; i < MAX_ALLOCATIONS; i++) {
|
||||
if (allocation_list[i].addr == 0) {
|
||||
allocation_list[i].addr = addr;
|
||||
allocation_list[i].size = size;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void remove_alloc(void *addr)
|
||||
{
|
||||
int i = find_alloc(addr);
|
||||
if (i == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
allocation_list[i].addr = 0;
|
||||
allocation_list[i].size = 0;
|
||||
}
|
||||
|
||||
#ifndef AXBOOT_UEFI
|
||||
|
||||
#warning "Memory management is not implemented yet, expect runtime errors."
|
||||
|
@ -40,7 +79,15 @@ int mem_allocat(void *addr, size_t npages)
|
|||
return 0;
|
||||
}
|
||||
|
||||
void mem_free(void **addr)
|
||||
void *mem_realloc(void *addr, size_t n)
|
||||
{
|
||||
(void)addr;
|
||||
(void)n;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void mem_free(void *addr)
|
||||
{
|
||||
(void)addr;
|
||||
}
|
||||
|
@ -61,6 +108,8 @@ void *mem_alloc(size_t n)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
add_alloc(alloc, n);
|
||||
|
||||
return alloc;
|
||||
}
|
||||
|
||||
|
@ -75,10 +124,36 @@ int mem_allocat(void *addr, size_t npages)
|
|||
return 0;
|
||||
}
|
||||
|
||||
add_alloc(addr, npages * PAGE_SIZE);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void mem_free(void **addr)
|
||||
void *mem_realloc(void *addr, size_t n)
|
||||
{
|
||||
size_t old_size;
|
||||
void *new = NULL;
|
||||
|
||||
int i = find_alloc(addr);
|
||||
if (i == -1) {
|
||||
debug("mem_realloc(): Couldn't find allocation for 0x%lx.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
old_size = allocation_list[i].size;
|
||||
|
||||
new = mem_alloc(n);
|
||||
if (!new) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(new, addr, old_size);
|
||||
mem_free(addr);
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
void mem_free(void *addr)
|
||||
{
|
||||
EFI_STATUS status;
|
||||
|
||||
|
@ -86,13 +161,13 @@ void mem_free(void **addr)
|
|||
return;
|
||||
}
|
||||
|
||||
status = gBootServices->FreePool(*addr);
|
||||
status = gBootServices->FreePool(addr);
|
||||
if (EFI_ERROR(status)) {
|
||||
debug("mem_free(): Couldn't free 0x%lx: %s (%lx)\n", *addr, efi_status_to_str(status), status);
|
||||
debug("mem_free(): Couldn't free 0x%lx: %s (%lx)\n", addr, efi_status_to_str(status), status);
|
||||
return;
|
||||
}
|
||||
|
||||
*addr = NULL;
|
||||
remove_alloc(addr);
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -66,3 +66,10 @@ void debug(const char *fmt, ...)
|
|||
#endif
|
||||
}
|
||||
|
||||
void snprintf(char *buf, size_t size, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
npf_vsnprintf(buf, size, fmt, args);
|
||||
va_end(args);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue