github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/cpvmm/common/include/array_iterators.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 _ARRAY_ITERATORS_H_ 16 #define _ARRAY_ITERATORS_H_ 17 18 #include "vmm_defs.h" 19 #include "vmm_dbg.h" 20 21 // Implementation of array iterators template 22 23 // 24 // Generic array iterator 25 // Usage: 26 // Provider: 27 // typedef GENERIC_ARRAY_ITERATOR MY_OBJ_ITERATOR; 28 // static MY_OBJ g_my_obj_array[size]; 29 // // forward iterator 30 // MY_OBJ* my_obj_iterator_first( <params>, MY_OBJ_ITERATOR* ctx ) 31 // { 32 // < some logic > 33 // return ARRAY_ITERATOR_FIRST( MY_OBJ, g_my_obj_array, size, ctx ); 34 // } 35 // MY_OBJ* my_obj_iterator_next( MY_OBJ_ITERATOR* ctx ) 36 // { 37 // return ARRAY_ITERATOR_NEXT( MY_OBJ, ctx ); 38 // } 39 // 40 // // backward iterator 41 // MY_OBJ* my_obj_reverse_iterator_first( <params>, MY_OBJ_ITERATOR* ctx ) 42 // { 43 // < some logic > 44 // return ARRAY_REVERSE_ITERATOR_FIRST( MY_OBJ, g_my_obj_array, size, ctx ); 45 // } 46 // MY_OBJ* my_obj_reverse_iterator_next( MY_OBJ_ITERATOR* ctx ) 47 // { 48 // return ARRAY_REVERSE_ITERATOR_NEXT( MY_OBJ, ctx ); 49 // } 50 // Consumer: 51 // MY_OBJ_ITERATOR ctx; 52 // MY_OBJ* obj; 53 // 54 // for (obj=my_obj_iterator_first(<params>, &ctx); obj; obj=my_obj_iterator_nextt(&ctx)) 55 // { 56 // ..... 57 // } 58 59 typedef struct _GENERIC_ARRAY_ITERATOR { 60 size_t array_start; // pointer to the array start 61 UINT32 start_idx; // count from 62 UINT32 end_idx; // count to 63 UINT32 addend; // count step - MUST be 64 // end_idx=start_idx + elem_count*addend 65 UINT32 cur_idx; // current state 66 } GENERIC_ARRAY_ITERATOR; 67 68 INLINE 69 size_t generic_array_iterator_next( GENERIC_ARRAY_ITERATOR* ctx, UINT32 elem_size ) 70 { 71 UINT32 ret_idx; 72 VMM_ASSERT( ctx != NULL ); 73 74 if (ctx->cur_idx != ctx->end_idx) { 75 ret_idx = ctx->cur_idx; 76 ctx->cur_idx += ctx->addend; 77 return (ctx->array_start + ret_idx*elem_size); 78 } 79 return 0; 80 } 81 82 INLINE 83 size_t generic_array_iterator_first( UINT32 start_idx, 84 UINT32 elem_count, 85 UINT32 addend, 86 size_t array_start, 87 UINT32 elem_size, 88 GENERIC_ARRAY_ITERATOR* ctx ) 89 { 90 VMM_ASSERT( ctx != NULL ); 91 92 ctx->array_start = array_start; 93 ctx->start_idx = start_idx; 94 ctx->end_idx = start_idx + elem_count*addend; 95 ctx->addend = addend; 96 ctx->cur_idx = start_idx; 97 98 return generic_array_iterator_next( ctx, elem_size ); 99 } 100 101 102 // Typeless generic macros 103 104 #define GENERIC_ARRAY_ITERATOR_FIRST( elem_type, \ 105 array_ptr, \ 106 start_elem_idx, \ 107 number_of_entries, \ 108 count_step, \ 109 ctx_ptr ) \ 110 (elem_type*)generic_array_iterator_first( start_elem_idx, \ 111 number_of_entries, \ 112 count_step, \ 113 (size_t)(array_ptr), \ 114 (UINT32)sizeof(elem_type), \ 115 ctx_ptr ) 116 117 118 119 #define GENERIC_ARRAY_ITERATOR_NEXT( elem_type, \ 120 ctx_ptr ) \ 121 (elem_type*)generic_array_iterator_next( ctx_ptr, (UINT32)sizeof(elem_type)) 122 123 124 125 // Typeless array iterator 0->end_of_array 126 #define ARRAY_ITERATOR_FIRST( elem_type, array_ptr, number_of_entries, ctx_ptr )\ 127 GENERIC_ARRAY_ITERATOR_FIRST( elem_type, array_ptr, 0, number_of_entries, 1, ctx_ptr ) 128 129 #define ARRAY_ITERATOR_NEXT( elem_type, ctx_ptr ) \ 130 GENERIC_ARRAY_ITERATOR_NEXT( elem_type, ctx_ptr ) 131 132 133 // Typeless array iterator end_of_array->0 134 135 #define ARRAY_REVERSE_ITERATOR_FIRST( elem_type, array_ptr, number_of_entries, ctx_ptr )\ 136 GENERIC_ARRAY_ITERATOR_FIRST( elem_type, array_ptr, number_of_entries-1, number_of_entries, -1, ctx_ptr ) 137 138 #define ARRAY_REVERSE_ITERATOR_NEXT( elem_type, ctx_ptr ) \ 139 GENERIC_ARRAY_ITERATOR_NEXT( elem_type, ctx_ptr ) 140 141 142 #endif // _ARRAY_ITERATORS_H_