/*
    Copyright 2000 (c) by Claudio Jeker,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public 
    Licence. A full copy of the GNU GPL is provided in the file COPYING 
    found in the development root of Topsy.
*/
/*
	
    File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/Net/NetMemoryManager.h,v $
    Affiliation:           ETH Zuerich, TIK
    Version:               $Revision: 2922 $
    Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $  by: $Author: baderma $
 
*/
#ifndef _NETMEMORYMANAGER_H_
#define _NETMEMORYMANAGER_H_

#include "Topsy.h"

//#define NMM_DEBUG 1

#define NET_MMBUF_SIZE \
			(NETBUF_POOLSIZE - sizeof(NetBufsPoolHdr) - NETBUF_HEADERSIZE)
/* size of MMBuffer, greatest NetBuf */

typedef struct NetMMEntryDesc_t *NetMMEntry;

typedef struct NetMMEntryDesc_t
{
#ifdef NMM_DEBUG
  long		magic_pre[3];
#endif

  short	next;	/* position in NetBuf of next Entry */
  short	prev;	/* position in NetBuf of prev Entry */
  short	len;	/* length of this mem area < NET_MMBUF_MAX_SIZE */
  short	flags;	/* flags ( used / free ) */
#ifdef NMM_DEBUG

  long		magic_post[3];
#endif
}
NetMMEntryDesc;

#define NET_MMBUF_MAX_SIZE (NET_MMBUF_SIZE - sizeof(NetMMEntryDesc))

#define NMM_FREE 0x1
#define NMM_USED 0x10

#define NMM_FAILED 0
#define NMM_OK 1

void netMMInit(void);
int netMMAlloc(Address *buf, int size);
void netMMFree(Address buf);
#ifdef NMM_DEBUG
void checkMEM(Address from, int size, Boolean ok);
#endif
void dumpMEM();

#define IS_NMM_FREE(x) ((x & NMM_FREE) == NMM_FREE)
#define NMM_THIS(x, y) (short)((Address)y - (Address) x)

#endif
