Source code of Windows XP (NT5)
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.

79 lines
1.6 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: winutil.c
  3. *
  4. * Copyright (c) 1991, Microsoft Corporation
  5. *
  6. * Implements windows specific utility functions
  7. *
  8. * History:
  9. * 12-09-91 Davidc Created.
  10. \***************************************************************************/
  11. #include "sec.h"
  12. #include <string.h>
  13. #include <stdio.h>
  14. /****************************************************************************
  15. FUNCTION: Alloc
  16. PURPOSE: Allocates memory to hold the specified number of bytes
  17. RETURNS : Pointer to allocated memory or NULL on failure
  18. ****************************************************************************/
  19. PVOID Alloc(
  20. DWORD Bytes)
  21. {
  22. HANDLE hMem;
  23. PVOID Buffer;
  24. hMem = LocalAlloc(LMEM_MOVEABLE, Bytes + sizeof(hMem));
  25. if (hMem == NULL) {
  26. return(NULL);
  27. }
  28. // Lock down the memory
  29. //
  30. Buffer = LocalLock(hMem);
  31. if (Buffer == NULL) {
  32. LocalFree(hMem);
  33. return(NULL);
  34. }
  35. //
  36. // Store the handle at the start of the memory block and return
  37. // a pointer to just beyond it.
  38. //
  39. *((PHANDLE)Buffer) = hMem;
  40. return (PVOID)(((PHANDLE)Buffer)+1);
  41. }
  42. /****************************************************************************
  43. FUNCTION: Free
  44. PURPOSE: Frees the memory previously allocated with Alloc
  45. RETURNS : TRUE on success, otherwise FALSE
  46. ****************************************************************************/
  47. BOOL Free(
  48. PVOID Buffer)
  49. {
  50. HANDLE hMem;
  51. hMem = *(((PHANDLE)Buffer) - 1);
  52. LocalUnlock(hMem);
  53. return(LocalFree(hMem) == NULL);
  54. }