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.

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