tests.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. Test:y_malloc_100()
  115. {
  116. new
  117. Alloc:a = malloc(1000);
  118. ASSERT(bool:a);
  119. free(a);
  120. }
  121. Test:y_malloc_2_100()
  122. {
  123. new
  124. Alloc:a = malloc(100);
  125. ASSERT(bool:a);
  126. free(a);
  127. new
  128. Alloc:b = malloc(100);
  129. ASSERT(bool:b);
  130. free(a);
  131. ASSERT(a == b);
  132. }
  133. Test:y_malloc_2000()
  134. {
  135. new
  136. Alloc:a = malloc(2000);
  137. ASSERT(bool:a);
  138. free(a);
  139. }
  140. Test:y_malloc_clear()
  141. {
  142. new Alloc:mem[3];
  143. mem[0] = malloc(2);
  144. mem[1] = malloc(1);
  145. free(mem[0]);
  146. mem[2] = malloc(1);
  147. free(mem[1]);
  148. free(mem[2]);
  149. mem[0] = malloc(30);
  150. ASSERT(bool:mem[0]);
  151. free(mem[0]);
  152. }
  153. Test:y_malloc_Loads()
  154. {
  155. // Allocate slightly more memory than we have room for.
  156. new
  157. Alloc:a = malloc(MALLOC_MEMORY + 1);
  158. ASSERT(a == NO_ALLOC);
  159. ASSERT(!free(a));
  160. }
  161. #define MALLOC_TEST(%0) for (new Alloc:a = malloc(%0), __loop = 1; a && (__loop || !free(a)); --__loop)
  162. Test:y_malloc_mset()
  163. {
  164. MALLOC_TEST(10)
  165. {
  166. ASSERT(mget(a, 5) == 0);
  167. mset(a, 5, 42);
  168. ASSERT(mget(a, 5) == 42);
  169. }
  170. }
  171. Test:y_malloc_mget()
  172. {
  173. MALLOC_TEST(10)
  174. {
  175. mset(a, 5, 11);
  176. ASSERT(mget(a, 5) == 11);
  177. mset(a, 5, 42);
  178. ASSERT(mget(a, 4) == 0);
  179. ASSERT(mget(a, 6) == 0);
  180. }
  181. }
  182. Test:y_malloc_SlotSize()
  183. {
  184. MALLOC_TEST(10)
  185. {
  186. ASSERT(Malloc_SlotSize(a) == 10);
  187. }
  188. MALLOC_TEST(100)
  189. {
  190. ASSERT(Malloc_SlotSize(a) == 100);
  191. }
  192. MALLOC_TEST(1000)
  193. {
  194. ASSERT(Malloc_SlotSize(a) == 1000);
  195. }
  196. MALLOC_TEST(1)
  197. {
  198. ASSERT(Malloc_SlotSize(a) == 1);
  199. }
  200. }
  201. Test:y_malloc_msets()
  202. {
  203. static const
  204. str0[] = "1111 ",
  205. str1[] = !"2222 ";
  206. MALLOC_TEST(10)
  207. {
  208. ASSERT(msets(a, 0, str0, false) == 8);
  209. ASSERT(mget(a, 0) == str0[0]);
  210. ASSERT(msets(a, 0, str1, false) == 8);
  211. ASSERT(mget(a, 0) == str1{0});
  212. ASSERT(msets(a, 0, str0, true) == 8 char);
  213. ASSERT(mget(a, 0) == ('1' << 24) | ('1' << 16) | ('1' << 08) | ('1' << 00));
  214. ASSERT(msets(a, 0, str1, true) == 8 char);
  215. ASSERT(mget(a, 0) == str1[0]);
  216. }
  217. }
  218. Test:y_malloc_mgets()
  219. {
  220. static const
  221. str0[] = "1111 ";
  222. new
  223. str2[20];
  224. MALLOC_TEST(10)
  225. {
  226. msets(a, 0, str0, false);
  227. mgets(str2, sizeof (str2), a, 0, false);
  228. ASSERT(str2[0] == '1');
  229. msets(a, 0, str0, false);
  230. mgets(str2, sizeof (str2), a, 0, true);
  231. ASSERT(str2{0} == '1');
  232. msets(a, 0, str0, true);
  233. mgets(str2, sizeof (str2), a, 0, false);
  234. ASSERT(str2[0] == '1');
  235. msets(a, 0, str0, true);
  236. mgets(str2, sizeof (str2), a, 0, true);
  237. ASSERT(str2{0} == '1');
  238. }
  239. }
  240. /*
  241. Test:y_malloc_()
  242. {
  243. MALLOC_TEST(10)
  244. {
  245. }
  246. }
  247. Test:y_malloc_()
  248. {
  249. MALLOC_TEST(10)
  250. {
  251. }
  252. }
  253. Test:y_malloc_()
  254. {
  255. MALLOC_TEST(10)
  256. {
  257. }
  258. }
  259. Test:y_malloc_()
  260. {
  261. MALLOC_TEST(10)
  262. {
  263. }
  264. }
  265. */
  266. Test:y_malloc_NewS_0()
  267. {
  268. new
  269. Alloc:a = Malloc_NewS("Hello There", false);
  270. ASSERT(bool:a);
  271. if (a)
  272. {
  273. ASSERT(mget(a, 0) == 'H');
  274. ASSERT(mget(a, 5) == ' ');
  275. ASSERT(mget(a, 11) == '\0');
  276. free(a);
  277. }
  278. }
  279. Test:y_malloc_NewS_1()
  280. {
  281. new
  282. Alloc:a = Malloc_NewS("Hello There", true);
  283. ASSERT(bool:a);
  284. if (a)
  285. {
  286. static const
  287. b[12 char] = !"Hello There";
  288. ASSERT(mget(a, 0) == b[0]);
  289. ASSERT(mget(a, 1) == b[1]);
  290. ASSERT(mget(a, 2) == b[2]);
  291. free(a);
  292. }
  293. }