joystick/linux: fix memleaks; streamline joylist item removal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index eeaf6c8..001db4c 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -268,6 +268,15 @@ static void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_clas
}
#endif /* SDL_USE_LIBUDEV */
+static void
+FreeJoylistItem(SDL_joylist_item *item)
+{
+ SDL_free(item->mapping);
+ SDL_free(item->path);
+ SDL_free(item->name);
+ SDL_free(item);
+}
+
static int
MaybeAddDevice(const char *path)
{
@@ -308,21 +317,18 @@ MaybeAddDevice(const char *path)
return -1;
}
- item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
+ item = (SDL_joylist_item *) SDL_calloc(1, sizeof (SDL_joylist_item));
if (item == NULL) {
return -1;
}
- SDL_zerop(item);
item->devnum = sb.st_rdev;
item->path = SDL_strdup(path);
item->name = name;
item->guid = guid;
if ((item->path == NULL) || (item->name == NULL)) {
- SDL_free(item->path);
- SDL_free(item->name);
- SDL_free(item);
+ FreeJoylistItem(item);
return -1;
}
@@ -342,6 +348,31 @@ MaybeAddDevice(const char *path)
return numjoysticks;
}
+static void
+RemoveJoylistItem(SDL_joylist_item *item, SDL_joylist_item *prev)
+{
+ if (item->hwdata) {
+ item->hwdata->item = NULL;
+ }
+
+ if (prev != NULL) {
+ prev->next = item->next;
+ } else {
+ SDL_assert(SDL_joylist == item);
+ SDL_joylist = item->next;
+ }
+
+ if (item == SDL_joylist_tail) {
+ SDL_joylist_tail = prev;
+ }
+
+ /* Need to decrement the joystick count before we post the event */
+ --numjoysticks;
+
+ SDL_PrivateJoystickRemoved(item->device_instance);
+ FreeJoylistItem(item);
+}
+
static int
MaybeRemoveDevice(const char *path)
{
@@ -356,30 +387,7 @@ MaybeRemoveDevice(const char *path)
/* found it, remove it. */
if (SDL_strcmp(path, item->path) == 0) {
const int retval = item->device_instance;
- if (item->hwdata) {
- item->hwdata->item = NULL;
- }
- if (prev != NULL) {
- prev->next = item->next;
- } else {
- SDL_assert(SDL_joylist == item);
- SDL_joylist = item->next;
- }
- if (item == SDL_joylist_tail) {
- SDL_joylist_tail = prev;
- }
-
- /* Need to decrement the joystick count before we post the event */
- --numjoysticks;
-
- SDL_PrivateJoystickRemoved(item->device_instance);
-
- if (item->mapping) {
- SDL_free(item->mapping);
- }
- SDL_free(item->path);
- SDL_free(item->name);
- SDL_free(item);
+ RemoveJoylistItem(item, prev);
return retval;
}
prev = item;
@@ -396,26 +404,7 @@ HandlePendingRemovals(void)
while (item != NULL) {
if (item->hwdata && item->hwdata->gone) {
- item->hwdata->item = NULL;
-
- if (prev != NULL) {
- prev->next = item->next;
- } else {
- SDL_assert(SDL_joylist == item);
- SDL_joylist = item->next;
- }
- if (item == SDL_joylist_tail) {
- SDL_joylist_tail = prev;
- }
-
- /* Need to decrement the joystick count before we post the event */
- --numjoysticks;
-
- SDL_PrivateJoystickRemoved(item->device_instance);
-
- SDL_free(item->path);
- SDL_free(item->name);
- SDL_free(item);
+ RemoveJoylistItem(item, prev);
if (prev != NULL) {
item = prev->next;
@@ -444,9 +433,7 @@ static SDL_bool SteamControllerConnectedCallback(const char *name, SDL_JoystickG
item->m_bSteamController = SDL_TRUE;
if ((item->path == NULL) || (item->name == NULL)) {
- SDL_free(item->path);
- SDL_free(item->name);
- SDL_free(item);
+ FreeJoylistItem(item);
return SDL_FALSE;
}
@@ -474,26 +461,7 @@ static void SteamControllerDisconnectedCallback(int device_instance)
for (item = SDL_joylist; item != NULL; item = item->next) {
/* found it, remove it. */
if (item->device_instance == device_instance) {
- if (item->hwdata) {
- item->hwdata->item = NULL;
- }
- if (prev != NULL) {
- prev->next = item->next;
- } else {
- SDL_assert(SDL_joylist == item);
- SDL_joylist = item->next;
- }
- if (item == SDL_joylist_tail) {
- SDL_joylist_tail = prev;
- }
-
- /* Need to decrement the joystick count before we post the event */
- --numjoysticks;
-
- SDL_PrivateJoystickRemoved(item->device_instance);
-
- SDL_free(item->name);
- SDL_free(item);
+ RemoveJoylistItem(item, prev);
return;
}
prev = item;
@@ -1405,9 +1373,7 @@ LINUX_JoystickQuit(void)
for (item = SDL_joylist; item; item = next) {
next = item->next;
- SDL_free(item->path);
- SDL_free(item->name);
- SDL_free(item);
+ FreeJoylistItem(item);
}
SDL_joylist = SDL_joylist_tail = NULL;