Skip to content

Commit 9992951

Browse files
authored
wpad: use malloc instead of lwp_wkspace alloc, check for OOM error (#266)
* wpad: use malloc instead of lwp_wkspace alloc, check for OOM error * Properly free alloc'd buffers on WPAD shutdown
1 parent df5c999 commit 9992951

5 files changed

Lines changed: 41 additions & 13 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: 6 additions & 9 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);
@@ -265,17 +263,16 @@ void wiiuse_sensorbar_enable(int enable)
265263
__wiiuse_sensorbar_enable(enable);
266264
}
267265

268-
void wiiuse_init_cmd_queue(struct wiimote_t *wm)
266+
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] = __lwp_wkspace_allocate(size);
275-
if(!__queue_buffer[wm->unid]) return;
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));
275+
return ERR_OK;
279276
}
280277

281278
int wiiuse_io_write(struct wiimote_t *wm,ubyte *buf,int len)

wiiuse/wiiuse.c

Lines changed: 32 additions & 3 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
@@ -66,14 +92,14 @@ struct wiimote_t** wiiuse_init(int wiimotes, wii_event_cb event_cb) {
6692
return NULL;
6793

6894
if (!__wm) {
69-
__wm = __lwp_wkspace_allocate(sizeof(struct wiimote_t*) * wiimotes);
95+
__wm = malloc(sizeof(struct wiimote_t*) * wiimotes);
7096
if(!__wm) return NULL;
7197
memset(__wm, 0, sizeof(struct wiimote_t*) * wiimotes);
7298
}
7399

74100
for (i = 0; i < wiimotes; ++i) {
75101
if(!__wm[i])
76-
__wm[i] = __lwp_wkspace_allocate(sizeof(struct wiimote_t));
102+
__wm[i] = malloc(sizeof(struct wiimote_t));
77103

78104
memset(__wm[i], 0, sizeof(struct wiimote_t));
79105
__wm[i]->unid = i;
@@ -88,7 +114,10 @@ struct wiimote_t** wiiuse_init(int wiimotes, wii_event_cb event_cb) {
88114
__wm[i]->sock = NULL;
89115
__wm[i]->bdaddr = *BD_ADDR_ANY;
90116
__wm[i]->event_cb = event_cb;
91-
wiiuse_init_cmd_queue(__wm[i]);
117+
if (wiiuse_init_cmd_queue(__wm[i])) {
118+
WIIUSE_ERROR("Could not allocate command queue");
119+
return NULL;
120+
}
92121
#elif defined(unix)
93122
__wm[i]->bdaddr = *BDADDR_ANY;
94123
__wm[i]->out_sock = -1;

wiiuse/wiiuse_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ struct op_t
267267
} __attribute__((packed));
268268

269269
/* not part of the api */
270-
void wiiuse_init_cmd_queue(struct wiimote_t *wm);
270+
int wiiuse_init_cmd_queue(struct wiimote_t *wm);
271271
void wiiuse_send_next_command(struct wiimote_t *wm);
272272
int wiiuse_set_report_type(struct wiimote_t* wm,cmd_blk_cb cb);
273273
int wiiuse_sendcmd(struct wiimote_t *wm,ubyte report_type,ubyte *msg,int len,cmd_blk_cb cb);

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)