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.

141 lines
3.5 KiB

  1. /*
  2. * MEDIATYP.C
  3. *
  4. * RSM Service : Media Type Objects
  5. *
  6. * Author: ErvinP
  7. *
  8. * (c) 2001 Microsoft Corporation
  9. *
  10. */
  11. #include <windows.h>
  12. #include <stdlib.h>
  13. #include <wtypes.h>
  14. #include <ntmsapi.h>
  15. #include "internal.h"
  16. #include "resource.h"
  17. #include "debug.h"
  18. MEDIA_TYPE_OBJECT *NewMediaTypeObject(LIBRARY *lib)
  19. {
  20. MEDIA_TYPE_OBJECT *mediaTypeObj;
  21. mediaTypeObj = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, sizeof(MEDIA_TYPE_OBJECT));
  22. if (mediaTypeObj){
  23. mediaTypeObj->lib = lib;
  24. mediaTypeObj->numPhysMediaReferences = 0;
  25. InitializeCriticalSection(&mediaTypeObj->lock);
  26. }
  27. else {
  28. ASSERT(mediaTypeObj);
  29. }
  30. return mediaTypeObj;
  31. }
  32. VOID DestroyMediaTypeObject(MEDIA_TYPE_OBJECT *mediaTypeObj)
  33. {
  34. // BUGBUG FINISH
  35. DeleteCriticalSection(&mediaTypeObj->lock);
  36. GlobalFree(mediaTypeObj);
  37. }
  38. MEDIA_TYPE_OBJECT *FindMediaTypeObject(LPNTMS_GUID lpMediaTypeId)
  39. {
  40. MEDIA_TYPE_OBJECT *mediaTypeObj = NULL;
  41. if (lpMediaTypeId){
  42. OBJECT_HEADER *objHdr;
  43. objHdr = FindObjectInGuidHash(lpMediaTypeId);
  44. if (objHdr){
  45. if (objHdr->objType == OBJECTTYPE_MEDIATYPEOBJECT){
  46. mediaTypeObj = (MEDIA_TYPE_OBJECT *)objHdr;
  47. }
  48. else {
  49. DerefObject(objHdr);
  50. }
  51. }
  52. }
  53. return mediaTypeObj;
  54. }
  55. HRESULT DeleteMediaTypeObject(MEDIA_TYPE_OBJECT *mediaTypeObj)
  56. {
  57. HRESULT result;
  58. EnterCriticalSection(&mediaTypeObj->lock);
  59. if (mediaTypeObj->numPhysMediaReferences == 0){
  60. /*
  61. * Dereference the media type object.
  62. * This will cause it to get deleted once its reference
  63. * count goes to zero. We can still use our pointer
  64. * since the caller has a reference.
  65. */
  66. mediaTypeObj->objHeader.isDeleted = TRUE;
  67. DerefObject(mediaTypeObj);
  68. result = ERROR_SUCCESS;
  69. }
  70. else {
  71. /*
  72. * There are physical media referencing this media type object
  73. * as their type. So we can't delete this type object.
  74. */
  75. result = ERROR_BUSY;
  76. }
  77. LeaveCriticalSection(&mediaTypeObj->lock);
  78. return result;
  79. }
  80. /*
  81. * SetMediaType
  82. *
  83. * Must be called with physical media lock held.
  84. * MEDIA_TYPE_OBJECT lock should NOT be held as we may have
  85. * to grab another MEDIA_TYPE_OBJECT's lock
  86. * (acquiring both simulataneously might lead to deadlock).
  87. */
  88. VOID SetMediaType(PHYSICAL_MEDIA *physMedia, MEDIA_TYPE_OBJECT *mediaTypeObj)
  89. {
  90. /*
  91. * Remove the current type, if any.
  92. */
  93. if (physMedia->mediaTypeObj){
  94. EnterCriticalSection(&physMedia->mediaTypeObj->lock);
  95. ASSERT(physMedia->mediaTypeObj->numPhysMediaReferences > 0);
  96. physMedia->mediaTypeObj->numPhysMediaReferences--;
  97. /*
  98. * Dereference both objects since they no longer point to each other.
  99. */
  100. DerefObject(physMedia);
  101. DerefObject(physMedia->mediaTypeObj);
  102. LeaveCriticalSection(&physMedia->mediaTypeObj->lock);
  103. physMedia->mediaTypeObj = NULL;
  104. }
  105. /*
  106. * Now set the new media type.
  107. */
  108. EnterCriticalSection(&mediaTypeObj->lock);
  109. mediaTypeObj->numPhysMediaReferences++;
  110. physMedia->mediaTypeObj = mediaTypeObj;
  111. RefObject(physMedia);
  112. RefObject(mediaTypeObj);
  113. LeaveCriticalSection(&mediaTypeObj->lock);
  114. }