From f51a238c261962d3d289c80574c19af3f26dc24f Mon Sep 17 00:00:00 2001 From: CoderAImen Date: Thu, 26 Dec 2024 14:11:07 +0300 Subject: [PATCH 1/3] Fixed work with Shared Memory on Windows. --- TSRM/tsrm_win32.c | 30 ++++++++++++++++++++++++++---- TSRM/tsrm_win32.h | 2 ++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/TSRM/tsrm_win32.c b/TSRM/tsrm_win32.c index 4b8445542fce8..610066c8e7c33 100644 --- a/TSRM/tsrm_win32.c +++ b/TSRM/tsrm_win32.c @@ -688,7 +688,20 @@ TSRM_API int shmget(key_t key, size_t size, int flags) CloseHandle(shm_handle); return -1; } - shm->segment = shm_handle; + if (NULL == shm->segment || shm->segment == INVALID_HANDLE_VALUE) { + shm->segment = shm_handle; + shm->created = created; + } + else if (created == TRUE) { + CloseHandle(shm->segment); + shm->segment = shm_handle; + shm->created = TRUE; + } + else { + CloseHandle(shm_handle); + } + if(shm->descriptor==NULL)shm->descriptor = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL); + shm->descriptor = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL); if (NULL != shm->descriptor && created) { @@ -730,6 +743,7 @@ TSRM_API void *shmat(int key, const void *shmaddr, int flags) shm->descriptor->shm_atime = time(NULL); shm->descriptor->shm_lpid = getpid(); shm->descriptor->shm_nattch++; + shm->shm_nattch++; return shm->addr; }/*}}}*/ @@ -746,12 +760,20 @@ TSRM_API int shmdt(const void *shmaddr) shm->descriptor->shm_dtime = time(NULL); shm->descriptor->shm_lpid = getpid(); shm->descriptor->shm_nattch--; + shm->shm_nattch--; ret = 0; - if (shm->descriptor->shm_nattch <= 0) { - ret = UnmapViewOfFile(shm->descriptor) ? 0 : -1; - shm->descriptor = NULL; + if (shm->shm_nattch <= 0) { + shm->shm_nattch = 0; + if (NULL != shm->segment && shm->segment != INVALID_HANDLE_VALUE && shm->created==FALSE) { + ret = UnmapViewOfFile(shm->descriptor) ? 0 : -1; + shm->descriptor = NULL; + CloseHandle(shm->segment); + shm->segment = INVALID_HANDLE_VALUE; + shm->addr = NULL; + } } + return ret; }/*}}}*/ diff --git a/TSRM/tsrm_win32.h b/TSRM/tsrm_win32.h index c5bdc492be1b8..a7f6ada89620f 100644 --- a/TSRM/tsrm_win32.h +++ b/TSRM/tsrm_win32.h @@ -51,6 +51,8 @@ typedef struct { typedef struct { void *addr; HANDLE segment; + BOOL created; + int shm_nattch; struct shmid_ds *descriptor; } shm_pair; From eddb03e9ed86821edfcd3a9c8026e7e25ac53931 Mon Sep 17 00:00:00 2001 From: CoderAImen Date: Fri, 27 Dec 2024 11:37:27 +0300 Subject: [PATCH 2/3] Update tsrm_win32.c Corrections based on comments. --- TSRM/tsrm_win32.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/TSRM/tsrm_win32.c b/TSRM/tsrm_win32.c index 610066c8e7c33..72ea8c37051ef 100644 --- a/TSRM/tsrm_win32.c +++ b/TSRM/tsrm_win32.c @@ -688,11 +688,11 @@ TSRM_API int shmget(key_t key, size_t size, int flags) CloseHandle(shm_handle); return -1; } - if (NULL == shm->segment || shm->segment == INVALID_HANDLE_VALUE) { + if (NULL == shm->segment || INVALID_HANDLE_VALUE == shm->segment) { shm->segment = shm_handle; shm->created = created; } - else if (created == TRUE) { + else if (TRUE == created) { CloseHandle(shm->segment); shm->segment = shm_handle; shm->created = TRUE; @@ -700,9 +700,7 @@ TSRM_API int shmget(key_t key, size_t size, int flags) else { CloseHandle(shm_handle); } - if(shm->descriptor==NULL)shm->descriptor = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL); - - shm->descriptor = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL); + if(NULL == shm->descriptor)shm->descriptor = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL); if (NULL != shm->descriptor && created) { shm->descriptor->shm_perm.key = key; @@ -765,8 +763,8 @@ TSRM_API int shmdt(const void *shmaddr) ret = 0; if (shm->shm_nattch <= 0) { shm->shm_nattch = 0; - if (NULL != shm->segment && shm->segment != INVALID_HANDLE_VALUE && shm->created==FALSE) { - ret = UnmapViewOfFile(shm->descriptor) ? 0 : -1; + if (NULL != shm->segment && INVALID_HANDLE_VALUE != shm->segment && FALSE == shm->created) { + if(NULL != shm->descriptor)ret = UnmapViewOfFile(shm->descriptor) ? 0 : -1; shm->descriptor = NULL; CloseHandle(shm->segment); shm->segment = INVALID_HANDLE_VALUE; From 0f57e0c7de60cd6af6b376dab7caa6c0da0dcf04 Mon Sep 17 00:00:00 2001 From: CoderAImen Date: Fri, 27 Dec 2024 16:20:36 +0300 Subject: [PATCH 3/3] Update tsrm_win32.c Syntax fixes --- TSRM/tsrm_win32.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/TSRM/tsrm_win32.c b/TSRM/tsrm_win32.c index 72ea8c37051ef..a5e8851a25287 100644 --- a/TSRM/tsrm_win32.c +++ b/TSRM/tsrm_win32.c @@ -700,7 +700,9 @@ TSRM_API int shmget(key_t key, size_t size, int flags) else { CloseHandle(shm_handle); } - if(NULL == shm->descriptor)shm->descriptor = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL); + if(NULL == shm->descriptor){ + shm->descriptor = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL); + } if (NULL != shm->descriptor && created) { shm->descriptor->shm_perm.key = key; @@ -764,7 +766,9 @@ TSRM_API int shmdt(const void *shmaddr) if (shm->shm_nattch <= 0) { shm->shm_nattch = 0; if (NULL != shm->segment && INVALID_HANDLE_VALUE != shm->segment && FALSE == shm->created) { - if(NULL != shm->descriptor)ret = UnmapViewOfFile(shm->descriptor) ? 0 : -1; + if(NULL != shm->descriptor){ + ret = UnmapViewOfFile(shm->descriptor) ? 0 : -1; + } shm->descriptor = NULL; CloseHandle(shm->segment); shm->segment = INVALID_HANDLE_VALUE;