Browse Source

remove memory leak in pokedex, update lookup entries to lazy array, resolves #26

SBird1337 6 years ago
parent
commit
a24e8bb14e

+ 1
- 1
g3headers

1
-Subproject commit ee7c8b28b58f61df864f7716c7ee9bbae82f5411
1
+Subproject commit e0402c99b51968537abb0f09ed017558e2055385

+ 12
- 28
src/pokedex/pokedex.c View File

77
 
77
 
78
 void pdex_update_balls(void) {
78
 void pdex_update_balls(void) {
79
     for (u8 i = 0; i < 8; ++i) {
79
     for (u8 i = 0; i < 8; ++i) {
80
-        if (pokedex_context->lookup[i + pokedex_context->cursor_position_top].caught)
80
+        if (pdex_lazy_lookup_entry(i + pokedex_context->cursor_position_top)->caught)
81
             OBJID_SHOW(pokedex_context->ball_oams[i]);
81
             OBJID_SHOW(pokedex_context->ball_oams[i]);
82
         else
82
         else
83
             OBJID_HIDE(pokedex_context->ball_oams[i]);
83
             OBJID_HIDE(pokedex_context->ball_oams[i]);
87
 void pdex_update_page_full() {
87
 void pdex_update_page_full() {
88
     rboxid_clear_pixels(TB_MAIN, 0);
88
     rboxid_clear_pixels(TB_MAIN, 0);
89
     for (u16 i = pokedex_context->cursor_position_top; i < pokedex_context->cursor_position_top + 8; ++i) {
89
     for (u16 i = pokedex_context->cursor_position_top; i < pokedex_context->cursor_position_top + 8; ++i) {
90
-        pdex_main_box_species_fill(i - pokedex_context->cursor_position_top, pokedex_context->lookup[i].species,
91
-                                   pokedex_context->lookup[i].seen, pokedex_context->lookup[i].caught);
90
+        pdex_main_box_species_fill(i - pokedex_context->cursor_position_top, pdex_lazy_lookup_entry(i)->species,
91
+                                   pdex_lazy_lookup_entry(i)->seen, pdex_lazy_lookup_entry(i)->caught);
92
     }
92
     }
93
     pdex_update_balls();
93
     pdex_update_balls();
94
     rboxid_update_tilemap_and_tileset(TB_MAIN);
94
     rboxid_update_tilemap_and_tileset(TB_MAIN);
309
 }
309
 }
310
 
310
 
311
 void pdex_data_setup(void) {
311
 void pdex_data_setup(void) {
312
-    /* fill the LUT */
313
-    /* TODO: get data from various pokedex lists */
314
-    pokedex_context->lookup = malloc_and_clear((PDEX_LAST_SHOWN + 1) * sizeof(struct PdexLookup));
315
-    bool first = false;
316
-    for (u32 i = 0; i <= PDEX_LAST_SHOWN; ++i) {
317
-        u16 species = pokedex_index_to_species(i);
318
-        if (species > 0) {
319
-            pokedex_context->lookup[i].species = species;
320
-            pokedex_context->lookup[i].seen = dex_flag_pokedex_index(i, DEX_FLAG_CHECK_SEEN);
321
-            pokedex_context->lookup[i].caught = dex_flag_pokedex_index(i, DEX_FLAG_CHECK_CAUGHT);
322
-            if (!first && (pokedex_context->lookup[i].seen)) {
323
-                pokedex_context->first_seen = 1; //i
324
-                //pokedex_context->cursor_position_top = 1; //i is set in region dex
325
-                first = true;
326
-            }
327
-        }
328
-    }
312
+    pokedex_context->first_seen = 1;
329
 }
313
 }
330
 
314
 
331
 void pdex_hardware_scroll(bool up) {
315
 void pdex_hardware_scroll(bool up) {
351
 
335
 
352
 void pdex_fill_previous_slot(void) {
336
 void pdex_fill_previous_slot(void) {
353
     u16 pIndex = pokedex_context->cursor_position_top - 1;
337
     u16 pIndex = pokedex_context->cursor_position_top - 1;
354
-    pdex_main_box_species_fill(-1, pokedex_context->lookup[pIndex].species, pokedex_context->lookup[pIndex].seen,
355
-                               pokedex_context->lookup[pIndex].caught);
338
+    pdex_main_box_species_fill(-1, pdex_lazy_lookup_entry(pIndex)->species, pdex_lazy_lookup_entry(pIndex)->seen,
339
+                               pdex_lazy_lookup_entry(pIndex)->caught);
356
 }
340
 }
357
 
341
 
358
 void pdex_fill_next_slot(void) {
342
 void pdex_fill_next_slot(void) {
359
     u16 pIndex = pokedex_context->cursor_position_top + 8;
343
     u16 pIndex = pokedex_context->cursor_position_top + 8;
360
-    pdex_main_box_species_fill(8, pokedex_context->lookup[pIndex].species, pokedex_context->lookup[pIndex].seen,
361
-                               pokedex_context->lookup[pIndex].caught);
344
+    pdex_main_box_species_fill(8, pdex_lazy_lookup_entry(pIndex)->species, pdex_lazy_lookup_entry(pIndex)->seen,
345
+                               pdex_lazy_lookup_entry(pIndex)->caught);
362
 }
346
 }
363
 
347
 
364
 void pdex_try_advance(u8 reverse) {
348
 void pdex_try_advance(u8 reverse) {
385
     }
369
     }
386
 
370
 
387
     u16 pkIndexToLoad = pokedex_context->cursor_position_internal + pokedex_context->cursor_position_top;
371
     u16 pkIndexToLoad = pokedex_context->cursor_position_internal + pokedex_context->cursor_position_top;
388
-    if (pokedex_context->lookup[pkIndexToLoad].seen || pokedex_context->lookup[pkIndexToLoad].caught)
389
-        pdex_pokemon_load(pokedex_context->lookup[pkIndexToLoad].species);
372
+    if (pdex_lazy_lookup_entry(pkIndexToLoad)->seen || pdex_lazy_lookup_entry(pkIndexToLoad)->caught)
373
+        pdex_pokemon_load(pdex_lazy_lookup_entry(pkIndexToLoad)->species);
390
     else
374
     else
391
-        pdex_pokemon_load(pokedex_context->lookup[pkIndexToLoad].species); /* debug, just display the mofo */
375
+        pdex_pokemon_load(pdex_lazy_lookup_entry(pkIndexToLoad)->species); /* debug, just display the mofo */
392
     pdex_update_balls();
376
     pdex_update_balls();
393
 }
377
 }
394
 
378
 
409
 
393
 
410
         pdex_pokeballs_init();
394
         pdex_pokeballs_init();
411
         pdex_load_scroll_ui();
395
         pdex_load_scroll_ui();
412
-        pdex_pokemon_load(pokedex_context->lookup[pokedex_context->cursor_position_top + pokedex_context->cursor_position_internal].species);
396
+        pdex_pokemon_load(pdex_lazy_lookup_entry(pokedex_context->cursor_position_top + pokedex_context->cursor_position_internal)->species);
413
         pdex_load_sc();
397
         pdex_load_sc();
414
         pdex_update_page_full();
398
         pdex_update_page_full();
415
 
399
 

+ 24
- 3
src/pokedex/pokedex_common.c View File

97
     }
97
     }
98
 }
98
 }
99
 
99
 
100
-void pdex_free_memory(void)
101
-{
102
-    if(pokedex_context->lookup != NULL)
100
+struct PdexLookup *pdex_fill_lookup(u16 dexIndex) {
101
+    u16 species = pokedex_index_to_species(dexIndex);
102
+    pokedex_context->lookup[dexIndex].species = species;
103
+    pokedex_context->lookup[dexIndex].seen = dex_flag_pokedex_index(dexIndex, DEX_FLAG_CHECK_SEEN);
104
+    pokedex_context->lookup[dexIndex].caught = dex_flag_pokedex_index(dexIndex, DEX_FLAG_CHECK_CAUGHT);
105
+    return &pokedex_context->lookup[dexIndex];
106
+}
107
+
108
+struct PdexLookup *pdex_lazy_lookup_entry(u16 dexIndex) {
109
+    if (pokedex_context->lookup[dexIndex].species != -1) {
110
+        return &pokedex_context->lookup[dexIndex];
111
+    } else {
112
+        return pdex_fill_lookup(dexIndex);
113
+    }
114
+}
115
+
116
+void pdex_free_memory(void) {
117
+    if (pokedex_context->lookup != NULL)
103
         free(pokedex_context->lookup);
118
         free(pokedex_context->lookup);
104
     free(pokedex_context);
119
     free(pokedex_context);
105
 }
120
 }
106
 
121
 
122
+void pdex_alloc_memory(void) {
123
+    pokedex_context = malloc_and_clear(sizeof(struct PdexCtx));
124
+    pokedex_context->lookup = malloc_and_clear((PDEX_LAST_SHOWN + 1) * sizeof(struct PdexLookup));
125
+    memset(pokedex_context->lookup, 0xFF, (PDEX_LAST_SHOWN + 1) * sizeof(struct PdexLookup));
126
+}
127
+
107
 void pdex_vram_allocate_bgmaps(void) {
128
 void pdex_vram_allocate_bgmaps(void) {
108
     bgid_set_tilemap(0, malloc(0x800));
129
     bgid_set_tilemap(0, malloc(0x800));
109
     bgid_set_tilemap(1, malloc(0x800));
130
     bgid_set_tilemap(1, malloc(0x800));

+ 2
- 0
src/pokedex/pokedex_common.h View File

44
 void pdex_vram_setup(void);
44
 void pdex_vram_setup(void);
45
 void pdex_vram_free_bgmaps(void);
45
 void pdex_vram_free_bgmaps(void);
46
 void pdex_free_memory(void);
46
 void pdex_free_memory(void);
47
+void pdex_alloc_memory(void);
48
+struct PdexLookup *pdex_lazy_lookup_entry(u16 dexIndex);
47
 
49
 
48
 void pdex_load(void);
50
 void pdex_load(void);
49
 
51
 

+ 4
- 7
src/pokedex/pokedex_detail.c View File

63
 }
63
 }
64
 
64
 
65
 void dexdetail_load_pokemon(u16 dexindex) {
65
 void dexdetail_load_pokemon(u16 dexindex) {
66
-    u16 species = pokedex_context->lookup[dexindex].species;
66
+    u16 species = pdex_lazy_lookup_entry(dexindex)->species;
67
     rboxid_clear_pixels(TB_PKNAME, 0);
67
     rboxid_clear_pixels(TB_PKNAME, 0);
68
     rboxid_clear_pixels(TB_PKSIZE, 0);
68
     rboxid_clear_pixels(TB_PKSIZE, 0);
69
     rboxid_clear_pixels(TB_DESC, 0);
69
     rboxid_clear_pixels(TB_DESC, 0);
180
         case KEY_B:
180
         case KEY_B:
181
             pokedex_context->state = 12;
181
             pokedex_context->state = 12;
182
             u16 dexindex = pokedex_context->cursor_position_top + pokedex_context->cursor_position_internal;
182
             u16 dexindex = pokedex_context->cursor_position_top + pokedex_context->cursor_position_internal;
183
-            if(dexindex <= PDEX_LAST_SHOWN - 7)
184
-            {
183
+            if (dexindex <= PDEX_LAST_SHOWN - 7) {
185
                 pokedex_context->cursor_position_top = dexindex;
184
                 pokedex_context->cursor_position_top = dexindex;
186
                 pokedex_context->cursor_position_internal = 0;
185
                 pokedex_context->cursor_position_internal = 0;
187
-            }
188
-            else
189
-            {
186
+            } else {
190
                 pokedex_context->cursor_position_top = PDEX_LAST_SHOWN - 7;
187
                 pokedex_context->cursor_position_top = PDEX_LAST_SHOWN - 7;
191
                 pokedex_context->cursor_position_internal = (dexindex - pokedex_context->cursor_position_top);
188
                 pokedex_context->cursor_position_internal = (dexindex - pokedex_context->cursor_position_top);
192
             }
189
             }
193
-            
190
+
194
             fade_screen(0xFFFFFFFF, PDEX_FADEIN_SPD, 0, 16, 0x0000);
191
             fade_screen(0xFFFFFFFF, PDEX_FADEIN_SPD, 0, 16, 0x0000);
195
             break;
192
             break;
196
         }
193
         }

+ 1
- 1
src/pokedex/pokedex_region.c View File

223
 
223
 
224
 void region_select_load(void) {
224
 void region_select_load(void) {
225
     pdex_vram_setup();
225
     pdex_vram_setup();
226
-    pokedex_context = malloc_and_clear(sizeof(struct PdexCtx));
226
+    pdex_alloc_memory();
227
 
227
 
228
     region_select_load_gfx();
228
     region_select_load_gfx();
229
     task_add(region_loop, 0);
229
     task_add(region_loop, 0);