ipc: Unmap shared memory before destroy

This commit is contained in:
huanchen 2023-07-17 15:49:00 +08:00 committed by Jakob Bornecrantz
parent b6cec9b94a
commit db230b3dcb
4 changed files with 59 additions and 6 deletions

View file

@ -25,6 +25,7 @@
#include "util/u_system_helpers.h" #include "util/u_system_helpers.h"
#include "shared/ipc_protocol.h" #include "shared/ipc_protocol.h"
#include "shared/ipc_shmem.h"
#include "client/ipc_client.h" #include "client/ipc_client.h"
#include "client/ipc_client_connection.h" #include "client/ipc_client_connection.h"
@ -202,6 +203,8 @@ ipc_client_instance_destroy(struct xrt_instance *xinst)
} }
ii->xtrack_count = 0; ii->xtrack_count = 0;
ipc_shmem_destroy(&ii->ipc_c.ism_handle, (void **)&ii->ipc_c.ism, sizeof(struct ipc_shared_memory));
free(ii); free(ii);
} }

View file

@ -141,6 +141,8 @@ teardown_all(struct ipc_server *s)
u_process_destroy(s->process); u_process_destroy(s->process);
os_mutex_destroy(&s->global_state.lock); os_mutex_destroy(&s->global_state.lock);
ipc_shmem_destroy(&s->ism_handle, (void **)&s->ism, sizeof(struct ipc_shared_memory));
} }
static int static int

View file

@ -105,9 +105,13 @@ ipc_shmem_create(size_t size, xrt_shmem_handle_t *out_handle, void **out_map)
#endif #endif
#if defined(XRT_OS_UNIX) #if defined(XRT_OS_UNIX)
void void
ipc_shmem_destroy(xrt_shmem_handle_t *handle_ptr) ipc_shmem_destroy(xrt_shmem_handle_t *handle_ptr, void **map_ptr, size_t size)
{ {
// Checks for NULL.
ipc_shmem_unmap((void **)map_ptr, size);
if (handle_ptr == NULL) { if (handle_ptr == NULL) {
return; return;
} }
@ -132,10 +136,25 @@ ipc_shmem_map(xrt_shmem_handle_t handle, size_t size, void **out_map)
*out_map = ptr; *out_map = ptr;
return XRT_SUCCESS; return XRT_SUCCESS;
} }
#elif defined(XRT_OS_WINDOWS)
void void
ipc_shmem_destroy(xrt_shmem_handle_t *handle_ptr) ipc_shmem_unmap(void **map_ptr, size_t size)
{ {
if (map_ptr == NULL) {
return;
}
munmap(*map_ptr, size);
*map_ptr = NULL;
}
#elif defined(XRT_OS_WINDOWS)
void
ipc_shmem_destroy(xrt_shmem_handle_t *handle_ptr, void **map_ptr, size_t size)
{
// Checks for NULL.
ipc_shmem_unmap((void **)map_ptr, size);
if (handle_ptr == NULL) { if (handle_ptr == NULL) {
return; return;
} }
@ -155,7 +174,20 @@ ipc_shmem_map(xrt_shmem_handle_t handle, size_t size, void **out_map)
return XRT_SUCCESS; return XRT_SUCCESS;
} }
// BUGBUG: unmap? void
ipc_shmem_unmap(void **map_ptr, size_t size)
{
if (map_ptr == NULL) {
return;
}
void *map = *map_ptr;
if (map == NULL) {
return;
}
UnmapViewOfFile(map);
*map_ptr = NULL;
}
#else #else
#error "OS not yet supported" #error "OS not yet supported"
#endif #endif

View file

@ -16,6 +16,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -51,6 +52,17 @@ ipc_shmem_create(size_t size, xrt_shmem_handle_t *out_handle, void **out_map);
xrt_result_t xrt_result_t
ipc_shmem_map(xrt_shmem_handle_t handle, size_t size, void **out_map); ipc_shmem_map(xrt_shmem_handle_t handle, size_t size, void **out_map);
/*!
* Unmap a shared memory region.
*
* @param[in] map_ptr pointer to region
* @param[in] size Size of region
*
* @public @memberof xrt_shmem_handle_t
*/
void
ipc_shmem_unmap(void **map_ptr, size_t size);
/*! /*!
* Destroy a handle to a shared memory region. * Destroy a handle to a shared memory region.
* *
@ -58,12 +70,16 @@ ipc_shmem_map(xrt_shmem_handle_t handle, size_t size, void **out_map);
* it (in this process or others) are still open. * it (in this process or others) are still open.
* *
* @param[in,out] handle_ptr Pointer to the handle to destroy - will be checked * @param[in,out] handle_ptr Pointer to the handle to destroy - will be checked
* for validity, destroyed, and cleared. * for validity, destroyed, and cleared.
* @param[in,out] map_ptr Pointer to the mapped memory to unmap - will be
* checked for validity, destroyed, and cleared. It's
* necessary unmap the region to destroy the shmem.
* @param[in] size Size of the mapped region.
* *
* @public @memberof xrt_shmem_handle_t * @public @memberof xrt_shmem_handle_t
*/ */
void void
ipc_shmem_destroy(xrt_shmem_handle_t *handle_ptr); ipc_shmem_destroy(xrt_shmem_handle_t *handle_ptr, void **map_ptr, size_t size);
#ifdef __cplusplus #ifdef __cplusplus
} }