github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/cpvmm/vmm/memory/memory_manager/pool.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  
    16  #ifndef POOL_H
    17  #define POOL_H
    18  
    19  #include <vmm_defs.h>
    20  #include <pool_api.h>
    21  #include <hash64_api.h>
    22  #include <lock.h>
    23  
    24  typedef struct POOL_LIST_ELEMENT_S {
    25      struct POOL_LIST_ELEMENT_S* prev;
    26      struct POOL_LIST_ELEMENT_S* next;
    27  } POOL_LIST_ELEMENT;
    28  
    29  /* POOL_LIST_ELEMENT* pool_list_element_get_prev(POOL_LIST_ELEMENT* element)  */
    30  #define pool_list_element_get_prev(element_) (element_->prev)
    31  
    32  /* void pool_list_element_set_prev(POOL_LIST_ELEMENT* element, POOL_LIST_ELEMENT* prev) */
    33  #define pool_list_element_set_prev(element_, prev_) {element_->prev = prev_;}
    34  
    35  /* POOL_LIST_ELEMENT* pool_list_element_get_next(POOL_LIST_ELEMENT* element) */
    36  #define pool_list_element_get_next(element_) (element_->next)
    37  
    38  /* void pool_list_element_set_next(POOL_LIST_ELEMENT* element, POOL_LIST_ELEMENT* next) */
    39  #define pool_list_element_set_next(element_, next_) {element_->next = next_;}
    40  
    41  
    42  typedef struct POOL_LIST_HEAD_S {
    43      POOL_LIST_ELEMENT* first_element;
    44      POOL_LIST_ELEMENT* last_element;
    45      UINT32 num_of_elements;
    46      UINT32 padding; // not for use
    47  } POOL_LIST_HEAD;
    48  
    49  /* POOL_LIST_ELEMENT* pool_list_head_get_first_element(const POOL_LIST_HEAD* list_head) */
    50  #define pool_list_head_get_first_element(list_head_) (list_head_->first_element)
    51  
    52  /* void pool_list_head_set_first_element (POOL_LIST_HEAD* list_head, POOL_LIST_ELEMENT* first_element) */
    53  #define pool_list_head_set_first_element(list_head_, first_element_) {list_head_->first_element = first_element_;}
    54  
    55  /* POOL_LIST_ELEMENT* pool_list_head_get_last_element(const POOL_LIST_HEAD* list_head) */
    56  #define pool_list_head_get_last_element(list_head_) (list_head_->last_element)
    57  
    58  /* void pool_list_head_set_last_element (POOL_LIST_HEAD* list_head, POOL_LIST_ELEMENT* last_element) */
    59  #define pool_list_head_set_last_element(list_head_, last_element_) {list_head_->last_element = last_element_;}
    60  
    61  /* UINT32 pool_list_head_get_num_of_elements(const POOL_LIST_HEAD* list_head) */
    62  #define pool_list_head_get_num_of_elements(list_head_) (list_head_->num_of_elements)
    63  
    64  /* void pool_list_head_set_num_of_elements (POOL_LIST_HEAD* list_head, UINT32 num_of_elements) */
    65  #define pool_list_head_set_num_of_elements(list_head_, num_of_elements_) {list_head_->num_of_elements = num_of_elements_;}
    66  
    67  
    68  typedef struct POOL_S {
    69      POOL_LIST_HEAD free_hash_elements;
    70      POOL_LIST_HEAD free_pool_elements;
    71      POOL_LIST_HEAD free_pages;
    72      POOL_LIST_ELEMENT* hash_element_to_allocate;
    73      HASH64_HANDLE hash;
    74      HEAP_ALLOC_HANDLE must_succeed_alloc_handle;
    75      UINT32 size_of_single_element;
    76      UINT32 num_of_elements_per_page;
    77      UINT32 size_of_hash_element;
    78      UINT32 num_of_hash_elements_per_page;
    79      UINT32 num_of_allocated_pages;
    80      UINT32 current_hash_size;
    81      BOOLEAN must_succeed_allocation;
    82      UINT32 alloc_free_ops_counter;
    83      UINT32 num_of_allocated_elements;
    84      UINT32 num_of_pages_used_for_hash_nodes;
    85      UINT32 num_of_pages_used_for_pool_elements;
    86      VMM_LOCK access_lock;
    87      BOOLEAN mutex_flag;
    88      UINT32  pad0;
    89  } POOL;
    90  
    91  /* POOL_LIST_HEAD* pool_get_free_hash_elements_list(POOL* pool) */
    92  #define pool_get_free_hash_elements_list(pool_) (&(pool_->free_hash_elements))
    93   
    94  /* POOL_LIST_HEAD* pool_get_free_pool_elements_list(POOL* pool) */
    95  #define pool_get_free_pool_elements_list(pool_) (&(pool_->free_pool_elements))
    96  
    97  /* POOL_LIST_HEAD* pool_get_free_pages_list(POOL* pool) */
    98  #define pool_get_free_pages_list(pool_) (&(pool_->free_pages))
    99  
   100  /* UINT32 pool_get_size_of_single_element(const POOL* pool) */
   101  #define pool_get_size_of_single_element(pool_) (pool_->size_of_single_element)
   102  
   103  /* void pool_set_size_of_single_element(POOL* pool, UINT32 size) */
   104  #define pool_set_size_of_single_element(pool_, size_) {pool_->size_of_single_element = size_;}
   105  
   106  /* POOL_LIST_ELEMENT* pool_get_hash_element_to_allocate(POOL* pool) */
   107  #define pool_get_hash_element_to_allocate(pool_) (pool_->hash_element_to_allocate)
   108  
   109  /* void pool_set_hash_element_to_allocate(POOL* pool, POOL_LIST_ELEMENT* element) */
   110  #define pool_set_hash_element_to_allocate(pool_, element_) {pool_->hash_element_to_allocate = element_;}
   111  
   112  /* UINT32 pool_get_num_of_elements_per_page(const POOL* pool) */
   113  #define pool_get_num_of_elements_per_page(pool_) (pool_->num_of_elements_per_page)
   114  
   115  /* void pool_set_num_of_elements_per_page(POOL* pool, UINT32 num) */
   116  #define pool_set_num_of_elements_per_page(pool_, num_) {pool_->num_of_elements_per_page = num_;}
   117  
   118  /* UINT32 pool_get_size_of_hash_element(const POOL* pool) */
   119  #define pool_get_size_of_hash_element(pool_) (pool_->size_of_hash_element)
   120  
   121  /* void pool_set_size_of_hash_element(POOL* pool, UINT32 size) */
   122  #define pool_set_size_of_hash_element(pool_, size_) {pool_->size_of_hash_element = size_;}
   123  
   124  /* UINT32 pool_get_num_of_hash_elements_per_page(const POOL* pool) */
   125  #define pool_get_num_of_hash_elements_per_page(pool_) (pool_->num_of_hash_elements_per_page)
   126  
   127  /* void pool_set_num_of_hash_elements_per_page(POOL* pool, UINT32 num) */
   128  #define pool_set_num_of_hash_elements_per_page(pool_, num_) {pool_->num_of_hash_elements_per_page = num_;}
   129  
   130  /* HASH64_HANDLE pool_get_hash(const POOL* pool) */
   131  #define pool_get_hash(pool_) (pool_->hash)
   132  
   133  /* void pool_set_hash(POOL* pool, HASH64_HANDLE hash) */
   134  #define pool_set_hash(pool_, hash_) {pool_->hash = hash_;}
   135  
   136  /* UINT32 pool_get_current_hash_size(const POOL* pool) */
   137  #define pool_get_current_hash_size(pool_) (pool_->current_hash_size)
   138  
   139  /* void pool_set_current_hash_size(POOL* pool, UINT32 new_size) */
   140  #define pool_set_current_hash_size(pool_, new_size_) {pool_->current_hash_size = new_size_;}
   141  
   142  /* UINT32 pool_get_num_of_allocated_pages(const POOL* pool) */
   143  #define pool_get_num_of_allocated_pages(pool_) (pool_->num_of_allocated_pages)
   144  
   145  /* void pool_set_num_of_allocated_pages(POOL* pool, UINT32 num) */
   146  #define pool_set_num_of_allocated_pages(pool_, num_) {pool_->num_of_allocated_pages = num_;}
   147  
   148  /* HEAP_ALLOC_HANDLE pool_get_must_succeed_alloc_handle(const POOL* pool) */
   149  #define pool_get_must_succeed_alloc_handle(pool_) (pool_->must_succeed_alloc_handle)
   150  
   151  /* void pool_set_must_succeed_alloc_handle(POOL* pool, HEAP_ALLOC_HANDLE handle) */
   152  #define pool_set_must_succeed_alloc_handle(pool_, handle_) {pool_->must_succeed_alloc_handle = handle_;}
   153  
   154  /* BOOLEAN pool_is_must_succeed_allocation(const POOL* pool) */
   155  #define pool_is_must_succeed_allocation(pool_) (pool_->must_succeed_allocation)
   156  
   157  /* void pool_set_must_succeed_allocation(POOL* pool) */
   158  #define pool_set_must_succeed_allocation(pool_) {pool_->must_succeed_allocation = TRUE;}
   159  
   160  /* void pool_clear_must_succeed_allocation(POOL* pool) */
   161  #define pool_clear_must_succeed_allocation(pool_) {pool_->must_succeed_allocation = FALSE;}
   162  
   163  /* UINT32 pool_get_alloc_free_ops_counter(const POOL* pool) */
   164  #define pool_get_alloc_free_ops_counter(pool_) (pool_->alloc_free_ops_counter)
   165  
   166  /* void pool_clear_alloc_free_ops_counter(POOL* pool) */
   167  #define pool_clear_alloc_free_ops_counter(pool_) {pool_->alloc_free_ops_counter = 0;}
   168  
   169  /* void pool_inc_alloc_free_ops_counter(POOL* pool) */
   170  #define pool_inc_alloc_free_ops_counter(pool_) {pool_->alloc_free_ops_counter += 1;}
   171  
   172  
   173  /* UINT32 pool_get_num_of_allocated_elements(const POOL* pool) */
   174  #define pool_get_num_of_allocated_elements(pool_) (pool_->num_of_allocated_elements)
   175  
   176  /* void pool_clear_num_of_allocated_elements(POOL* pool) */
   177  #define pool_clear_num_of_allocated_elements(pool_) {pool_->num_of_allocated_elements = 0;}
   178  
   179  /* void pool_inc_num_of_allocated_elements(POOL* pool) */
   180  #define pool_inc_num_of_allocated_elements(pool_) {pool_->num_of_allocated_elements += 1;}
   181  
   182  /* void pool_dec_num_of_allocated_elements(POOL* pool) */
   183  #define pool_dec_num_of_allocated_elements(pool_) {pool_->num_of_allocated_elements -= 1;}
   184  
   185  /* UINT32 pool_get_num_of_pages_used_for_hash_nodes(POOL* pool) */
   186  #define pool_get_num_of_pages_used_for_hash_nodes(pool_) (pool_->num_of_pages_used_for_hash_nodes)
   187  
   188  /* void pool_clear_num_of_pages_used_for_hash_nodes(POOL* pool) */
   189  #define pool_clear_num_of_pages_used_for_hash_nodes(pool_) {pool_->num_of_pages_used_for_hash_nodes = 0;}
   190  
   191  /* void pool_inc_num_of_pages_used_for_hash_nodes(POOL* pool) */
   192  #define pool_inc_num_of_pages_used_for_hash_nodes(pool_) {pool_->num_of_pages_used_for_hash_nodes += 1;}
   193  
   194  /* void pool_dec_num_of_pages_used_for_hash_nodes(POOL* pool) */
   195  #define pool_dec_num_of_pages_used_for_hash_nodes(pool_) {pool_->num_of_pages_used_for_hash_nodes -= 1;}
   196  
   197  /* UINT32 pool_get_num_of_pages_used_for_pool_elements(POOL* pool) */
   198  #define pool_get_num_of_pages_used_for_pool_elements(pool_) (pool_->num_of_pages_used_for_pool_elements)
   199  
   200  /* void pool_clear_num_of_pages_used_for_pool_elements(POOL* pool) */
   201  #define pool_clear_num_of_pages_used_for_pool_elements(pool_) {pool_->num_of_pages_used_for_pool_elements = 0;}
   202  
   203  /* void pool_inc_num_of_pages_used_for_pool_elements(POOL* pool) */
   204  #define pool_inc_num_of_pages_used_for_pool_elements(pool_) {pool_->num_of_pages_used_for_pool_elements += 1;}
   205  
   206  /* void pool_dec_num_of_pages_used_for_pool_elements(POOL* pool) */
   207  #define pool_dec_num_of_pages_used_for_pool_elements(pool_) {pool_->num_of_pages_used_for_pool_elements -= 1;}
   208  
   209  
   210  #define POOL_HASH_NUM_OF_CELLS 512
   211  #define POOL_PAGES_TO_FREE_THRESHOLD 4
   212  #define POOL_PAGES_TO_KEEP_THRESHOLD 4
   213  #define POOL_PAGES_TO_ALLOCATE_THRESHOLD 8
   214  #define POOL_MAX_NUM_OF_PAGES_TO_ALLOCATE 100
   215  #define POOL_MIN_NUMBER_OF_FREE_PAGES 4
   216  #define POOL_MIN_NUMBER_OF_PAGES_TO_FREE 6
   217  #define POOL_REHASH_THRESHOLD 4
   218  #define POOL_FREE_UNUSED_PAGES_THRESHOLD 5000
   219  
   220  #endif