libc: Add (de)allocate_memory wrappers located in sys/mman.h

This commit is contained in:
apio 2023-01-13 19:08:02 +01:00
parent 139c0b5eb1
commit d864bda751
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 46 additions and 11 deletions

View File

@ -1,11 +1,9 @@
#include <bits/mmap-flags.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
void bye()
{
@ -24,17 +22,11 @@ int main()
console_write("\n", 1);
long rc = syscall(SYS_allocate_memory, 4096, PROT_READ | PROT_WRITE);
if (rc < 0)
{
printf("allocate_memory: %s\n", strerror(-rc));
return 1;
}
char* address = (char*)rc;
char* address = (char*)allocate_memory(PAGE_SIZE * 2, PROT_READ | PROT_WRITE);
printf("address: %p\n", address);
printf("memory at address: %c\n", *address);
*address = 'e';
printf("memory at address: %c\n", *address);
syscall(SYS_deallocate_memory, address);
deallocate_memory(address, PAGE_SIZE * 2);
}

View File

@ -10,6 +10,7 @@ set(SOURCES
src/atexit.cpp
src/ctype.cpp
src/time.cpp
src/sys/mman.cpp
)
if(${ARCH} STREQUAL "x86_64")

23
libc/include/sys/mman.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef _LUNA_MMAN_H
#define _LUNA_MMAN_H
#include <bits/mmap-flags.h>
#include <sys/types.h>
#define PAGE_SIZE 4096
#define MAP_FAILED (void*)-1
#ifdef __cplusplus
extern "C"
{
#endif
void* allocate_memory(size_t size, int flags);
int deallocate_memory(void* address, size_t size);
#ifdef __cplusplus
}
#endif
#endif

19
libc/src/sys/mman.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <bits/errno-return.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <unistd.h>
extern "C"
{
void* allocate_memory(size_t size, int flags)
{
long rc = syscall(SYS_allocate_memory, size, flags);
__errno_return(rc, void*);
}
int deallocate_memory(void* address, size_t size)
{
long rc = syscall(SYS_deallocate_memory, address, size);
__errno_return(rc, int);
}
}