map: handle more error cases

This commit is contained in:
Wim Taymans 2019-05-15 15:19:25 +02:00
parent 448c1937ad
commit c348790ca1
4 changed files with 29 additions and 19 deletions

View file

@ -230,11 +230,13 @@ struct spa_bt_transport {
#define spa_bt_transport_set_implementation(t,_impl,_data) \
(t)->impl = SPA_CALLBACKS_INIT(_impl, _data)
#define spa_bt_transport_impl(t,m,v,...) \
({ \
int res = 0; \
spa_callbacks_call_res(&(t)->impl, struct spa_bt_transport_implementation, res, m, v, ##__VA_ARGS__); \
res; \
#define spa_bt_transport_impl(t,m,v,...) \
({ \
int res = 0; \
spa_callbacks_call_res(&(t)->impl, \
struct spa_bt_transport_implementation, \
res, m, v, ##__VA_ARGS__); \
res; \
})
#define spa_bt_transport_acquire(t,o) spa_bt_transport_impl(t, acquire, 0, o)

View file

@ -29,6 +29,8 @@
extern "C" {
#endif
#include <errno.h>
#include <spa/utils/defs.h>
/** \class pw_array
@ -94,7 +96,7 @@ static inline void pw_array_reset(struct pw_array *arr)
}
/** Make sure \a size bytes can be added to the array \memberof pw_array */
static inline bool pw_array_ensure_size(struct pw_array *arr, size_t size)
static inline int pw_array_ensure_size(struct pw_array *arr, size_t size)
{
size_t alloc, need;
@ -107,11 +109,11 @@ static inline bool pw_array_ensure_size(struct pw_array *arr, size_t size)
while (alloc < need)
alloc *= 2;
if (SPA_UNLIKELY((data = realloc(arr->data, alloc)) == NULL))
return false;
return -errno;
arr->data = data;
arr->alloc = alloc;
}
return true;
return 0;
}
/** Add \a ref size bytes to \a arr. A pointer to memory that can
@ -120,7 +122,7 @@ static inline void *pw_array_add(struct pw_array *arr, size_t size)
{
void *p;
if (!pw_array_ensure_size(arr, size))
if (pw_array_ensure_size(arr, size) < 0)
return NULL;
p = SPA_MEMBER(arr->data, arr->size, void);

View file

@ -30,6 +30,7 @@ extern "C" {
#endif
#include <string.h>
#include <errno.h>
#include <spa/utils/defs.h>
#include <pipewire/array.h>
@ -97,7 +98,8 @@ static inline void pw_map_reset(struct pw_map *map)
/** Insert data in the map
* \param map the map to insert into
* \param data the item to add
* \return the id where the item was inserted
* \return the id where the item was inserted or SPA_ID_INVALID when the
* item can not be inserted.
* \memberof pw_map
*/
static inline uint32_t pw_map_insert_new(struct pw_map *map, void *data)
@ -111,7 +113,7 @@ static inline uint32_t pw_map_insert_new(struct pw_map *map, void *data)
map->free_list = item->next;
} else {
item = (union pw_map_item *) pw_array_add(&map->items, sizeof(union pw_map_item));
if (!item)
if (item == NULL)
return SPA_ID_INVALID;
start = (union pw_map_item *) map->items.data;
}
@ -124,23 +126,27 @@ static inline uint32_t pw_map_insert_new(struct pw_map *map, void *data)
* \param map the map to inser into
* \param id the index to insert at
* \param data the data to insert
* \return true on success, false when the index is invalid
* \return 0 on success, -ENOSPC value when the index is invalid or a < 0
* errno value.
* \memberof pw_map
*/
static inline bool pw_map_insert_at(struct pw_map *map, uint32_t id, void *data)
static inline int pw_map_insert_at(struct pw_map *map, uint32_t id, void *data)
{
size_t size = pw_map_get_size(map);
union pw_map_item *item;
if (id > size)
return false;
else if (id == size)
return -ENOSPC;
else if (id == size) {
item = (union pw_map_item *) pw_array_add(&map->items, sizeof(union pw_map_item));
else
if (item == NULL)
return -errno;
}
else {
item = pw_map_get_item(map, id);
}
item->data = data;
return true;
return 0;
}
/** Remove an item at index

View file

@ -66,7 +66,7 @@ struct pw_resource *pw_resource_new(struct pw_client *client,
if (id == SPA_ID_INVALID) {
id = pw_map_insert_new(&client->objects, this);
} else if (!pw_map_insert_at(&client->objects, id, this))
} else if (pw_map_insert_at(&client->objects, id, this) < 0)
goto in_use;
this->id = id;