home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Internet 2000 May / MICD_2000_05.iso / CBuilder5 / INSTALL / DATA1.CAB / Program_Built_Files / Include / Atl / atlimpl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-02-01  |  4.8 KB  |  196 lines

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10.  
  11. #ifndef __ATLBASE_H__
  12.     #error atlimpl.cpp requires atlbase.h to be included first
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // Minimize CRT
  17. // Specify DllMain as EntryPoint
  18. // Turn off exception handling
  19. // Define _ATL_MIN_CRT
  20. #ifdef _ATL_MIN_CRT
  21. /////////////////////////////////////////////////////////////////////////////
  22. // Startup Code
  23.  
  24. #if defined(_WINDLL) || defined(_USRDLL)
  25.  
  26. // Declare DllMain
  27. extern "C" BOOL WINAPI DllMain(HANDLE hDllHandle, DWORD dwReason, LPVOID lpReserved);
  28.  
  29. extern "C" BOOL WINAPI _DllMainCRTStartup(HANDLE hDllHandle, DWORD dwReason, LPVOID lpReserved)
  30. {
  31.     return DllMain(hDllHandle, dwReason, lpReserved);
  32. }
  33.  
  34. #else
  35.  
  36. // wWinMain is not defined in winbase.h.
  37. extern "C" int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd);
  38.  
  39. #define SPACECHAR   _T(' ')
  40. #define DQUOTECHAR  _T('\"')
  41.  
  42.  
  43. #ifdef _UNICODE
  44. extern "C" void wWinMainCRTStartup()
  45. #else // _UNICODE
  46. extern "C" void WinMainCRTStartup()
  47. #endif // _UNICODE
  48. {
  49.     LPTSTR lpszCommandLine = ::GetCommandLine();
  50.     if(lpszCommandLine == NULL)
  51.         ::ExitProcess((UINT)-1);
  52.  
  53.     // Skip past program name (first token in command line).
  54.     // Check for and handle quoted program name.
  55.     if(*lpszCommandLine == DQUOTECHAR)
  56.     {
  57.         // Scan, and skip over, subsequent characters until
  58.         // another double-quote or a null is encountered.
  59.         do
  60.         {
  61.             lpszCommandLine = ::CharNext(lpszCommandLine);
  62.         }
  63.         while((*lpszCommandLine != DQUOTECHAR) && (*lpszCommandLine != _T('\0')));
  64.  
  65.         // If we stopped on a double-quote (usual case), skip over it.
  66.         if(*lpszCommandLine == DQUOTECHAR)
  67.             lpszCommandLine = ::CharNext(lpszCommandLine);
  68.     }
  69.     else
  70.     {
  71.         while(*lpszCommandLine > SPACECHAR)
  72.             lpszCommandLine = ::CharNext(lpszCommandLine);
  73.     }
  74.  
  75.     // Skip past any white space preceeding the second token.
  76.     while(*lpszCommandLine && (*lpszCommandLine <= SPACECHAR))
  77.         lpszCommandLine = ::CharNext(lpszCommandLine);
  78.  
  79.     STARTUPINFO StartupInfo;
  80.     StartupInfo.dwFlags = 0;
  81.     ::GetStartupInfo(&StartupInfo);
  82.  
  83.     int nRet = _tWinMain(::GetModuleHandle(NULL), NULL, lpszCommandLine,
  84.         (StartupInfo.dwFlags & STARTF_USESHOWWINDOW) ?
  85.         StartupInfo.wShowWindow : SW_SHOWDEFAULT);
  86.  
  87.     ::ExitProcess((UINT)nRet);
  88. }
  89.  
  90. #endif // defined(_WINDLL) | defined(_USRDLL)
  91.  
  92. /////////////////////////////////////////////////////////////////////////////
  93. // Heap Allocation
  94.  
  95. #ifndef _DEBUG
  96.  
  97. #ifndef _MERGE_PROXYSTUB
  98. //rpcproxy.h does the same thing as this
  99. int __cdecl _purecall()
  100. {
  101.     DebugBreak();
  102.     return 0;
  103. }
  104. #endif
  105.  
  106. #if !defined(_M_ALPHA) && !defined(_M_PPC)
  107. //RISC always initializes floating point and always defines _fltused
  108. extern "C" const int _fltused = 0;
  109. #endif
  110.  
  111. static const int nExtraAlloc = 8;
  112. static const int nOffsetBlock = nExtraAlloc/sizeof(HANDLE);
  113.  
  114. void* __cdecl malloc(size_t n)
  115. {
  116.     void* pv = NULL;
  117. #ifndef _ATL_NO_MP_HEAP
  118.     if (_Module.m_phHeaps == NULL)
  119. #endif
  120.     {
  121.         pv = (HANDLE*) HeapAlloc(_Module.m_hHeap, 0, n);
  122.     }
  123. #ifndef _ATL_NO_MP_HEAP
  124.     else
  125.     {
  126.         // overallocate to remember the heap handle
  127.         int nHeap = _Module.m_nHeap++;
  128.         HANDLE hHeap = _Module.m_phHeaps[nHeap & _Module.m_dwHeaps];
  129.         HANDLE* pBlock = (HANDLE*) HeapAlloc(hHeap, 0, n + nExtraAlloc);
  130.         if (pBlock != NULL)
  131.         {
  132.             *pBlock = hHeap;
  133.             pv = (void*)(pBlock + nOffsetBlock);
  134.         }
  135.         else
  136.             pv = NULL;
  137.     }
  138. #endif
  139.     return pv;
  140. }
  141.  
  142. void* __cdecl calloc(size_t n, size_t s)
  143. {
  144.     return malloc(n*s);
  145. }
  146.  
  147. void* __cdecl realloc(void* p, size_t n)
  148. {
  149.     if (p == NULL)
  150.         return malloc(n);
  151. #ifndef _ATL_NO_MP_HEAP
  152.     if (_Module.m_phHeaps == NULL)
  153. #endif
  154.         return HeapReAlloc(_Module.m_hHeap, 0, p, n);
  155. #ifndef _ATL_NO_MP_HEAP
  156.     else
  157.     {
  158.         HANDLE* pHeap = ((HANDLE*)p)-nOffsetBlock;
  159.         pHeap = (HANDLE*) HeapReAlloc(*pHeap, 0, pHeap, n + nExtraAlloc);
  160.         return (pHeap != NULL) ? pHeap + nOffsetBlock : NULL;
  161.     }
  162. #endif
  163. }
  164.  
  165. void __cdecl free(void* p)
  166. {
  167.   /* Test for null needed to avoid calling HeapFree on an invalid
  168.    * handle on SMP machines. This code will leak. See MS Q190531. */
  169.   if (p==NULL) return;
  170. #ifndef _ATL_NO_MP_HEAP
  171.     if (_Module.m_phHeaps == NULL)
  172. #endif
  173.         HeapFree(_Module.m_hHeap, 0, p);
  174. #ifndef _ATL_NO_MP_HEAP
  175.     else
  176.     {
  177.         HANDLE* pHeap = ((HANDLE*)p)-nOffsetBlock;
  178.         HeapFree(*pHeap, 0, pHeap);
  179.     }
  180. #endif
  181. }
  182.  
  183. void* __cdecl operator new(size_t n)
  184. {
  185.     return malloc(n);
  186. }
  187.  
  188. void __cdecl operator delete(void* p)
  189. {
  190.     free(p);
  191. }
  192.  
  193. #endif  //_DEBUG
  194.  
  195. #endif //_ATL_MIN_CRT
  196.