Skip to content

Commit d7bdc04

Browse files
committed
Join shm_pair.info and .segment of Windows shm implementation
There is not much point in having two distinct file mappings; since the info mapping is very small and of fixed size, we can put it at the beginning of a single mapping. Besides the obvious resource savings, that also simplifies the error handling. Closes GH-8648.
1 parent e9e3a14 commit d7bdc04

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

TSRM/tsrm_win32.c

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,8 @@ static void tsrm_win32_dtor(tsrm_win32_globals *globals)
6969

7070
if (globals->shm) {
7171
for (ptr = globals->shm; ptr < (globals->shm + globals->shm_size); ptr++) {
72-
UnmapViewOfFile(ptr->addr);
73-
CloseHandle(ptr->segment);
7472
UnmapViewOfFile(ptr->descriptor);
75-
CloseHandle(ptr->info);
73+
CloseHandle(ptr->segment);
7674
}
7775
free(globals->shm);
7876
}
@@ -610,26 +608,27 @@ TSRM_API int pclose(FILE *stream)
610608
}/*}}}*/
611609

612610
#define SEGMENT_PREFIX "TSRM_SHM_SEGMENT:"
613-
#define DESCRIPTOR_PREFIX "TSRM_SHM_DESCRIPTOR:"
614611
#define INT_MIN_AS_STRING "-2147483648"
615612

616613
TSRM_API int shmget(key_t key, size_t size, int flags)
617614
{/*{{{*/
618615
shm_pair *shm;
619-
char shm_segment[sizeof(SEGMENT_PREFIX INT_MIN_AS_STRING)], shm_info[sizeof(DESCRIPTOR_PREFIX INT_MIN_AS_STRING)];
616+
char shm_segment[sizeof(SEGMENT_PREFIX INT_MIN_AS_STRING)];
620617
HANDLE shm_handle = NULL, info_handle = NULL;
621618
BOOL created = FALSE;
622619

623620
if (key != IPC_PRIVATE) {
624621
snprintf(shm_segment, sizeof(shm_segment), SEGMENT_PREFIX "%d", key);
625-
snprintf(shm_info, sizeof(shm_info), DESCRIPTOR_PREFIX "%d", key);
626622

627623
shm_handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shm_segment);
628-
info_handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shm_info);
629624
}
630625

631-
if (!shm_handle && !info_handle) {
626+
if (!shm_handle) {
632627
if (flags & IPC_CREAT) {
628+
if (size > SIZE_MAX - sizeof(shm->descriptor)) {
629+
return -1;
630+
}
631+
size += sizeof(shm->descriptor);
633632
#if SIZEOF_SIZE_T == 8
634633
DWORD high = size >> 32;
635634
DWORD low = (DWORD)size;
@@ -638,39 +637,25 @@ TSRM_API int shmget(key_t key, size_t size, int flags)
638637
DWORD low = size;
639638
#endif
640639
shm_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, high, low, key == IPC_PRIVATE ? NULL : shm_segment);
641-
info_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(shm->descriptor), key == IPC_PRIVATE ? NULL : shm_info);
642640
created = TRUE;
643641
}
644-
if (!shm_handle || !info_handle) {
645-
if (shm_handle) {
646-
CloseHandle(shm_handle);
647-
}
648-
if (info_handle) {
649-
CloseHandle(info_handle);
650-
}
642+
if (!shm_handle) {
651643
return -1;
652644
}
653645
} else {
654646
if (flags & IPC_EXCL) {
655-
if (shm_handle) {
656-
CloseHandle(shm_handle);
657-
}
658-
if (info_handle) {
659-
CloseHandle(info_handle);
660-
}
647+
CloseHandle(shm_handle);
661648
return -1;
662649
}
663650
}
664651

665652
shm = shm_get(key, NULL);
666653
if (!shm) {
667654
CloseHandle(shm_handle);
668-
CloseHandle(info_handle);
669655
return -1;
670656
}
671657
shm->segment = shm_handle;
672-
shm->info = info_handle;
673-
shm->descriptor = MapViewOfFileEx(shm->info, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
658+
shm->descriptor = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
674659

675660
if (NULL != shm->descriptor && created) {
676661
shm->descriptor->shm_perm.key = key;
@@ -691,7 +676,6 @@ TSRM_API int shmget(key_t key, size_t size, int flags)
691676
CloseHandle(shm->segment);
692677
}
693678
UnmapViewOfFile(shm->descriptor);
694-
CloseHandle(shm->info);
695679
return -1;
696680
}
697681

@@ -706,7 +690,7 @@ TSRM_API void *shmat(int key, const void *shmaddr, int flags)
706690
return (void*)-1;
707691
}
708692

709-
shm->addr = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
693+
shm->addr = shm->descriptor + sizeof(shm->descriptor);
710694

711695
if (NULL == shm->addr) {
712696
int err = GetLastError();
@@ -734,7 +718,7 @@ TSRM_API int shmdt(const void *shmaddr)
734718
shm->descriptor->shm_lpid = getpid();
735719
shm->descriptor->shm_nattch--;
736720

737-
ret = UnmapViewOfFile(shm->addr) ? 0 : -1;
721+
ret = 1;
738722
if (!ret && shm->descriptor->shm_nattch <= 0) {
739723
ret = UnmapViewOfFile(shm->descriptor) ? 0 : -1;
740724
shm->descriptor = NULL;

TSRM/tsrm_win32.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ typedef struct {
5050

5151
typedef struct {
5252
void *addr;
53-
HANDLE info;
5453
HANDLE segment;
5554
struct shmid_ds *descriptor;
5655
} shm_pair;

0 commit comments

Comments
 (0)