Skip to content

Commit 065dba5

Browse files
committed
Properly free alloc'd buffers on WPAD shutdown
1 parent add7c5e commit 065dba5

4 files changed

Lines changed: 32 additions & 8 deletions

File tree

gc/wiiuse/wiiuse.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ typedef struct wiimote_t {
625625
WCONST ubyte exp_timeout; /**< timeout for expansion handshake */
626626
#elif defined(GEKKO)
627627
WCONST lwp_queue cmdq;
628+
WCONST u8 *queue_buffer;
628629
WCONST struct bd_addr bdaddr; /**< bt address */
629630
WCONST char bdaddr_str[18]; /**< readable bt address */
630631
WCONST struct bte_pcb *sock; /**< output socket */

wiiuse/io_wii.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
#include "io.h"
1414

1515
#define MAX_COMMANDS 0x100
16-
#define MAX_WIIMOTES 6
1716

1817
static vu32* const _ipcReg = (u32*)0xCD000000;
19-
static u8 *__queue_buffer[MAX_WIIMOTES] = { 0, 0, 0, 0, 0 };
2018

2119
extern void parse_event(struct wiimote_t *wm);
2220
extern void idle_cycle(struct wiimote_t* wm);
@@ -269,13 +267,11 @@ int wiiuse_init_cmd_queue(struct wiimote_t *wm)
269267
{
270268
u32 size;
271269

272-
if (!__queue_buffer[wm->unid]) {
273-
size = (MAX_COMMANDS*sizeof(struct cmd_blk_t));
274-
__queue_buffer[wm->unid] = malloc(size);
275-
if(!__queue_buffer[wm->unid]) return ERR_MEM;
276-
}
270+
size = (MAX_COMMANDS*sizeof(struct cmd_blk_t));
271+
wm->queue_buffer = malloc(size);
272+
if(!wm->queue_buffer) return ERR_MEM;
277273

278-
__lwp_queue_initialize(&wm->cmdq,__queue_buffer[wm->unid],MAX_COMMANDS,sizeof(struct cmd_blk_t));
274+
__lwp_queue_initialize(&wm->cmdq,wm->queue_buffer,MAX_COMMANDS,sizeof(struct cmd_blk_t));
279275
return ERR_OK;
280276
}
281277

wiiuse/wiiuse.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,32 @@ static __inline__ void __wiiuse_push_command(struct wiimote_t *wm,struct cmd_blk
5454
_CPU_ISR_Restore(level);
5555
}
5656

57+
void wiiuse_cleanup(struct wiimote_t **wm, int wiimotes)
58+
{
59+
int i = 0;
60+
61+
if (!wm)
62+
return;
63+
64+
for (; i < wiimotes; ++i) {
65+
if (wm[i]) {
66+
if (wm[i]->queue_buffer) {
67+
free(wm[i]->queue_buffer);
68+
wm[i]->queue_buffer = NULL;
69+
}
70+
71+
if (wm[i]->sock) {
72+
bte_free(wm[i]->sock);
73+
wm[i]->sock = NULL;
74+
}
75+
76+
free(wm[i]);
77+
}
78+
}
79+
80+
free(wm);
81+
}
82+
5783
#ifndef GEKKO
5884
struct wiimote_t** wiiuse_init(int wiimotes) {
5985
#else

wiiuse/wpad.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,7 @@ s32 WPAD_Shutdown(void)
17741774
__wpads_used = 0;
17751775

17761776
__wiiuse_sensorbar_enable(0);
1777+
wiiuse_cleanup(__wpads, WPAD_MAX_DEVICES);
17771778
_CPU_ISR_Restore(level);
17781779

17791780
BTE_Shutdown();

0 commit comments

Comments
 (0)