~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/backend/port/win32/shmem.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * shmem.c
 
4
 *        Microsoft Windows Win32 Shared Memory Emulation
 
5
 *
 
6
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 
7
 *
 
8
 * IDENTIFICATION
 
9
 *        $PostgreSQL: pgsql/src/backend/port/win32/shmem.c,v 1.10 2004-12-31 22:00:37 pgsql Exp $
 
10
 *
 
11
 *-------------------------------------------------------------------------
 
12
 */
 
13
 
 
14
#include "postgres.h"
 
15
 
 
16
#include <stdio.h>
 
17
#include <errno.h>
 
18
 
 
19
static DWORD s_segsize = 0;
 
20
 
 
21
/* Detach from a shared mem area based on its address */
 
22
int
 
23
shmdt(const void *shmaddr)
 
24
{
 
25
        if (UnmapViewOfFile((LPCVOID *) shmaddr))
 
26
                return 0;
 
27
        else
 
28
                return -1;
 
29
}
 
30
 
 
31
/* Attach to an existing area */
 
32
void *
 
33
shmat(int memId, void *shmaddr, int flag)
 
34
{
 
35
        /* TODO -- shmat needs to count # attached to shared mem */
 
36
        void       *lpmem = MapViewOfFileEx((HANDLE) memId,
 
37
                                                                                FILE_MAP_WRITE | FILE_MAP_READ,
 
38
                0, 0, /* (DWORD)pshmdsc->segsize */ 0 /* s_segsize */ , shmaddr);
 
39
 
 
40
        if (lpmem == NULL)
 
41
        {
 
42
                lpmem = (void *) -1;
 
43
                _dosmaperr(GetLastError());
 
44
        }
 
45
 
 
46
        return lpmem;
 
47
}
 
48
 
 
49
/* Control a shared mem area */
 
50
int
 
51
shmctl(int shmid, int flag, struct shmid_ds * dummy)
 
52
{
 
53
        if (flag == IPC_RMID)
 
54
        {
 
55
                /* Delete the area */
 
56
                CloseHandle((HANDLE) shmid);
 
57
                return 0;
 
58
        }
 
59
        if (flag == IPC_STAT)
 
60
        {
 
61
                /* Can only test for if exists */
 
62
                int                     hmap = shmget(shmid, 0, 0);
 
63
 
 
64
                if (hmap < 0)
 
65
                {
 
66
                        /* Shared memory does not exist */
 
67
                        errno = EINVAL;
 
68
                        return -1;
 
69
                }
 
70
                else
 
71
                {
 
72
                        /* Shared memory does exist and must be in use */
 
73
                        shmctl(hmap, IPC_RMID, NULL);           /* Release our hold on it */
 
74
                        errno = 0;
 
75
                        return 0;
 
76
                }
 
77
        }
 
78
 
 
79
        errno = EINVAL;
 
80
        return -1;
 
81
}
 
82
 
 
83
/* Get an area based on the IPC key */
 
84
int
 
85
shmget(int memKey, int size, int flag)
 
86
{
 
87
        HANDLE          hmap;
 
88
        char            szShareMem[32];
 
89
        DWORD           dwRet;
 
90
 
 
91
        s_segsize = size;
 
92
        sprintf(szShareMem, "PostgreSQL.%d", memKey);
 
93
 
 
94
        if (flag & IPC_CREAT)
 
95
        {
 
96
                hmap = CreateFileMapping((HANDLE) 0xFFFFFFFF,   /* Use the swap file    */
 
97
                                                                 NULL,
 
98
                                                                 PAGE_READWRITE,                /* Memory is Read/Write */
 
99
                                                                 0L,    /* Size Upper 32 Bits   */
 
100
                                                                 (DWORD) s_segsize,             /* Size Lower 32 bits */
 
101
                                                                 szShareMem);
 
102
        }
 
103
        else
 
104
        {
 
105
                hmap = OpenFileMapping(FILE_MAP_ALL_ACCESS,
 
106
                                                           FALSE,
 
107
                                                           szShareMem);
 
108
                if (!hmap)
 
109
                {
 
110
                        errno = ENOENT;
 
111
                        return -1;
 
112
                }
 
113
        }
 
114
 
 
115
        dwRet = GetLastError();
 
116
        if (dwRet == ERROR_ALREADY_EXISTS && hmap && (flag & (IPC_CREAT | IPC_EXCL)))
 
117
        {
 
118
                /* Caller wanted to create the segment -- error if already exists */
 
119
                CloseHandle(hmap);
 
120
                errno = EEXIST;
 
121
                return -1;
 
122
        }
 
123
        else if (!hmap)
 
124
        {
 
125
                /* Unable to get shared memory */
 
126
                _dosmaperr(GetLastError());
 
127
                return -1;
 
128
        }
 
129
 
 
130
        return (int) hmap;
 
131
}