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.

157 lines
3.9 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // rampmap.cpp
  4. //
  5. // Implements required RLDDI stuff for rampmaps.
  6. //
  7. // Copyright (C) Microsoft Corporation, 1997.
  8. //
  9. //----------------------------------------------------------------------------
  10. #include "pch.cpp"
  11. #pragma hdrstop
  12. static RLDDIRamp* NewRamp(int base, int size);
  13. RLDDIRampmap* RLDDICreateRampmap(RLDDIColormap* cmap)
  14. {
  15. RLDDIRampmap* rampmap;
  16. RLDDIRamp* ramp;
  17. if (D3DMalloc((void**) &rampmap, sizeof(RLDDIRampmap)))
  18. return NULL;
  19. CIRCLE_QUEUE_INITIALIZE(&rampmap->free,RLDDIRamp);
  20. CIRCLE_QUEUE_INITIALIZE(&rampmap->allocated,RLDDIRamp);
  21. rampmap->cmap = cmap;
  22. ramp = NewRamp(0, cmap->size);
  23. if (ramp == NULL)
  24. {
  25. D3DFree(rampmap);
  26. return NULL;
  27. }
  28. CIRCLE_QUEUE_INSERT_ROOT(&rampmap->free, RLDDIRamp, ramp, queue);
  29. return rampmap;
  30. }
  31. void RLDDIDestroyRampmap(RLDDIRampmap* rampmap)
  32. {
  33. RLDDIRamp* ramp;
  34. RLDDIRamp* ramp_next;
  35. if (!rampmap)
  36. return ;
  37. for (ramp = CIRCLE_QUEUE_FIRST(&rampmap->allocated); ramp;
  38. ramp = ramp_next)
  39. {
  40. ramp_next = CIRCLE_QUEUE_NEXT(&rampmap->allocated,ramp,queue);
  41. D3DFree(ramp);
  42. }
  43. for (ramp = CIRCLE_QUEUE_FIRST(&rampmap->free); ramp;
  44. ramp = ramp_next)
  45. {
  46. ramp_next = CIRCLE_QUEUE_NEXT(&rampmap->free,ramp,queue);
  47. D3DFree(ramp);
  48. }
  49. D3DFree(rampmap);
  50. }
  51. RLDDIRamp* RLDDIRampmapAllocate(RLDDIRampmap* rampmap, int size)
  52. {
  53. RLDDIRamp* ramp;
  54. RLDDIRamp* newramp;
  55. RLDDIRamp* ramp_next;
  56. if (!rampmap)
  57. return NULL;
  58. ramp = CIRCLE_QUEUE_FIRST(&rampmap->free);
  59. if (!ramp) return NULL;
  60. /*
  61. * cycle thru free rampmaps
  62. */
  63. for (; ramp && ramp->size < size; ramp = ramp_next)
  64. {
  65. ramp_next = CIRCLE_QUEUE_NEXT(&rampmap->free,ramp,queue);
  66. }
  67. /*
  68. * if we can't find a large enough ramp give up,
  69. * should try coalescing but it is non functional
  70. */
  71. if (!ramp || size > ramp->size)
  72. return NULL;
  73. /*
  74. * Remove the ramp from the freelist and add it to the allocated list.
  75. */
  76. CIRCLE_QUEUE_DELETE(&rampmap->free, ramp, queue);
  77. CIRCLE_QUEUE_INSERT_ROOT(&rampmap->allocated, RLDDIRamp, ramp, queue);
  78. ramp->free = FALSE;
  79. /*
  80. * If the size is right, return it.
  81. */
  82. if (size == ramp->size)
  83. return ramp;
  84. /*
  85. * Otherwise create a new ramp from the unneeded tail of this one and
  86. * throw it back into the freelist.
  87. */
  88. newramp = NewRamp(ramp->base + size, ramp->size - size);
  89. ramp->size = size;
  90. RLDDIRampmapFree(rampmap, newramp);
  91. return ramp;
  92. }
  93. void RLDDIRampmapFree(RLDDIRampmap* rampmap, RLDDIRamp* ramp)
  94. {
  95. RLDDIRamp* free;
  96. if (!rampmap || !ramp)
  97. return ;
  98. DDASSERT(!ramp->free);
  99. ramp->free = TRUE;
  100. if (CIRCLE_QUEUE_NEXT(&rampmap->free,ramp,queue))
  101. {
  102. CIRCLE_QUEUE_DELETE(&rampmap->allocated, ramp, queue);
  103. }
  104. for (free = CIRCLE_QUEUE_FIRST(&rampmap->free); free;
  105. free = CIRCLE_QUEUE_NEXT(&rampmap->free,free,queue))
  106. {
  107. if (free->size > ramp->size)
  108. {
  109. /*
  110. * Add this ramp before the current one.
  111. */
  112. CIRCLE_QUEUE_INSERT_PREVIOUS(&rampmap->free, free, ramp, queue);
  113. return;
  114. }
  115. }
  116. /*
  117. * Must be the smallest so far, so add it to the end.
  118. */
  119. CIRCLE_QUEUE_INSERT_END(&rampmap->free, RLDDIRamp, ramp, queue);
  120. }
  121. static RLDDIRamp* NewRamp(int base, int size)
  122. {
  123. RLDDIRamp* ramp;
  124. if (D3DMalloc((void**) &ramp, sizeof(RLDDIRamp)))
  125. return NULL;
  126. CIRCLE_QUEUE_INITIALIZE_MEMBER(ramp,queue);
  127. ramp->base = base;
  128. ramp->size = size;
  129. ramp->free = FALSE;
  130. return ramp;
  131. }