You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
7.8 KiB

  1. $export_common #library "ZMEMORY"
  2. $export_common #include "zcommon.acs"
  3. #define muffins 42
  4. $export_common #include "ZMEMORY_g.acs"
  5. $export_common #include "ZMEMORY_w.acs"
  6. //Simple linked list implementation of malloc
  7. //$export_common global int 63:gmemory[];
  8. //$export_common world int 255:wmemory[];
  9. $export_option
  10. global int 63:gmemory[];
  11. $
  12. world int 255:wmemory[];
  13. $
  14. $export_common #libdefine nullptr 0
  15. $export_common //#define nullptr 0
  16. $export_common #define malloc_allocated 0
  17. $export_common #define malloc_size 1
  18. $export_common #define malloc_next_header 2
  19. $export_common #define malloc_prev_header 3
  20. $export_common #define malloc_num_header_properties 4
  21. $export_common #define p_malloc_init_flag_location 0
  22. $export_common #define p_malloc_first_header_location 1
  23. $$export_split
  24. //[Z] The reason I do it like this is because whoever made the acc is insane.
  25. #define @nullptr 0
  26. #define @malloc_allocated 0
  27. #define @malloc_size 1
  28. #define @malloc_next_header 2
  29. #define @malloc_prev_header 3
  30. #define @malloc_num_header_properties 4
  31. #define @p_malloc_init_flag_location 0
  32. #define @p_malloc_first_header_location 1
  33. $$
  34. $$export_split function int @malloc (int size) {
  35. //Do the setup on the first run of this function.
  36. if(@memory[@p_malloc_init_flag_location] == FALSE) { //Default values for global values is 0, so this is true.
  37. @memory[@p_malloc_init_flag_location] = TRUE;
  38. @memory[@p_malloc_first_header_location+@malloc_allocated] = FALSE;
  39. @memory[@p_malloc_first_header_location+@malloc_size] = -1; //"infinite"
  40. @memory[@p_malloc_first_header_location+@malloc_next_header] = @nullptr; //nullptr
  41. @memory[@p_malloc_first_header_location+@malloc_prev_header] = @nullptr; //nullptr
  42. }
  43. int p_previous_header = @nullptr;
  44. int p_current_header = @p_malloc_first_header_location;
  45. int p_retval = @nullptr;
  46. while(p_retval == @nullptr) {
  47. int memalloced = @memory[p_current_header+@malloc_allocated];
  48. int memsize = @memory[p_current_header+@malloc_size];
  49. if(memsize == -1) { //The end of the list.
  50. @memory[p_current_header+@malloc_allocated] = TRUE;
  51. @memory[p_current_header+@malloc_size] = size;
  52. @memory[p_current_header+@malloc_next_header] = p_current_header+@malloc_num_header_properties+size; //New EOL
  53. @memory[p_current_header+@malloc_prev_header] = p_previous_header;
  54. //Retrieve the return value while we are at the allocated space.
  55. p_retval = p_current_header+@malloc_num_header_properties;
  56. //Remember to initialize the new end list node.
  57. p_previous_header = p_current_header; //This is the header previous to the EOL.
  58. p_current_header = @memory[p_current_header+@malloc_next_header];
  59. //Set the tail node constants.
  60. @memory[p_current_header+@malloc_allocated] = FALSE;
  61. @memory[p_current_header+@malloc_size] = -1;
  62. @memory[p_current_header+@malloc_next_header] = @nullptr;
  63. @memory[p_current_header+@malloc_prev_header] = p_previous_header;
  64. } else if(memsize >= size && memalloced == FALSE) { //There is room here AND it isn't in use,
  65. @memory[p_current_header+@malloc_allocated] = TRUE;
  66. //The size isn't modified because we are re-using an existing space.
  67. // It would be a good idea to check just how large this space is and act accordingly rather
  68. // than using a 500 indexes large space for a 4 indexes large object.
  69. // Objects allocated in a doom mod probably won't be outside the 1-16 indexes range so it
  70. // should still be fine for most applications.
  71. //The next header isn't changed either for the same reason.
  72. if(memsize >= (size+@malloc_num_header_properties+5)) { //Assume that 5 is the smallest useful allocation size.
  73. int p_split_newheader = p_current_header+@malloc_num_header_properties+size; //Just to the end of the allocation.
  74. @memory[p_split_newheader+@malloc_allocated] = FALSE;
  75. @memory[p_split_newheader+@malloc_size] = @memory[p_current_header+@malloc_size]-@malloc_num_header_properties-size;
  76. @memory[p_split_newheader+@malloc_next_header] = @memory[p_current_header+@malloc_next_header];
  77. @memory[p_split_newheader+@malloc_prev_header] = p_current_header;
  78. @memory[p_current_header+@malloc_next_header] = p_split_newheader; //The header whose block was split should have its next pointer set to its other half.
  79. @memory[p_current_header+@malloc_size] = size; //Set the size of the allocation to reflect the split.
  80. }
  81. //Retrieve the return value while we are at the allocated space.
  82. p_retval = p_current_header+@malloc_num_header_properties;
  83. } else {
  84. //The observed node isn't useful for allocating the request. Go to the next node.
  85. p_previous_header = p_current_header;
  86. p_current_header = @memory[p_current_header+@malloc_next_header];
  87. }
  88. }
  89. //log(s:"Malloc allocated ", d:size, s:" indexes at location ", d:p_retval, s:" with header location ", d:p_current_header);
  90. return p_retval;
  91. }
  92. $$
  93. $$export_split function int @free (int p_ptr) {
  94. log(s:"Invoked free");
  95. int p_header = p_ptr - @malloc_num_header_properties;
  96. @memory[p_header+@malloc_allocated] = FALSE;
  97. int p_next = @memory[p_header+@malloc_next_header];
  98. int p_prev = @memory[p_header+@malloc_prev_header];
  99. //Below is the merging of free blocks.
  100. //It merges to the left (lower indexes) first becaue the right (larger
  101. // indexes) has a special case. (the end of the list)
  102. //the previous block is unused. Merge.
  103. if(p_prev != @nullptr //This doesn't make sense if the previous block doesn't exist.
  104. && @memory[p_prev+@malloc_allocated] == FALSE) {
  105. log(s:"Free attempting merge of header ", d:p_header, s: " to the left with header ", d:p_prev);
  106. @memory[p_prev+@malloc_size] += @memory[p_header+@malloc_size] + @malloc_num_header_properties;
  107. @memory[p_prev+@malloc_next_header] = p_next; //The prev header needs to know the new next.
  108. @memory[p_next+@malloc_prev_header] = p_prev; //The next header needs to know the new prev.
  109. //Now the two blocks are the same block. requires new initializations of the
  110. // variables for the other check to function correctly.
  111. //The header is the result of the merge, and the prev is the one before it.
  112. p_header = p_prev;
  113. p_prev = @memory[p_header+@malloc_prev_header];
  114. }
  115. //The next block is unused. Merge.
  116. //Note that p_next will never be a nullptr with correct usage.
  117. if(@memory[p_next+@malloc_allocated] == FALSE) {
  118. if(@memory[p_next+@malloc_size] == -1) { //EOL
  119. @memory[p_header+@malloc_size] = -1;
  120. @memory[p_header+@malloc_next_header] = @nullptr;
  121. } else {
  122. int p_next_next = @memory[p_next+@malloc_next_header];
  123. @memory[p_header+@malloc_size] += @memory[p_next+@malloc_size] + @malloc_num_header_properties;
  124. @memory[p_header+@malloc_next_header] = p_next_next; //The header on the other side of the next header needs to know its new prev.
  125. @memory[p_next_next+@malloc_prev_header] = p_header; //This header needs to know its new next.
  126. }
  127. }
  128. return -1;
  129. }
  130. $$
  131. //These are for debugging
  132. /*
  133. script "memory_write" (int index, int value) {
  134. memory[index] = value;
  135. }
  136. script "memory_read" (int index) {
  137. log(d:memory[index]);
  138. }
  139. script "memory_print_allocation_list" (void) {
  140. int p_current_header = p_malloc_first_header_location;
  141. while(p_current_header != nullptr) {
  142. log(s: "Header location: ", d:p_current_header,
  143. s:"\n Allocated flag: ", d:memory[p_current_header+malloc_allocated],
  144. s:"\n Allocation size: ", d:memory[p_current_header+malloc_size],
  145. s:"\n Prev header pointer: ", d:memory[p_current_header+malloc_prev_header],
  146. s:"\n Next header pointer: ", d:memory[p_current_header+malloc_next_header],
  147. s:"\n"
  148. );
  149. p_current_header = memory[p_current_header+malloc_next_header];
  150. }
  151. }
  152. */