libc: Add (de)allocate_memory wrappers located in sys/mman.h
This commit is contained in:
parent
139c0b5eb1
commit
d864bda751
14
apps/app.c
14
apps/app.c
@ -1,11 +1,9 @@
|
|||||||
#include <bits/mmap-flags.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/syscall.h>
|
#include <sys/mman.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
void bye()
|
void bye()
|
||||||
{
|
{
|
||||||
@ -24,17 +22,11 @@ int main()
|
|||||||
|
|
||||||
console_write("\n", 1);
|
console_write("\n", 1);
|
||||||
|
|
||||||
long rc = syscall(SYS_allocate_memory, 4096, PROT_READ | PROT_WRITE);
|
char* address = (char*)allocate_memory(PAGE_SIZE * 2, PROT_READ | PROT_WRITE);
|
||||||
if (rc < 0)
|
|
||||||
{
|
|
||||||
printf("allocate_memory: %s\n", strerror(-rc));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
char* address = (char*)rc;
|
|
||||||
printf("address: %p\n", address);
|
printf("address: %p\n", address);
|
||||||
printf("memory at address: %c\n", *address);
|
printf("memory at address: %c\n", *address);
|
||||||
*address = 'e';
|
*address = 'e';
|
||||||
printf("memory at address: %c\n", *address);
|
printf("memory at address: %c\n", *address);
|
||||||
|
|
||||||
syscall(SYS_deallocate_memory, address);
|
deallocate_memory(address, PAGE_SIZE * 2);
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ set(SOURCES
|
|||||||
src/atexit.cpp
|
src/atexit.cpp
|
||||||
src/ctype.cpp
|
src/ctype.cpp
|
||||||
src/time.cpp
|
src/time.cpp
|
||||||
|
src/sys/mman.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
if(${ARCH} STREQUAL "x86_64")
|
if(${ARCH} STREQUAL "x86_64")
|
||||||
|
23
libc/include/sys/mman.h
Normal file
23
libc/include/sys/mman.h
Normal 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
19
libc/src/sys/mman.cpp
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user