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.

228 lines
5.8 KiB

  1. /*++
  2. Copyright (c) 1991-1992 Microsoft Corporation
  3. Module Name:
  4. MidlUser.c
  5. Abstract:
  6. This file contains common functions and utilities that the API
  7. DLLs can use in making remote calls. This includes the
  8. MIDL_USER_ALLOCATE functions.
  9. The following routines are called by MIDL-generated code and the
  10. NetApiBufferAllocate and NetApiBufferFree routines:
  11. MIDL_user_allocate
  12. MIDL_user_free
  13. The following routines are NOT called by MIDL-generated code; they are
  14. only called by the NetApiBufferReallocate and NetApiBufferSize routines:
  15. MIDL_user_reallocate
  16. MIDL_user_size
  17. Author:
  18. Dan Lafferty danl 06-Feb-1991
  19. Environment:
  20. User Mode - Win32
  21. Revision History:
  22. 06-Feb-1991 danl
  23. Created
  24. 25-Apr-1991 JohnRo
  25. Split out MIDL user (allocate,free) into seperate source file, so
  26. linker doesn't get confused.
  27. 03-Dec-1991 JohnRo
  28. Added MIDL_user_reallocate and MIDL_user_size APIs. (These are so we
  29. create the NetApiBufferAllocate, NetApiBufferReallocate, and
  30. NetApiBufferSize APIs.)
  31. Also check alignment of allocated data.
  32. 12-Jan-1992 JohnRo
  33. Workaround a LocalReAlloc() bug where 2nd or 3rd realloc messes up.
  34. (See WIN32_WORKAROUND code below.)
  35. 08-Jun-1992 JohnRo
  36. RAID 9258: return non-null pointer when allocating zero bytes.
  37. Also, SteveWo finally fixed LocalReAlloc() bug, so use it again.
  38. Avoid calling LocalFree() with null pointer, to avoid access viol.
  39. 01-Dec-1992 JohnRo
  40. Fix MIDL_user_ func signatures.
  41. Avoid compiler warnings (const vs. volatile).
  42. --*/
  43. // These must be included first:
  44. #include <windef.h> // win32 typedefs
  45. #include <rpc.h> // rpc prototypes
  46. // These may be included in any order:
  47. #include <align.h> // POINTER_IS_ALIGNED(), ALIGN_WORST.
  48. #include <rpcutil.h> // My prototypes.
  49. #include <netdebug.h> // NetpAssert().
  50. #include <stdarg.h> // memcpy().
  51. #include <winbase.h> // LocalAlloc(), LMEM_ flags, etc.
  52. #define LOCAL_ALLOCATION_FLAGS LMEM_FIXED
  53. void __RPC_FAR * __RPC_API
  54. MIDL_user_allocate(
  55. IN size_t NumBytes
  56. )
  57. /*++
  58. Routine Description:
  59. Allocates storage for RPC transactions. The RPC stubs will either call
  60. MIDL_user_allocate when it needs to un-marshall data into a buffer
  61. that the user must free. RPC servers will use MIDL_user_allocate to
  62. allocate storage that the RPC server stub will free after marshalling
  63. the data.
  64. Arguments:
  65. NumBytes - The number of bytes to allocate. (Note that NetApiBufferAllocate
  66. depends on being able to request that zero bytes be allocated and get
  67. back a non-null pointer.)
  68. Return Value:
  69. Pointer to the allocated memory.
  70. --*/
  71. {
  72. LPVOID NewPointer;
  73. NewPointer = (LPVOID) LocalAlloc(
  74. LOCAL_ALLOCATION_FLAGS,
  75. NumBytes);
  76. NetpAssert( POINTER_IS_ALIGNED( NewPointer, ALIGN_WORST) );
  77. return (NewPointer);
  78. } // MIDL_user_allocate
  79. void __RPC_API
  80. MIDL_user_free(
  81. IN void __RPC_FAR *MemPointer
  82. )
  83. /*++
  84. Routine Description:
  85. Frees storage used in RPC transactions. The RPC client can call this
  86. function to free buffer space that was allocated by the RPC client
  87. stub when un-marshalling data that is to be returned to the client.
  88. The Client calls MIDL_user_free when it is finished with the data and
  89. desires to free up the storage.
  90. The RPC server stub calls MIDL_user_free when it has completed
  91. marshalling server data that is to be passed back to the client.
  92. Arguments:
  93. MemPointer - This points to the memory block that is to be released.
  94. Return Value:
  95. none.
  96. --*/
  97. {
  98. NetpAssert( POINTER_IS_ALIGNED( MemPointer, ALIGN_WORST) );
  99. if (MemPointer != NULL) {
  100. (void) LocalFree(MemPointer);
  101. }
  102. } // MIDL_user_free
  103. void *
  104. MIDL_user_reallocate(
  105. IN void * OldPointer OPTIONAL,
  106. IN size_t NewByteCount
  107. )
  108. {
  109. LPVOID NewPointer; // may be NULL.
  110. NetpAssert( POINTER_IS_ALIGNED( OldPointer, ALIGN_WORST) );
  111. // Special cases: something into nothing, or nothing into something.
  112. if (OldPointer == NULL) {
  113. NewPointer = (LPVOID) LocalAlloc(
  114. LOCAL_ALLOCATION_FLAGS,
  115. NewByteCount);
  116. } else if (NewByteCount == 0) {
  117. (void) LocalFree( OldPointer );
  118. NewPointer = NULL;
  119. } else { // must be realloc of something to something else.
  120. HANDLE hOldMem;
  121. HANDLE hNewMem; // handle for new (may = old handle)
  122. hOldMem = LocalHandle( (LPSTR) OldPointer);
  123. NetpAssert(hOldMem != NULL);
  124. hNewMem = LocalReAlloc(
  125. hOldMem, // old handle
  126. NewByteCount, // new size in bytes
  127. LOCAL_ALLOCATION_FLAGS | // flags
  128. LMEM_MOVEABLE); // (motion okay)
  129. if (hNewMem == NULL) {
  130. return (NULL);
  131. }
  132. NewPointer = (LPVOID) hNewMem;
  133. }
  134. NetpAssert( POINTER_IS_ALIGNED( NewPointer, ALIGN_WORST) );
  135. return (NewPointer);
  136. } // MIDL_user_reallocate
  137. unsigned long
  138. MIDL_user_size(
  139. IN void * Pointer
  140. )
  141. {
  142. DWORD ByteCount;
  143. HANDLE hMemory;
  144. NetpAssert( Pointer != NULL );
  145. NetpAssert( POINTER_IS_ALIGNED( Pointer, ALIGN_WORST ) );
  146. hMemory = LocalHandle( (LPSTR) Pointer );
  147. NetpAssert( hMemory != NULL );
  148. ByteCount = (DWORD)LocalSize( hMemory );
  149. NetpAssert( ByteCount > 0 );
  150. return (ByteCount);
  151. } // MIDL_user_size