y_malloc_entry.inc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #if defined _INC_y_malloc
  2. #endinput
  3. #endif
  4. #define _INC_y_malloc
  5. /*
  6. Legal:
  7. Version: MPL 1.1
  8. The contents of this file are subject to the Mozilla Public License Version
  9. 1.1 the "License"; you may not use this file except in compliance with
  10. the License. You may obtain a copy of the License at
  11. http://www.mozilla.org/MPL/
  12. Software distributed under the License is distributed on an "AS IS" basis,
  13. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. for the specific language governing rights and limitations under the
  15. License.
  16. The Original Code is the YSI framework.
  17. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  18. Portions created by the Initial Developer are Copyright C 2011
  19. the Initial Developer. All Rights Reserved.
  20. Contributors:
  21. Y_Less
  22. koolk
  23. JoeBullet/Google63
  24. g_aSlice/Slice
  25. Misiur
  26. samphunter
  27. tianmeta
  28. maddinat0r
  29. spacemud
  30. Crayder
  31. Dayvison
  32. Ahmad45123
  33. Zeex
  34. irinel1996
  35. Yiin-
  36. Chaprnks
  37. Konstantinos
  38. Masterchen09
  39. Southclaws
  40. PatchwerkQWER
  41. m0k1
  42. paulommu
  43. udan111
  44. Thanks:
  45. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  46. ZeeX - Very productive conversations.
  47. koolk - IsPlayerinAreaEx code.
  48. TheAlpha - Danish translation.
  49. breadfish - German translation.
  50. Fireburn - Dutch translation.
  51. yom - French translation.
  52. 50p - Polish translation.
  53. Zamaroht - Spanish translation.
  54. Los - Portuguese translation.
  55. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  56. me to strive to better.
  57. Pixels^ - Running XScripters where the idea was born.
  58. Matite - Pestering me to release it and using it.
  59. Very special thanks to:
  60. Thiadmer - PAWN, whose limits continue to amaze me!
  61. Kye/Kalcor - SA:MP.
  62. SA:MP Team past, present and future - SA:MP.
  63. Optional plugins:
  64. Gamer_Z - GPS.
  65. Incognito - Streamer.
  66. Me - sscanf2, fixes2, Whirlpool.
  67. */
  68. #include "..\..\YSI_Core\y_utils"
  69. #include "..\..\YSI_Core\y_core\y_thirdpartyinclude"
  70. #if defined YSI_MALLOC_SECURE
  71. #error YSI_MALLOC_SECURE has been removed.
  72. #endif
  73. #define GB_TO_MB(%0) ((%0) * 1024)
  74. #define MB_TO_KB(%0) ((%0) * 1024)
  75. #define GB_TO_KB(%0) ((%0) * 1024 * 1024)
  76. #define KB_TO_BYTE(%0) ((%0) * 1024)
  77. #define MB_TO_BYTE(%0) ((%0) * 1024 * 1024)
  78. #define GB_TO_BYTE(%0) ((%0) * 1024 * 1024 * 1024)
  79. #define BYTE_TO_CELL(%0) ((%0) * 8 / cellbits)
  80. #define KB_TO_CELL(%0) BYTE_TO_CELL(KB_TO_BYTE(%0))
  81. #define MB_TO_CELL(%0) BYTE_TO_CELL(MB_TO_BYTE(%0))
  82. #define GB_TO_CELL(%0) BYTE_TO_CELL(GB_TO_BYTE(%0))
  83. #define NO_ALLOC (Alloc:0)
  84. #if !defined MALLOC_MEMORY
  85. #if defined MALLOC_MEMORY_GB
  86. #define MALLOC_MEMORY GB_TO_CELL(MALLOC_MEMORY_GB)
  87. #elseif defined MALLOC_MEMORY_MB
  88. #define MALLOC_MEMORY MB_TO_CELL(MALLOC_MEMORY_MB)
  89. #elseif defined MALLOC_MEMORY_KB
  90. #define MALLOC_MEMORY KB_TO_CELL(MALLOC_MEMORY_KB)
  91. #elseif defined MALLOC_MEMORY_B
  92. #define MALLOC_MEMORY BYTE_TO_CELL(MALLOC_MEMORY_B)
  93. #else
  94. #define MALLOC_MEMORY MB_TO_CELL(16)
  95. #endif
  96. #endif
  97. CHAIN_HOOK(Malloc)
  98. #undef CHAIN_ORDER
  99. #define CHAIN_ORDER CHAIN_NEXT(Malloc)
  100. #define MALLOC_FORWARD:%0(%1); \
  101. forward Malloc_%0(%1); \
  102. public Malloc_%0(%1) <_ALS : _ALS_x0, _ALS : _ALS_x1> { return 1; } \
  103. public Malloc_%0(%1) <> { return 1; }
  104. #if defined YSI_NO_HEAP_MALLOC
  105. #if defined DYNAMIC_MEMORY
  106. #pragma dynamic DYNAMIC_MEMORY
  107. #define dynamic dynamic_is_now_DYNAMIC_MEMORY_ // See below...
  108. #endif
  109. #else
  110. // Allocate extra memory for the normal stack and heap (64k, 16 times the
  111. // size of the default stack)!
  112. #if !defined DYNAMIC_MEMORY
  113. #define DYNAMIC_MEMORY (65536)
  114. #endif
  115. #pragma dynamic MALLOC_MEMORY + DYNAMIC_MEMORY
  116. // ====================================================================== //
  117. // //
  118. // IMPORTANT! //
  119. // //
  120. // ====================================================================== //
  121. #define dynamic dynamic_is_now_DYNAMIC_MEMORY_ //
  122. // ====================================================================== //
  123. // //
  124. // dynamic_is_now_DYNAMIC_MEMORY_ //
  125. // //
  126. // If you get a warning or error about "dynamic_is_now_DYNAMIC_MEMORY_", //
  127. // hopefully (though doubtfully) you searched for something related to it //
  128. // and found this. If you are slightly confused, just remember that any //
  129. // reference to that variable is actually a reference to a variable //
  130. // called "dynamic", and so the error/warning refers to that. //
  131. // //
  132. // _is_now_DYNAMIC_MEMORY_ //
  133. // //
  134. // If you get an error or warning about "_is_now_DYNAMIC_MEMORY_", it is //
  135. // because you have tried to use "#pragma dynamic <number>" after //
  136. // including "y_malloc" (or another library that eventually includes //
  137. // "y_malloc"). Because that library allocates memory from the heap, it //
  138. // needs control over how much memory is in the heap to begin with. You //
  139. // do this with "#pragma dynamic <number>", which that library does. //
  140. // Because using that a second time somewhere in your mode will break //
  141. // "y_malloc", it disables the pragma, and instead outputs that error. //
  142. // If you need to increase your stack or heap size (due to a stack/heap //
  143. // collision), add this to the very top of your mode: //
  144. // //
  145. // #define DYNAMIC_MEMORY <number> //
  146. // //
  147. // ====================================================================== //
  148. #endif
  149. // Sort of "module local" variables.
  150. stock
  151. __YSI_g_sHeapStart = 0,
  152. __YSI_g_sUnusedStart = 0;
  153. #define YSI_g_sHeapStart __YSI_g_sHeapStart
  154. #define YSI_g_sUnusedStart __YSI_g_sUnusedStart
  155. forward Alloc:Malloc_Allocate(size, const bool:clear = false);
  156. forward Alloc:calloc(size);
  157. #if defined YSI_NO_HEAP_MALLOC
  158. new
  159. YSI_gMallocMemory[MALLOC_MEMORY];
  160. public OnCodeInit()
  161. {
  162. YSI_g_sHeapStart = 0;
  163. YSI_gMallocMemory[0] = MALLOC_MEMORY - 1;
  164. YSI_g_sUnusedStart = 1;
  165. #if defined Malloc_OnCodeInit
  166. Malloc_OnCodeInit();
  167. #endif
  168. return 1;
  169. }
  170. #undef OnCodeInit
  171. #define OnCodeInit Malloc_OnCodeInit
  172. #if defined Malloc_OnCodeInit
  173. forward Malloc_OnCodeInit();
  174. #endif
  175. #else
  176. new
  177. YSI_gMallocMemory[1];
  178. #if !defined _ALS_OnJITCompile
  179. forward OnJITCompile();
  180. #endif
  181. public OnJITCompile()
  182. {
  183. P:E("y_malloc with JIT requires \"#define YSI_NO_HEAP_MALLOC\"");
  184. // Disable the JIT.
  185. Malloc_OnJITCompile();
  186. return 0;
  187. }
  188. CHAIN_FORWARD:Malloc_OnJITCompile() = 1;
  189. #if defined _ALS_OnJITCompile
  190. #undef OnJITCompile
  191. #else
  192. #define _ALS_OnJITCompile
  193. #endif
  194. #define OnJITCompile(%0) CHAIN_PUBLIC:Malloc_OnJITCompile(%0)
  195. // Allocate space on the heap permanently.
  196. #include "y_malloc_warning"
  197. #include "y_malloc_heapalloc"
  198. #endif
  199. // Functions to access the data on the heap.
  200. #include "y_malloc_funcs"
  201. #if defined YSI_TESTS
  202. #include "y_malloc_tests"
  203. #endif
  204. #undef YSI_g_sHeapStart
  205. #undef YSI_g_sUnusedStart