devices: Use new(std::nothrow) for creating devices
Namhyung Kim
When allocation fails, just skip the device and proceed if possible. This is also for consistency with others.
--- src/devices/ahci.cpp | 5 ++++- src/devices/alsa.cpp | 5 ++++- src/devices/backlight.cpp | 5 ++++- src/devices/i915-gpu.cpp | 6 ++++-- src/devices/rfkill.cpp | 5 ++++- src/devices/runtime_pm.cpp | 4 +++- src/devices/thinkpad-fan.cpp | 8 +++++--- src/devices/thinkpad-light.cpp | 6 ++++-- src/devices/usb.cpp | 6 ++++-- 9 files changed, 36 insertions(+), 14 deletions(-)
src/devices/ahci.cpp +4 -1
@@ -275,7 +275,10 @@ void create_all_ahcis(void) file.close(); sprintf(filename, "/sys/class/scsi_host/%s", entry->d_name); - bl = new class ahci(entry->d_name, filename); + bl = new(std::nothrow) class ahci(entry->d_name, filename); + if (!bl) + continue; + all_devices.push_back(bl); register_parameter("ahci-link-power-active", 0.6); /* active sata link takes about 0.6 W */ register_parameter("ahci-link-power-partial");
src/devices/alsa.cpp +4 -1
@@ -176,7 +176,10 @@ void create_all_alsa(void) sprintf(filename, "/sys/class/sound/card0/%s", entry->d_name); - bl = new class alsa(entry->d_name, filename); + bl = new(std::nothrow) class alsa(entry->d_name, filename); + if (!bl) + continue; + all_devices.push_back(bl); register_parameter("alsa-codec-power", 0.5); }
src/devices/backlight.cpp +4 -1
@@ -171,7 +171,10 @@ void create_all_backlights(void) if (entry->d_name[0] == '.') continue; sprintf(filename, "/sys/class/backlight/%s", entry->d_name); - bl = new class backlight(entry->d_name, filename); + bl = new(std::nothrow) class backlight(entry->d_name, filename); + if (!bl) + continue; + all_devices.push_back(bl); register_parameter("backlight"); register_parameter("backlight-power");
src/devices/i915-gpu.cpp +4 -2
@@ -76,10 +76,12 @@ void create_i915_gpu(void) return; } - register_parameter("gpu-operations"); + gpu = new(std::nothrow) class i915gpu(); + if (!gpu) + return; - gpu = new class i915gpu(); all_devices.push_back(gpu); + register_parameter("gpu-operations"); }
src/devices/rfkill.cpp +4 -1
@@ -163,7 +163,10 @@ void create_all_rfkills(void) } sprintf(filename, "/sys/class/rfkill/%s", entry->d_name); - bl = new class rfkill(name, filename); + bl = new(std::nothrow) class rfkill(name, filename); + if (!bl) + continue; + all_devices.push_back(bl); } closedir(dir);
src/devices/runtime_pm.cpp +3 -1
@@ -199,7 +199,9 @@ static void do_bus(const char *bus) if (!device_has_runtime_pm(filename)) continue; - dev = new class runtime_pmdevice(entry->d_name, filename); + dev = new(std::nothrow) class runtime_pmdevice(entry->d_name, filename); + if (!dev) + continue; if (strcmp(bus, "pci") == 0) { uint16_t vendor = 0, device = 0;
src/devices/thinkpad-fan.cpp +5 -3
@@ -82,12 +82,14 @@ void create_thinkpad_fan(void) if (access(filename, R_OK) !=0) return; + fan = new(std::nothrow) class thinkpad_fan(); + if (!fan) + return; + + all_devices.push_back(fan); register_parameter("thinkpad-fan", 10); register_parameter("thinkpad-fan-sqr", 5); register_parameter("thinkpad-fan-cub", 10); - - fan = new class thinkpad_fan(); - all_devices.push_back(fan); }
src/devices/thinkpad-light.cpp +4 -2
@@ -80,10 +80,12 @@ void create_thinkpad_light(void) if (access(filename, R_OK) !=0) return; - register_parameter("thinkpad-light", 10); + light = new(std::nothrow) class thinkpad_light(); + if (!light) + return; - light = new class thinkpad_light(); all_devices.push_back(light); + register_parameter("thinkpad-light", 10); }
src/devices/usb.cpp +4 -2
@@ -233,9 +233,11 @@ void create_all_usb_devices(void) if (result_device_exists(device_name)) continue; - usb = new class usbdevice(device_name, filename, devid_name); - all_devices.push_back(usb); + usb = new(std::nothrow) class usbdevice(device_name, filename, devid_name); + if (!usb) + continue; + all_devices.push_back(usb); register_parameter(devid_name, 0.1); } closedir(dir);
Signature · 1 line
1.7.9.2
Arjan van de Ven
Quoted 2 lines · Namhyung Kim
When allocation fails, just skip the device and proceed if possible. This is also for consistency with others.
hmm...
if we're that low on memory something is so seriously wrong, exiting is the right answer really.
I'm getting strong advice from other userspace coders that checking allocations like this is actually pointless...
based on that, I'd say we need to go for better code readability, not more complexity for no actual gain.
Namhyung Kim
2012-10-18 (목), 08:36 -0700, Arjan van de Ven:
Quoted 11 lines · Namhyung Kim
Quoted 2 lines
When allocation fails, just skip the device and proceed if possible. This is also for consistency with others.
hmm...
if we're that low on memory something is so seriously wrong, exiting is the right answer really.I'm getting strong advice from other userspace coders that checking allocations like this is actually pointless...
based on that, I'd say we need to go for better code readability, not more complexity for no actual gain.
Understood. Thanks for the quick reply!
Namhyung