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.

91 lines
1.6 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. alloc.c
  6. Abstract:
  7. Generic realloc code for any api that can fail with
  8. ERROR_INSUFFICIENT_BUFFER.
  9. Author:
  10. Albert Ting (AlbertT) 25-Sept-1996
  11. Revision History:
  12. --*/
  13. #include "precomp.hxx"
  14. #pragma hdrstop
  15. #include "alloc.hxx"
  16. PBYTE
  17. pAllocRead(
  18. HANDLE hUserData,
  19. ALLOC_FUNC AllocFunc,
  20. DWORD dwLenHint,
  21. PDWORD pdwLen OPTIONAL
  22. )
  23. {
  24. ALLOC_DATA AllocData;
  25. PBYTE pBufferOut = NULL;
  26. DWORD dwLastError;
  27. DWORD cbActual;
  28. if( pdwLen ){
  29. *pdwLen = 0;
  30. }
  31. if( !dwLenHint ){
  32. DBGMSG( DBG_ERROR, ( "ReallocRead: dwLenHint = 0\n" ));
  33. SetLastError( ERROR_INVALID_PARAMETER );
  34. return FALSE;
  35. }
  36. AllocData.pBuffer = NULL;
  37. AllocData.cbBuffer = dwLenHint;
  38. for( ; ; ){
  39. cbActual = AllocData.cbBuffer;
  40. AllocData.pBuffer = (PBYTE)LocalAlloc( LMEM_FIXED, cbActual );
  41. if( !AllocData.pBuffer ){
  42. break;
  43. }
  44. if( !AllocFunc( hUserData, &AllocData )){
  45. //
  46. // Call failed.
  47. //
  48. dwLastError = GetLastError();
  49. LocalFree( (HLOCAL)AllocData.pBuffer );
  50. if( dwLastError != ERROR_INSUFFICIENT_BUFFER &&
  51. dwLastError != ERROR_MORE_DATA ){
  52. break;
  53. }
  54. } else {
  55. pBufferOut = AllocData.pBuffer;
  56. if( pdwLen ){
  57. *pdwLen = cbActual;
  58. }
  59. break;
  60. }
  61. }
  62. return pBufferOut;
  63. }