y_malloc.inc 9.3 KB

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