Back to index

lightning-sunbird  0.9+nobinonly
smem.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 //
00038 //  smem.cpp - This deals with all shared memory functions needed for 
00039 //  the MAPI component of Communicator
00040 //  Written by: Rich Pizzarro (rhp@netscape.com)
00041 //  November 1997
00042 //
00043 #include <windows.h>
00044 #include <windowsx.h>
00045 
00046 #include "mapismem.h"
00047 
00048 #ifndef ZeroMemory
00049 #include <memory.h>
00050 #define ZeroMemory(PTR, SIZE) memset(PTR, 0, SIZE)
00051 #endif // ZeroMemory
00052 
00053 // 
00054 // *create new* shared memory chunk 
00055 // once this is created, use the pointer
00056 // to the segment to to store data 
00057 // e.g.: 
00058 //     lpString = "string for communicator"; 
00059 //     lstrcpy((LPSTR)pData->m_buf[0], lpString); 
00060 //  
00061 CSharedMem *
00062 NSCreateSharedMemory(DWORD memSize, LPCTSTR memName, HANDLE *hSharedMemory)
00063 {
00064 #ifdef WIN32
00065 
00066   BOOL bExistedBefore;
00067   CSharedMem *pData;
00068 
00069   LPCTSTR szObjectName = memName; 
00070   DWORD dwSize = sizeof(CSharedMem) + memSize; 
00071   *hSharedMemory = CreateFileMapping( 
00072     (HANDLE)0xFFFFFFFF,0,PAGE_READWRITE,0,dwSize,szObjectName); 
00073   if(*hSharedMemory == 0) 
00074   { 
00075     return NULL; 
00076   } 
00077   bExistedBefore = (GetLastError() == ERROR_ALREADY_EXISTS); 
00078   if(bExistedBefore) 
00079   { 
00080        return NULL;
00081   } 
00082 
00083   pData = (CSharedMem *)MapViewOfFile( 
00084             *hSharedMemory, FILE_MAP_ALL_ACCESS, 0, 0, 0); 
00085   if(pData == NULL) 
00086   { 
00087     return NULL; 
00088   } 
00089 
00090   ZeroMemory(pData, dwSize); 
00091   pData->m_dwSize = memSize; 
00092 
00093   return pData;
00094 #else
00095   CSharedMem  *sMemChunk = NULL;
00096   DWORD dwSize = memSize = (sizeof(CSharedMem) + memSize); 
00097 
00098   if (sMemChunk != NULL)
00099          return(sMemChunk);
00100 
00101   sMemChunk = (CSharedMem *) GlobalAllocPtr(GMEM_MOVEABLE, dwSize);
00102   ZeroMemory(sMemChunk, (size_t) dwSize);
00103   sMemChunk->m_dwSize = dwSize;   // Missing in Communicator code!
00104   return(sMemChunk);
00105 
00106 #endif // WIN32
00107 }
00108 
00109 // 
00110 // *open existing* shared memory chunk 
00111 // once you have the pointer to the new segment 
00112 // use this pointer to access data, e.g.: 
00113 // 
00114 CSharedMem *
00115 NSOpenExistingSharedMemory(LPCTSTR memName, HANDLE *hSharedMemory)
00116 {
00117 #ifdef WIN32
00118   CSharedMem *pData;
00119   DWORD dwSize;
00120 
00121   LPCTSTR szObjectName = memName; 
00122   *hSharedMemory = OpenFileMapping( 
00123     PAGE_READWRITE,FALSE,szObjectName); 
00124   if(*hSharedMemory == 0) 
00125   { 
00126     return NULL; 
00127   } 
00128 
00129   pData = (CSharedMem *)MapViewOfFile( 
00130               *hSharedMemory,FILE_MAP_ALL_ACCESS,0,0,0); 
00131   if(pData == NULL) 
00132   { 
00133     return NULL; 
00134   } 
00135   
00136   dwSize = pData->m_dwSize; 
00137   return pData;
00138 #else
00139 
00140   return(NULL);   // In Win16, this is really meaningless...
00141 
00142 #endif
00143 }  
00144 
00145 // 
00146 // to close shared memory segment
00147 // 
00148 void
00149 NSCloseSharedMemory(CSharedMem *pData, HANDLE hSharedMemory)
00150 {
00151 #ifdef WIN32
00152   if(pData != 0) 
00153   { 
00154     UnmapViewOfFile(pData); 
00155     pData = 0; 
00156   } 
00157 
00158   if(hSharedMemory != 0) 
00159   { 
00160     CloseHandle(hSharedMemory); 
00161     hSharedMemory = 0; 
00162   } 
00163 #else
00164 
00165        if (pData != NULL)
00166        {
00167               GlobalFreePtr(pData);
00168               pData = NULL;
00169        }
00170 
00171 #endif // WIN32
00172 }