/*
    Copyright 2001 (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/VMemory/Attic/vm_map.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 __VM_MAP_H__
#define __VM_MAP_H__

#include "Lock.h"

#include "vm_pmap.h"
#include "doubleq.h"
#include "vm_object.h"

struct vm_region
{
  DOUBLEQ_ENTRY(vm_region) regionq;

  address_t				start;	/* virtual start address of region */
  address_t				end;		/* virtual end address of region */

  struct vm_oref	oref;		/* underlying object */

  unsigned short	flags;		/* XXX additional flags */
  unsigned char		cur;			/* current protection */
  unsigned char		max;			/* maximum protection */

#define VM_SUBMAP					0x0001 /* this region is a submap */
};

/*
		vm_map: describes a subset of the virtual address space of a protection
		        domain. A vm_map is subdivided into multiple vm_regions. The 
						vm_regions are kept on a doubly linked ordered list.
						numentries counts the regions in this mapping. Whereas the refcount
						the number of references to this vm_map counts. If refcount drops 
						to zero the vm_map can be freed. 
						The vm_region header stores information about the vm_map and not 
						of the first region.
		
		Locking: XXX
*/

struct vm_map
{
  struct vm_region	header;			/* list of regions in this map */
  BigLockDesc				lock;				/* lock for the region map */
  int 							numentries;	/* number of regions in this map */
  struct vm_region*	loan;				/* pointer to the loan object */
  struct vm_region*	hint;				/* hint pointer for faster access */
  LockDesc					hint_lock;	/* fast lock for the hint */
  int								refcount;		/* reference count of this vm_map */
  LockDesc					ref_lock;		/* fast lock for the refcount */
  unsigned int			flags;			/* flags e.g. for the locking protocol */
  LockDesc					flag_lock;	/* fast lock for the flags */
};


int vm_map_lookup_region(struct vm_map** vm_map, RegionPtr_t hint,
                         struct vm_region** region);

int vm_map_find_near(struct vm_map** vm_map, address_t hint, size_t size,
                     RegionPtr_t region, struct vm_region** prevreg, int direction);

void vm_map_init(struct vm_map* vm_map, address_t start, address_t end);

int vm_map_link_region(struct vm_map* vm_map, struct vm_region* prev,
                       struct vm_region* new);

int vm_map_unlink_region(struct vm_map* vm_map, struct vm_region* reg);

int vm_map_create_region(RegionPtr_t region, struct vm_region** new);

int vm_map_share_region(RegionPtr_t region, struct vm_region* source,
                        struct vm_region** new, int flags);

int vm_map_free_region(struct vm_region* r);

#endif
