2015-10-19 20:30:54

by Szymon Janc

[permalink] [raw]
Subject: [RFC 0/4] Abort on small allocations

Hi,

This patchset is an initial work to simplify handling of memory allocation
failes in BlueZ. The solution is simple: don't check for errors and just
abort if allocation fails.

This make error paths (which were most likely never executed) *much* simpler.

Another benefit is increased similarity of src/shared APIs to already used
Glib APIs which also abort on allocation error (unless _try variant is used).
This makes transition from Glib APIs to shared APIs easier.

Patches 1-3 are samples that show how much of the error handling code
can be removed. Those are initial patches as 'never-fail' API propagates up
(new0()->queue_new()->foo_add()) and futher error handling can be removed.

Comments are welcome.

Szymon Janc (4):
shared: Make new0 abort on failure
shared: Remove dead code
android: Remove dead code
core: Remove dead code

android/avdtp.c | 5 +-
android/avdtptest.c | 4 -
android/bas.c | 55 +------
android/bluetooth.c | 17 ---
android/dis.c | 11 --
android/gatt.c | 336 +++++-------------------------------------
android/handsfree-client.c | 14 +-
android/handsfree.c | 9 +-
android/health.c | 67 ++-------
android/hog.c | 2 -
android/scpp.c | 22 +--
android/tester-a2dp.c | 7 +-
android/tester-avrcp.c | 7 +-
android/tester-bluetooth.c | 7 +-
android/tester-gatt.c | 7 +-
android/tester-hdp.c | 7 +-
android/tester-hidhost.c | 7 +-
android/tester-map-client.c | 7 +-
android/tester-pan.c | 7 +-
android/tester-socket.c | 7 +-
src/advertising.c | 6 -
src/gatt-client.c | 67 ---------
src/gatt-database.c | 100 -------------
src/shared/ad.c | 31 ----
src/shared/att.c | 34 +----
src/shared/crypto.c | 2 -
src/shared/gap.c | 7 -
src/shared/gatt-client.c | 59 --------
src/shared/gatt-db.c | 37 -----
src/shared/gatt-helpers.c | 24 ---
src/shared/gatt-server.c | 47 ------
src/shared/hci-crypto.c | 6 -
src/shared/hci.c | 29 ----
src/shared/hfp.c | 40 -----
src/shared/io-mainloop.c | 3 -
src/shared/mgmt.c | 42 ------
src/shared/queue.c | 12 --
src/shared/ringbuf.c | 3 -
src/shared/tester.c | 9 --
src/shared/timeout-mainloop.c | 3 -
src/shared/uhid.c | 8 -
src/shared/util.c | 16 ++
src/shared/util.h | 14 +-
43 files changed, 97 insertions(+), 1107 deletions(-)

--
2.5.0



2015-10-19 20:30:55

by Szymon Janc

[permalink] [raw]
Subject: [RFC 1/4] shared: Make new0 abort on failure

New is used to allocate small (typically much less than 1 page) and if
such allocation fails system is most likely in state where recovery is
unlikely. Also by default Linux follows an optimistic memory allocation
strategy with OOM killer.

Aborting on allocationg failure allows to significantly simplify error
paths (which were most likely never tested anyway) and thus makes code
easier to understand.

btd_malloc name is used as malloc wrapper so that it can be exported
by bluetoothd and used also in external plugins.
---
src/shared/util.c | 16 ++++++++++++++++
src/shared/util.h | 14 +++++++++++++-
2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/src/shared/util.c b/src/shared/util.c
index a70c709..7878552 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -37,6 +37,22 @@

#include "src/shared/util.h"

+void *btd_malloc(size_t size)
+{
+ if (__builtin_expect(!!size, 1)) {
+ void *ptr;
+
+ ptr = malloc(size);
+ if (ptr)
+ return ptr;
+
+ fprintf(stderr, "failed to allocate %zu bytes\n", size);
+ abort();
+ }
+
+ return NULL;
+}
+
void util_debug(util_debug_func_t function, void *user_data,
const char *format, ...)
{
diff --git a/src/shared/util.h b/src/shared/util.h
index 65f5359..ff705d0 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -25,6 +25,7 @@
#include <stdlib.h>
#include <alloca.h>
#include <byteswap.h>
+#include <string.h>

#if __BYTE_ORDER == __LITTLE_ENDIAN
#define le16_to_cpu(val) (val)
@@ -78,10 +79,21 @@ do { \
#define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
#define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))

-#define new0(t, n) ((t*) calloc((n), sizeof(t)))
+#define new0(type, count) \
+ (type *) (__extension__ ({ \
+ size_t __n = (size_t) (count); \
+ size_t __s = sizeof(type); \
+ void *__p; \
+ __p = btd_malloc(__n * __s); \
+ memset(__p, 0, __n * __s); \
+ __p; \
+ }))
+
#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
#define malloc0(n) (calloc((n), 1))

+void *btd_malloc(size_t size);
+
typedef void (*util_debug_func_t)(const char *str, void *user_data);

void util_debug(util_debug_func_t function, void *user_data,
--
2.5.0