list2.c 9.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

gauthier's avatar
Licence  
gauthier committed
22

nikaeinn's avatar
nikaeinn committed
23 24 25 26 27 28 29
/*! \file pad_list.c
* \brief list management primimtives
* \author Mohamed Said MOSLI BOUKSIAA, Lionel GAUTHIER, Navid Nikaein
* \date 2012 - 2014
* \version 0.5
* @ingroup util
*/
gauthier's avatar
Licence  
gauthier committed
30

31 32 33 34 35 36 37 38 39 40 41
/***************************************************************************
                          list2.c  -  description
                             -------------------
                             -------------------
  AUTHOR  : Lionel GAUTHIER
  COMPANY : EURECOM
  EMAIL   : Lionel.Gauthier@eurecom.fr
 ***************************************************************************/
#define LIST2_C
#include "list.h"
#ifdef USER_MODE
gauthier's avatar
gauthier committed
42
#include "assertions.h"
43 44 45 46
#else
#define NULL 0
#endif

47 48
#include <string.h>

49 50 51 52 53 54 55 56 57

//-----------------------------------------------------------------------------
/*
 * initialize list
 */
//-----------------------------------------------------------------------------
void
list2_init (list2_t * listP, char *nameP)
{
58
  //-----------------------------------------------------------------------------
59
  if (nameP) {
60 61
    strncpy( listP->name, nameP, LIST_NAME_MAX_CHAR );
    listP->name[LIST_NAME_MAX_CHAR-1] = 0; // terminate string
62
  }
63

64 65 66 67 68 69 70 71 72
  listP->tail = NULL;
  listP->head = NULL;
  listP->nb_elements = 0;
}

//-----------------------------------------------------------------------------
void
list2_free (list2_t * listP)
{
73
  //-----------------------------------------------------------------------------
74 75 76 77

  mem_block_t      *le;

  while ((le = list2_remove_head (listP))) {
78
    free_mem_block (le, __func__);
79 80 81 82 83 84 85 86 87 88 89 90 91
  }
}

//-----------------------------------------------------------------------------
/*
 *  remove an element from list
 *  @param  pointer on targeted list
 *  @param  mem_block_t to remove
 *  @return pointer on removed mem_block_t
 */
mem_block_t *
list2_remove_element (mem_block_t * elementP, list2_t * listP)
{
92
  //-----------------------------------------------------------------------------
93 94 95 96 97

  if (elementP != NULL) {
    // head of list
    if (elementP == listP->head) {
      listP->head = elementP->next;
98

99 100 101 102 103 104
      if (listP->head == NULL) {
        listP->tail = NULL;
      } else {
        elementP->next->previous = NULL;
        elementP->next = NULL;
      }
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
      // tail of the list
      // note : case of 1 remaining element in the list has been treated above
    } else if (elementP == listP->tail) {
      // so several elements in the list
      listP->tail = elementP->previous;
      listP->tail->next = NULL;
      elementP->previous = NULL;
      // in the middle of the list, after a head element and before the tail element
    } else {
      // link element n-1 with element n+1
      elementP->previous->next = elementP->next;
      elementP->next->previous = elementP->previous;
      elementP->next = NULL;
      elementP->previous = NULL;
    }
121

122 123
    listP->nb_elements = listP->nb_elements - 1;
  }
124

125 126 127 128 129 130
  return elementP;
}
//-----------------------------------------------------------------------------
mem_block_t *
list2_get_head (list2_t * listP)
{
131
  //-----------------------------------------------------------------------------
132 133 134 135
  return listP->head;
}

//-----------------------------------------------------------------------------
136 137 138
mem_block_t *
list2_get_tail (list2_t * listP)
{
139
  //-----------------------------------------------------------------------------
140 141 142 143
  return listP->tail;
}

//-----------------------------------------------------------------------------
144 145 146 147 148 149 150 151
/*
 *  remove an element from head of a list
 *  @param  pointer on targeted list
 *  @return pointer on removed mem_block_t
 */
mem_block_t *
list2_remove_head (list2_t * listP)
{
152
  //-----------------------------------------------------------------------------
153 154 155 156 157

  // access optimisation
  mem_block_t      *head;

  head = listP->head;
158

159 160 161 162
  // almost one element
  if (head != NULL) {
    listP->head = head->next;
    listP->nb_elements = listP->nb_elements - 1;
163

164 165 166 167 168 169 170 171 172 173
    // if only one element, update tail
    if (listP->head == NULL) {
      listP->tail = NULL;
    } else {
      listP->head->previous = NULL;
      head->next = NULL;
    }
  } else {
    //msg("[MEM_MGT][WARNING] remove_head_from_list(%s) no elements\n",listP->name);
  }
174

175 176 177 178 179 180 181 182 183 184 185 186
  return head;
}

//-----------------------------------------------------------------------------
/*
 *  remove an element from tail of a list
 *  @param  pointer on targeted list
 *  @return pointer on removed mem_block_t
 */
mem_block_t *
list2_remove_tail (list2_t * listP)
{
187
  //-----------------------------------------------------------------------------
188 189 190 191 192 193

  // access optimisation;
  mem_block_t      *tail;


  tail = listP->tail;
194

195 196 197
  // almost one element;
  if (tail != NULL) {
    listP->nb_elements = listP->nb_elements - 1;
198

199 200 201 202 203 204 205 206
    // if only one element, update head, tail;
    if (listP->head == tail) {
      listP->head = NULL;
      listP->tail = NULL;
    } else {
      listP->tail = tail->previous;
      tail->previous->next = NULL;
    }
207

208 209 210 211
    tail->previous = NULL;
  } else {
    //msg("[MEM_MGT][WARNING] remove_head_from_list(%s) no elements\n",listP->name);
  }
212

213 214 215 216 217 218 219 220 221 222 223 224
  return tail;
}

//-----------------------------------------------------------------------------
/*
 *  add an element to the beginning of a list
 *  @param  pointer on targeted list
 *  @return pointer on removed mem_block_t
 */
void
list2_add_head (mem_block_t * elementP, list2_t * listP)
{
225
  //-----------------------------------------------------------------------------
226 227 228 229 230 231 232

  // access optimisation;
  mem_block_t      *head;

  if (elementP != NULL) {
    head = listP->head;
    listP->nb_elements = listP->nb_elements + 1;
233

234 235
    // almost one element
    if (head == NULL) {
Bilel's avatar
Bilel committed
236 237
      elementP->previous = NULL;
      elementP->next = NULL;
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
      listP->head = elementP;
      listP->tail = elementP;
    } else {
      elementP->next = head;
      head->previous = elementP;
      elementP->previous = NULL;
      listP->head = elementP;
    }
  }
}

//-----------------------------------------------------------------------------
/*
 *  add an element to the end of a list
 *  @param  pointer on targeted list
 *  @return pointer on removed mem_block_t
 */
void
list2_add_tail (mem_block_t * elementP, list2_t * listP)
{
  mem_block_t      *tail;
259
  //-----------------------------------------------------------------------------
260 261 262 263 264 265

  if (elementP != NULL) {
    // access optimisation
    listP->nb_elements = listP->nb_elements + 1;
    elementP->next = NULL;
    tail = listP->tail;
266

267 268 269 270 271 272 273 274
    // almost one element
    if (tail == NULL) {
      elementP->previous = NULL;
      listP->head = elementP;
    } else {
      tail->next = elementP;
      elementP->previous = tail;
    }
275

276 277 278 279 280 281 282 283
    listP->tail = elementP;
  }
}

//-----------------------------------------------------------------------------
void
list2_add_list (list2_t * sublistP, list2_t * listP)
{
284
  //-----------------------------------------------------------------------------
285 286 287 288 289 290 291

  if (sublistP) {
    if (sublistP->head) {
      // access optimisation
      mem_block_t      *tail;

      tail = listP->tail;
292

293 294 295 296 297 298 299
      // almost one element
      if (tail == NULL) {
        listP->head = sublistP->head;
      } else {
        tail->next = sublistP->head;
        sublistP->head->previous = tail;
      }
300

301 302 303 304 305 306 307 308 309 310 311 312 313 314
      listP->tail = sublistP->tail;
      // clear sublist
      sublistP->head = NULL;
      sublistP->tail = NULL;
    }

    listP->nb_elements = listP->nb_elements + sublistP->nb_elements;
  }
}

//-----------------------------------------------------------------------------
void
list2_display (list2_t * listP)
{
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  //-----------------------------------------------------------------------------
  /*
    mem_block_t      *cursor;
    unsigned short             nb_elements = 0;
    //uint32_t nb_bytes;
    // uint32_t index;

    // test lists
    if (listP) {
      cursor = listP->head;
      if (cursor) {
        // almost one element
        msg ("Display list %s %p", listP->name, listP);
        while (cursor != NULL) {
          msg ("%d:", cursor->pool_id);
          //nb_bytes = (( sdu_management*)(cursor->misc))->size;
          //   for (index=0; index < nb_bytes; index++) {
          //   msg("%02X.",cursor->data[index]);
          //   }
          msg ("\n");
          cursor = cursor->next;
          nb_elements++;
        }
        msg (" found nb_elements %d nb_elements %d\n", nb_elements, listP->nb_elements);
  #ifdef USER_MODE
        AssertFatal(nb_elements == listP->nb_elements, "Bad count of elements %d != %d", nb_elements, listP->nb_elements);
  #endif
342
      }
343
    }*/
344
}