home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWCommon / SLPriMem.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  4.5 KB  |  185 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SLPriMem.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef   SLPRIMEM_H
  13. #include "SLPriMem.h"
  14. #endif
  15.  
  16. // ----- OpenDoc Includes -----
  17.  
  18. #define FW_OPENDOC 1
  19.  
  20. #if FW_OPENDOC && defined(FW_BUILD_MAC)
  21. #include <MemMgr.h>
  22. #endif
  23.  
  24. #if FW_OPENDOC && defined(FW_BUILD_WIN)
  25. #include <MMStubs.h>
  26. #endif
  27.  
  28. // ----- Platform Includes -----
  29.  
  30. #if defined(FW_BUILD_WIN) && !defined(_INC_WINDOWS)
  31. #include <Windows.h>
  32. #endif
  33.  
  34.  
  35. #if defined(FW_BUILD_WIN) && !defined(_INC_WINDOWSX)
  36. #include <WindowsX.h>
  37. #endif
  38.  
  39. #if defined(FW_BUILD_MAC) && !defined(__MEMORY__)
  40. #include <Memory.h>
  41. #endif
  42.  
  43. #ifdef FW_BUILD_MAC
  44. #pragma segment FWCommon
  45. #endif
  46.  
  47. #ifndef FW_USE_STANDARD_LIBRARY
  48. #define FW_USE_STANDARD_LIBRARY 1
  49. #endif
  50.  
  51. #if FW_USE_STANDARD_LIBRARY
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #endif
  55.  
  56. //----------------------------------------------------------------------------------------
  57. // FW_PrimitiveAllocateBlock
  58. //----------------------------------------------------------------------------------------
  59. void* FW_PrimitiveAllocateBlock(size_t n)
  60. {
  61.     // No try block necessary - Do not throw
  62. #if FW_OPENDOC
  63.     return ::MMAllocate(n);
  64. #elif defined(FW_BUILD_MAC)
  65.     return ::NewPtr(n);
  66. #elif defined(FW_BUILD_WIN)
  67.     return ::GlobalAllocPtr(GHND, n);
  68. #endif
  69. }
  70.  
  71. //----------------------------------------------------------------------------------------
  72. // FW_PrimitiveResizeBlock
  73. //----------------------------------------------------------------------------------------
  74. void* FW_PrimitiveResizeBlock(void* p, size_t n)
  75. {
  76.     // No try block necessary - Do not throw
  77. #if FW_OPENDOC
  78.     return ::MMReallocate(p, n);
  79. #elif defined(FW_BUILD_MAC)
  80.     void* q = p;
  81.     if (q==NULL)
  82.         q = ::FW_PrimitiveAllocateBlock(n);
  83.     else
  84.     {
  85.         ::SetPtrSize((Ptr) q, n);
  86.         if (::MemError() != noErr)
  87.         {
  88.             q = ::FW_PrimitiveAllocateBlock(n);
  89.             if (q)
  90.             {
  91.                 ::BlockMoveData(p, q, n);
  92.                 ::FW_PrimitiveFreeBlock(p);
  93.             }
  94.         }
  95.     }
  96.     return q;
  97. #elif defined(FW_BUILD_WIN)
  98.     return ::GlobalReAllocPtr(p, n, 0);
  99. #endif
  100. }
  101.  
  102.  
  103. //----------------------------------------------------------------------------------------
  104. // FW_PrimitiveGetBlockSize
  105. //----------------------------------------------------------------------------------------
  106. size_t FW_PrimitiveGetBlockSize(void* p)
  107. {
  108.     // No try block necessary - Do not throw
  109. #if FW_OPENDOC
  110.     return ::MMBlockSize(p);
  111. #elif defined(FW_BUILD_MAC)
  112.     return ::GetPtrSize((Ptr) p);
  113. #elif defined(FW_BUILD_WIN)
  114.     return ::GlobalSize(GlobalPtrHandle(p));
  115. #endif
  116. }
  117.  
  118. //----------------------------------------------------------------------------------------
  119. // FW_PrimitiveFreeBlock
  120. //----------------------------------------------------------------------------------------
  121. void FW_PrimitiveFreeBlock(void* p)
  122. {
  123.     // No try block necessary - Do not throw
  124. #if FW_OPENDOC
  125.     if (p)
  126.         ::MMFree(p);
  127. #elif defined(FW_BUILD_MAC)
  128.     if (p)
  129.         ::DisposePtr((Ptr) p);
  130. #elif defined(FW_BUILD_WIN)
  131.     if (p)
  132.         ::GlobalFreePtr(p);
  133. #endif
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // FW_PrimitiveCopyMemory
  138. //----------------------------------------------------------------------------------------
  139.  
  140. void FW_PrimitiveCopyMemory(const void *source, void *destination, size_t bytes)
  141. {
  142.     // No try block necessary - Do not throw
  143.  
  144.     // This code must work correctly for overlapping blocks.
  145. #if defined(FW_BUILD_MAC)
  146.     ::BlockMoveData(source, destination, bytes);
  147. #elif defined(FW_BUILD_WIN)
  148.     ::MoveMemory(destination, source, bytes);
  149. #elif FW_USE_STANDARD_LIBRARY
  150.     ::memmove(destination, source, bytes);
  151. #else
  152.     if (source > destination)
  153.     {
  154.         char *dst = (char *) destination;
  155.         const char *src = (const char *) source;
  156.         while (bytes--)
  157.             *dst++ = *src++;
  158.     }
  159.     else if (source < destination)
  160.     {
  161.         char *dst = (char *) destination + bytes;
  162.         const char *src = (const char *) source + bytes;
  163.         while (bytes--)
  164.             *--dst = *--src;
  165.     }
  166. #endif
  167. }
  168.  
  169. //----------------------------------------------------------------------------------------
  170. // FW_PrimitiveSetMemory
  171. //----------------------------------------------------------------------------------------
  172.  
  173. void FW_PrimitiveSetMemory(void* block, size_t bytes, unsigned char value)
  174. {
  175.     // No try block necessary - Do not throw
  176. #if FW_USE_STANDARD_LIBRARY
  177.     ::memset(block, value, bytes);
  178. #else
  179.     register unsigned char *p = (unsigned char*) block;
  180.     while (bytes--)
  181.         *p++ = value;
  182. #endif
  183. }
  184.  
  185.