github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/cpvmm/vmm/include/heap.h (about)

     1  /*
     2   * Copyright (c) 2013 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *     http://www.apache.org/licenses/LICENSE-2.0
     8   * Unless required by applicable law or agreed to in writing, software
     9   * distributed under the License is distributed on an "AS IS" BASIS,
    10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11   * See the License for the specific language governing permissions and
    12   * limitations under the License.
    13   */
    14  
    15  #ifndef _HEAP_H_
    16  #define _HEAP_H_
    17  
    18  //#ifdef __cplusplus
    19  //extern "C" {
    20  //#endif
    21  
    22  #include "vmm_defs.h"
    23  
    24  //typedef UINT32 HEAP_PAGE_INT;
    25  #define HEAP_PAGE_INT UINT32
    26  
    27  typedef struct {
    28      HEAP_PAGE_INT number_of_pages:31; // When in_use=1, this represents the number of allocated pages
    29                                        // When in_use=0, represents the number of contiguos pages from this address
    30      HEAP_PAGE_INT in_use:1;           // 1=InUse
    31  
    32  #ifdef DEBUG
    33      INT32 line_number;
    34      char  *file_name;
    35  #endif
    36  } HEAP_PAGE_DESCRIPTOR;
    37  
    38  
    39  // FUNCTION : vmm_heap_get_max_used_pages()
    40  // PURPOSE  : Returns the max amount of uVmm heap pages used
    41  //            from post-launch vmm
    42  // ARGUMENTS:
    43  // RETURNS  : HEAP max heap used in pages
    44  HEAP_PAGE_INT vmm_heap_get_max_used_pages(void);
    45  
    46  
    47  // FUNCTION : vmm_heap_initialize()
    48  // PURPOSE  : Format memory block for memory allocation / free services.
    49  //          : Calculate actual number of pages.
    50  // ARGUMENTS:IN ADDRESS heap_base_address - address at which the heap is located
    51  //          : size_t    heap_size - in bytes
    52  // RETURNS  : Last occupied address
    53  ADDRESS vmm_heap_initialize(IN ADDRESS heap_base_address, IN size_t heap_size);
    54  
    55  
    56  // FUNCTION : vmm_heap_extend()
    57  // PURPOSE  : Extend the heap to an additional memory block 
    58  //		: update actual number of pages.
    59  // ARGUMENTS:IN ADDRESS ex_heap_base_address - address at which the heap is located
    60  //          : size_t    ex_heap_size - in bytes
    61  // RETURNS  : Last occupied address
    62  ADDRESS vmm_heap_extend(IN ADDRESS ex_heap_buffer_address, IN size_t  ex_heap_buffer_size);
    63  
    64  
    65  /*-------------------------------------------------------*
    66  *  FUNCTION : vmm_head_get_details()
    67  *  PURPOSE  : Retrieve information about heap area.
    68  *  ARGUMENTS: OUT HVA* base_addr - address at which the heap is located
    69  *           : UINT32   size - in bytes
    70  *-------------------------------------------------------*/
    71  void vmm_heap_get_details(OUT HVA* base_addr, OUT UINT32* size);
    72  
    73  /*-------------------------------------------------------*
    74  *  FUNCTION : vmm_page_alloc()
    75  *  PURPOSE  : Allocates contiguous buffer of given size
    76  *  ARGUMENTS: IN HEAP_PAGE_INT number_of_pages - size of the buffer in 4K pages
    77  *  RETURNS  : void*  address of allocted buffer if OK, NULL if failed
    78  *-------------------------------------------------------*/
    79  void* vmm_page_allocate(
    80  #ifdef DEBUG
    81      char    *file_name,
    82      INT32   line_number,
    83  #endif
    84      HEAP_PAGE_INT number_of_pages
    85      );
    86  
    87  /*-------------------------------------------------------*
    88  *  FUNCTION : vmm_page_alloc_scattered()
    89  *  PURPOSE  : Fills given array with addresses of allocated 4K pages
    90  *  ARGUMENTS: IN HEAP_PAGE_INT number_of_pages - number of 4K pages
    91  *           : OUT void * p_page_array[] - contains the addresses of allocated pages
    92  *  RETURNS  : number of successfully allocated pages
    93  *-------------------------------------------------------*/
    94  HEAP_PAGE_INT vmm_page_allocate_scattered(
    95  #ifdef DEBUG
    96      char    *file_name,
    97      INT32   line_number,
    98  #endif
    99      IN HEAP_PAGE_INT number_of_pages,
   100      OUT void *p_page_array[]
   101      );
   102  
   103  /*-------------------------------------------------------*
   104  *  FUNCTION : vmm_page_free()
   105  *  PURPOSE  : Release previously allocated buffer
   106  *  ARGUMENTS: IN void *p_buffer - buffer to be released
   107  *  RETURNS  : void
   108  *-------------------------------------------------------*/
   109  void vmm_page_free(IN void *p_buffer);
   110  
   111  /*-------------------------------------------------------*
   112  *  FUNCTION : vmm_page_buff_size()
   113  *  PURPOSE  : Identify number of pages in previously allocated buffer
   114  *  ARGUMENTS: IN void *p_buffer - the buffer
   115  *  RETURNS  : UINT32 - Num pages this buffer is using
   116  *-------------------------------------------------------*/
   117  UINT32 vmm_page_buff_size(IN void *p_buffer);
   118  
   119  HEAP_PAGE_INT vmm_heap_get_total_pages(void);
   120  
   121  /*-------------------------------------------------------*
   122  *  FUNCTION : vmm_memory_allocate()
   123  *  PURPOSE  : Allocates contiguous buffer of given size, filled with zeroes
   124  *  ARGUMENTS: IN UINT32 size - size of the buffer in bytes
   125  *  RETURNS  : void*  address of allocted buffer if OK, NULL if failed
   126  *             returned buffer is always 4K page alinged
   127  *-------------------------------------------------------*/
   128  void* vmm_memory_allocate(
   129  #ifdef DEBUG
   130      char    *file_name,
   131      INT32   line_number,
   132  #endif
   133      IN UINT32 size
   134      );
   135  
   136  /*-------------------------------------------------------*
   137  *  FUNCTION : vmm_memory_free()
   138  *  PURPOSE  : Release previously allocated buffer
   139  *  ARGUMENTS: IN void *p_buffer - buffer to be released
   140  *  RETURNS  : void
   141  *-------------------------------------------------------*/
   142  #define vmm_memory_free( p_buffer ) vmm_page_free( p_buffer )
   143  
   144  
   145  typedef void (*VMM_FREE_MEM_CALLBACK_FUNC)(IN void* context);
   146  typedef UINT32 HEAP_ALLOC_HANDLE;
   147  #define HEAP_INVALID_ALLOC_HANDLE ((HEAP_ALLOC_HANDLE)(~0))
   148  
   149  
   150  /*-------------------------------------------------------*
   151  *  FUNCTION : vmm_heap_register_free_mem_callback()
   152  *  PURPOSE  : Provides an opportunity for some module to register
   153  *             memory deallocation callback. In case when some other
   154  *             component requires more memory and the request cannot
   155  *             be fulfilled due to a low memory, this callback function
   156  *             will be called.
   157  *  ARGUMENTS: IN VMM_FREE_MEM_CALLBACK_FUNC callback_func - pointer to callback function
   158  *             IN void* context - context which will be passed to callback function upon call.
   159  *  RETURNS  : HEAP_ALLOC_HANDLE - handle which can be used for allocation. In case of failure
   160  *             "HEAP_INVALID_ALLOC_HANDLE" will be returned.
   161  *-------------------------------------------------------*/
   162  HEAP_ALLOC_HANDLE vmm_heap_register_free_mem_callback(IN VMM_FREE_MEM_CALLBACK_FUNC callback_func, IN void* context);
   163  
   164  
   165  /*-------------------------------------------------------*
   166  *  FUNCTION : vmm_memory_allocate_must_succeed()
   167  *  PURPOSE  : The function tries to allocate requested memory. In case of insufficient
   168  *             memory, the heap will call all the registered deallocation functions except
   169  *             the one which was recorded under the passed HEAP_ALLOC_HANDLE.
   170  *  ARGUMENTS: IN HEAP_ALLOC_HANDLE handle - handle returned by "vmm_heap_register_free_mem_callback".
   171  *                                           It is possible to pass HEAP_INVALID_ALLOC_HANDLE, but
   172  *                                           in this case all the recorded callbacks will be called, no exceptions.
   173  *             IN UINT32 size - requested size.
   174  *  RETURNS  : void* - allocated memory.
   175  *-------------------------------------------------------*/
   176  void* vmm_memory_allocate_must_succeed(
   177  #ifdef DEBUG
   178      char    *file_name,
   179      INT32   line_number,
   180  #endif
   181      HEAP_ALLOC_HANDLE handle,
   182      UINT32 size
   183      );
   184  
   185  
   186  #ifdef DEBUG
   187  
   188  #define vmm_page_alloc(__num_of_pages)                                         \
   189          vmm_page_allocate(__FILE__, __LINE__, __num_of_pages)
   190  
   191  #define vmm_memory_alloc(__size)                                               \
   192          vmm_memory_allocate(__FILE__, __LINE__, __size)
   193  
   194  #define vmm_page_alloc_scattered(__num_of_pages, __p_page_array)               \
   195          vmm_page_allocate_scattered(__FILE__, __LINE__, __num_of_pages, __p_page_array)
   196  
   197  #define vmm_memory_alloc_must_succeed(__handle, __size)                        \
   198      vmm_memory_allocate_must_succeed(__FILE__, __LINE__, __handle, __size)
   199  
   200  
   201  void vmm_heap_show(void);
   202  
   203  #else
   204  
   205  #define vmm_page_alloc                  vmm_page_allocate
   206  #define vmm_memory_alloc                vmm_memory_allocate
   207  #define vmm_page_alloc_scattered        vmm_page_allocate_scattered
   208  #define vmm_memory_alloc_must_succeed   vmm_memory_allocate_must_succeed
   209  
   210  
   211  #define vmm_heap_show()
   212  
   213  #endif
   214  
   215  
   216  //#ifdef __cplusplus
   217  //}
   218  //#endif
   219  
   220  #endif // _HEAP_H_
   221