/* Copyright (C) 2000, 2001  SWsoft, Singapore
 *
 *  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 2 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*  $Id: buddy.h,v 1.1 2001/11/10 15:38:31 al Exp $
    Author : Alexander F. Avdonkin
*/

#ifndef _BUDDY_
#define _BUDDY_

#include <pthread.h>
#include <sys/user.h>
#include <string.h>

#include "defines.h"

typedef struct
{
	ULONG m_flag;
	void* m_next;
	void** m_prev;
	int m_pool;
} FreeHead;

class CBuddyHeap
{
public:
	pthread_mutex_t m_mutex;
	void* m_pools[PAGE_SHIFT - 3];
	unsigned long m_pgCount;
	unsigned long m_allocated;
	
public:
	CBuddyHeap()
	{
		m_pgCount = 0;
		m_allocated = 0;
		memset(m_pools, 0, sizeof(m_pools));
		pthread_mutex_init(&m_mutex, NULL);
	}
	~CBuddyHeap()
	{
		pthread_mutex_destroy(&m_mutex);
	}
	void* Alloc(int pool);
	void Free(void* ptr, int pool);
	void* AllocPage();
	void FreePage(void* page);
	void* AllocBytes(ULONG size);
	void FreeBytes(void* ptr, ULONG size);
};

class CWordBuddyVector
{
public:
	WORD* m_data;
	int m_size;
	int m_allocated;
	CBuddyHeap* m_heap;
	
public:
	CWordBuddyVector()
	{
		m_data = NULL;
		m_size = 1;
		m_allocated = 0;
		m_heap = NULL;
	}
	~CWordBuddyVector();
	WORD* begin()
	{
		return m_data + 1;
	}
	WORD* end()
	{
		return m_data + m_size;
	}
	int size()
	{
		return m_size - 1;
	}
	void push_back(WORD value);
};

//extern CBuddyHeap vwHeap;

#endif
