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.

153 lines
3.3 KiB

  1. /*==========================================================================;
  2. *
  3. * Copyright (C) 1995 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: d3dmem.c
  6. * Content: Direct3D mem allocation
  7. *@@BEGIN_MSINTERNAL
  8. *
  9. * History:
  10. * Date By Reason
  11. * ==== == ======
  12. * 10/12/95 stevela Initial rev with this header.
  13. *@@END_MSINTERNAL
  14. *
  15. ***************************************************************************/
  16. #include "pch.cpp"
  17. #pragma hdrstop
  18. static D3DMALLOCFUNCTION malloc_function = (D3DMALLOCFUNCTION) MemAlloc;
  19. static D3DFREEFUNCTION free_function = MemFree;
  20. #ifdef __USEGLOBALNEWANDDELETE
  21. //----------------------------------------------------------------------------
  22. //
  23. // Global new and delete
  24. //
  25. //----------------------------------------------------------------------------
  26. void* operator new(size_t s)
  27. {
  28. void *p;
  29. MallocAligned(&p,s);
  30. return p;
  31. };
  32. void operator delete(void* p)
  33. {
  34. FreeAligned(p);
  35. };
  36. #endif
  37. #undef DPF_MODNAME
  38. #define DPF_MODNAME "D3DMalloc"
  39. HRESULT D3DAPI D3DMalloc(LPVOID* p_return, size_t size)
  40. {
  41. void* p;
  42. if (!VALID_OUTPTR(p_return))
  43. {
  44. DPF_ERR("Bad pointer given. Memory allocation fails");
  45. return D3DERR_INVALIDCALL;
  46. }
  47. if (size > 0)
  48. {
  49. p = malloc_function(size);
  50. if (p == NULL)
  51. return (E_OUTOFMEMORY);
  52. }
  53. else
  54. {
  55. p = NULL;
  56. }
  57. *p_return = p;
  58. return (D3D_OK);
  59. }
  60. #undef DPF_MODNAME
  61. #define DPF_MODNAME "D3DFree"
  62. VOID D3DAPI D3DFree(LPVOID p)
  63. {
  64. if (p == NULL)
  65. return;
  66. if (!VALID_DWORD_PTR(p))
  67. {
  68. DPF_ERR("invalid pointer. Memory Free ignored");
  69. return;
  70. }
  71. if (p)
  72. {
  73. free_function(p);
  74. }
  75. }
  76. #undef DPF_MODNAME
  77. #define DPF_MODNAME "MallocAligned"
  78. #define CACHE_LINE 32
  79. HRESULT MallocAligned(void** p_return, size_t size)
  80. {
  81. char* p;
  82. size_t offset;
  83. HRESULT error;
  84. if (!p_return)
  85. return D3DERR_INVALIDCALL;
  86. if (size > 0)
  87. {
  88. if ((error = D3DMalloc((void**) &p, size + CACHE_LINE)) != S_OK)
  89. {
  90. *p_return = NULL;
  91. return error;
  92. }
  93. offset = (size_t)(CACHE_LINE - ((ULONG_PTR)p & (CACHE_LINE - 1)));
  94. p += offset;
  95. ((size_t*)p)[-1] = offset;
  96. }
  97. else
  98. {
  99. p = NULL;
  100. }
  101. *p_return = p;
  102. return S_OK;
  103. }
  104. #undef DPF_MODNAME
  105. #define DPF_MODNAME "FreeAligned"
  106. void FreeAligned(void* p)
  107. {
  108. if (p)
  109. {
  110. size_t offset = ((size_t*)p)[-1];
  111. p = (void*) ((unsigned char*)p - offset);
  112. D3DFree(p);
  113. }
  114. }
  115. #undef DPF_MODNAME
  116. #define DPF_MODNAME "CAlignedBuffer32::Grow"
  117. //----------------------------------------------------------------------------
  118. // Growing aligned buffer implementation.
  119. //
  120. HRESULT CAlignedBuffer32::Grow(DWORD growSize)
  121. {
  122. if (allocatedBuf)
  123. D3DFree(allocatedBuf);
  124. size = growSize;
  125. if (D3DMalloc(&allocatedBuf, size + 31) != S_OK)
  126. {
  127. allocatedBuf = 0;
  128. alignedBuf = 0;
  129. size = 0;
  130. return E_OUTOFMEMORY;
  131. }
  132. alignedBuf = (LPVOID)(((ULONG_PTR)allocatedBuf + 31 ) & ~31);
  133. return D3D_OK;
  134. }