Multi-ApplicationOnlineProfiling  2.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Bit_Array.h
Go to the documentation of this file.
1 /* ############################ MALP License ############################## */
2 /* # Fri Jan 18 14:00:00 CET 2013 # */
3 /* # Copyright or (C) or Copr. Commissariat a l'Energie Atomique # */
4 /* # # */
5 /* # This software is governed by the CeCILL-C license under French law # */
6 /* # and abiding by the rules of distribution of free software. You can # */
7 /* # use, modify and/ or redistribute the software under the terms of # */
8 /* # the CeCILL-C license as circulated by CEA, CNRS and INRIA at the # */
9 /* # following URL http://www.cecill.info. # */
10 /* # # */
11 /* # The fact that you are presently reading this means that you have # */
12 /* # had knowledge of the CeCILL-C license and that you accept its # */
13 /* # terms. # */
14 /* # # */
15 /* # Authors: # */
16 /* # - BESNARD Jean-Baptiste jean-baptiste.besnard@cea.fr # */
17 /* # # */
18 /* ######################################################################## */
19 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdint.h>
28 
29 #ifndef BIT_ARRAY_H
30 #define BIT_ARRAY_H
31 
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #endif
36 
51 static const int get_set_bit_mask[] = {
52  1,
53  2,
54  4,
55  8,
56  16,
57  32,
58  64,
59  128
60 };
61 
62 
76 static const int unset_bit_mask[] = {
77  254,
78  253,
79  251,
80  247,
81  239,
82  223,
83  191,
84  127
85 };
86 
90 struct Bit_Array {
91  uint8_t *array;
92  uint64_t size;
93  uint64_t real_size;
94 };
95 
101 static inline uint64_t Bit_Array_size(struct Bit_Array *ba)
102 {
103  return ba->real_size;
104 }
105 
111 void Bit_Array_init( struct Bit_Array *ba, uint64_t size );
112 
117 void Bit_Array_release( struct Bit_Array *ba );
118 
126 void Bit_Array_replicate( struct Bit_Array *dest, struct Bit_Array *src );
127 
132 void Bit_Array_dump( struct Bit_Array *ba );
133 
140 static inline void Bit_Array_set( struct Bit_Array *ba, uint64_t key, uint8_t value )
141 {
142 
143  if( ba->real_size <= key )
144  {
145  //printf(" SET Out of bound (%ld)\n", key);
146  return;
147  }
148 
149  uint64_t extern_offset = key >> 3;
150  uint8_t local_offset = key - extern_offset*8;
151 
152  uint8_t *target = &ba->array[extern_offset];
153 
154  if( value )
155  *target |= get_set_bit_mask[local_offset];
156  else
157  *target &= unset_bit_mask[local_offset];
158 
159 }
160 
167 static inline uint8_t Bit_Array_get( struct Bit_Array *ba, uint64_t key )
168 {
169  if( ba->real_size <= key )
170  {
171  //printf(" GET Out of bound (%ld) \n", key);
172  return 3;
173  }
174 
175  uint64_t extern_offset = key >> 3;
176  uint8_t local_offset = key - extern_offset*8;
177 
178 
179  return (ba->array[extern_offset] & get_set_bit_mask[local_offset]) >> local_offset;
180 
181 }
182 
183 #ifdef __cplusplus
184 }
185 #endif
186 
187 #endif
188 
uint8_t * array
The data.
Definition: Bit_Array.h:91
void Bit_Array_dump(struct Bit_Array *ba)
Prints a Bit_Array on standard output.
Definition: Bit_Array.c:68
uint64_t size
The number of uint8_t array contains.
Definition: Bit_Array.h:92
void Bit_Array_release(struct Bit_Array *ba)
Bit_Array deinitializer.
Definition: Bit_Array.c:41
void Bit_Array_init(struct Bit_Array *ba, uint64_t size)
Bit_Array initializer.
Definition: Bit_Array.c:23
uint64_t real_size
The number of bits the array contains.
Definition: Bit_Array.h:93
static uint64_t Bit_Array_size(struct Bit_Array *ba)
Getter on the array size.
Definition: Bit_Array.h:101
static const int get_set_bit_mask[]
Masks used to get and set bits.
Definition: Bit_Array.h:51
static const int unset_bit_mask[]
Masks used to unset bits.
Definition: Bit_Array.h:76
static void Bit_Array_set(struct Bit_Array *ba, uint64_t key, uint8_t value)
Sets a bit in a Bit_Array.
Definition: Bit_Array.h:140
static uint8_t Bit_Array_get(struct Bit_Array *ba, uint64_t key)
Gets a bit in a Bit_Array.
Definition: Bit_Array.h:167
void Bit_Array_replicate(struct Bit_Array *dest, struct Bit_Array *src)
Copies a Bit_Array into another.
Definition: Bit_Array.c:55
Defines a bit array.
Definition: Bit_Array.h:90