y_malloc.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #if defined _INC_y_malloc
  2. #endinput
  3. #endif
  4. #define _INC_y_malloc
  5. /**
  6. * <library name="y_malloc">
  7. * <section>
  8. * Description
  9. * </section>
  10. * Functions for using malloc/calloc/free type functions in PAWN.
  11. * <section>
  12. * Version
  13. * </section>
  14. * 0.1
  15. * <section>
  16. * Functions
  17. * </section>
  18. * <subsection>
  19. * Stock
  20. * </subsection><ul>
  21. * <symbol name="malloc">Allocate a block of memory (may be inline).</symbol>
  22. * <symbol name="calloc">Allocate a block of memory and blank.</symbol>
  23. * <symbol name="free">Free an allocated block of memory (may be inline).</symbol>
  24. * <symbol name="Malloc_Set">Set a value in an allocated array (may be inline).</symbol>
  25. * <symbol name="Malloc_Get">Get a value in an allocated array (may be inline).</symbol>
  26. * <symbol name="Malloc_SetS">Set a string in an allocated array.</symbol>
  27. * <symbol name="Malloc_GetS">Get a string in an allocated array.</symbol>
  28. * <symbol name="Malloc_Allocate">Do the memory allocation (may be static).</symbol>
  29. * <symbol name="Malloc_Free">Do the memory freeing (may be static).</symbol>
  30. * <symbol name="Malloc_SlotSize">Get the size of an allocated block (may be inline).</symbol>
  31. * <symbol name="Malloc_NewS">Allocate for and store a given string.</symbol>
  32. * </ul><subsection>
  33. * Static
  34. * </subsection><ul>
  35. * <symbol name="Malloc_Allocate">Do the memory allocation (may be stock).</symbol>
  36. * <symbol name="Malloc_Free">Do the memory freeing (may be stock).</symbol>
  37. * </ul><subsection>
  38. * Inline
  39. * </subsection><ul>
  40. * <symbol name="mget">Get data from an allocation unit.</symbol>
  41. * <symbol name="mset">Set data in an allocation unit.</symbol>
  42. * <symbol name="mgets">Get a string from an allocation unit.</symbol>
  43. * <symbol name="msets">Set a string in an allocation unit.</symbol>
  44. * <symbol name="malloc">Allocate a block of memory (may be stock).</symbol>
  45. * <symbol name="free">Free an allocated block of memory (may be stock).</symbol>
  46. * <symbol name="Malloc_Set">Set a value in an allocated array (may be stock).</symbol>
  47. * <symbol name="Malloc_Get">Get a value in an allocated array (may be stock).</symbol>
  48. * <symbol name="Malloc_NextSlot">Get the next free data block.</symbol>
  49. * <symbol name="Malloc_GetSlotSize">Get the size of a slot.</symbol>
  50. * <symbol name="Malloc_SetSlotSize">Set the size of a block.</symbol>
  51. * <symbol name="Malloc_GetData">Direct data access getter.</symbol>
  52. * <symbol name="Malloc_SetData">Direct data access setter.</symbol>
  53. * <symbol name="Malloc_SlotSize">Get the size of an allocated block (may be stock).</symbol>
  54. * </ul><section>
  55. * Definitions
  56. * </section><ul>
  57. * <symbol name="MALLOC_KB_TO_CELL">Multiplication value to convert kb to cells.</symbol>
  58. * <symbol name="NO_ALLOC">A failed allocation (NULL, but YSI already has NULL).</symbol>
  59. * </ul><section>
  60. * Tags
  61. * </section><ul>
  62. * <symbol name="Alloc">An allocated block handle variable.</symbol>
  63. * </ul><section>
  64. * Variables
  65. * </section>
  66. * <subsection>
  67. * Global
  68. * </subsection><ul>
  69. * <symbol name="YSI_gMallocMemory">Stores the data (may be static).</symbol>
  70. * </ul><subsection>
  71. * Static
  72. * </subsection><ul>
  73. * <symbol name="YSI_gMallocMemory">Stores the data (may be global).</symbol>
  74. * <symbol name="_YSI_g_sUnusedStart">Start of free memory.</symbol>
  75. * </ul><section>Compile options</section><ul>
  76. * <symbol name="MALLOC_MEMORY">Number of cells to reserve.</symbol>
  77. * <symbol name="MALLOC_MEMORY_KB">Number of killobytes to reserve.</symbol>
  78. * <symbol name="MALLOC_MEMORY_B">Number of bytes to reserve.</symbol>
  79. * <symbol name="MALLOC_MEMORY_MB">Number of megabytes to reserve.</symbol>
  80. * <symbol name="YSI_MALLOC_SECURE">Use enhanced bounds checking.</symbol>
  81. * <symbol name="YSI_MALLOC_NO_SHORT">Avoid conflicts with mget/mset.</symbol>
  82. * </ul>
  83. * </library>
  84. *//** *//*
  85. Legal:
  86. Version: MPL 1.1
  87. The contents of this file are subject to the Mozilla Public License Version
  88. 1.1 the "License"; you may not use this file except in compliance with
  89. the License. You may obtain a copy of the License at
  90. http://www.mozilla.org/MPL/
  91. Software distributed under the License is distributed on an "AS IS" basis,
  92. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  93. for the specific language governing rights and limitations under the
  94. License.
  95. The Original Code is the YSI framework.
  96. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  97. Portions created by the Initial Developer are Copyright C 2011
  98. the Initial Developer. All Rights Reserved.
  99. Contributors:
  100. Y_Less
  101. koolk
  102. JoeBullet/Google63
  103. g_aSlice/Slice
  104. Misiur
  105. samphunter
  106. tianmeta
  107. maddinat0r
  108. spacemud
  109. Crayder
  110. Dayvison
  111. Ahmad45123
  112. Zeex
  113. irinel1996
  114. Yiin-
  115. Chaprnks
  116. Konstantinos
  117. Masterchen09
  118. Southclaws
  119. PatchwerkQWER
  120. m0k1
  121. paulommu
  122. udan111
  123. Thanks:
  124. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  125. ZeeX - Very productive conversations.
  126. koolk - IsPlayerinAreaEx code.
  127. TheAlpha - Danish translation.
  128. breadfish - German translation.
  129. Fireburn - Dutch translation.
  130. yom - French translation.
  131. 50p - Polish translation.
  132. Zamaroht - Spanish translation.
  133. Los - Portuguese translation.
  134. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  135. me to strive to better.
  136. Pixels^ - Running XScripters where the idea was born.
  137. Matite - Pestering me to release it and using it.
  138. Very special thanks to:
  139. Thiadmer - PAWN, whose limits continue to amaze me!
  140. Kye/Kalcor - SA:MP.
  141. SA:MP Team past, present and future - SA:MP.
  142. Optional plugins:
  143. Gamer_Z - GPS.
  144. Incognito - Streamer.
  145. Me - sscanf2, fixes2, Whirlpool.
  146. */
  147. #include "..\YSI_Internal\y_compilerdata"
  148. #if defined YSI_MALLOC_SECURE
  149. #error YSI_MALLOC_SECURE has been removed.
  150. #endif
  151. #include "..\YSI_Internal\y_version"
  152. #include "..\YSI_Internal\y_funcinc"
  153. #include "..\YSI_Core\y_debug"
  154. #include "..\YSI_Storage\y_amx"
  155. #include "..\YSI_Core\y_als"
  156. #include "..\YSI_Core\y_utils"
  157. #include "..\YSI_Internal\y_thirdpartyinclude"
  158. #define GB_TO_MB(%0) ((%0) * 1024)
  159. #define MB_TO_KB(%0) ((%0) * 1024)
  160. #define GB_TO_KB(%0) ((%0) * 1024 * 1024)
  161. #define KB_TO_BYTE(%0) ((%0) * 1024)
  162. #define MB_TO_BYTE(%0) ((%0) * 1024 * 1024)
  163. #define GB_TO_BYTE(%0) ((%0) * 1024 * 1024 * 1024)
  164. #define BYTE_TO_CELL(%0) ((%0) * 8 / cellbits)
  165. #define KB_TO_CELL(%0) BYTE_TO_CELL(KB_TO_BYTE(%0))
  166. #define MB_TO_CELL(%0) BYTE_TO_CELL(MB_TO_BYTE(%0))
  167. #define GB_TO_CELL(%0) BYTE_TO_CELL(GB_TO_BYTE(%0))
  168. #define NO_ALLOC (Alloc:0)
  169. #if !defined MALLOC_MEMORY
  170. #if defined MALLOC_MEMORY_GB
  171. #define MALLOC_MEMORY GB_TO_CELL(MALLOC_MEMORY_GB)
  172. #elseif defined MALLOC_MEMORY_MB
  173. #define MALLOC_MEMORY MB_TO_CELL(MALLOC_MEMORY_MB)
  174. #elseif defined MALLOC_MEMORY_KB
  175. #define MALLOC_MEMORY KB_TO_CELL(MALLOC_MEMORY_KB)
  176. #elseif defined MALLOC_MEMORY_B
  177. #define MALLOC_MEMORY BYTE_TO_CELL(MALLOC_MEMORY_B)
  178. #else
  179. #define MALLOC_MEMORY MB_TO_CELL(16)
  180. #endif
  181. #endif
  182. #if defined YSI_NO_HEAP_MALLOC
  183. #if defined DYNAMIC_MEMORY
  184. #pragma dynamic DYNAMIC_MEMORY
  185. #define dynamic dynamic_is_now_DYNAMIC_MEMORY_ // See below...
  186. #endif
  187. #else
  188. // Allocate extra memory for the normal stack and heap (64k, 16 times the
  189. // size of the default stack)!
  190. #if !defined DYNAMIC_MEMORY
  191. #define DYNAMIC_MEMORY (65536)
  192. #endif
  193. #pragma dynamic MALLOC_MEMORY + DYNAMIC_MEMORY
  194. // ====================================================================== //
  195. // //
  196. // IMPORTANT! //
  197. // //
  198. // ====================================================================== //
  199. #define dynamic dynamic_is_now_DYNAMIC_MEMORY_ //
  200. // ====================================================================== //
  201. // //
  202. // dynamic_is_now_DYNAMIC_MEMORY_ //
  203. // //
  204. // If you get a warning or error about "dynamic_is_now_DYNAMIC_MEMORY_", //
  205. // hopefully (though doubtfully) you searched for something related to it //
  206. // and found this. If you are slightly confused, just remember that any //
  207. // reference to that variable is actually a reference to a variable //
  208. // called "dynamic", and so the error/warning refers to that. //
  209. // //
  210. // _is_now_DYNAMIC_MEMORY_ //
  211. // //
  212. // If you get an error or warning about "_is_now_DYNAMIC_MEMORY_", it is //
  213. // because you have tried to use "#pragma dynamic <number>" after //
  214. // including "y_malloc" (or another library that eventually includes //
  215. // "y_malloc"). Because that library allocates memory from the heap, it //
  216. // needs control over how much memory is in the heap to begin with. You //
  217. // do this with "#pragma dynamic <number>", which that library does. //
  218. // Because using that a second time somewhere in your mode will break //
  219. // "y_malloc", it disables the pragma, and instead outputs that error. //
  220. // If you need to increase your stack or heap size (due to a stack/heap //
  221. // collision), add this to the very top of your mode: //
  222. // //
  223. // #define DYNAMIC_MEMORY <number> //
  224. // //
  225. // ====================================================================== //
  226. #endif
  227. // Sort of "module local" variables.
  228. stock
  229. __YSI_g_sHeapStart = 0,
  230. __YSI_g_sUnusedStart = 0;
  231. #define YSI_g_sHeapStart __YSI_g_sHeapStart
  232. #define YSI_g_sUnusedStart __YSI_g_sUnusedStart
  233. forward Alloc:Malloc_Allocate(size, const bool:clear = true);
  234. forward Alloc:calloc(size);
  235. #if defined YSI_NO_HEAP_MALLOC
  236. new
  237. YSI_gMallocMemory[MALLOC_MEMORY];
  238. public OnScriptInit()
  239. {
  240. YSI_g_sHeapStart = 0;
  241. YSI_gMallocMemory[0] = MALLOC_MEMORY - 1;
  242. YSI_g_sUnusedStart = 1;
  243. memset(YSI_gMallocMemory[1], 0, MALLOC_MEMORY - 1);
  244. #if defined Malloc_OnScriptInit
  245. Malloc_OnScriptInit();
  246. #endif
  247. return 1;
  248. }
  249. #undef OnScriptInit
  250. #define OnScriptInit Malloc_OnScriptInit
  251. #if defined Malloc_OnScriptInit
  252. forward Malloc_OnScriptInit();
  253. #endif
  254. #else
  255. new
  256. YSI_gMallocMemory[1];
  257. // Allocate space on the heap permanently.
  258. #include "y_malloc/heapalloc"
  259. #endif
  260. // Functions to access the data on the heap.
  261. #include "y_malloc/funcs"
  262. #if defined YSI_TESTS
  263. #include "..\YSI_Core\y_testing"
  264. #include "y_malloc/tests"
  265. #endif
  266. #undef YSI_g_sHeapStart
  267. #undef YSI_g_sUnusedStart