UniRec  2.9.3
links.h
Go to the documentation of this file.
1 
14  /*
15  * Copyright (C) 2013-2015 CESNET
16  *
17  * LICENSE TERMS
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  * notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  * 3. Neither the name of the Company nor the names of its contributors
29  * may be used to endorse or promote products derived from this
30  * software without specific prior written permission.
31  *
32  * ALTERNATIVELY, provided that this notice is retained in full, this
33  * product may be distributed under the terms of the GNU General Public
34  * License (GPL) version 2 or later, in which case the provisions
35  * of the GPL apply INSTEAD OF those given above.
36  *
37  * This software is provided ``as is'', and any express or implied
38  * warranties, including, but not limited to, the implied warranties of
39  * merchantability and fitness for a particular purpose are disclaimed.
40  * In no event shall the company or contributors be liable for any
41  * direct, indirect, incidental, special, exemplary, or consequential
42  * damages (including, but not limited to, procurement of substitute
43  * goods or services; loss of use, data, or profits; or business
44  * interruption) however caused and on any theory of liability, whether
45  * in contract, strict liability, or tort (including negligence or
46  * otherwise) arising in any way out of the use of this software, even
47  * if advised of the possibility of such damage.
48  *
49  */
50 #ifndef _UNIREC_LINKS_H_
51 #define _UNIREC_LINKS_H_
52 
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 
62 #include "inline.h"
63 
64 #define MAX_LINK_COUNT 64 //since link mask is held by uint64_t
65 
70 typedef struct {
71  unsigned int link_count;
72  uint64_t link_mask;
73  uint64_t *link_indexes;
74 } ur_links_t;
75 
84 ur_links_t *ur_create_links(const char *mask);
85 
91 void ur_free_links(ur_links_t *links);
92 
103 INLINE int ur_get_link_index(ur_links_t *links, uint64_t link_bit_field)
104 {
105  unsigned int i;
106  for (i = 0; i < links->link_count; ++i) {
107  /* search for corresponding value */
108  if ((link_bit_field >> links->link_indexes[i]) & 1L) {
109  return i;
110  }
111  }
112  /* ERROR */
113  return -1;
114 }
115 
124 INLINE uint64_t ur_get_link_bit_field_position(ur_links_t *links, unsigned int index)
125 {
126  if (index < links->link_count) {//index have to be from interval 0 - (n-1)
127  return links->link_indexes[index];
128  } else {
129  return 0;//returns 0 on error since no link can possibly have value 1 on
130  //position 0 in LINK_BIT_FIELD
131  }
132 }
133 
139 {
140  return links->link_mask;
141 }
142 
147 INLINE unsigned int ur_get_link_count(ur_links_t *links)
148 {
149  return links->link_count;
150 }
151 
152 #ifdef __cplusplus
153 } // extern "C"
154 #endif
155 
160 #endif
161 // END OF links.h
#define INLINE
Definition: inline.h:12