ZoriaRPG

mem.zh for ZC 2.55 (Possibly Deprecated)

Nov 25th, 2016
89
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //! Array memory management Utilities
  2. // 12th January, 2016
  3.  
  4. const int MALLOC_FREE = 214747.9898;
  5. const int MALLOC_RESERVED = 214747.8989;
  6.  
  7. //marks array indices in range as being freed. Only affects indices that are Malloc'd.
  8. void Free(int arr, int startindex, int lastindex){
  9.     for ( int q = startindex; q < lastindex; q++ ) if ( arr[q] == MALLOC_RESERVED ) arr[q] = MALLOC_FREE;
  10. }
  11.  
  12. //marks array indices in range as being freed.
  13. void ForceFree(int arr, int startindex, int lastindex){
  14.     for ( int q = startindex; q < lastindex; q++ ) if ( arr[q] == MALLOC_RESERVED ) arr[q] = MALLOC_FREE;
  15. }
  16.  
  17. //marks array indices in range as being freed. Will ignore any indices in this range that are not 'ignoreunlessvalue'
  18. void Free(int arr, int startindex, int lastindex, int ignoreunlessvalue){
  19.     for ( int q = startindex; q < lastindex; q++ ) if ( arr[q] == ignoreunlessvalue ) arr[q] = MALLOC_FREE;
  20. }
  21.  
  22. //marks an range of array indices as reserved. Will ignore any indices in this range that match 'ignoreindices'
  23. void FreeExcept(int arr, int startindex, int lastindex, int ignoreindices){
  24.     for ( int q = startindex; q < lastindex; q++ )
  25.     if ( arr[q] != ignoreindices ) arr[q] = MALLOC_FREE;
  26. }
  27.  
  28. //marks a range of array indices as being reserved.
  29. void ForceMalloc(int arr, int startindex, int lastindex){
  30.     for ( int q = startindex; q < lastindex; q++ ) arr[q] = MALLOC_RESERVED;
  31. }
  32.  
  33.  
  34. //marks a range of array indices as reserved. Will ignore any indices in thsi range that aren't free.
  35. void Malloc(int arr, int startindex, int lastindex){
  36.     for ( int q = startindex; q < lastindex; q++ )
  37.     if ( arr[q] == MALLOC_FREE ) arr[q] = MALLOC_RESERVED;
  38. }
  39.  
  40. //marks a range of array indices as reserved. Will ignore any indices in this range that are not 'ignoreunlessvalue'
  41. void Malloc(int arr, int startindex, int lastindex, int ignoreunlessvalue){
  42.     for ( int q = startindex; q < lastindex; q++ )
  43.     if ( arr[q] == ignoreunlessvalue ) arr[q] = MALLOC_RESERVED;
  44. }
  45.  
  46. //marks an range of array indices as reserved. Will ignore any indices in this range that match 'ignoreindices'
  47. void MallocExcept(int arr, int startindex, int lastindex, int ignoreindices){
  48.     for ( int q = startindex; q < lastindex; q++ )
  49.     if ( arr[q] != ignoreindices ) arr[q] = MALLOC_RESERVED;
  50. }
  51.  
  52. //Marks a specific array index as reserved.
  53. void Malloc(int arr, int index){
  54.     arr[index] = MALLOC_RESERVED;
  55. }
  56.  
  57. //Parses an array to find a number of contiginous free indices.
  58. //Returns the lowest index number, of the range that it finds.
  59. //Returns -1 on failure.
  60. int FindFree(int arr, int numbercont) {
  61.     bool free;
  62.     int a;
  63.     for ( int q = SizeOfArray(arr); q > 0; q-- ) {
  64.         if ( arr[q] == MALLOC_FREE ) {
  65.             free = true;
  66.             ++a;
  67.         }
  68.         else if ( arr[q] != MALLOC_FREE ) {
  69.             free = false;
  70.             a = 0;
  71.         }
  72.         if ( a == numbercont && free ) return q;
  73.     }
  74.     return -1;
  75. }
  76.  
  77. //Parses an array to find a number of contiginous free indices.
  78. //Returns the lowest index number, of the range that it finds.
  79. //Returns -1 on failure.
  80. int FindMalloc(int arr, int numbercont) {
  81.     bool malloc;
  82.     int a;
  83.     for ( int q = SizeOfArray(arr); q > 0; q-- ) {
  84.         if ( arr[q] == MALLOC_RESERVED ) {
  85.             malloc = true;
  86.             ++a;
  87.         }
  88.         else if ( arr[q] != MALLOC_RESERVED ) {
  89.             malloc = false;
  90.             a = 0;
  91.         }
  92.         if ( a == numbercont && malloc ) return q;
  93.     }
  94.     return -1;
  95. }
  96.  
  97. //Finds a single free index in an array. Starts with highest numbered index.
  98. int FreeIndex(int arr){
  99.     for ( int q = SizeOfArray(arr); q > 0; q-- ) {
  100.         if ( arr[q] == MALLOC_FREE ) return q;
  101.     }
  102. }
  103.  
  104. //Finds a single free index in an array. Starts with highest numbered index.
  105. int ReservedIndex(int arr){
  106.     for ( int q = SizeOfArray(arr); q > 0; q-- ) {
  107.         if ( arr[q] == MALLOC_RESERVED ) return q;
  108.     }
  109. }
  110.  
  111. //Finds a single reserved index in an array. Starts with highest numbered index.
  112. int MallocIndex(int arr){
  113.     for ( int q = SizeOfArray(arr); q > 0; q-- ) {
  114.         if ( arr[q] == MALLOC_RESERVED ) return q;
  115.     }
  116. }
  117.  
  118. //Finds a single free index in an array. Starts with lowest numbered index.
  119. int FreeIndexLow(int arr){
  120.     for ( int q = 0; q < SizeOfArray(arr); q++ ) {
  121.         if ( arr[q] == MALLOC_FREE ) return q;
  122.     }
  123. }
  124.  
  125. //Finds a single free index in an array. Starts with lowest numbered index.
  126. int ReservedIndexLow(int arr){
  127.     for ( int q = 0; q < SizeOfArray(arr); q++ ) {
  128.         if ( arr[q] == MALLOC_RESERVED ) return q;
  129.     }
  130. }
  131.  
  132. //Finds a single free index in an array. Starts with lowest numbered index.
  133. int MallocIndexLow(int arr){
  134.     for ( int q = 0; q < SizeOfArray(arr); q++ ) {
  135.         if ( arr[q] == MALLOC_RESERVED ) return q;
  136.     }
  137. }
  138.  
  139.  
  140. //Checks if a specific index in an array is free.
  141. bool IsFreeIndex(int arr, int index){
  142.     if ( arr[index] == MALLOC_FREE ) return true;
  143.     return false;
  144. }
  145.  
  146. //Checks if a specific index in an array is free.
  147. bool IsReservedIndex(int arr, int index){
  148.     if ( arr[index] == MALLOC_RESERVED ) return true;
  149.     return false;
  150. }
  151.  
  152. //Checks if a specific index in an array is free.
  153. bool IsMallocIndex(int arr, int index){
  154.     if ( arr[index] == MALLOC_RESERVED ) return true;
  155.     return false;
  156. }
  157.  
  158. //Inits , or Mallocs an entire array.
  159. void MallocArray(int arr){
  160.     for ( int q = 0; q < SizeOfArray(arr); q++ ) arr[q] = MALLOC_RESERVED;
  161. }
  162.  
  163. //Inits , or Mallocs an entire array. Only Malloc's free values.  
  164. void MallocFreeArray(int arr){
  165.     for ( int q = 0; q < SizeOfArray(arr); q++ ) if ( arr[q] == MALLOC_FREE ) arr[q] = MALLOC_RESERVED;
  166. }
  167.  
  168. // Frees an entire array.
  169. void FreeArray(int arr){
  170.     for ( int q = 0; q < SizeOfArray(arr); q++ ) arr[q] = MALLOC_FREE;
  171. }
  172.  
  173. //Frees an entire array. ignores present values.
  174. void FreeArrayForce(int arr){
  175.     for ( int q = 0; q < SizeOfArray(arr); q++ ) arr[q] = MALLOC_FREE;
  176. }
  177.  
  178. // Frees an entire array. Only frees Malloc'd values.
  179. void FreeMallocArray(int arr){
  180.     for ( int q = 0; q < SizeOfArray(arr); q++ ) if ( arr[q] == MALLOC_RESERVED ) arr[q] = MALLOC_FREE;
  181. }
  182.  
  183. //Mallocs an entire array, but only indices that have the value specified by 'onlyvalue'.
  184. void MallocArray(int arr, int onlyvalue) {
  185.     for ( int q = 0; q < SizeOfArray(arr); q++ ) {
  186.         if ( arr[q] == onlyvalue ) arr[q] = MALLOC_RESERVED;
  187.     }
  188. }
  189.  
  190. //Frees an entire array, but only indices that have the value specified by 'onlyvalue'.
  191. void FreeArray(int arr, int onlyvalue) {
  192.     for ( int q = 0; q < SizeOfArray(arr); q++ ) {
  193.         if ( arr[q] == onlyvalue ) arr[q] = MALLOC_FREE;
  194.     }
  195. }
  196.  
  197. void MallocArrayExcept(int arr, int exclude){}
  198.    
  199.    
  200. //Returns the size (indices with a zero value) in a given array.
  201. //int MemSize(int arr){
  202. //  int size;
  203. //  for ( int q = 0; q < SizeOfArray(arr); q++ ) {
  204. //      if ( !arr[q] ) size++;
  205. //  }
  206. //  return size;
  207. //}
  208.  
  209. //Returns the RESERVED size (total number of reserved indices) for a given array.
  210. int MallocSize(int arr){
  211.     int size;
  212.     for ( int q = 0; q < SizeOfArray(arr); q++ ) {
  213.         if ( arr[q] == MALLOC_RESERVED ) size++;
  214.     }
  215.     return size;
  216. }
  217.  
  218. //Returns the FREE size of a given array.
  219. int FreeSize(int arr){
  220.     int size;
  221.     for ( int q = 0; q < SizeOfArray(arr); q++ ) {
  222.         if ( arr[q] == MALLOC_FREE ) size++;
  223.     }
  224.     return size;
  225. }
  226.  
  227. //Returns the RESERVED size (total number of reserved indices) for a given array.
  228. //Only from a range of indices starting with 'rangeStart' and engind with 'rangeStop.
  229. //Both 'rangeStart', and 'rangeStop' are included in this return, if reserved.
  230. int MallocSize(int arr, int rangeStart, int rangeStop){
  231.     int size;
  232.     for ( int q = rangeStart; q <= rangeStop; q++ ) {
  233.         if ( arr[q] == MALLOC_RESERVED ) size++;
  234.     }
  235.     return size;
  236. }
  237.  
  238. //Returns the FREE size of a given array.
  239. //Only from a range of indices starting with 'rangeStart' and engind with 'rangeStop.
  240. //Both 'rangeStart', and 'rangeStop' are included in this return, if free.
  241. int FreeSize(int arr, int rangeStart, int rangeStop){
  242.     int size;
  243.     for ( int q = rangeStart; q <= rangeStop; q++ ) {
  244.         if ( arr[q] == MALLOC_RESERVED ) size++;
  245.     }
  246.     return size;
  247. }
  248.  
  249.  
  250. //Returns the RESERVED size (total number of reserved indices) for a given array.
  251. //Only from a range of indices starting with 'rangeStart' and engind with 'rangeStop.
  252. //Both 'rangeStart', and 'rangeStop' are included in this return, if reserved.
  253. //'Set contiguous true, if you wish to require that free memory be in a contiguous
  254. //block no smaller than a size set by 'span'
  255. //Set 'returnpos' true to return the first index in the range this finds.
  256. //The default return is the size that it discovers.
  257.  
  258. //Returns the size of the largest number of contiguous blocks of reserved mem in a given range.
  259. //Args: 'rangeStart' and 'rangeStop' determine the first, and last indices to scan.
  260. //  'minspan' is the minimum number of contiguous indices.
  261. //  'returnpos' determines if the function returns the size of the discovered reserved blocks,
  262. //  ...or the first index in that block set.
  263.  
  264. int MallocSize(int arr, int rangeStart, int rangeStop, int minspan, bool returnpos){
  265.     int size;
  266.     bool free;
  267.     int q;
  268.     //int a;
  269.     int sizeB;
  270.     for ( q = rangeStart; q <= rangeStop; q++ ) {
  271.         if ( arr[q] == MALLOC_RESERVED ) {
  272.             free = true;
  273.             //++a;
  274.             ++size;
  275.             if ( size > sizeB ) sizeB = size; //Store the value that we will return.
  276.                 //This allows us to parse more blocks, resetting free, and size,
  277.                 //trying to find a larger one to meet the requirements of minspan..
  278.         }
  279.         if ( arr[q] != MALLOC_RESERVED ) {
  280.             free = false;
  281.             size = 0;
  282.             //a = 0;
  283.         }
  284.            
  285.     }
  286.    
  287.     if ( sizeB >= minspan && !returnpos ) return sizeB;
  288.     if ( sizeB >= minspan && returnpos ) return q - sizeB; //Return the first index of the discovered span.
  289.    
  290.     else {
  291.    
  292.         if ( returnpos ) return -1;
  293.         else return 0;
  294.     }
  295. }
  296.  
  297. //Returns the size of the largest number of contiguous blocks of FREE mem in a given range.
  298. //Args: 'rangeStart' and 'rangeStop' determine the first, and last indices to scan.
  299. //  'minspan' is the minimum number of contiguous indices.
  300. //  'returnpos' determines if the function returns the size of the discovered reserved blocks,
  301. //  ...or the first index in that block set.
  302.  
  303. int FreeSize(int arr, int rangeStart, int rangeStop, int minspan, bool returnpos){
  304.     int size;
  305.     bool free;
  306.     int q;
  307.     //int a;
  308.     int sizeB;
  309.     for ( q = rangeStart; q <= rangeStop; q++ ) {
  310.         if ( arr[q] == MALLOC_FREE ) {
  311.             free = true;
  312.             //++a;
  313.             ++size;
  314.             if ( size > sizeB ) sizeB = size; //Store the value that we will return.
  315.                 //This allows us to parse more blocks, resetting free, and size,
  316.                 //trying to find a larger one to meet the requirements of minspan..
  317.         }
  318.         if ( arr[q] != MALLOC_FREE ) {
  319.             free = false;
  320.             size = 0;
  321.             //a = 0;
  322.         }
  323.            
  324.     }
  325.    
  326.     if ( sizeB >= minspan && !returnpos ) return sizeB;
  327.     if ( sizeB >= minspan && returnpos ) return q - sizeB; //Return the first index of the discovered span.
  328.    
  329.     else {
  330.    
  331.         if ( returnpos ) return -1;
  332.         else return 0;
  333.     }
  334. }
  335.  
  336. //Returns the FREE size of a given array.
  337. //Only from a range of indices starting with 'rangeStart' and engind with 'rangeStop.
  338. //Both 'rangeStart', and 'rangeStop' are included in this return, if free.
  339. //int FreeSize(int arr, int rangeStart, int rangeStop, bool contiguous, int span){
  340. //  int size;
  341. //  for ( int q = rangeStart; q <= rangeStop; q++ ) {
  342. //      if ( arr[q] == MALLOC_RESERVED ) size++;
  343. //  }
  344. //  return size;
  345. //}
  346.  
  347.  
  348. //Returns the contiguous RESERVED size (total number of reserved indices) for a given array.
  349. int MallocSize(int arr, bool contiguous, int minspan, bool returnpos){
  350.     int size;
  351.     int sizeB;
  352.     int q;
  353.     for ( q = 0; q < SizeOfArray(arr); q++ ) {
  354.         if ( arr[q] == MALLOC_RESERVED ) {
  355.             ++size;
  356.             if ( size > sizeB ) sizeB = size; //Store the value that we will return.
  357.                 //This allows us to parse more blocks, resetting free, and size,
  358.                 //trying to find a larger one to meet the requirements of minspan..
  359.         }
  360.         if ( arr[q] != MALLOC_RESERVED ) {
  361.             size = 0;
  362.         }
  363.            
  364.     }
  365.    
  366.     if ( sizeB >= minspan && !returnpos ) return sizeB;
  367.     if ( sizeB >= minspan && returnpos ) return q - sizeB; //Return the first index of the discovered span.
  368.    
  369.     else {
  370.    
  371.         if ( returnpos ) return -1;
  372.         else return 0;
  373.     }
  374. }
  375.  
  376. //!Z This has an error.
  377.  
  378. //Returns the FREE size of a given array.
  379. //int FreeSize(int arr, bool contiguous, int span, bool returnpos){
  380.     //int size;
  381.     //int sizeB;
  382.     //for ( int q = 0; q < SizeOfArray(arr); q++ ) {
  383.     //  if ( arr[q] == MALLOC_FREE ) {
  384.     //      ++size;
  385.     //      if ( size > sizeB ) sizeB = size; //Store the value that we will return.
  386.                 //This allows us to parse more blocks, resetting free, and size,
  387.                 //trying to find a larger one to meet the requirements of minspan..
  388.     //  }
  389.     //  if ( arr[q] != MALLOC_FREE ) {
  390.     //      size = 0;
  391.     //  }
  392.            
  393.     //}
  394.    
  395.     //if ( sizeB >= minspan && !returnpos ) return sizeB;
  396.     //if ( sizeB >= minspan && returnpos ) return q - sizeB; //Return the first index of the discovered span.
  397.    
  398.     //else {
  399.    
  400.     //  if ( returnpos ) return -1;
  401.     //  else return 0;
  402.     //}
  403. //}
  404.  
  405. //Returns the size (indices with a zero value) in a given array.
  406. int MemSize(int arr){
  407.     int size;
  408.     for ( int q = 0; q < SizeOfArray(arr); q++ ) {
  409.         if ( !arr[q] ) size++;
  410.     }
  411.     return size;
  412. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×