/*
 *  Copyright (C) 2015 - Preacher
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

unsigned int get_chunk_size(unsigned int size);

int main(void)
{
	char *p0 = malloc(30);
	char *p1 = malloc(25);
	char *p2 = malloc(133);

	puts("ptr\t\tstart_addr\t\tend_addr\t\tfound_start\t\tfound_end\n");

	printf("p0\t\t0x%08X\t\t0x%08X\t\t0x%08X\t\t0x%08X\n", p0, ((*(unsigned int *)(p0-sizeof(int *))) & ~7) + p0, p0, p0 + get_chunk_size(30));
	printf("p1\t\t0x%08X\t\t0x%08X\t\t0x%08X\t\t0x%08X\n", p1, ((*(unsigned int *)(p1-sizeof(int *))) & ~7) + p1, p0 + get_chunk_size(30), p1 + get_chunk_size(25));
	printf("p2\t\t0x%08X\t\t0x%08X\t\t0x%08X\t\t0x%08X\n", p2, ((*(unsigned int *)(p2-sizeof(int *))) & ~7) + p2, p1 + get_chunk_size(25), p2 + get_chunk_size(133));

	return 0;
}

unsigned int get_chunk_size(unsigned int size)
{
	size += sizeof(int *);

	for(; (size%(sizeof(int *)*2)); size++);

	return size;
}
