Multi-ApplicationOnlineProfiling  2.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Chained_List.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 <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #ifndef CHAINED_LIST
30 #define CHAINED_LIST
31 
32 #ifdef __cplusplus
33  extern "C"
34  {
35 #endif
36 
37 #include <stdio.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
47 struct chained_list {
48  void *p_payload;
49  uint32_t current_offset;
50  uint32_t size_of_trunk;
51  size_t size_of_payload;
52  struct chained_list *p_prev;
53 };
54 
55 
63 
68 void chained_list_release(struct chained_list **list);
69 
76  *current_chunk);
77 
85  size_t size_of_payload);
86 
92 int chained_list_count(struct chained_list *list);
93 
100 void *chained_list_push(struct chained_list **list, void *p_payload);
101 
108 void cond_chained_list_merge(struct chained_list **dest,
109  struct chained_list *source,
110  int (*condition) (void *));
111 
117 void chained_list_walkthrough(struct chained_list *list,
118  void (*action) (void *));
119 
127  void (*action) (void *, void *), void *arg);
135  uint64_t last_chunk_offset;
136 };
137 
142 static inline void chained_list_view_init( struct chained_list_view *clv )
143 {
144  clv->last_chunk = NULL;
145  clv->last_chunk_offset = 0;
146 }
147 
155 void *chained_list_get_elem( struct chained_list *list, int (*test_func)(void *, void *), void *value );
156 
157 
158 /* Inlining of hot getters **/
159 
165 static inline void *chained_list_get_nth(struct chained_list *list, uint64_t n)
166 {
167  uint64_t local_n = n + 1;
168 
169  //printf("BEGIN PROCESS %d ( in %p)\n", n, list);
170  struct chained_list *tmp = list;
171 
172  while (tmp) {
173 
174  //printf("tmp % p OFF %d, local_n %d\n",tmp, tmp->current_offset, local_n);
175 
176  if ( (local_n <= tmp->current_offset) && (tmp->current_offset) ) {
177 
178  int applied_offset = ( tmp->current_offset *tmp->size_of_payload) - (local_n * tmp->size_of_payload);
179 
180  //printf("AP OFF FOR %lld is %d over %d\n", local_n , applied_offset, tmp->current_offset * tmp->size_of_payload );
181  return (void *) ((char *)tmp->p_payload + applied_offset);
182  }
183 
184  else {
185 
186  //printf("Going to next %p\n", tmp->p_prev);
187  local_n -= tmp->current_offset;
188  tmp = tmp->p_prev;
189  }
190  }
191 
192  // printf ("Get nth failed \n");
193  return NULL;
194 }
195 
196 
207 static inline void *chained_list_get_nth_seq(struct chained_list *list, struct chained_list_view *view, uint64_t n)
208 {
209  struct chained_list *tmp = list;
210  n++;
211 
212  uint64_t tmp_n = 0;
213 
214  if( !list )
215  return NULL;
216 
217  //If we already known an offset and it is lower than n
218  if( view->last_chunk ) {
219  if( view->last_chunk_offset < n ) {
220  //printf("FAST FORWARD TO OFFSET %lld (%p) \n", view->last_chunk_offset, view->last_chunk);
221  tmp = view->last_chunk;
222 
223  tmp_n = view->last_chunk_offset;
224  n -= view->last_chunk_offset;
225  }
226  }
227 
228 
229  if( tmp->current_offset <= tmp->current_offset ) {
230 
231  while( tmp ) {
232  if( (n <= tmp->current_offset) && (tmp->current_offset) ) {
233  //Data is in this chunk
234  //Save this chunk infos
235  view->last_chunk = tmp;
236  view->last_chunk_offset = tmp_n;
237 
238  //printf("SAVING CHUNK %p OFF : %lld \n", view->last_chunk, tmp_n );
239 
240  break;
241  } else {
242  n -= tmp->current_offset;
243  tmp_n += tmp->current_offset ;
244  tmp = tmp->p_prev;
245  }
246  }
247  }
248 
249  //printf("CANDIDANDE N : %d over %d \n", n, tmp->current_offset );
250  /* n was too large */
251  if( !tmp )
252  return NULL;
253 
254  int applied_offset = ( tmp->current_offset * tmp->size_of_payload ) - (n * tmp->size_of_payload);
255 
256  //printf("AP OFF FOR %lld is %d over %d\n", n , applied_offset, tmp->current_offset * tmp->size_of_payload );
257  return (void *) ((char *)tmp->p_payload + applied_offset);
258 }
259 
273 struct Chained_List {
274  struct chained_list *list;
275  uint64_t count;
276  uint64_t chunk_size;
277  size_t payload_size;
278 };
279 
286 static inline void Chained_List_init( struct Chained_List *list, uint64_t chunk_size, size_t payload_size )
287 {
288  list->list = NULL;
289  list->count = 0;
290  list->chunk_size = chunk_size;
291  list->payload_size = payload_size;
292 }
293 
298 static inline void Chained_List_release( struct Chained_List *list )
299 {
300  chained_list_release( &list->list );
301  list->count = 0;
302 }
303 
312 static inline void* Chained_List_push( struct Chained_List *list, void *payload )
313 {
314  if( list->list == NULL )
315  list->list = chained_list_init( list->chunk_size, list->payload_size );
316 
317  void *ret = chained_list_push( &(list->list), payload );
318  if( ret )
319  list->count++;
320  return ret;
321 }
322 
328 static inline uint64_t Chained_List_count( struct Chained_List *list )
329 {
330  return list->count;
331 }
332 
339 static inline void Chained_List_cond_merge(struct Chained_List *dest, struct Chained_List *source, int (*condition) (void *))
340 {
341  if( source->list == NULL )
342  return;
343 
344  if( dest->list == NULL )
345  dest->list = chained_list_init( source->chunk_size, source->payload_size );
346 
347  cond_chained_list_merge( &dest->list, source->list, condition);
348 }
349 
355 static inline void Chained_List_walkthrough(struct Chained_List *list, void (*action) (void *))
356 {
357  if( list->list == NULL || list->count == 0 )
358  return;
359 
360  chained_list_walkthrough( list->list, action);
361 }
362 
369 static inline void Chained_List_walkthrough_arg(struct Chained_List *list, void (*action) (void *, void *), void *arg)
370 {
371  if( list->list == NULL || list->count == 0)
372  return;
373 
374  chained_list_walkthrough_arg( list->list, action, arg);
375 }
376 
383 static inline void * Chained_List_get_nth(struct Chained_List *list, uint64_t n)
384 {
385  if( list->list == NULL || list->count == 0)
386  return NULL;
387 
388  return chained_list_get_nth( list->list, n);
389 }
390 
401 static inline void *Chained_List_get_nth_seq(struct Chained_List *cl, struct chained_list_view *view, uint64_t n)
402 {
403  if( cl->list == NULL || cl->count == 0 )
404  return NULL;
405 
406  return chained_list_get_nth_seq( cl->list, view, n);
407 }
408 
416 static inline void *Chained_List_get_elem( struct Chained_List *list, int (*test_func)(void *, void *), void *value )
417 {
418  if( list->list == NULL )
419  return NULL;
420 
421  return chained_list_get_elem( list->list, test_func, value );
422 }
423 #ifdef __cplusplus
424 }
425 #endif
426 #endif
427 
struct chained_list * chained_list_alloc_next_chunk(struct chained_list *current_chunk)
Alocate a new chunk pointing to current_chunk (Recopy MODE )
Definition: Chained_List.c:26
int chained_list_count(struct chained_list *list)
Count elements in the chained list.
Definition: Chained_List.c:151
This struct is used to improve performance when sequentially walking through.
Definition: Chained_List.h:133
void * chained_list_push(struct chained_list **list, void *p_payload)
Push an element in the chained list.
Definition: Chained_List.c:62
Basic buffered chained list.
Definition: Chained_List.h:47
void chained_list_release(struct chained_list **list)
Release a buffered chained list.
Definition: Chained_List.c:167
static void * chained_list_get_nth(struct chained_list *list, uint64_t n)
Get a reference to the nth element of the chained list.
Definition: Chained_List.h:165
static void chained_list_view_init(struct chained_list_view *clv)
Initializes a chained_list_view.
Definition: Chained_List.h:142
size_t size_of_payload
Size of the payload.
Definition: Chained_List.h:51
size_t payload_size
the size of elements that can be stored in the list
Definition: Chained_List.h:277
static void * Chained_List_get_nth(struct Chained_List *list, uint64_t n)
Gets the nth element of the list.
Definition: Chained_List.h:383
static void Chained_List_cond_merge(struct Chained_List *dest, struct Chained_List *source, int(*condition)(void *))
Push all element of source satisfying condition in dest.
Definition: Chained_List.h:339
uint64_t count
the number of elements in the list
Definition: Chained_List.h:275
struct chained_list * last_chunk
the chunk containing last element
Definition: Chained_List.h:134
void chained_list_walkthrough_arg(struct chained_list *list, void(*action)(void *, void *), void *arg)
Call a function uppon each element of the chained list.
Definition: Chained_List.c:134
static void Chained_List_init(struct Chained_List *list, uint64_t chunk_size, size_t payload_size)
Initializes a Chained_List.
Definition: Chained_List.h:286
static uint64_t Chained_List_count(struct Chained_List *list)
Counts the elements in the list.
Definition: Chained_List.h:328
struct chained_list * p_prev
previous trunk
Definition: Chained_List.h:52
struct chained_list * list
the head chained_list chunk
Definition: Chained_List.h:274
This a structure wrapps the use of chained_list This is the main chain list structure to use...
Definition: Chained_List.h:273
static void * Chained_List_push(struct Chained_List *list, void *payload)
Adds an element to the list.
Definition: Chained_List.h:312
static void Chained_List_walkthrough(struct Chained_List *list, void(*action)(void *))
Call a function uppon each element of the chained list.
Definition: Chained_List.h:355
void * chained_list_get_elem(struct chained_list *list, int(*test_func)(void *, void *), void *value)
Get a pointer to the first element which matches a value.
Definition: Chained_List.c:185
void chained_list_walkthrough(struct chained_list *list, void(*action)(void *))
Call a function uppon each element of the chained list.
Definition: Chained_List.c:116
void * p_payload
payload array of trunk
Definition: Chained_List.h:48
uint64_t last_chunk_offset
the offset of last element in last chunk
Definition: Chained_List.h:135
void cond_chained_list_merge(struct chained_list **dest, struct chained_list *source, int(*condition)(void *))
Push all element of source satisfying condition in dest.
Definition: Chained_List.c:91
uint32_t current_offset
Current offset in trunk.
Definition: Chained_List.h:49
uint64_t chunk_size
the size of the chunks
Definition: Chained_List.h:276
struct chained_list * chained_list_init(uint32_t size_of_trunk, size_t size_of_payload)
Ininitalizes a buffered chained list.
Definition: Chained_List.c:21
struct chained_list * chained_list_alloc_chunk(uint32_t size_of_trunk, size_t size_of_payload)
Alocate a new chunk ( first chunk )
Definition: Chained_List.c:38
uint32_t size_of_trunk
Size of a trunk.
Definition: Chained_List.h:50
static void Chained_List_release(struct Chained_List *list)
Clears a Chained_List.
Definition: Chained_List.h:298
static void * chained_list_get_nth_seq(struct chained_list *list, struct chained_list_view *view, uint64_t n)
Get a reference to the nth element of the chained list (optimised for sequential) ...
Definition: Chained_List.h:207
static void Chained_List_walkthrough_arg(struct Chained_List *list, void(*action)(void *, void *), void *arg)
Call a function uppon each element of the chained list.
Definition: Chained_List.h:369
static void * Chained_List_get_elem(struct Chained_List *list, int(*test_func)(void *, void *), void *value)
Get a pointer to the first element which matches a value.
Definition: Chained_List.h:416
static void * Chained_List_get_nth_seq(struct Chained_List *cl, struct chained_list_view *view, uint64_t n)
Get a reference to the nth element of the chained list (optimised for sequential) ...
Definition: Chained_List.h:401