<commit_before>#include <string.h>
#include "constructors.h"

struct StrictlyProperBlock * block_new(struct StrictlyProperBlock const stackb)
{
	struct StrictlyProperBlock * heapb = malloc(sizeof(struct StrictlyProperBlock));
	if (heapb)
		memcpy(heapb, &stackb, sizeof(struct StrictlyProperBlock));
	return heapb;
}

void block_free(struct StrictlyProperBlock * heapb)
{
	free(heapb);
}

struct BlockSystemStorage * blockSystemStorage_new
(
	size_t const numBlocks,
	struct StrictlyProperBlock * const blocks,
	CalcBlockInputsFunction const calcBlockInputs,
	CalcSystemOutputFunction const calcSystemOutputs,
	void * const systemStorage
)
{
	size_t totalBlockInputs = 0;
	size_t totalBlockOutputs = 0;
	for (size_t i = 0; i < numBlocks; i++)
	{
		totalBlockInputs += blocks[i].numInputs;
		totalBlockOutputs += blocks[i].numOutputs;
	}

	double * storage = malloc((totalBlockInputs + totalBlockOutputs) * sizeof(double));
	double ** input_storage = malloc(numBlocks * sizeof(double*));
	double ** output_storage = malloc(numBlocks * sizeof(double*));
	struct BlockSystemStorage * const bheap = malloc(sizeof(struct BlockSystemStorage));

	if (storage == NULL || input_storage == NULL || output_storage == NULL || bheap == NULL)
	{
#define freeif(ptr) if (ptr) free(ptr)
		freeif(storage);
		freeif(input_storage);
		freeif(output_storage);
		freeif(bheap);
#undef freeif
		return NULL;
	}

	size_t inputi = 0;
	size_t outputi = totalBlockInputs;
	for (size_t i = 0; i < numBlocks; i++)
	{
		input_storage[i] = &storage[inputi];
		output_storage[i] = &storage[outputi];
		inputi += blocks[i].numInputs;
		outputi += blocks[i].numOutputs;
	}

	struct BlockSystemStorage bstack;
	bstack.numBlocks = numBlocks;
	bstack.blocks = blocks;
	bstack.blockInputs = input_storage;
	bstack.blockOutputs = output_storage;
	bstack.calcBlockInputs = calcBlockInputs;
	bstack.calcSystemOutput = calcSystemOutputs;
	bstack.systemStorage = systemStorage;

	memcpy(bheap, &bstack, sizeof(struct BlockSystemStorage));
	return bheap;
}

void blockSystemStorage_free(struct BlockSystemStorage * storage)
{
	free(storage->blockInputs[0]);
	free(storage->blockInputs);
	free(storage->blockOutputs);
	free(storage);
}<commit_msg>reddit comments<commit_after>#include <string.h>
#include "constructors.h"

struct StrictlyProperBlock * block_new(struct StrictlyProperBlock const stackb)
{
	struct StrictlyProperBlock * heapb = malloc(sizeof(struct StrictlyProperBlock));
	if (heapb)
		*heapb = stackb;
	return heapb;
}

void block_free(struct StrictlyProperBlock * heapb)
{
	free(heapb);
}

struct BlockSystemStorage * blockSystemStorage_new
(
	size_t const numBlocks,
	struct StrictlyProperBlock * const blocks,
	CalcBlockInputsFunction const calcBlockInputs,
	CalcSystemOutputFunction const calcSystemOutputs,
	void * const systemStorage
)
{
	size_t totalBlockInputs = 0;
	size_t totalBlockOutputs = 0;
	for (size_t i = 0; i < numBlocks; i++)
	{
		totalBlockInputs += blocks[i].numInputs;
		totalBlockOutputs += blocks[i].numOutputs;
	}

	double * storage = malloc((totalBlockInputs + totalBlockOutputs) * sizeof(double));
	double ** input_storage = malloc(numBlocks * sizeof(double*));
	double ** output_storage = malloc(numBlocks * sizeof(double*));
	struct BlockSystemStorage * const bheap = malloc(sizeof(struct BlockSystemStorage));

	if (storage == NULL || input_storage == NULL || output_storage == NULL || bheap == NULL)
	{
		free(storage);
		free(input_storage);
		free(output_storage);
		free(bheap);
		return NULL;
	}

	size_t inputi = 0;
	size_t outputi = totalBlockInputs;
	for (size_t i = 0; i < numBlocks; i++)
	{
		input_storage[i] = &storage[inputi];
		output_storage[i] = &storage[outputi];
		inputi += blocks[i].numInputs;
		outputi += blocks[i].numOutputs;
	}

	struct BlockSystemStorage bstack;
	bstack.numBlocks = numBlocks;
	bstack.blocks = blocks;
	bstack.blockInputs = input_storage;
	bstack.blockOutputs = output_storage;
	bstack.calcBlockInputs = calcBlockInputs;
	bstack.calcSystemOutput = calcSystemOutputs;
	bstack.systemStorage = systemStorage;

	*bheap = bstack;
	return bheap;
}

void blockSystemStorage_free(struct BlockSystemStorage * storage)
{
	free(storage->blockInputs[0]);
	free(storage->blockInputs);
	free(storage->blockOutputs);
	free(storage);
}
{'fraction_non_alphanumeric': 0.11397748592870544, 'fraction_numerical': 0.0028142589118198874, 'mean_word_length': 5.980360065466448, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '3996190', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1443, 'n_words': 304}