<commit_before>#include "header.h"


void write_header(Header h, unsigned int addr) {
	memory_copy((char*)&h, (char*)addr, (int) sizeof(Header));
}

void write_block(Block b, unsigned int addr) {
	memory_copy((char*)&b, (char*)addr, (int) sizeof(Block));	
}

void init_pages(unsigned int size) {
	float curve[len_curve] = {
		0.0019, // 2^5 = 32
		0.0056, // 2^6 = 64
		0.0146, // 2^7 = 128
		0.032 , // 2^8 = 256
		0.0603, // 2^9 = 512
		0.0968, // 2^10 = 1024
		0.1327, // 2^11 = 2048
		0.1554, // 2^12 = 4096
		0.1554, // 2^13 = 8192
		0.1327, // 2^14 = 16384
		0.0968, // 2^15 = 32768
		0.0603, // 2^16 = 65536
		0.032,  // 2^17 = 131072
		0.0146, // 2^18 = 262144
		0.0056, // 2^19 = 524288
		0.0019, // 2^20 = 1048576
	};

	int amounts = 0;
	float smallest_data_size = 1 << (5); // 2^5
	for (int i = 0; i < len_curve; i++) {
		float data_size = 1 << (i+5); // 2^(i+5)
		float frames = (size/smallest_data_size);
		int amount = (curve[i]*frames)/data_size;
		amounts += amount;
	}
	unsigned int data_addr = base + len_curve*(sizeof(Block)) + amounts*(sizeof(Header));
	unsigned int header_addr = base + len_curve*(sizeof(Block));
	unsigned int block_addr = base;
	for (int i = 0; i < len_curve; ++i) {	//for each block
		float data_size = 1 << (i+5); // 2^(i+5)
		float frames = (size/smallest_data_size); //(size/data_size);
		int amount = (curve[i]*frames)/data_size;
		unsigned int first_header_of_block_addr = header_addr;
		for (int j = 0; j < amount; ++j) {
			Header pageStart;
			pageStart.addr = data_addr;
			pageStart.data_size = data_size;
			pageStart.used = 0;
			data_addr += data_size;
			write_header(pageStart, header_addr);
			header_addr += sizeof(Header);
		}

		Block b;
		b.headers = (Header*)first_header_of_block_addr;
		b.len = amount;
		// print("Set len of block to: ");
		// char str[200];
		// itoa(amount, str);
		// print(str);
		// print("\n");
		write_block(b, block_addr);
		block_addr += sizeof(Block);
	}
}

char* malloc(unsigned int n_bytes) {
	Block* blocks = (Block* ) base;
	char block = find_category(n_bytes);
	if(block == -1) {
		//error handling
		print("CATEGORY NOT FOUND\n");
		return 0;
	}
	Block correct_block = *(Block *)(base + block*sizeof(Block));//blocks[block];
	for (int i = 0; i < correct_block.len; ++i)	{
		Header h = correct_block.headers[i];
		if(h.used == 0) {	//if h is not being used at the moment
			h.used = 1;	//mark as used
			// print("\nALLOCATED HEADER WITH ADDR: ");
			// char tmp[20]; itoa(h.addr, tmp); print(tmp); print("\n");
			return h.addr;	//return the space allocated for your use.
		}
	}

	print("Something went wrong returned null\n");
	return 0;
}

void free(unsigned int addr) {
	Block* blocks = (Block* ) base;
	for(unsigned int i = 0; i < len_curve; i++) {
		Block tested_block = *(Block *)(base + i*sizeof(Block));//blocks[block];
		for(unsigned int j = 0; j < tested_block.len; j++) {
			Header h = tested_block.headers[j];<commit_msg>Fixed malloc and free accessing blocks through array<commit_after>#include "header.h"


void write_header(Header h, unsigned int addr) {
	memory_copy((char*)&h, (char*)addr, (int) sizeof(Header));
}

void write_block(Block b, unsigned int addr) {
	memory_copy((char*)&b, (char*)addr, (int) sizeof(Block));	
}

void init_pages(unsigned int size) {
	float curve[len_curve] = {
		0.0019, // 2^5 = 32
		0.0056, // 2^6 = 64
		0.0146, // 2^7 = 128
		0.032 , // 2^8 = 256
		0.0603, // 2^9 = 512
		0.0968, // 2^10 = 1024
		0.1327, // 2^11 = 2048
		0.1554, // 2^12 = 4096
		0.1554, // 2^13 = 8192
		0.1327, // 2^14 = 16384
		0.0968, // 2^15 = 32768
		0.0603, // 2^16 = 65536
		0.032,  // 2^17 = 131072
		0.0146, // 2^18 = 262144
		0.0056, // 2^19 = 524288
		0.0019, // 2^20 = 1048576
	};

	int amounts = 0;
	float smallest_data_size = 1 << (5); // 2^5
	for (int i = 0; i < len_curve; i++) {
		float data_size = 1 << (i+5); // 2^(i+5)
		float frames = (size/smallest_data_size);
		int amount = (curve[i]*frames)/data_size;
		amounts += amount;
	}
	unsigned int data_addr = base + len_curve*(sizeof(Block)) + amounts*(sizeof(Header));
	unsigned int header_addr = base + len_curve*(sizeof(Block));
	unsigned int block_addr = base;
	for (int i = 0; i < len_curve; ++i) {	//for each block
		float data_size = 1 << (i+5); // 2^(i+5)
		float frames = (size/smallest_data_size); //(size/data_size);
		int amount = (curve[i]*frames)/data_size;
		unsigned int first_header_of_block_addr = header_addr;
		for (int j = 0; j < amount; ++j) {
			Header pageStart;
			pageStart.addr = data_addr;
			pageStart.data_size = data_size;
			pageStart.used = 0;
			data_addr += data_size;
			write_header(pageStart, header_addr);
			header_addr += sizeof(Header);
		}

		Block b;
		b.headers = (Header*)first_header_of_block_addr;
		b.len = amount;
		// print("Set len of block to: ");
		// char str[200];
		// itoa(amount, str);
		// print(str);
		// print("\n");
		write_block(b, block_addr);
		block_addr += sizeof(Block);
	}
}

char* malloc(unsigned int n_bytes) {
	Block* blocks = (Block* ) base;
	char block = find_category(n_bytes);
	if(block == -1) {
		//error handling
		print("CATEGORY NOT FOUND\n");
		return 0;
	}
	Block correct_block = blocks[block];
	for (int i = 0; i < correct_block.len; ++i)	{
		Header h = correct_block.headers[i];
		if(h.used == 0) {	//if h is not being used at the moment
			h.used = 1;	//mark as used
			// print("\nALLOCATED HEADER WITH ADDR: ");
			// char tmp[20]; itoa(h.addr, tmp); print(tmp); print("\n");
			return h.addr;	//return the space allocated for your use.
		}
	}

	print("Something went wrong returned null\n");
	return 0;
}

void free(unsigned int addr) {
	Block* blocks = (Block* ) base;
	for(unsigned int i = 0; i < len_curve; i++) {
		Block tested_block = blocks[i];
		for(unsigned int j = 0; j < tested_block.len; j++) {
			Header h = tested_block.headers[j];
{'fraction_non_alphanumeric': 0.17631895514693247, 'fraction_numerical': 0.07561436672967864, 'mean_word_length': 3.2481751824817517, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 3, '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': '3658816', 'n_tokens_mistral': 2716, 'n_tokens_neox': 2269, 'n_words': 590}