Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
4.5 KiB

  1. /*
  2. * Adobe Universal Font Library
  3. *
  4. * Copyright (c) 1996 Adobe Systems Inc.
  5. * All Rights Reserved
  6. *
  7. * UFLMem.c
  8. *
  9. * These are the memory allocation, deletion, etc... routines used by UFL.
  10. * All memory blocks are allocated at the given size plus the size of 1
  11. * unsigned long. The current size of the block is then stored in the first
  12. * unsigned long in the block. The address of the block plus the first unsigned long
  13. * is returned to the caller.
  14. *
  15. * $Header:
  16. */
  17. #include "UFLCnfig.h"
  18. #ifdef MAC_ENV
  19. #include <Memory.h>
  20. #endif
  21. #include "UFLMem.h"
  22. #include "UFLStd.h"
  23. #ifdef KERNEL_MODE
  24. PVOID UFLEXPORT
  25. KMNewPtr(
  26. PVOID p,
  27. ULONG ulSize
  28. )
  29. {
  30. if (p != NULL)
  31. {
  32. *((PULONG) p) = ulSize;
  33. return (PBYTE) p + sizeof(ULONG_PTR);
  34. }
  35. else
  36. return NULL;
  37. }
  38. VOID UFLEXPORT
  39. KMDeletePtr(
  40. PVOID p
  41. )
  42. {
  43. if (p != NULL)
  44. MemFree((PBYTE) p - sizeof(ULONG_PTR));
  45. }
  46. #else // !KERNEL_MODE
  47. #if defined(MAC_ENV) && defined(__MWERKS__) && !defined(powerc)
  48. #pragma pointers_in_D0
  49. #endif
  50. /* Global static variable */
  51. void *UFLEXPORT
  52. UFLNewPtr(
  53. const UFLMemObj *mem,
  54. unsigned long size
  55. )
  56. {
  57. unsigned long* p = (unsigned long*)(*mem->alloc)((UFLsize_t) (size + sizeof(ULONG_PTR)), mem->userData );
  58. if ( p == (unsigned long*)nil )
  59. return nil;
  60. // Memory allocated by UFLNewPtr is zero-initalized.
  61. *p = size;
  62. UFLmemset(mem, (void*)((char *)p + sizeof(ULONG_PTR)), 0, size);
  63. return (void*)((char *)p + sizeof(ULONG_PTR));
  64. }
  65. void UFLEXPORT
  66. UFLDeletePtr(
  67. const UFLMemObj *mem,
  68. void *ptr
  69. )
  70. {
  71. if ( ptr != nil )
  72. (*mem->free)( (void*)( ((unsigned char*)ptr) - sizeof(ULONG_PTR) ), mem->userData );
  73. }
  74. #if defined(MAC_ENV) && defined(__MWERKS__) && !defined(powerc)
  75. #pragma pointers_in_A0
  76. #endif
  77. #ifdef MAC_ENV
  78. // on the Macintosh, memcpy is frequently not well implemented because
  79. // most applications call BlockMove instead.
  80. void UFLEXPORT
  81. UFLmemcpy(
  82. const UFLMemObj *mem,
  83. void *destination,
  84. void *source,
  85. unsigned long size
  86. )
  87. {
  88. if ( (((unsigned long)source) & 3) || (((long)destination) & 3) )
  89. BlockMove( source, destination, size );
  90. else {
  91. unsigned long *src = (unsigned long*)source;
  92. unsigned long *dst = (unsigned long*)destination;
  93. unsigned char *srcb, *dstb;
  94. long count = (size >> 2) + 1;
  95. while ( --count )
  96. *dst++ = *src++;
  97. count = (size & 3);
  98. if ( count != 0 ) {
  99. srcb = (unsigned char*)src;
  100. dstb = (unsigned char*)dst;
  101. ++count;
  102. while ( --count )
  103. *dstb++ = *srcb++;
  104. }
  105. }
  106. }
  107. #else
  108. void UFLEXPORT
  109. UFLmemcpy(
  110. const UFLMemObj *mem,
  111. void *destination,
  112. void *source,
  113. unsigned long size
  114. )
  115. {
  116. if ( destination != nil && source != nil)
  117. (*mem->copy)( (void*)destination, (void*)source, (UFLsize_t) size , mem->userData );
  118. // don't want to use this because size parameter is system dependend
  119. // allow the client to chose whichever way.
  120. // Warning!!! be carefull of platforms on which size_t is a 2 byte integer
  121. //memcpy( destination, source, (size_t)size );
  122. }
  123. #endif // !MAC_ENV
  124. #endif // !KERNEL_MODE
  125. void UFLEXPORT
  126. UFLmemset(
  127. const UFLMemObj *mem,
  128. void *destination,
  129. unsigned int value,
  130. unsigned long size
  131. )
  132. {
  133. if ( destination != nil )
  134. (*mem->set)( (void*)destination, value , size , mem->userData );
  135. // don't want to use this because size parameter is system dependend
  136. // allow the client to chose whichever way.
  137. // memset( destination, value, (UFLsize_t)size );
  138. }
  139. unsigned long
  140. UFLMemSize(
  141. void *ptr
  142. )
  143. {
  144. return *(unsigned long *) ((unsigned char *)ptr - sizeof(ULONG_PTR));
  145. }
  146. UFLBool
  147. UFLEnlargePtr(
  148. const UFLMemObj *mem,
  149. void **ptrAddr,
  150. unsigned long newSize,
  151. UFLBool bCopy
  152. )
  153. {
  154. unsigned long oldSize = *(unsigned long *) ((unsigned char *)(*ptrAddr) - sizeof(ULONG_PTR));
  155. void *newBuf;
  156. newBuf = UFLNewPtr( mem, newSize );
  157. if ( newBuf == nil )
  158. return 0;
  159. if ( bCopy )
  160. UFLmemcpy( mem, newBuf, *ptrAddr, (unsigned long)min(oldSize, newSize) );
  161. UFLDeletePtr( mem, *ptrAddr );
  162. *ptrAddr = newBuf;
  163. return 1;
  164. }