portable error reporting for memory API
This commit is contained in:
parent
f773a96b59
commit
29ffcdf810
54
memory.c
54
memory.c
@ -25,30 +25,28 @@ void *memory_map(size_t size) {
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
int memory_map_fixed(void *ptr, size_t size) {
|
bool memory_map_fixed(void *ptr, size_t size) {
|
||||||
void *p = mmap(ptr, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED, -1, 0);
|
void *p = mmap(ptr, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED, -1, 0);
|
||||||
if (unlikely(p == MAP_FAILED)) {
|
bool ret = p == MAP_FAILED;
|
||||||
if (errno != ENOMEM) {
|
if (unlikely(ret) && errno != ENOMEM) {
|
||||||
fatal_error("non-ENOMEM MAP_FIXED mmap failure");
|
fatal_error("non-ENOMEM MAP_FIXED mmap failure");
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
return 0;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int memory_unmap(void *ptr, size_t size) {
|
bool memory_unmap(void *ptr, size_t size) {
|
||||||
int ret = munmap(ptr, size);
|
bool ret = munmap(ptr, size);
|
||||||
if (unlikely(ret) && errno != ENOMEM) {
|
if (unlikely(ret) && errno != ENOMEM) {
|
||||||
fatal_error("non-ENOMEM munmap failure");
|
fatal_error("non-ENOMEM munmap failure");
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int memory_protect_prot(void *ptr, size_t size, int prot, UNUSED int pkey) {
|
static bool memory_protect_prot(void *ptr, size_t size, int prot, UNUSED int pkey) {
|
||||||
#ifdef USE_PKEY
|
#ifdef USE_PKEY
|
||||||
int ret = pkey_mprotect(ptr, size, prot, pkey);
|
bool ret = pkey_mprotect(ptr, size, prot, pkey);
|
||||||
#else
|
#else
|
||||||
int ret = mprotect(ptr, size, prot);
|
bool ret = mprotect(ptr, size, prot);
|
||||||
#endif
|
#endif
|
||||||
if (unlikely(ret) && errno != ENOMEM) {
|
if (unlikely(ret) && errno != ENOMEM) {
|
||||||
fatal_error("non-ENOMEM mprotect failure");
|
fatal_error("non-ENOMEM mprotect failure");
|
||||||
@ -56,43 +54,39 @@ static int memory_protect_prot(void *ptr, size_t size, int prot, UNUSED int pkey
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int memory_protect_ro(void *ptr, size_t size) {
|
bool memory_protect_ro(void *ptr, size_t size) {
|
||||||
return memory_protect_prot(ptr, size, PROT_READ, -1);
|
return memory_protect_prot(ptr, size, PROT_READ, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int memory_protect_rw(void *ptr, size_t size) {
|
bool memory_protect_rw(void *ptr, size_t size) {
|
||||||
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, -1);
|
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int memory_protect_rw_metadata(void *ptr, size_t size) {
|
bool memory_protect_rw_metadata(void *ptr, size_t size) {
|
||||||
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, get_metadata_key());
|
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, get_metadata_key());
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_COMPATIBLE_MREMAP
|
#ifdef HAVE_COMPATIBLE_MREMAP
|
||||||
int memory_remap(void *old, size_t old_size, size_t new_size) {
|
bool memory_remap(void *old, size_t old_size, size_t new_size) {
|
||||||
void *ptr = mremap(old, old_size, new_size, 0);
|
void *ptr = mremap(old, old_size, new_size, 0);
|
||||||
if (unlikely(ptr == MAP_FAILED)) {
|
bool ret = ptr == MAP_FAILED;
|
||||||
if (errno != ENOMEM) {
|
if (unlikely(ret) && errno != ENOMEM) {
|
||||||
fatal_error("non-ENOMEM mremap failure");
|
fatal_error("non-ENOMEM mremap failure");
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
return 0;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size) {
|
bool memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size) {
|
||||||
void *ptr = mremap(old, old_size, new_size, MREMAP_MAYMOVE|MREMAP_FIXED, new);
|
void *ptr = mremap(old, old_size, new_size, MREMAP_MAYMOVE|MREMAP_FIXED, new);
|
||||||
if (unlikely(ptr == MAP_FAILED)) {
|
bool ret = ptr == MAP_FAILED;
|
||||||
if (errno != ENOMEM) {
|
if (unlikely(ret) && errno != ENOMEM) {
|
||||||
fatal_error("non-ENOMEM MREMAP_FIXED mremap failure");
|
fatal_error("non-ENOMEM MREMAP_FIXED mremap failure");
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
return 0;
|
return ret;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int memory_purge(void *ptr, size_t size) {
|
bool memory_purge(void *ptr, size_t size) {
|
||||||
int ret = madvise(ptr, size, MADV_DONTNEED);
|
int ret = madvise(ptr, size, MADV_DONTNEED);
|
||||||
if (unlikely(ret) && errno != ENOMEM) {
|
if (unlikely(ret) && errno != ENOMEM) {
|
||||||
fatal_error("non-ENOMEM MADV_DONTNEED madvise failure");
|
fatal_error("non-ENOMEM MADV_DONTNEED madvise failure");
|
||||||
|
17
memory.h
17
memory.h
@ -1,6 +1,7 @@
|
|||||||
#ifndef MEMORY_H
|
#ifndef MEMORY_H
|
||||||
#define MEMORY_H
|
#define MEMORY_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
@ -10,16 +11,16 @@
|
|||||||
int get_metadata_key(void);
|
int get_metadata_key(void);
|
||||||
|
|
||||||
void *memory_map(size_t size);
|
void *memory_map(size_t size);
|
||||||
int memory_map_fixed(void *ptr, size_t size);
|
bool memory_map_fixed(void *ptr, size_t size);
|
||||||
int memory_unmap(void *ptr, size_t size);
|
bool memory_unmap(void *ptr, size_t size);
|
||||||
int memory_protect_ro(void *ptr, size_t size);
|
bool memory_protect_ro(void *ptr, size_t size);
|
||||||
int memory_protect_rw(void *ptr, size_t size);
|
bool memory_protect_rw(void *ptr, size_t size);
|
||||||
int memory_protect_rw_metadata(void *ptr, size_t size);
|
bool memory_protect_rw_metadata(void *ptr, size_t size);
|
||||||
#ifdef HAVE_COMPATIBLE_MREMAP
|
#ifdef HAVE_COMPATIBLE_MREMAP
|
||||||
int memory_remap(void *old, size_t old_size, size_t new_size);
|
bool memory_remap(void *old, size_t old_size, size_t new_size);
|
||||||
int memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size);
|
bool memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size);
|
||||||
#endif
|
#endif
|
||||||
int memory_purge(void *ptr, size_t size);
|
bool memory_purge(void *ptr, size_t size);
|
||||||
void memory_set_name(void *ptr, size_t size, const char *name);
|
void memory_set_name(void *ptr, size_t size, const char *name);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user