mirror of
https://github.com/BepInEx/mono.git
synced 2026-11-16 11:21:08 -10:00
Store information about file shares in a hash table instead of a 4MB array if
SHM is disabled. Fixes #634529.
This commit is contained in:
1 parent
ec820e9208
commit
5c2e3bf772
5 files changed
+142
-51
No files matched your search
@@ -84,6 +84,9 @@ void _wapi_handle_collect (void)
|
||||
{
|
||||
guint32 count = _wapi_shared_layout->collection_count;
|
||||
int i, thr_ret;
|
||||
|
||||
if (!_wapi_shm_enabled ())
|
||||
return;
|
||||
|
||||
LOGDEBUG ("%s: (%d) Starting a collection", __func__, _wapi_getpid ());
|
||||
|
||||
|
||||
@@ -96,6 +96,7 @@ extern void _wapi_handle_update_refs (void);
|
||||
extern void _wapi_handle_foreach (WapiHandleType type,
|
||||
gboolean (*on_each)(gpointer test, gpointer user),
|
||||
gpointer user_data);
|
||||
void _wapi_free_share_info (_WapiFileShare *share_info);
|
||||
|
||||
/* This is OK to use for atomic writes of individual struct members, as they
|
||||
* are independent
|
||||
@@ -358,7 +359,7 @@ static inline int _wapi_namespace_unlock (gpointer data G_GNUC_UNUSED)
|
||||
static inline void _wapi_handle_share_release (struct _WapiFileShare *info)
|
||||
{
|
||||
int thr_ret;
|
||||
|
||||
|
||||
g_assert (info->handle_refs > 0);
|
||||
|
||||
/* Prevent new entries racing with us */
|
||||
@@ -366,7 +367,7 @@ static inline void _wapi_handle_share_release (struct _WapiFileShare *info)
|
||||
g_assert(thr_ret == 0);
|
||||
|
||||
if (InterlockedDecrement ((gint32 *)&info->handle_refs) == 0) {
|
||||
memset (info, '\0', sizeof(struct _WapiFileShare));
|
||||
_wapi_free_share_info (info);
|
||||
}
|
||||
|
||||
thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
|
||||
|
||||
+128
-46
@@ -112,8 +112,19 @@ static guint32 _wapi_private_handle_count = 0;
|
||||
static guint32 _wapi_private_handle_slot_count = 0;
|
||||
|
||||
struct _WapiHandleSharedLayout *_wapi_shared_layout = NULL;
|
||||
|
||||
/*
|
||||
* If SHM is enabled, this will point to shared memory, otherwise it will be NULL.
|
||||
*/
|
||||
struct _WapiFileShareLayout *_wapi_fileshare_layout = NULL;
|
||||
|
||||
/*
|
||||
* If SHM is disabled, this will point to a hash of _WapiFileShare structures, otherwise
|
||||
* it will be NULL. We use this instead of _wapi_fileshare_layout to avoid allocating a
|
||||
* 4MB array.
|
||||
*/
|
||||
static GHashTable *file_share_hash;
|
||||
|
||||
guint32 _wapi_fd_reserve;
|
||||
|
||||
/*
|
||||
@@ -209,6 +220,9 @@ static void handle_cleanup (void)
|
||||
_wapi_shm_detach (WAPI_SHM_DATA);
|
||||
_wapi_shm_detach (WAPI_SHM_FILESHARE);
|
||||
|
||||
if (file_share_hash)
|
||||
g_hash_table_destroy (file_share_hash);
|
||||
|
||||
for (i = 0; i < _WAPI_PRIVATE_MAX_SLOTS; ++i)
|
||||
g_free (_wapi_private_handles [i]);
|
||||
}
|
||||
@@ -254,8 +268,11 @@ static void shared_init (void)
|
||||
_wapi_shared_layout = _wapi_shm_attach (WAPI_SHM_DATA);
|
||||
g_assert (_wapi_shared_layout != NULL);
|
||||
|
||||
_wapi_fileshare_layout = _wapi_shm_attach (WAPI_SHM_FILESHARE);
|
||||
g_assert (_wapi_fileshare_layout != NULL);
|
||||
if (_wapi_shm_enabled ()) {
|
||||
/* This allocates a 4mb array, so do it only if SHM is enabled */
|
||||
_wapi_fileshare_layout = _wapi_shm_attach (WAPI_SHM_FILESHARE);
|
||||
g_assert (_wapi_fileshare_layout != NULL);
|
||||
}
|
||||
|
||||
#if !defined (DISABLE_SHARED_HANDLES)
|
||||
if (_wapi_shm_enabled ())
|
||||
@@ -1622,6 +1639,35 @@ int _wapi_handle_timedwait_signal_handle (gpointer handle,
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
_wapi_free_share_info (_WapiFileShare *share_info)
|
||||
{
|
||||
if (!_wapi_shm_enabled ()) {
|
||||
_wapi_handle_lock_shared_handles ();
|
||||
g_hash_table_remove (file_share_hash, share_info);
|
||||
_wapi_handle_unlock_shared_handles ();
|
||||
} else {
|
||||
memset (share_info, '\0', sizeof(struct _WapiFileShare));
|
||||
}
|
||||
}
|
||||
|
||||
static gint
|
||||
wapi_share_info_equal (gconstpointer ka, gconstpointer kb)
|
||||
{
|
||||
const _WapiFileShare *s1 = ka;
|
||||
const _WapiFileShare *s2 = kb;
|
||||
|
||||
return (s1->device == s2->device && s1->inode == s2->inode) ? 1 : 0;
|
||||
}
|
||||
|
||||
static guint
|
||||
wapi_share_info_hash (gconstpointer data)
|
||||
{
|
||||
const _WapiFileShare *s = data;
|
||||
|
||||
return s->inode;
|
||||
}
|
||||
|
||||
gboolean _wapi_handle_get_or_set_share (dev_t device, ino_t inode,
|
||||
guint32 new_sharemode,
|
||||
guint32 new_access,
|
||||
@@ -1642,55 +1688,31 @@ gboolean _wapi_handle_get_or_set_share (dev_t device, ino_t inode,
|
||||
/* Prevent new entries racing with us */
|
||||
thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
|
||||
g_assert (thr_ret == 0);
|
||||
|
||||
/* If a linear scan gets too slow we'll have to fit a hash
|
||||
* table onto the shared mem backing store
|
||||
*/
|
||||
*share_info = NULL;
|
||||
for (i = 0; i <= _wapi_fileshare_layout->hwm; i++) {
|
||||
file_share = &_wapi_fileshare_layout->share_info[i];
|
||||
|
||||
/* Make a note of an unused slot, in case we need to
|
||||
* store share info
|
||||
if (!_wapi_shm_enabled ()) {
|
||||
_WapiFileShare tmp;
|
||||
|
||||
/*
|
||||
* Instead of allocating a 4MB array, we use a hash table to keep track of this
|
||||
* info. This is needed even if SHM is disabled, to track sharing inside
|
||||
* the current process.
|
||||
*/
|
||||
if (first_unused == -1 && file_share->handle_refs == 0) {
|
||||
first_unused = i;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (file_share->handle_refs == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (file_share->device == device &&
|
||||
file_share->inode == inode) {
|
||||
if (!file_share_hash)
|
||||
file_share_hash = g_hash_table_new_full (wapi_share_info_hash, wapi_share_info_equal, NULL, g_free);
|
||||
tmp.device = device;
|
||||
tmp.inode = inode;
|
||||
|
||||
file_share = g_hash_table_lookup (file_share_hash, &tmp);
|
||||
if (file_share) {
|
||||
*old_sharemode = file_share->sharemode;
|
||||
*old_access = file_share->access;
|
||||
*share_info = file_share;
|
||||
|
||||
/* Increment the reference count while we
|
||||
* still have sole access to the shared area.
|
||||
* This makes the increment atomic wrt
|
||||
* collections
|
||||
*/
|
||||
InterlockedIncrement ((gint32 *)&file_share->handle_refs);
|
||||
|
||||
exists = TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!exists) {
|
||||
if (i == _WAPI_FILESHARE_SIZE && first_unused == -1) {
|
||||
/* No more space */
|
||||
} else {
|
||||
if (first_unused == -1) {
|
||||
file_share = &_wapi_fileshare_layout->share_info[++i];
|
||||
_wapi_fileshare_layout->hwm = i;
|
||||
} else {
|
||||
file_share = &_wapi_fileshare_layout->share_info[first_unused];
|
||||
}
|
||||
|
||||
file_share = g_new0 (_WapiFileShare, 1);
|
||||
|
||||
file_share->device = device;
|
||||
file_share->inode = inode;
|
||||
file_share->opened_by_pid = _wapi_getpid ();
|
||||
@@ -1698,11 +1720,71 @@ gboolean _wapi_handle_get_or_set_share (dev_t device, ino_t inode,
|
||||
file_share->access = new_access;
|
||||
file_share->handle_refs = 1;
|
||||
*share_info = file_share;
|
||||
}
|
||||
}
|
||||
|
||||
if (*share_info != NULL) {
|
||||
InterlockedExchange ((gint32 *)&(*share_info)->timestamp, now);
|
||||
g_hash_table_insert (file_share_hash, file_share, file_share);
|
||||
}
|
||||
} else {
|
||||
/* If a linear scan gets too slow we'll have to fit a hash
|
||||
* table onto the shared mem backing store
|
||||
*/
|
||||
*share_info = NULL;
|
||||
for (i = 0; i <= _wapi_fileshare_layout->hwm; i++) {
|
||||
file_share = &_wapi_fileshare_layout->share_info[i];
|
||||
|
||||
/* Make a note of an unused slot, in case we need to
|
||||
* store share info
|
||||
*/
|
||||
if (first_unused == -1 && file_share->handle_refs == 0) {
|
||||
first_unused = i;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (file_share->handle_refs == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (file_share->device == device &&
|
||||
file_share->inode == inode) {
|
||||
*old_sharemode = file_share->sharemode;
|
||||
*old_access = file_share->access;
|
||||
*share_info = file_share;
|
||||
|
||||
/* Increment the reference count while we
|
||||
* still have sole access to the shared area.
|
||||
* This makes the increment atomic wrt
|
||||
* collections
|
||||
*/
|
||||
InterlockedIncrement ((gint32 *)&file_share->handle_refs);
|
||||
|
||||
exists = TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!exists) {
|
||||
if (i == _WAPI_FILESHARE_SIZE && first_unused == -1) {
|
||||
/* No more space */
|
||||
} else {
|
||||
if (first_unused == -1) {
|
||||
file_share = &_wapi_fileshare_layout->share_info[++i];
|
||||
_wapi_fileshare_layout->hwm = i;
|
||||
} else {
|
||||
file_share = &_wapi_fileshare_layout->share_info[first_unused];
|
||||
}
|
||||
|
||||
file_share->device = device;
|
||||
file_share->inode = inode;
|
||||
file_share->opened_by_pid = _wapi_getpid ();
|
||||
file_share->sharemode = new_sharemode;
|
||||
file_share->access = new_access;
|
||||
file_share->handle_refs = 1;
|
||||
*share_info = file_share;
|
||||
}
|
||||
}
|
||||
|
||||
if (*share_info != NULL) {
|
||||
InterlockedExchange ((gint32 *)&(*share_info)->timestamp, now);
|
||||
}
|
||||
}
|
||||
|
||||
thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
|
||||
|
||||
+6
-3
@@ -326,7 +326,8 @@ static void file_close (gpointer handle, gpointer data)
|
||||
|
||||
g_free (file_handle->filename);
|
||||
|
||||
_wapi_handle_share_release (file_handle->share_info);
|
||||
if (file_handle->share_info)
|
||||
_wapi_handle_share_release (file_handle->share_info);
|
||||
|
||||
close (GPOINTER_TO_UINT(handle));
|
||||
}
|
||||
@@ -1732,7 +1733,8 @@ gboolean DeleteFile(const gunichar2 *name)
|
||||
g_free (filename);
|
||||
return FALSE;
|
||||
}
|
||||
_wapi_handle_share_release (shareinfo);
|
||||
if (shareinfo)
|
||||
_wapi_handle_share_release (shareinfo);
|
||||
#endif
|
||||
|
||||
retval = _wapi_unlink (filename);
|
||||
@@ -1843,7 +1845,8 @@ gboolean MoveFile (const gunichar2 *name, const gunichar2 *dest_name)
|
||||
SetLastError (ERROR_SHARING_VIOLATION);
|
||||
return FALSE;
|
||||
}
|
||||
_wapi_handle_share_release (shareinfo);
|
||||
if (shareinfo)
|
||||
_wapi_handle_share_release (shareinfo);
|
||||
|
||||
result = _wapi_rename (utf8_name, utf8_dest_name);
|
||||
errno_copy = errno;
|
||||
|
||||
@@ -188,6 +188,8 @@ struct _WapiFileShare
|
||||
guint32 timestamp;
|
||||
};
|
||||
|
||||
typedef struct _WapiFileShare _WapiFileShare;
|
||||
|
||||
struct _WapiFileShareLayout
|
||||
{
|
||||
guint32 hwm;
|
||||
|
||||
Reference in new issue
Block a user