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.

209 lines
3.4 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. All rights reserved
  4. Module Name:
  5. jobid.c
  6. Abstract:
  7. Handles job id bitmap.
  8. Author:
  9. Albert Ting (AlbertT) 24-Oct-96
  10. Environment:
  11. User Mode -Win32
  12. Revision History:
  13. Ported from spooler.c code.
  14. --*/
  15. #include "precomp.h"
  16. #pragma hdrstop
  17. #include "jobid.h"
  18. BOOL
  19. ReallocJobIdMap(
  20. HANDLE hJobIdMap,
  21. DWORD dwNewMinSize
  22. )
  23. /*++
  24. Routine Description:
  25. Reallocates the job id bitmap to a new minimum size.
  26. Arguments:
  27. hJobId - Handle to job ID bitmap.
  28. dwNewMinSize - Specifies the minimum size of the job id bitmap.
  29. Note that the allocation size may be larger. Also, if 0x10
  30. is requested, only ids 0x0-0xf are guaranteed to be valid
  31. (0x10 is the 11th id, and therefore not valid).
  32. Return Value:
  33. TRUE - Success
  34. False - Failed.
  35. --*/
  36. {
  37. PJOB_ID_MAP pJobIdMap = (PJOB_ID_MAP)hJobIdMap;
  38. PDWORD pMap;
  39. if( dwNewMinSize & 7 ){
  40. dwNewMinSize&=~7;
  41. dwNewMinSize+=8;
  42. }
  43. pMap = ReallocSplMem( pJobIdMap->pMap,
  44. pJobIdMap->dwMaxJobId/8,
  45. dwNewMinSize/8 );
  46. if( !pMap ){
  47. DBGMSG( DBG_WARN,
  48. ( "ReallocJobIdMap failed ReallocSplMem dwNewMinSize %d Error %d\n",
  49. dwNewMinSize, GetLastError() ));
  50. } else {
  51. pJobIdMap->pMap = pMap;
  52. pJobIdMap->dwMaxJobId = dwNewMinSize;
  53. }
  54. return pMap != NULL;
  55. }
  56. DWORD
  57. GetNextId(
  58. HANDLE hJobIdMap
  59. )
  60. /*++
  61. Routine Description:
  62. Retrieves a free job id, although not necessarily the next
  63. free bit.
  64. Arguments:
  65. hJobId - Handle to job ID bitmap.
  66. Return Value:
  67. DWORD - Next job.
  68. --*/
  69. {
  70. PJOB_ID_MAP pJobIdMap = (PJOB_ID_MAP)hJobIdMap;
  71. DWORD id;
  72. do {
  73. //
  74. // Scan forward from current job.
  75. //
  76. for( id = pJobIdMap->dwCurrentJobId + 1;
  77. id < pJobIdMap->dwMaxJobId;
  78. ++id ){
  79. if( !bBitOn( hJobIdMap, id )){
  80. goto FoundJobId;
  81. }
  82. }
  83. //
  84. // Scan from beginning to current job.
  85. //
  86. for( id = 1; id < pJobIdMap->dwCurrentJobId; ++id ){
  87. if( !bBitOn( hJobIdMap, id )){
  88. goto FoundJobId;
  89. }
  90. }
  91. } while( ReallocJobIdMap( hJobIdMap, pJobIdMap->dwMaxJobId + 128 ));
  92. //
  93. // No job ids; fail.
  94. //
  95. return 0;
  96. FoundJobId:
  97. vMarkOn( hJobIdMap, id );
  98. pJobIdMap->dwCurrentJobId = id;
  99. return id;
  100. }
  101. /********************************************************************
  102. Create and delete functions.
  103. ********************************************************************/
  104. HANDLE
  105. hCreateJobIdMap(
  106. DWORD dwMinSize
  107. )
  108. {
  109. PJOB_ID_MAP pJobIdMap;
  110. pJobIdMap = AllocSplMem( sizeof( JOB_ID_MAP ));
  111. if( !pJobIdMap ){
  112. goto Fail;
  113. }
  114. pJobIdMap->pMap = AllocSplMem( dwMinSize/8 );
  115. if( !pJobIdMap->pMap ){
  116. goto Fail;
  117. }
  118. pJobIdMap->dwMaxJobId = dwMinSize;
  119. pJobIdMap->dwCurrentJobId = 1;
  120. return (HANDLE)pJobIdMap;
  121. Fail:
  122. if( pJobIdMap ){
  123. FreeSplMem( pJobIdMap );
  124. }
  125. return NULL;
  126. }
  127. VOID
  128. vDeleteJobIdMap(
  129. HANDLE hJobIdMap
  130. )
  131. {
  132. PJOB_ID_MAP pJobIdMap = (PJOB_ID_MAP)hJobIdMap;
  133. if( pJobIdMap ){
  134. if( pJobIdMap->pMap ){
  135. FreeSplMem( pJobIdMap->pMap );
  136. }
  137. FreeSplMem( pJobIdMap );
  138. }
  139. }