y_malloc.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "..\amx\opcode"
  128. #include "..\amx\stack_trace"
  129. #define MALLOC_KB_TO_CELL ((1024 * 8) / cellbits)
  130. #define NO_ALLOC (Alloc:0)
  131. #if !defined MALLOC_MEMORY
  132. #if defined MALLOC_MEMORY_KB
  133. #define MALLOC_MEMORY ((MALLOC_MEMORY_KB) * MALLOC_KB_TO_CELL)
  134. #else
  135. #if defined MALLOC_MEMORY_MB
  136. #define MALLOC_MEMORY ((MALLOC_MEMORY_MB) * 1024 * MALLOC_KB_TO_CELL)
  137. #else
  138. #if defined MALLOC_MEMORY_B
  139. #define MALLOC_MEMORY (((MALLOC_MEMORY_B) * 8) / cellbits)
  140. #else
  141. #define MALLOC_MEMORY (16 * 1024 * MALLOC_KB_TO_CELL)
  142. #endif
  143. #endif
  144. #endif
  145. #endif
  146. // Allocate extra memory for the normal stack and heap (64k, 16 times the size
  147. // of the default stack)!
  148. #pragma dynamic MALLOC_MEMORY + 65536
  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. new
  156. YSI_gMallocMemory[1];
  157. forward Alloc:Malloc_Allocate(size, const bool:clear = true);
  158. forward Alloc:calloc(size);
  159. // Allocate space on the heap permanently.
  160. #include "y_malloc/heapalloc"
  161. // Functions to access the data on the heap.
  162. #include "y_malloc/funcs"
  163. #if defined YSI_TESTS
  164. #include "..\YSI_Core\y_testing"
  165. #include "y_malloc/tests"
  166. #endif
  167. #undef YSI_g_sHeapStart
  168. #undef YSI_g_sUnusedStart