1  /*
   2   * \brief  RAM session interface
   3   * \author Norman Feske
   4   * \date   2006-05-11
   5   */

   6  
   7  /*
   8   * Copyright (C) 2006-2010 Genode Labs GmbH
   9   *
  10   * This file is part of the Genode OS framework, which is distributed
  11   * under the terms of the GNU General Public License version 2.
  12   */

  13  
  14  #ifndef _INCLUDE__RAM_SESSION__RAM_SESSION_H_
  15  #define _INCLUDE__RAM_SESSION__RAM_SESSION_H_
  16  
  17  #include <base/stdint.h>
  18  #include <base/capability.h>
  19  #include <base/exception.h>
  20  #include <dataspace/capability.h>
  21  #include <ram_session/capability.h>
  22  
  23  namespace Genode {
  24  
  25     typedef Typed_capability<Dataspace, Dataspace_capability> Ram_dataspace_capability;
  26  
  27     class Ram_session
  28     {
  29        protected:
  30  
  31           enum Opcode {
  32              ALLOC, FREE, REF_ACCOUNT, TRANSFER_QUOTA, QUOTA, USED
  33           }
;

  34  
  35        public:
  36  
  37           static const char *service_name() { return "RAM"; }
  38  
  39  
  40           /*********************
  41            ** Exception types **
  42            *********************/

  43  
  44           class Alloc_failed   public Exception    { };
  45           class Quota_exceeded public Alloc_failed { };
  46  
  47  
  48           /**
  49            * Destructor
  50            */

  51           virtual ~Ram_session() { }

  52  
  53           /**
  54            * Allocate RAM dataspace
  55            *
  56            * \param  size  size of RAM dataspace
  57            *
  58            * \throw  Quota_exceeded
  59            * \return capability to new RAM dataspace
  60            */

  61           virtual Ram_dataspace_capability alloc(size_t size) = 0;

  62  
  63           /**
  64            * Free RAM dataspace
  65            *
  66            * \param ds  dataspace capability as returned by alloc
  67            */

  68           virtual void free(Ram_dataspace_capability ds) = 0;

  69  
  70           /**
  71            * Define reference account for the RAM session
  72            *
  73            * \param   ram_session    reference account
  74            *
  75            * \return  0 on success
  76            *
  77            * Each RAM session requires another RAM session as
  78            * reference account to transfer quota to and from.
  79            * The reference account can be defined only once.
  80            */

  81           virtual int ref_account(Ram_session_capability ram_session) = 0;

  82  
  83           /**
  84            * Transfer quota the another ram session
  85            *
  86            * \param ram_session  receiver of quota donation
  87            * \param amount       amount of quota to donate
  88            * \return             0 on success
  89            *
  90            * Quota can only be transfered if the specified
  91            * RAM session is either the reference account for
  92            * this session or vice versa.
  93            */

  94           virtual int transfer_quota(Ram_session_capability ram_sessionsize_t amount) = 0;

  95  
  96           /**
  97            * Return current quota limit
  98            */

  99           virtual size_t quota() = 0;

 100  
 101           /**
 102            * Return used quota
 103            */

 104           virtual size_t used() = 0;

 105  
 106           /**
 107            * Return amount of available quota
 108            */

 109           size_t avail()
 110           {
 111              size_t q = quota()u = used();
 112              return q > u ? q - u : 0;

 113           }

 114     }
;

 115  }

 116  
 117  #endif /* _INCLUDE__RAM_SESSION__RAM_SESSION_H_ */