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.

213 lines
5.0 KiB

  1. /*++
  2. Copyright (c) 1999 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. 10-Feb-1993 RitaW
  28. Copied to the NetWare tree so that the LPC transport can used for
  29. the local case.
  30. --*/
  31. #include <nt.h>
  32. #include <ntrtl.h> // needed for nturtl.h
  33. #include <nturtl.h> // needed for windows.h
  34. #include <windows.h> // win32 typedefs
  35. #include <rpc.h> // rpc prototypes
  36. #include <align.h> // POINTER_IS_ALIGNED(), ALIGN_WORST.
  37. #include <winbase.h> // LocalAlloc
  38. #include <debug.h>
  39. PVOID
  40. MIDL_user_allocate (
  41. IN unsigned int 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 = (LPVOID) LocalAlloc(
  59. LMEM_ZEROINIT,
  60. NumBytes
  61. );
  62. ASSERT( POINTER_IS_ALIGNED( NewPointer, ALIGN_WORST) );
  63. return (NewPointer);
  64. } // MIDL_user_allocate
  65. VOID
  66. MIDL_user_free (
  67. IN void *MemPointer
  68. )
  69. /*++
  70. Routine Description:
  71. Frees storage used in RPC transactions. The RPC client can call this
  72. function to free buffer space that was allocated by the RPC client
  73. stub when un-marshalling data that is to be returned to the client.
  74. The Client calls MIDL_user_free when it is finished with the data and
  75. desires to free up the storage.
  76. The RPC server stub calls MIDL_user_free when it has completed
  77. marshalling server data that is to be passed back to the client.
  78. Arguments:
  79. MemPointer - This points to the memory block that is to be released.
  80. Return Value:
  81. none.
  82. Note:
  83. --*/
  84. {
  85. ASSERT( POINTER_IS_ALIGNED( MemPointer, ALIGN_WORST) );
  86. (void) LocalFree((HLOCAL) MemPointer);
  87. } // MIDL_user_free
  88. void *
  89. MIDL_user_reallocate(
  90. IN void * OldPointer OPTIONAL,
  91. IN unsigned long NewByteCount
  92. )
  93. {
  94. LPVOID NewPointer; // may be NULL.
  95. ASSERT( POINTER_IS_ALIGNED( OldPointer, ALIGN_WORST) );
  96. // Special cases: something into nothing, or nothing into something.
  97. if (OldPointer == NULL) {
  98. NewPointer = (LPVOID) LocalAlloc(
  99. LMEM_ZEROINIT,
  100. NewByteCount
  101. );
  102. } else if (NewByteCount == 0) {
  103. (void) LocalFree((HLOCAL) OldPointer );
  104. NewPointer = NULL;
  105. } else { // must be realloc of something to something else.
  106. HANDLE hOldMem;
  107. HANDLE hNewMem; // handle for new (may = old handle)
  108. hOldMem = LocalHandle( (LPSTR) OldPointer);
  109. ASSERT(hOldMem != NULL);
  110. hNewMem = (PVOID) LocalReAlloc(
  111. hOldMem, // old handle
  112. NewByteCount, // new size in bytes
  113. LMEM_ZEROINIT | // flags
  114. LMEM_MOVEABLE // (motion okay)
  115. );
  116. if (hNewMem == NULL) {
  117. return (NULL);
  118. }
  119. NewPointer = (LPVOID) hNewMem;
  120. } // must be realloc of something to something else
  121. ASSERT( POINTER_IS_ALIGNED( NewPointer, ALIGN_WORST) );
  122. return (NewPointer);
  123. } // MIDL_user_reallocate
  124. ULONG_PTR
  125. MIDL_user_size(
  126. IN void * Pointer
  127. )
  128. {
  129. ULONG_PTR ByteCount;
  130. HANDLE hMemory;
  131. ASSERT( Pointer != NULL );
  132. ASSERT( POINTER_IS_ALIGNED( Pointer, ALIGN_WORST ) );
  133. hMemory = LocalHandle( (LPSTR) Pointer );
  134. ASSERT( hMemory != NULL );
  135. ByteCount = LocalSize( hMemory );
  136. ASSERT( ByteCount > 0 );
  137. return (ByteCount);
  138. } // MIDL_user_size