github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/cpvmm/vmm/include/list.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 _LIST_H
    16  #define _LIST_H
    17  
    18  #include "vmm_defs.h"
    19  #include "common_libc.h"
    20  
    21  #define LIST_ENTRY(list, entry_type, list_entry_name) \
    22      ((entry_type *) ((char *) list - OFFSET_OF(entry_type, list_entry_name)))
    23  
    24  
    25  typedef struct _LIST_ELEMENT
    26  {
    27      struct _LIST_ELEMENT *next;
    28      struct _LIST_ELEMENT *prev;
    29  } LIST_ELEMENT;
    30  
    31  INLINE void list_init(LIST_ELEMENT *entry)
    32  {
    33      entry->next = entry;
    34      entry->prev = entry;
    35  }
    36  
    37  INLINE void _list_add(LIST_ELEMENT *prev, LIST_ELEMENT *next, LIST_ELEMENT *new_entry)
    38  {
    39      prev->next = new_entry;
    40      new_entry->prev = prev;
    41      next->prev = new_entry;
    42      new_entry->next = next;
    43  }
    44  
    45  INLINE void list_add(LIST_ELEMENT *list, LIST_ELEMENT *new_entry)
    46  {
    47      _list_add(list, list->next, new_entry);
    48  }
    49  #ifdef INCLUDE_UNUSED_CODE
    50  INLINE void list_add_after(LIST_ELEMENT *list, LIST_ELEMENT *new_entry)
    51  {
    52      list_add(list, new_entry);
    53  }
    54  
    55  INLINE void list_add_before(LIST_ELEMENT *list, LIST_ELEMENT *new_entry)
    56  {
    57      _list_add(list->prev, list, new_entry);
    58  }
    59  INLINE void list_merge(LIST_ELEMENT *list1, LIST_ELEMENT *list2)
    60  {
    61      list1->next->prev = list2->prev;
    62      list2->prev->next = list1->next;
    63      list1->next = list2->next;
    64      list2->next->prev = list1;
    65  }
    66  #endif
    67  
    68  INLINE void list_remove(LIST_ELEMENT *list)
    69  {
    70      list->prev->next = list->next;
    71      list->next->prev = list->prev;
    72      list->prev = list->next = NULL;
    73  }
    74  
    75  INLINE BOOLEAN list_is_empty(LIST_ELEMENT *list)
    76  {
    77      return (BOOLEAN) (list->next == list);
    78  }
    79  
    80  
    81  #define LIST_FOR_EACH(list, iter) \
    82      for(iter = (list)->next; iter != (list); iter = iter->next)
    83  
    84  INLINE UINT16 list_size(LIST_ELEMENT *list)
    85  {
    86      UINT16 size = 0;
    87      LIST_ELEMENT *curr_element = list;
    88  
    89      while(curr_element->next != list)
    90      {
    91          size++;
    92          curr_element = curr_element->next;
    93      }
    94  
    95      return size;
    96  }
    97  
    98  #define LIST_NEXT(list, entry_type, list_entry_name) (LIST_ENTRY((list)->next, entry_type, list_entry_name))
    99  
   100  typedef struct _ARRAY_LIST *ARRAY_LIST_HANDLE;
   101  
   102  typedef struct _ARRAY_LIST_ELEMENT *ARRAY_LIST_ELEMENT_HANDLE;
   103  
   104  typedef struct _ARRAY_LIST_ITERATOR
   105  {
   106      ARRAY_LIST_HANDLE alist;
   107      ARRAY_LIST_ELEMENT_HANDLE element;
   108  } ARRAY_LIST_ITERATOR;
   109  
   110  // FUNCTION:        array_list_memory_size
   111  // DESCRIPTION:     Calculate memory size required for list.
   112  // RETURN VALUE:    Memory size required for list
   113  UINT32 array_list_memory_size(char *buffer, UINT32 element_size, UINT32 num_of_elements, UINT32 alignment);
   114  
   115  // FUNCTION:        array_list_init
   116  // DESCRIPTION:     Initialize the list.
   117  // RETURN VALUE:    Array list handle to use for list manipulation
   118  ARRAY_LIST_HANDLE array_list_init(char *buffer, UINT32 buffer_size, UINT32 element_size, UINT32 num_of_elements, UINT32 alignment);
   119  
   120  // FUNCTION:        array_list_size
   121  // DESCRIPTION:     Number of elements in the list.
   122  // RETURN VALUE:    Number of elements in the list.
   123  UINT32 array_list_size(ARRAY_LIST_HANDLE alist);
   124  
   125  // FUNCTION:        array_list_add
   126  // DESCRIPTION:     Add element to the list.
   127  // RETURN VALUE:    TRUE if element was successfully added, FALSE for error.
   128  BOOLEAN array_list_add(ARRAY_LIST_HANDLE alist, void* data);
   129  
   130  // FUNCTION:        array_list_remove
   131  // DESCRIPTION:     Remove element from the list.
   132  // RETURN VALUE:    TRUE if element was successfully removed, FALSE for error.
   133  BOOLEAN array_list_remove(ARRAY_LIST_HANDLE alist, void *data);
   134  
   135  // FUNCTION:        array_list_first
   136  // DESCRIPTION:     Get first element.
   137  // RETURN VALUE:    The first element.
   138  char *array_list_first(ARRAY_LIST_HANDLE alist, ARRAY_LIST_ITERATOR *iter);
   139  
   140  // FUNCTION:        array_list_next
   141  // DESCRIPTION:     Get next element for iteration.
   142  // RETURN VALUE:    The next element.
   143  char *array_list_next(ARRAY_LIST_ITERATOR *iter);
   144  
   145  #endif