Mercurial > repos > blastem
annotate arena.c @ 1442:59e1dbb795a7
Update README in anticipation of 0.5.1 release
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 25 Aug 2017 20:12:21 -0700 |
parents | 4b8ab2d82aee |
children |
rev | line source |
---|---|
883
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 /* |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 Copyright 2015 Michael Pavone |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 This file is part of BlastEm. |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text. |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 */ |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 #include <stdlib.h> |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 #include <stdint.h> |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 #include "arena.h" |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 struct arena { |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 void **used_blocks; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 void **free_blocks; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 size_t used_count; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 size_t used_storage; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 size_t free_count; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 size_t free_storage; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 }; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 |
1030
c7c573f0229e
Fixed a really egregious bug in the arena implementation. Not sure how this even worked at all before.
Michael Pavone <pavone@retrodev.com>
parents:
883
diff
changeset
|
20 #define DEFAULT_STORAGE_SIZE 8 |
c7c573f0229e
Fixed a really egregious bug in the arena implementation. Not sure how this even worked at all before.
Michael Pavone <pavone@retrodev.com>
parents:
883
diff
changeset
|
21 |
883
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 static arena *current_arena; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 arena *get_current_arena() |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 { |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 if (!current_arena) { |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 current_arena = calloc(1, sizeof(arena)); |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 } |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 return current_arena; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 } |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 arena *set_current_arena(arena *a) |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 { |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 arena *tmp = current_arena; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 current_arena = a; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 return tmp; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 } |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 arena *start_new_arena() |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 { |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 arena *tmp = current_arena; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 current_arena = NULL; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 return tmp; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 } |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 void track_block(void *block) |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 { |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 arena *cur = get_current_arena(); |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 if (cur->used_count == cur->used_storage) { |
1030
c7c573f0229e
Fixed a really egregious bug in the arena implementation. Not sure how this even worked at all before.
Michael Pavone <pavone@retrodev.com>
parents:
883
diff
changeset
|
50 if (cur->used_storage) { |
c7c573f0229e
Fixed a really egregious bug in the arena implementation. Not sure how this even worked at all before.
Michael Pavone <pavone@retrodev.com>
parents:
883
diff
changeset
|
51 cur->used_storage *= 2; |
c7c573f0229e
Fixed a really egregious bug in the arena implementation. Not sure how this even worked at all before.
Michael Pavone <pavone@retrodev.com>
parents:
883
diff
changeset
|
52 } else { |
c7c573f0229e
Fixed a really egregious bug in the arena implementation. Not sure how this even worked at all before.
Michael Pavone <pavone@retrodev.com>
parents:
883
diff
changeset
|
53 cur->used_storage = DEFAULT_STORAGE_SIZE; |
c7c573f0229e
Fixed a really egregious bug in the arena implementation. Not sure how this even worked at all before.
Michael Pavone <pavone@retrodev.com>
parents:
883
diff
changeset
|
54 } |
883
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 cur->used_blocks = realloc(cur->used_blocks, cur->used_storage * sizeof(void *)); |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 } |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 cur->used_blocks[cur->used_count++] = block; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 } |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 void mark_all_free() |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 { |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 arena *cur = get_current_arena(); |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 if (!cur->free_blocks) { |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 cur->free_blocks = cur->used_blocks; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 cur->free_storage = cur->used_storage; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 cur->free_count = cur->used_count; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 cur->used_count = cur->used_storage = 0; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 cur->used_blocks = NULL; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 } else { |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 if (cur->free_storage < cur->used_count + cur->free_count) { |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 cur->free_storage = cur->used_count + cur->free_count; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 cur->free_blocks = realloc(cur->free_blocks, cur->free_storage * sizeof(void*)); |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 } |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 for (; cur->used_count > 0; cur->used_count--) |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 { |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 cur->free_blocks[cur->free_count++] = cur->used_blocks[cur->used_count-1]; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 } |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 } |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
79 } |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 void *try_alloc_arena() |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 { |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 if (!current_arena || !current_arena->free_count) { |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 return NULL; |
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 } |
1075
4b8ab2d82aee
Fix a memory leak in try_alloc_arena
Michael Pavone <pavone@retrodev.com>
parents:
1030
diff
changeset
|
86 void *ret = current_arena->free_blocks[--current_arena->free_count]; |
4b8ab2d82aee
Fix a memory leak in try_alloc_arena
Michael Pavone <pavone@retrodev.com>
parents:
1030
diff
changeset
|
87 track_block(ret); |
4b8ab2d82aee
Fix a memory leak in try_alloc_arena
Michael Pavone <pavone@retrodev.com>
parents:
1030
diff
changeset
|
88 return ret; |
883
9f149f0e98b7
It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
89 } |