Memory.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #region MIT License
  2. /*Copyright (c) 2012 Robert Rouhani <robert.rouhani@gmail.com>
  3. SharpFont based on Tao.FreeType, Copyright (c) 2003-2007 Tao Framework Team
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do
  9. so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.*/
  19. #endregion
  20. using System;
  21. using System.Runtime.InteropServices;
  22. using SharpFont.Internal;
  23. namespace SharpFont
  24. {
  25. /// <summary>
  26. /// A function used to allocate ‘size’ bytes from ‘memory’.
  27. /// </summary>
  28. /// <param name="memory">A handle to the source memory manager.</param>
  29. /// <param name="size">The size in bytes to allocate.</param>
  30. /// <returns>Address of new memory block. 0 in case of failure.</returns>
  31. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  32. public delegate IntPtr AllocFunc([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MemoryMarshaler))] Memory memory, IntPtr size);
  33. /// <summary>
  34. /// A function used to release a given block of memory.
  35. /// </summary>
  36. /// <param name="memory">A handle to the source memory manager.</param>
  37. /// <param name="block">The address of the target memory block.</param>
  38. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  39. public delegate void FreeFunc([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MemoryMarshaler))] Memory memory, IntPtr block);
  40. /// <summary>
  41. /// A function used to re-allocate a given block of memory.
  42. /// </summary>
  43. /// <remarks>
  44. /// In case of error, the old block must still be available.
  45. /// </remarks>
  46. /// <param name="memory">A handle to the source memory manager.</param>
  47. /// <param name="currentSize">The block's current size in bytes.</param>
  48. /// <param name="newSize">The block's requested new size.</param>
  49. /// <param name="block">The block's current address.</param>
  50. /// <returns>New block address. 0 in case of memory shortage.</returns>
  51. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  52. public delegate IntPtr ReallocFunc([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MemoryMarshaler))] Memory memory, IntPtr currentSize, IntPtr newSize, IntPtr block);
  53. /// <summary>
  54. /// A structure used to describe a given memory manager to FreeType 2.
  55. /// </summary>
  56. public class Memory
  57. {
  58. #region Fields
  59. private IntPtr reference;
  60. private MemoryRec rec;
  61. #endregion
  62. #region Constructors
  63. internal Memory(IntPtr reference)
  64. {
  65. Reference = reference;
  66. }
  67. internal Memory(IntPtr reference, IntPtr offset)
  68. : this(new IntPtr(reference.ToInt64() + offset.ToInt64()))
  69. {
  70. }
  71. #endregion
  72. #region Properties
  73. /// <summary>
  74. /// Gets a generic typeless pointer for user data.
  75. /// </summary>
  76. public IntPtr User
  77. {
  78. get
  79. {
  80. return rec.user;
  81. }
  82. }
  83. /// <summary>
  84. /// Gets a pointer type to an allocation function.
  85. /// </summary>
  86. public AllocFunc Allocate
  87. {
  88. get
  89. {
  90. return rec.alloc;
  91. }
  92. }
  93. /// <summary>
  94. /// Gets a pointer type to an memory freeing function.
  95. /// </summary>
  96. public FreeFunc Free
  97. {
  98. get
  99. {
  100. return rec.free;
  101. }
  102. }
  103. /// <summary>
  104. /// Gets a pointer type to a reallocation function.
  105. /// </summary>
  106. public ReallocFunc Reallocate
  107. {
  108. get
  109. {
  110. return rec.realloc;
  111. }
  112. }
  113. internal IntPtr Reference
  114. {
  115. get
  116. {
  117. return reference;
  118. }
  119. set
  120. {
  121. reference = value;
  122. rec = PInvokeHelper.PtrToStructure<MemoryRec>(reference);
  123. }
  124. }
  125. #endregion
  126. }
  127. }