diff --git a/librz/analysis/similarity.c b/librz/analysis/similarity.c index bb722955672..36914f5aec1 100644 --- a/librz/analysis/similarity.c +++ b/librz/analysis/similarity.c @@ -45,7 +45,7 @@ typedef struct match_ui_info_t { static bool shared_context_init(SharedContext *context, RzAnalysis *analysis_a, RzAnalysis *analysis_b, RzList /**/ *list_a, RzList /**/ *list_b, AllocateBuffer alloc_cb) { RzThreadLock *lock_a = rz_th_lock_new(true); RzThreadLock *lock_b = analysis_a == analysis_b ? lock_a : rz_th_lock_new(true); - RzThreadQueue *queue = rz_th_queue_new2(rz_list_clone(list_a)); + RzThreadQueue *queue = rz_th_queue_from_list(list_a, NULL); RzThreadQueue *matches = rz_th_queue_new(RZ_THREAD_QUEUE_UNLIMITED, NULL); RzThreadQueue *unmatch = rz_th_queue_new(RZ_THREAD_QUEUE_UNLIMITED, NULL); RzAtomicBool *loop = rz_atomic_bool_new(true); diff --git a/librz/include/rz_th.h b/librz/include/rz_th.h index 070afbc7f17..913667092c6 100644 --- a/librz/include/rz_th.h +++ b/librz/include/rz_th.h @@ -72,7 +72,8 @@ RZ_API bool rz_th_pool_wait(RZ_NONNULL RzThreadPool *pool); RZ_API size_t rz_th_pool_size(RZ_NONNULL RzThreadPool *pool); RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new(size_t max_size, RZ_NULLABLE RzListFree qfree); -RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new2(RZ_NONNULL RZ_OWN RzList /**/ *list); +RZ_API RZ_OWN RzThreadQueue *rz_th_queue_from_list(RZ_NONNULL RZ_BORROW RzList /**/ *list, RZ_NULLABLE RzListFree qfree); +RZ_API RZ_OWN RzThreadQueue *rz_th_queue_from_pvector(RZ_NONNULL RZ_BORROW RzPVector /**/ *vector, RZ_NULLABLE RzListFree qfree); RZ_API void rz_th_queue_free(RZ_NULLABLE RzThreadQueue *queue); RZ_API bool rz_th_queue_push(RZ_NONNULL RzThreadQueue *queue, RZ_NONNULL void *user, bool tail); RZ_API RZ_OWN void *rz_th_queue_pop(RZ_NONNULL RzThreadQueue *queue, bool tail); diff --git a/librz/util/thread_queue.c b/librz/util/thread_queue.c index b56d6436cdc..2248cf5f274 100644 --- a/librz/util/thread_queue.c +++ b/librz/util/thread_queue.c @@ -50,19 +50,25 @@ RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new(size_t max_size, RZ_NULLABLE RzList /** * \brief Allocates and initializes a new fifo queue using a user-defined list * - * \param list Pointer to the list that will be owned by the queue. + * \param list Pointer to the list that will be used to initialize the queue. * * \return On success returns a valid pointer, otherwise NULL */ -RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new2(RZ_NONNULL RZ_OWN RzList /**/ *list) { +RZ_API RZ_OWN RzThreadQueue *rz_th_queue_from_list(RZ_NONNULL RZ_BORROW RzList /**/ *list, RZ_NULLABLE RzListFree qfree) { rz_return_val_if_fail(list, NULL); RzThreadQueue *queue = RZ_NEW0(RzThreadQueue); if (!queue) { return NULL; } + queue->list = rz_list_clone(list); + if (!queue->list) { + free(queue); + return NULL; + } + + queue->list->free = qfree; queue->max_size = rz_list_length(list); - queue->list = list; queue->lock = rz_th_lock_new(false); queue->cond = rz_th_cond_new(); if (!queue->list || !queue->lock || !queue->cond) { @@ -73,6 +79,36 @@ RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new2(RZ_NONNULL RZ_OWN RzList /**/ *vector, RZ_NULLABLE RzListFree qfree) { + rz_return_val_if_fail(vector, NULL); + RzThreadQueue *queue = rz_th_queue_new(rz_pvector_len(vector), qfree); + if (!queue) { + return NULL; + } + + void **it; + rz_pvector_foreach (vector, it) { + void *value = *it; + if (!value) { + continue; + } + if (!rz_list_append(queue->list, value)) { + rz_th_queue_free(queue); + return NULL; + } + } + + return queue; +} + /** * \brief Frees a RzThreadQueue structure *