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.

208 lines
4.8 KiB

  1. /*++
  2. Copyright (c) 1990,91 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. Author:
  10. Dan Lafferty danl 06-Feb-1991
  11. Environment:
  12. User Mode - Win32
  13. Revision History:
  14. 06-Feb-1991 danl
  15. Created
  16. 25-Apr-1991 JohnRo
  17. Split out MIDL user (allocate,free) into seperate source file, so
  18. linker doesn't get confused.
  19. 03-July-1991 JimK
  20. Moved to a common directory so services available to more than just
  21. LM code.
  22. 03-Dec-1991 JohnRo
  23. Added MIDL_user_reallocate and MIDL_user_size APIs. (These are so we
  24. create the NetApiBufferAllocate, NetApiBufferReallocate, and
  25. NetApiBufferSize APIs.)
  26. Also check alignment of allocated data.
  27. --*/
  28. // These must be included first:
  29. #include <nt.h>
  30. #include <ntrtl.h> // needed for nturtl.h
  31. #include <nturtl.h> // needed for windows.h
  32. #include <windows.h> // win32 typedefs
  33. #include <rpc.h> // rpc prototypes
  34. // These may be included in any order:
  35. #include <align.h> // POINTER_IS_ALIGNED(), ALIGN_WORST.
  36. #include <ntrpcp.h> // MIDL_user_allocate(), MIDL_user_free().
  37. #include <winbase.h> // LocalAlloc
  38. #define LOCAL_ALLOCATION_FLAGS LMEM_ZEROINIT
  39. PVOID
  40. MIDL_user_allocate (
  41. IN size_t NumBytes
  42. )
  43. /*++
  44. Routine Description:
  45. Allocates storage for RPC transactions. The RPC stubs will either call
  46. MIDL_user_allocate when it needs to un-marshall data into a buffer
  47. that the user must free. RPC servers will use MIDL_user_allocate to
  48. allocate storage that the RPC server stub will free after marshalling
  49. the data.
  50. Arguments:
  51. NumBytes - The number of bytes to allocate.
  52. Return Value:
  53. none
  54. Note:
  55. --*/
  56. {
  57. LPVOID NewPointer;
  58. NewPointer = LocalAlloc(
  59. LOCAL_ALLOCATION_FLAGS,
  60. NumBytes);
  61. ASSERT( POINTER_IS_ALIGNED( NewPointer, ALIGN_WORST) );
  62. return (NewPointer);
  63. } // MIDL_user_allocate
  64. VOID
  65. MIDL_user_free (
  66. IN void *MemPointer
  67. )
  68. /*++
  69. Routine Description:
  70. Frees storage used in RPC transactions. The RPC client can call this
  71. function to free buffer space that was allocated by the RPC client
  72. stub when un-marshalling data that is to be returned to the client.
  73. The Client calls MIDL_user_free when it is finished with the data and
  74. desires to free up the storage.
  75. The RPC server stub calls MIDL_user_free when it has completed
  76. marshalling server data that is to be passed back to the client.
  77. Arguments:
  78. MemPointer - This points to the memory block that is to be released.
  79. Return Value:
  80. none.
  81. Note:
  82. --*/
  83. {
  84. ASSERT( POINTER_IS_ALIGNED( MemPointer, ALIGN_WORST) );
  85. (void) LocalFree(MemPointer);
  86. } // MIDL_user_free
  87. void *
  88. MIDL_user_reallocate(
  89. IN void * OldPointer OPTIONAL,
  90. IN size_t NewByteCount
  91. )
  92. {
  93. LPVOID NewPointer; // may be NULL.
  94. ASSERT( POINTER_IS_ALIGNED( OldPointer, ALIGN_WORST) );
  95. // Special cases: something into nothing, or nothing into something.
  96. if (OldPointer == NULL) {
  97. NewPointer = LocalAlloc(
  98. LOCAL_ALLOCATION_FLAGS,
  99. NewByteCount);
  100. } else if (NewByteCount == 0) {
  101. (void) LocalFree( OldPointer );
  102. NewPointer = NULL;
  103. } else { // must be realloc of something to something else.
  104. HANDLE hOldMem;
  105. HANDLE hNewMem; // handle for new (may = old handle)
  106. hOldMem = LocalHandle( (LPSTR) OldPointer);
  107. ASSERT(hOldMem != NULL);
  108. hNewMem = LocalReAlloc(
  109. hOldMem, // old handle
  110. NewByteCount, // new size in bytes
  111. LOCAL_ALLOCATION_FLAGS | // flags
  112. LMEM_MOVEABLE); // (motion okay)
  113. if (hNewMem == NULL) {
  114. // BUGBUG: call GetLastError, could be out of memory or error.
  115. return (NULL);
  116. }
  117. NewPointer = (LPVOID) hNewMem;
  118. } // must be realloc of something to something else
  119. ASSERT( POINTER_IS_ALIGNED( NewPointer, ALIGN_WORST) );
  120. return (NewPointer);
  121. } // MIDL_user_reallocate
  122. unsigned long
  123. MIDL_user_size(
  124. IN void * Pointer
  125. )
  126. {
  127. DWORD ByteCount;
  128. HANDLE hMemory;
  129. ASSERT( Pointer != NULL );
  130. ASSERT( POINTER_IS_ALIGNED( Pointer, ALIGN_WORST ) );
  131. hMemory = LocalHandle( (LPSTR) Pointer );
  132. ASSERT( hMemory != NULL );
  133. ByteCount = (DWORD)LocalSize( hMemory );
  134. ASSERT( ByteCount > 0 );
  135. return (ByteCount);
  136. } // MIDL_user_size
  137.