Multi-ApplicationOnlineProfiling  2.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Chained_List.c
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 #include "Chained_List.h"
20 
22  return chained_list_alloc_chunk(size_of_trunk, size_of_payload);
23 }
24 
25 
27  if (current_chunk == NULL)
28  return NULL;
29 
30  struct chained_list *tmp = chained_list_alloc_chunk(current_chunk->size_of_trunk, current_chunk->size_of_payload);
31  tmp->p_prev = current_chunk;
32 
33  return tmp;
34 }
35 
36 
37 
39  struct chained_list *tmp = malloc(sizeof(struct chained_list));
40 
41  if( !tmp ) {
42  printf("Failled to allocate chunk \n");
43  return NULL;
44  }
45 
46  tmp->p_payload = malloc(size_of_trunk * size_of_payload);
47 
48  if( !tmp->p_payload ) {
49  printf("Failled to alloc chunk \n");
50  return NULL;
51  }
52 
55  tmp->current_offset = 0;
56  tmp->p_prev = NULL;
57 
58  return tmp;
59 }
60 
61 
62 void *chained_list_push(struct chained_list **list, void *p_payload)
63 {
64 
65 
66  if (*list == NULL) {
67  fprintf(stderr,"Can't write to uninitialized chained_list in %s:%d\n", __FILE__, __LINE__);
68  return NULL;
69  }
70 
71  if ( (*list)->size_of_trunk <= (*list)->current_offset ) {
72  struct chained_list *tmp = chained_list_alloc_next_chunk(*list);
73 
74  if( !tmp )
75  return NULL;
76 
77 
78  *list = tmp;
79  }
80 
81  void *push_addr = (char *) (*list)->p_payload + ((*list)->current_offset * (*list)->size_of_payload);
82 
83  memcpy( push_addr, (char *) p_payload, (*list)->size_of_payload);
84 
85  (*list)->current_offset++;
86 
87 
88  return push_addr;
89 }
90 
91 void cond_chained_list_merge(struct chained_list **dest, struct chained_list *source, int (*condition) (void *))
92 {
93  if ((*dest)->size_of_payload != source->size_of_payload) {
94  fprintf(stderr, "Can't merge chained lists of different types in %s:%d\n", __FILE__, __LINE__);
95  return;
96  }
97 
98  struct chained_list *tmp = source;
99 
100  int i = 0;
101 
102  while (tmp) {
103  for (i = 0; i < tmp->current_offset; i++) {
104  void *current_elem = NULL;
105 
106  current_elem = (struct symbol_descriptor *) ((char *) tmp->p_payload + (i * tmp->size_of_payload));
107  if ((condition) (current_elem))
108  chained_list_push(dest, current_elem);
109  }
110  tmp = tmp->p_prev;
111  }
112 }
113 
114 
115 
116 void chained_list_walkthrough(struct chained_list *list, void (*action) (void *))
117 {
118  if( !action )
119  return;
120 
121  struct chained_list *tmp = list;
122  int i = 0;
123 
124  while (tmp) {
125  for (i = 0; i < tmp->current_offset; i++)
126  (action) ((char *) tmp->p_payload + (i * tmp->size_of_payload));
127 
128  tmp = tmp->p_prev;
129  }
130 }
131 
132 
133 
135  void (*action) (void *, void *), void *arg)
136 {
137  struct chained_list *tmp = list;
138  int i = 0;
139  while (tmp) {
140  for (i = 0; i < tmp->current_offset; i++)
141  (action) ((char *) tmp->p_payload +
142  (i * tmp->size_of_payload), arg);
143  tmp = tmp->p_prev;
144  }
145 }
146 
147 
148 
149 
150 
152 {
153  struct chained_list *tmp = list;
154  int count = 0;
155  while (tmp) {
156 
157  // printf("CLCount cout : %d tmp : %p \n", count, tmp);
158  count += tmp->current_offset;
159  tmp = tmp->p_prev;
160  }
161 
162  // printf("Count returning %d \n", count);
163  return count;
164 }
165 
166 
168 {
169  struct chained_list *tmp = *list;
170  struct chained_list *p_prev = NULL;
171 
172  while (tmp) {
173  p_prev = tmp->p_prev;
174  free(tmp->p_payload);
175  free(tmp);
176  tmp = p_prev;
177  }
178 
179  *list = NULL;
180 }
181 
182 
183 
184 
185 void *chained_list_get_elem( struct chained_list *list, int (*test_func)(void *, void *), void *value )
186 {
187  struct chained_list *tmp = list;
188  int i = 0;
189  void *elem = NULL;
190 
191  while (tmp) {
192  for (i = 0; i < tmp->current_offset; i++) {
193  elem = (char *) tmp->p_payload + (i * tmp->size_of_payload);
194 
195  if( test_func ) {
196  if( (test_func) (elem, value) ) {
197  return elem;
198  }
199  } else {
200  if( *((uint64_t *)elem) == *((uint64_t *)value) ) {
201  return elem;
202  }
203  }
204 
205  }
206  tmp = tmp->p_prev;
207  }
208 
209  return NULL;
210 }
211 
212 //void print_int(void *p_payload)
213 //{
214 // int *i = p_payload;
215 // printf("> %d \n", *i);
216 //}
217 
218 // int main()
219 // {
220 // int i = 0;
221 // struct chained_list *list = NULL;
222 // chained_list_push( &list, &i);
223 // list = chained_list_init( 10, sizeof(int) );
224 //
225 // for (i = 0 ; i < 210 ; i++ )
226 // chained_list_push( &list, &i);
227 //
228 // chained_list_walkthrough( list, print_int );
229 //
230 // int *pt = NULL;
231 // for( i = 0 ; i < 210 ; i++)
232 // {
233 // pt = (int *)chained_list_get_nth( list , i);
234 // printf("+> %d \n", *pt);
235 // }
236 // chained_list_release( &list );
237 //
238 // return 0;
239 // }
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
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
size_t size_of_payload
Size of the payload.
Definition: Chained_List.h:51
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
struct chained_list * p_prev
previous trunk
Definition: Chained_List.h:52
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
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
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