<commit_before>#include <limits.h>
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include "prioq.h"
#include "thread-utils.h"

int prioq_init(struct prioq* self, size_t size)
{
	self->sequence = 0;
	self->size = size;
	self->index = 0;
	self->head = calloc(size, sizeof(*self->head));

	pthread_mutex_init(&self->mutex, NULL);
	pthread_cond_init(&self->suspend_cond, NULL);

	return self->head ? 0 : -1;
}

void prioq_clear(struct prioq* self)
{
	pthread_mutex_destroy(&self->mutex);
	pthread_cond_destroy(&self->suspend_cond);
	free(self->head);
}

int prioq_grow(struct prioq* self, size_t size)
{
	if (self->size >= size)
		return 0;

	struct prioq_elem* new_head = realloc(self->head,
					      size*sizeof(*self->head));
	if (!new_head)
		return -1;

	self->head = new_head;

	return 1;
}

int prioq_insert(struct prioq* self, unsigned long priority, void* data)
{
	_prioq_lock(self);

	if (self->index >= self->size)
		if (prioq_grow(self, self->size*2))
			return -1;
<commit_msg>prioq: set the size of the buffer when growing<commit_after>#include <limits.h>
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include "prioq.h"
#include "thread-utils.h"

int prioq_init(struct prioq* self, size_t size)
{
	self->sequence = 0;
	self->size = size;
	self->index = 0;
	self->head = calloc(size, sizeof(*self->head));

	pthread_mutex_init(&self->mutex, NULL);
	pthread_cond_init(&self->suspend_cond, NULL);

	return self->head ? 0 : -1;
}

void prioq_clear(struct prioq* self)
{
	pthread_mutex_destroy(&self->mutex);
	pthread_cond_destroy(&self->suspend_cond);
	free(self->head);
}

int prioq_grow(struct prioq* self, size_t size)
{
	if (self->size >= size)
		return 0;

	struct prioq_elem* new_head = realloc(self->head,
					      size*sizeof(*self->head));
	if (!new_head)
		return -1;

	self->size = size;
	self->head = new_head;

	return 1;
}

int prioq_insert(struct prioq* self, unsigned long priority, void* data)
{
	_prioq_lock(self);

	if (self->index >= self->size)
		if (prioq_grow(self, self->size*2))
			return -1;

{'fraction_non_alphanumeric': 0.16285853184248905, 'fraction_numerical': 0.008750607681088965, 'mean_word_length': 4.622950819672131, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5468298', 'n_tokens_mistral': 862, 'n_tokens_neox': 823, 'n_words': 178}