<commit_before>#include "db.h"

static void list_newlist(list_head_t *list) {
	list->head = (list_node_t *)&list->tail;
	list->tail = NULL;
	list->tailpred = (list_node_t *)list;
}

static void list_addtail(list_head_t *list, list_node_t *node) {
	node->succ = (list_node_t *)&list->tail;
	node->pred = list->tailpred;
	list->tailpred->succ = node;
	list->tailpred = node;
}

static void list_remove(list_node_t *node) {
	node->pred->succ = node->succ;
	node->succ->pred = node->pred;
}

int c_init(connection_t **res_conn, int sock, user_t *user,
           prot_err_func_t error) {
	connection_t *conn;

	conn = malloc(sizeof(*conn));
	if (!conn) return 1;
	memset(conn, 0, sizeof(*conn));
	list_newlist(&conn->mem_list);
	conn->mem_used = sizeof(*conn);
	conn->sock  = sock;
	conn->user  = user;
	conn->error = error;
	conn->flags = CONNFLAG_GOING;
	*res_conn = conn;
	return 0;
}

void c_cleanup(connection_t *conn) {
	list_node_t *node;
	list_node_t *next;

	node = conn->mem_list.head;
	while ((next = node->succ)) {
		free(node);
		node = next;
	}
	free(conn);
}

int c_alloc(connection_t *conn, void **res, unsigned int size) {
	unsigned int new_used;
	unsigned int new_size;
	void *mem;

	new_size = size + sizeof(list_node_t);
	assert(new_size > size);
	new_used = conn->mem_used + new_size;
	assert(new_used > conn->mem_used && new_used > new_size);
	mem = malloc(size);
	if (mem) {
		list_node_t *node = mem;
		list_addtail(&conn->mem_list, node);
		node->size = size;
		conn->mem_used = new_used;
		*res = node + 1;
		return 0;
	} else {
		*res = NULL;<commit_msg>Fix c_alloc() to not allocate less memory than asked for.<commit_after>#include "db.h"

static void list_newlist(list_head_t *list) {
	list->head = (list_node_t *)&list->tail;
	list->tail = NULL;
	list->tailpred = (list_node_t *)list;
}

static void list_addtail(list_head_t *list, list_node_t *node) {
	node->succ = (list_node_t *)&list->tail;
	node->pred = list->tailpred;
	list->tailpred->succ = node;
	list->tailpred = node;
}

static void list_remove(list_node_t *node) {
	node->pred->succ = node->succ;
	node->succ->pred = node->pred;
}

int c_init(connection_t **res_conn, int sock, user_t *user,
           prot_err_func_t error) {
	connection_t *conn;

	conn = malloc(sizeof(*conn));
	if (!conn) return 1;
	memset(conn, 0, sizeof(*conn));
	list_newlist(&conn->mem_list);
	conn->mem_used = sizeof(*conn);
	conn->sock  = sock;
	conn->user  = user;
	conn->error = error;
	conn->flags = CONNFLAG_GOING;
	*res_conn = conn;
	return 0;
}

void c_cleanup(connection_t *conn) {
	list_node_t *node;
	list_node_t *next;

	node = conn->mem_list.head;
	while ((next = node->succ)) {
		free(node);
		node = next;
	}
	free(conn);
}

int c_alloc(connection_t *conn, void **res, unsigned int size) {
	unsigned int new_used;
	unsigned int new_size;
	void *mem;

	new_size = size + sizeof(list_node_t);
	assert(new_size > size);
	new_used = conn->mem_used + new_size;
	assert(new_used > conn->mem_used && new_used > new_size);
	mem = malloc(new_size);
	if (mem) {
		list_node_t *node = mem;
		list_addtail(&conn->mem_list, node);
		node->size = size;
		conn->mem_used = new_used;
		*res = node + 1;
		return 0;
	} else {
		*res = NULL;
{'fraction_non_alphanumeric': 0.15833853841349158, 'fraction_numerical': 0.003123048094940662, 'mean_word_length': 4.329450915141431, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 69, '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': '3975413', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1271, 'n_words': 282}