Sony PlayStation2 Study
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.

457 lines
10 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. # _____ ___ ____ ___ ____
  3. # ____| | ____| | | |____|
  4. # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
  5. #-----------------------------------------------------------------------
  6. # Copyright (c) 2003 Marcus R. Brown <mrbrown@0xd6.org>
  7. # Copyright (c) 2004 adresd <adresd_ps2dev@yahoo.com>
  8. # Licenced under Academic Free License version 2.0
  9. # Review ps2sdk README & LICENSE files for further details.
  10. */
  11. /**
  12. * @file
  13. * Basic alloc library
  14. */
  15. #include <stdarg.h>
  16. #include "types.h"
  17. #include "defs.h"
  18. #include "alloc.h"
  19. #include "irx_imports.h"
  20. #define MODNAME "alloc"
  21. IRX_ID("Basic alloc library", 1, 1);
  22. extern struct irx_export_table _exp_alloc;
  23. static vs32 alloc_sema = -1;
  24. #define DEFAULT_HEAP_SIZE 128 * 1024
  25. u32 heap_size = DEFAULT_HEAP_SIZE;
  26. static u8 * heap_start, * heap_end, * _heap_ptr;
  27. static void alloc_lock() {
  28. if (alloc_sema >= 0) {
  29. WaitSema(alloc_sema);
  30. }
  31. }
  32. static void alloc_unlock() {
  33. if (alloc_sema >= 0) {
  34. SignalSema(alloc_sema);
  35. }
  36. }
  37. int _start(int argc, char **argv)
  38. {
  39. iop_sema_t sem_info;
  40. if (RegisterLibraryEntries(&_exp_alloc) != 0)
  41. return 1;
  42. // check arguments for a heap_size parameter.
  43. if (argc > 1) {
  44. heap_size = strtoul(argv[1], NULL, 0);
  45. }
  46. if (!(heap_start = AllocSysMemory(ALLOC_FIRST, heap_size, NULL)))
  47. return -1;
  48. heap_end = heap_start + heap_size;
  49. _heap_ptr = heap_start;
  50. sem_info.attr = 1;
  51. sem_info.option = 1;
  52. sem_info.initial = 1;
  53. sem_info.max = 1;
  54. alloc_sema = CreateSema(&sem_info);
  55. return 0;
  56. }
  57. int shutdown()
  58. {
  59. if (alloc_sema >= 0) {
  60. DeleteSema(alloc_sema);
  61. }
  62. FreeSysMemory(heap_start);
  63. return 0;
  64. }
  65. /** Use this to set the default malloc() alignment. */
  66. #define DEFAULT_ALIGNMENT 16
  67. #ifndef ALIGN
  68. #define ALIGN(x, align) (((x)+((align)-1))&~((align)-1))
  69. #endif
  70. /* _heap_mem_block_header structure. */
  71. typedef struct _heap_mem_header {
  72. void * ptr;
  73. size_t size;
  74. struct _heap_mem_header * prev;
  75. struct _heap_mem_header * next;
  76. } heap_mem_header_t;
  77. static void * __alloc_heap_base = NULL;
  78. static heap_mem_header_t *__alloc_heap_head = NULL;
  79. static heap_mem_header_t *__alloc_heap_tail = NULL;
  80. static void * alloc_sbrk(size_t increment)
  81. {
  82. u8 *mp, *ret = (void *)-1;
  83. if (increment == 0)
  84. return _heap_ptr;
  85. /* If the area we want to allocated is past the end of our heap, we have a problem. */
  86. mp = _heap_ptr + increment;
  87. if (mp <= heap_end) {
  88. ret = _heap_ptr;
  89. _heap_ptr = mp;
  90. }
  91. return ret;
  92. }
  93. /** Find the lowest block that we can allocate AFTER, returning NULL if there are none. */
  94. static heap_mem_header_t * _heap_mem_fit(heap_mem_header_t *head, size_t size)
  95. {
  96. heap_mem_header_t *prev_mem = head;
  97. u32 prev_top, next_bot;
  98. while (prev_mem != NULL) {
  99. if (prev_mem->next != NULL) {
  100. prev_top = (u32)prev_mem->ptr + prev_mem->size;
  101. next_bot = (u32)prev_mem->next - prev_top;
  102. if (next_bot >= size)
  103. return prev_mem;
  104. }
  105. prev_mem = prev_mem->next;
  106. }
  107. return prev_mem;
  108. }
  109. void * malloc(size_t size)
  110. {
  111. void *ptr = NULL, *mem_ptr;
  112. heap_mem_header_t *new_mem, *prev_mem;
  113. size_t mem_sz, heap_align_bytes;
  114. mem_sz = size + sizeof(heap_mem_header_t);
  115. if ((mem_sz & (DEFAULT_ALIGNMENT - 1)) != 0)
  116. mem_sz = ALIGN(mem_sz, DEFAULT_ALIGNMENT);
  117. alloc_lock();
  118. /* If we don't have any allocated blocks, reserve the first block from
  119. the OS and initialize __alloc_heap_tail. */
  120. if (__alloc_heap_head == NULL) {
  121. /* Align the bottom of the heap to our default alignment. */
  122. if (__alloc_heap_base == NULL) {
  123. heap_align_bytes = (u32)alloc_sbrk(0) & (DEFAULT_ALIGNMENT - 1);
  124. alloc_sbrk(heap_align_bytes);
  125. __alloc_heap_base = alloc_sbrk(0);
  126. }
  127. /* Allocate the physical heap and setup the head block. */
  128. if ((mem_ptr = alloc_sbrk(mem_sz)) == (void *)-1)
  129. return ptr; /* NULL */
  130. ptr = (void *)((u32)mem_ptr + sizeof(heap_mem_header_t));
  131. __alloc_heap_head = (heap_mem_header_t *)mem_ptr;
  132. __alloc_heap_head->ptr = ptr;
  133. __alloc_heap_head->size = mem_sz - sizeof(heap_mem_header_t);
  134. __alloc_heap_head->prev = NULL;
  135. __alloc_heap_head->next = NULL;
  136. __alloc_heap_tail = __alloc_heap_head;
  137. alloc_unlock();
  138. return ptr;
  139. }
  140. /* Check to see if there's free space at the bottom of the heap. */
  141. if ((__alloc_heap_base + mem_sz) < (void *)__alloc_heap_head) {
  142. new_mem = (heap_mem_header_t *)__alloc_heap_base;
  143. ptr = (void *)((u32)new_mem + sizeof(heap_mem_header_t));
  144. new_mem->ptr = ptr;
  145. new_mem->size = mem_sz - sizeof(heap_mem_header_t);
  146. new_mem->prev = NULL;
  147. new_mem->next = __alloc_heap_head;
  148. new_mem->next->prev = new_mem;
  149. __alloc_heap_head = new_mem;
  150. alloc_unlock();
  151. return ptr;
  152. }
  153. /* See if we can allocate the block without extending the heap. */
  154. prev_mem = _heap_mem_fit(__alloc_heap_head, mem_sz);
  155. if (prev_mem != NULL) {
  156. new_mem = (heap_mem_header_t *)((u32)prev_mem->ptr + prev_mem->size);
  157. ptr = (void *)((u32)new_mem + sizeof(heap_mem_header_t));
  158. new_mem->ptr = ptr;
  159. new_mem->size = mem_sz - sizeof(heap_mem_header_t);
  160. new_mem->prev = prev_mem;
  161. new_mem->next = prev_mem->next;
  162. new_mem->next->prev = new_mem;
  163. prev_mem->next = new_mem;
  164. alloc_unlock();
  165. return ptr;
  166. }
  167. /* Extend the heap, but make certain the block is inserted in
  168. order. */
  169. if ((mem_ptr = alloc_sbrk(mem_sz)) == (void *)-1) {
  170. alloc_unlock();
  171. return ptr; /* NULL */
  172. }
  173. ptr = (void *)((u32)mem_ptr + sizeof(heap_mem_header_t));
  174. new_mem = (heap_mem_header_t *)mem_ptr;
  175. new_mem->ptr = ptr;
  176. new_mem->size = mem_sz - sizeof(heap_mem_header_t);
  177. new_mem->prev = __alloc_heap_tail;
  178. new_mem->next = NULL;
  179. __alloc_heap_tail->next = new_mem;
  180. __alloc_heap_tail = new_mem;
  181. alloc_unlock();
  182. return ptr;
  183. }
  184. void * realloc(void *ptr, size_t size)
  185. {
  186. heap_mem_header_t *prev_mem;
  187. void *new_ptr = NULL;
  188. if (!size && ptr != NULL) {
  189. free(ptr);
  190. return new_ptr;
  191. }
  192. if (ptr == NULL)
  193. return malloc(size);
  194. if ((size & (DEFAULT_ALIGNMENT - 1)) != 0)
  195. size = ALIGN(size, DEFAULT_ALIGNMENT);
  196. alloc_lock();
  197. prev_mem = (heap_mem_header_t *)((u32)ptr - sizeof(heap_mem_header_t));
  198. /* Don't do anything if asked for same sized block. */
  199. /* If the new size is shorter, let's just shorten the block. */
  200. if (prev_mem->size >= size) {
  201. /* However, if this is the last block, we have to shrink the heap. */
  202. if (!prev_mem->next)
  203. alloc_sbrk(ptr + size - alloc_sbrk(0));
  204. prev_mem->size = size;
  205. alloc_unlock();
  206. return ptr;
  207. }
  208. /* We are asked for a larger block of memory. */
  209. /* Are we the last memory block ? */
  210. if (!prev_mem->next) {
  211. /* Yes, let's just extend the heap then. */
  212. if (alloc_sbrk(size - prev_mem->size) == (void*) -1)
  213. return NULL;
  214. prev_mem->size = size;
  215. alloc_unlock();
  216. return ptr;
  217. }
  218. /* Is the next block far enough so we can extend the current block ? */
  219. if ((prev_mem->next->ptr - ptr) > size) {
  220. prev_mem->size = size;
  221. alloc_unlock();
  222. return ptr;
  223. }
  224. alloc_unlock();
  225. /* We got out of luck, let's allocate a new block of memory. */
  226. if ((new_ptr = malloc(size)) == NULL)
  227. return new_ptr;
  228. /* New block is larger, we only copy the old data. */
  229. memcpy(new_ptr, ptr, prev_mem->size);
  230. free(ptr);
  231. return new_ptr;
  232. }
  233. void * calloc(size_t n, size_t size)
  234. {
  235. void *ptr = NULL;
  236. size_t sz = n * size;
  237. if ((ptr = malloc(sz)) == NULL)
  238. return ptr;
  239. memset(ptr, 0, sz);
  240. return ptr;
  241. }
  242. void * memalign(size_t align, size_t size)
  243. {
  244. heap_mem_header_t new_mem;
  245. heap_mem_header_t *cur_mem;
  246. heap_mem_header_t *old_mem;
  247. void *ptr = NULL;
  248. if (align <= DEFAULT_ALIGNMENT)
  249. return malloc(size);
  250. /* Allocate with extra alignment bytes just in case it isn't aligned
  251. properly by malloc. */
  252. if ((ptr = malloc(size + align)) == NULL)
  253. return ptr; /* NULL */
  254. /* If malloc returned it aligned for us we're fine. */
  255. if (((u32)ptr & (align - 1)) == 0)
  256. return ptr;
  257. alloc_lock();
  258. cur_mem = (heap_mem_header_t *)((u32)ptr - sizeof(heap_mem_header_t));
  259. cur_mem->size -= align;
  260. /* Otherwise, align the pointer and fixup our hearder accordingly. */
  261. ptr = (void *)ALIGN((u32)ptr, align);
  262. old_mem = cur_mem;
  263. /* Copy the heap_mem_header_t locally, before repositioning (to make
  264. sure we don't overwrite ourselves. */
  265. memcpy(&new_mem, cur_mem, sizeof(heap_mem_header_t));
  266. cur_mem = (heap_mem_header_t *)((u32)ptr - sizeof(heap_mem_header_t));
  267. memcpy(cur_mem, &new_mem, sizeof(heap_mem_header_t));
  268. if (cur_mem->prev)
  269. cur_mem->prev->next = cur_mem;
  270. if (cur_mem->next)
  271. cur_mem->next->prev = cur_mem;
  272. if (__alloc_heap_head == old_mem)
  273. __alloc_heap_head = cur_mem;
  274. if (__alloc_heap_tail == old_mem)
  275. __alloc_heap_tail = cur_mem;
  276. cur_mem->ptr = ptr;
  277. alloc_unlock();
  278. return ptr;
  279. }
  280. void free(void *ptr)
  281. {
  282. heap_mem_header_t *cur;
  283. void *heap_top;
  284. size_t size;
  285. if (!ptr)
  286. return;
  287. alloc_lock();
  288. if (!__alloc_heap_head) {
  289. alloc_unlock();
  290. return;
  291. }
  292. /* Freeing the head pointer is a special case. */
  293. if (ptr == __alloc_heap_head->ptr) {
  294. size = __alloc_heap_head->size +
  295. (size_t)(__alloc_heap_head->ptr - (void *)__alloc_heap_head);
  296. __alloc_heap_head = __alloc_heap_head->next;
  297. if (__alloc_heap_head != NULL) {
  298. __alloc_heap_head->prev = NULL;
  299. } else {
  300. __alloc_heap_tail = NULL;
  301. alloc_sbrk(-size);
  302. }
  303. alloc_unlock();
  304. return;
  305. }
  306. cur = __alloc_heap_head;
  307. while (ptr != cur->ptr) {
  308. /* ptr isn't in our list */
  309. if (cur->next == NULL) {
  310. alloc_unlock();
  311. return;
  312. }
  313. cur = cur->next;
  314. }
  315. /* Deallocate the block. */
  316. if (cur->next != NULL) {
  317. cur->next->prev = cur->prev;
  318. } else {
  319. /* If this block was the last one in the list, shrink the heap. */
  320. __alloc_heap_tail = cur->prev;
  321. /* We need to free (heap top) - (prev->ptr + prev->size), or else
  322. we'll end up with an unallocatable block of heap. */
  323. heap_top = alloc_sbrk(0);
  324. size = (u32)heap_top - (u32)(cur->prev->ptr + cur->prev->size);
  325. alloc_sbrk(-size);
  326. }
  327. cur->prev->next = cur->next;
  328. alloc_unlock();
  329. }
  330. void * __mem_walk_begin() {
  331. return __alloc_heap_head;
  332. }
  333. void __mem_walk_read(void * token, u32 * size, void ** ptr, int * valid) {
  334. heap_mem_header_t * cur = (heap_mem_header_t *) token;
  335. *valid = 1;
  336. *size = cur->size;
  337. *ptr = cur->ptr;
  338. }
  339. void * __mem_walk_inc(void * token) {
  340. heap_mem_header_t * cur = (heap_mem_header_t *) token;
  341. return cur->next;
  342. }
  343. int __mem_walk_end(void * token) {
  344. return token == NULL;
  345. }