/* 
 *	Copyright (C) Chia-chen Kuo - April 2001
 *
 *  This file is part of DVD2AVI, a free MPEG-2 decoder
 *	
 *  DVD2AVI 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 2, or (at your option)
 *  any later version.
 *   
 *  DVD2AVI 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 GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 */

// replace with one that doesn't need fixed size table - trbarry 3-22-2002

#include <stdlib.h>
#include "alloc.h"
#include <windows.h>


struct B { BYTE* yy; };

/*extern "C" */void *aligned_malloc(size_t size, size_t alignment)
{
	size_t algn = 4;
	size_t mask = 0xfffffffc;
	B* pB;

	BYTE* x;
	BYTE* y;

	while (algn < alignment)		// align to next power of 2
	{
		algn <<= 1;
		mask <<= 1;
	}

	x = (BYTE*)malloc(size+algn);
	y = (BYTE*) (((unsigned int) (x+algn) & mask) - 4);
	pB = (B*) y;
	pB->yy = x;
	return  y+4;
}

/*extern "C"*/ void aligned_free(void *x)
{
	struct B { BYTE* yy; };
	B* pB = (B*) ((BYTE*)x-4);
	free(pB->yy);
}

