Skip to content

Commit 9dfe4d8

Browse files
authored
add embedding api lost in mono master merge (#769)
* add embedding api lost in mono master * Add MMAP overrides
1 parent 92f3755 commit 9dfe4d8

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

mono/utils/mono-filemap.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,30 @@
2424

2525
#include "mono-mmap.h"
2626

27+
static MonoFileMapOpen file_open_func = 0;
28+
static MonoFileMapSize file_size_func = 0;
29+
static MonoFileMapFd file_fd_func = 0;
30+
static MonoFileMapClose file_close_func = 0;
31+
MonoFileMapMap file_map_func = 0;
32+
MonoFileMapUnmap file_unmap_func = 0;
33+
34+
#if defined(ANDROID)
35+
void mono_file_map_override(MonoFileMapOpen open_func, MonoFileMapSize size_func, MonoFileMapFd fd_func, MonoFileMapClose close_func, MonoFileMapMap map_func, MonoFileMapUnmap unmap_func)
36+
{
37+
file_open_func = open_func;
38+
file_size_func = size_func;
39+
file_fd_func = fd_func;
40+
file_close_func = close_func;
41+
file_map_func = map_func;
42+
file_unmap_func = unmap_func;
43+
}
44+
#endif
45+
46+
2747
MonoFileMap *
2848
mono_file_map_open (const char* name)
2949
{
50+
if (file_open_func) return file_open_func(name);
3051
#ifdef WIN32
3152
gunichar2 *wname = g_utf8_to_utf16 (name, -1, 0, 0, 0);
3253
MonoFileMap *result;
@@ -47,6 +68,7 @@ mono_file_map_open (const char* name)
4768
guint64
4869
mono_file_map_size (MonoFileMap *fmap)
4970
{
71+
if (file_size_func) return file_size_func(fmap);
5072
struct stat stat_buf;
5173
if (fstat (mono_file_map_fd (fmap), &stat_buf) < 0)
5274
return 0;
@@ -56,6 +78,7 @@ mono_file_map_size (MonoFileMap *fmap)
5678
int
5779
mono_file_map_fd (MonoFileMap *fmap)
5880
{
81+
if (file_fd_func) return file_fd_func(fmap);
5982
#ifdef WIN32
6083
return fileno ((FILE*)fmap);
6184
#else
@@ -66,6 +89,7 @@ mono_file_map_fd (MonoFileMap *fmap)
6689
int
6790
mono_file_map_close (MonoFileMap *fmap)
6891
{
92+
if (file_close_func) return file_close_func(fmap);
6993
#ifdef WIN32
7094
return fclose ((FILE*)fmap);
7195
#else

mono/utils/mono-mmap.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,20 @@ mono_vfree (void *addr, size_t length, MonoMemAccountType type)
281281
* \p ret_handle must point to a void*: this value must be used when unmapping
282282
* the memory area using \c mono_file_unmap().
283283
*/
284+
extern MonoFileMapMap file_map_func;
285+
extern MonoFileMapUnmap file_unmap_func;
286+
284287
void*
285288
mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
286289
{
287290
void *ptr;
291+
if (file_map_func)
292+
{
293+
BEGIN_CRITICAL_SECTION;
294+
ptr = file_map_func(length, flags, fd, offset, ret_handle);
295+
END_CRITICAL_SECTION;
296+
return ptr;
297+
}
288298
int mflags = 0;
289299
int prot = prot_from_flags (flags);
290300
/* translate the flags */
@@ -320,7 +330,10 @@ mono_file_unmap (void *addr, void *handle)
320330
int res;
321331

322332
BEGIN_CRITICAL_SECTION;
323-
res = munmap (addr, (size_t)handle);
333+
if (file_unmap_func)
334+
res = file_unmap_func(addr, handle);
335+
else
336+
res = munmap(addr, (size_t)handle);
324337
END_CRITICAL_SECTION;
325338

326339
return res;

mono/utils/mono-mmap.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,16 @@ typedef void (*mono_file_map_release_fn) (void *addr);
8484

8585
MONO_API void mono_file_map_set_allocator (mono_file_map_alloc_fn alloc, mono_file_map_release_fn release);
8686

87+
typedef MonoFileMap* (*MonoFileMapOpen) (const char* name);
88+
typedef guint64 (*MonoFileMapSize) (MonoFileMap *fmap);
89+
typedef int (*MonoFileMapFd) (MonoFileMap *fmap);
90+
typedef int (*MonoFileMapClose) (MonoFileMap *fmap);
91+
typedef void * (*MonoFileMapMap) (size_t length, int flags, int fd, guint64 offset, void **ret_handle);
92+
typedef int (*MonoFileMapUnmap) (void *addr, void *handle);
93+
94+
#if defined(ANDROID)
95+
MONO_API void mono_file_map_override(MonoFileMapOpen open_func, MonoFileMapSize size_func, MonoFileMapFd fd_func, MonoFileMapClose close_func, MonoFileMapMap map_func, MonoFileMapUnmap unmap_func);
96+
#endif
97+
8798
#endif /* __MONO_UTILS_MMAP_H__ */
8899

0 commit comments

Comments
 (0)