first
This commit is contained in:
11
rt-thread/components/libc/posix/io/mman/SConscript
Normal file
11
rt-thread/components/libc/posix/io/mman/SConscript
Normal file
@@ -0,0 +1,11 @@
|
||||
# RT-Thread building script for component
|
||||
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = ['mman.c']
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('POSIX', src, depend = ['RT_USING_POSIX_MMAN'], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
88
rt-thread/components/libc/posix/io/mman/mman.c
Normal file
88
rt-thread/components/libc/posix/io/mman/mman.c
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2017/11/30 Bernard The first version.
|
||||
* 2024/03/29 TroyMitchelle Add all function comments
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/statfs.h>
|
||||
#include <sys/errno.h>
|
||||
|
||||
#include "sys/mman.h"
|
||||
|
||||
/**
|
||||
* @brief Maps a region of memory into the calling process's address space.
|
||||
* @param addr Desired starting address of the mapping.
|
||||
* @param length Length of the mapping.
|
||||
* @param prot Protection of the mapped memory region.
|
||||
* @param flags Type of the mapped memory region.
|
||||
* @param fd File descriptor of the file to be mapped.
|
||||
* @param offset Offset within the file to start the mapping.
|
||||
* @return Upon success, returns a pointer to the mapped region; otherwise, MAP_FAILED is returned.
|
||||
*/
|
||||
void *mmap(void *addr, size_t length, int prot, int flags,
|
||||
int fd, off_t offset)
|
||||
{
|
||||
uint8_t *mem;
|
||||
|
||||
if (addr)
|
||||
{
|
||||
mem = addr;
|
||||
}
|
||||
else mem = (uint8_t *)malloc(length);
|
||||
|
||||
if (mem)
|
||||
{
|
||||
off_t cur;
|
||||
size_t read_bytes;
|
||||
|
||||
cur = lseek(fd, 0, SEEK_SET);
|
||||
|
||||
lseek(fd, offset, SEEK_SET);
|
||||
read_bytes = read(fd, mem, length);
|
||||
if (read_bytes != length)
|
||||
{
|
||||
if (addr == RT_NULL)
|
||||
{
|
||||
/* read failed */
|
||||
free(mem);
|
||||
mem = RT_NULL;
|
||||
}
|
||||
}
|
||||
lseek(fd, cur, SEEK_SET);
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
errno = ENOMEM;
|
||||
|
||||
return MAP_FAILED;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Unmaps a mapped region of memory.
|
||||
* @param addr Starting address of the mapping to be unmapped.
|
||||
* @param length Length of the mapping.
|
||||
* @return Upon success, returns 0; otherwise, -1 is returned.
|
||||
*/
|
||||
int munmap(void *addr, size_t length)
|
||||
{
|
||||
if (addr)
|
||||
{
|
||||
free(addr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
66
rt-thread/components/libc/posix/io/mman/sys/mman.h
Normal file
66
rt-thread/components/libc/posix/io/mman/sys/mman.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2017/11/30 Bernard The first version.
|
||||
* 2024/03/29 TroyMitchelle Add comments for all macros
|
||||
*/
|
||||
|
||||
#ifndef __SYS_MMAN_H__
|
||||
#define __SYS_MMAN_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#define MAP_FAILED ((void *) -1)
|
||||
|
||||
/* mmap flags */
|
||||
#define MAP_SHARED 0x01 /**< Share the mapping with other processes. */
|
||||
#define MAP_PRIVATE 0x02 /**< Create a private copy-on-write mapping. */
|
||||
#define MAP_TYPE 0x0f /**< Mask for type of mapping. */
|
||||
#define MAP_FIXED 0x10 /**< Interpret addr exactly. */
|
||||
#define MAP_ANON 0x20 /**< Anonymous mapping. */
|
||||
#define MAP_ANONYMOUS MAP_ANON /**< Synonym for MAP_ANON. */
|
||||
#define MAP_NORESERVE 0x4000 /**< Don't reserve swap space for this mapping. */
|
||||
#define MAP_GROWSDOWN 0x0100 /**< Stack-like segment. */
|
||||
#define MAP_DENYWRITE 0x0800 /**< ETXTBSY. */
|
||||
#define MAP_EXECUTABLE 0x1000 /**< Mark it as an executable. */
|
||||
#define MAP_LOCKED 0x2000 /**< Lock the mapping's pages. */
|
||||
#define MAP_POPULATE 0x8000 /**< Populate (prefault) pagetables. */
|
||||
#define MAP_NONBLOCK 0x10000 /**< Do not block on IO. */
|
||||
#define MAP_STACK 0x20000 /**< Allocation is a stack segment. */
|
||||
#define MAP_HUGETLB 0x40000 /**< Create a huge page mapping. */
|
||||
#define MAP_FILE 0 /**< Compatibility */
|
||||
|
||||
/* mmap protections */
|
||||
#define PROT_NONE 0 /**< No access. */
|
||||
#define PROT_READ 1 /**< Page can be read. */
|
||||
#define PROT_WRITE 2 /**< Page can be written. */
|
||||
#define PROT_EXEC 4 /**< Page can be executed. */
|
||||
#define PROT_GROWSDOWN 0x01000000/**< Extend change to start of growsdown vma (mprotect only). */
|
||||
#define PROT_GROWSUP 0x02000000/**< Extend change to start of growsup vma (mprotect only). */
|
||||
|
||||
/* msync flags */
|
||||
#define MS_ASYNC 1 /**< Perform asynchronous writes. */
|
||||
#define MS_INVALIDATE 2 /**< Invalidate mappings after writing. */
|
||||
#define MS_SYNC 4 /**< Perform synchronous writes. */
|
||||
|
||||
/* mlockall flags */
|
||||
#define MCL_CURRENT 1 /**< Lock all pages which are currently mapped into the address space of the process. */
|
||||
#define MCL_FUTURE 2 /**< Lock all pages which will become mapped into the address space of the process in the future. */
|
||||
#define MCL_ONFAULT 4 /**< Lock all pages which are currently mapped into the address space of the process on access. */
|
||||
|
||||
|
||||
void *mmap (void *start, size_t len, int prot, int flags, int fd, off_t off);
|
||||
int munmap (void *start, size_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
Reference in New Issue
Block a user