mem_block.c 23.9 KB
Newer Older
1 2 3 4 5
/*
 * 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
Cedric Roux's avatar
Cedric Roux committed
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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
 */

22 23 24 25 26 27 28 29 30 31 32 33 34 35
/***************************************************************************
                          mem_block.c  -  description
                             -------------------
  AUTHOR  : Lionel GAUTHIER
  COMPANY : EURECOM
  EMAIL   : Lionel.Gauthier@eurecom.fr


 ***************************************************************************/
#define MEM_BLOCK_C
//#include "rtos_header.h"
#include "mem_block.h"
#include "mem_pool.h"
#include "list.h"
36
#include "LAYER2/MAC/mac_extern.h"
knopp's avatar
knopp committed
37
#include "assertions.h"
roux's avatar
roux committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51
/* all function calls are protected by a mutex
 * to ensure that many threads calling them at
 * the same time don't mess up.
 * We might be more clever in the future, it's a
 * bit overkill.
 * Commenting this define removes the protection,
 * so be careful with it.
 */
#define MEMBLOCK_BIG_LOCK

#ifdef MEMBLOCK_BIG_LOCK
static pthread_mutex_t mtex = PTHREAD_MUTEX_INITIALIZER;
#endif

52 53 54 55 56
//-----------------------------------------------------------------------------
//#define DEBUG_MEM_MNGT_FREE
//#define DEBUG_MEM_MNGT_ALLOC_SIZE
//#define DEBUG_MEM_MNGT_ALLOC
//-----------------------------------------------------------------------------
Mongazon's avatar
Mongazon committed
57
#if defined(DEBUG_MEM_MNGT_ALLOC)
58
uint32_t             counters[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
59 60 61 62 63
#endif
//-----------------------------------------------------------------------------
/*
 * initialize all ures
 */
Thomas Laurent's avatar
Thomas Laurent committed
64
extern mem_pool  *memBlockVar;
65
void           *
66
pool_buffer_init (void)
67
{
68
  //-----------------------------------------------------------------------------
69

70
  uint32_t             index, mb_index, pool_index;
71
  mem_pool       *memory = (mem_pool *) &mem_block_var;
Thomas Laurent's avatar
Thomas Laurent committed
72
  memBlockVar=malloc(sizeof(mem_pool));
73
  int             pool_sizes[14] = { MEM_MNGT_MB0_NB_BLOCKS, MEM_MNGT_MB1_NB_BLOCKS,
74 75 76 77 78 79 80
                                     MEM_MNGT_MB2_NB_BLOCKS, MEM_MNGT_MB3_NB_BLOCKS,
                                     MEM_MNGT_MB4_NB_BLOCKS, MEM_MNGT_MB5_NB_BLOCKS,
                                     MEM_MNGT_MB6_NB_BLOCKS, MEM_MNGT_MB7_NB_BLOCKS,
                                     MEM_MNGT_MB8_NB_BLOCKS, MEM_MNGT_MB9_NB_BLOCKS,
                                     MEM_MNGT_MB10_NB_BLOCKS, MEM_MNGT_MB11_NB_BLOCKS,
                                     MEM_MNGT_MB12_NB_BLOCKS, MEM_MNGT_MBCOPY_NB_BLOCKS
                                   };
81

roux's avatar
roux committed
82 83 84 85
#ifdef MEMBLOCK_BIG_LOCK
  if (pthread_mutex_lock(&mtex)) abort();
#endif

86 87
  memset (memory, 0, sizeof (mem_pool));
  mb_index = 0;
88

89 90 91
  // LG_TEST
  for (pool_index = 0; pool_index <= MEM_MNGT_POOL_ID_COPY; pool_index++) {
    list_init (&memory->mem_lists[pool_index], "POOL");
92

93 94 95 96
    for (index = 0; index < pool_sizes[pool_index]; index++) {
      //memory->mem_blocks[mb_index + index].previous = NULL; -> done in memset 0
      //memory->mem_blocks[mb_index + index].next     = NULL; -> done in memset 0
      switch (pool_index) {
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
      case 0:
        memory->mem_blocks[mb_index + index].data = (unsigned char*)&(memory->mem_pool0[index][0]);
        break;

      case 1:
        memory->mem_blocks[mb_index + index].data = (unsigned char*)&(memory->mem_pool1[index][0]);
        break;

      case 2:
        memory->mem_blocks[mb_index + index].data = (unsigned char*)&(memory->mem_pool2[index][0]);
        break;

      case 3:
        memory->mem_blocks[mb_index + index].data = (unsigned char*)&(memory->mem_pool3[index][0]);
        break;

      case 4:
        memory->mem_blocks[mb_index + index].data = (unsigned char*)&(memory->mem_pool4[index][0]);
        break;

      case 5:
        memory->mem_blocks[mb_index + index].data = (unsigned char*)&(memory->mem_pool5[index][0]);
        break;

      case 6:
        memory->mem_blocks[mb_index + index].data = (unsigned char*)&(memory->mem_pool6[index][0]);
        break;

      case 7:
        memory->mem_blocks[mb_index + index].data = (unsigned char*)&(memory->mem_pool7[index][0]);
        break;

      case 8:
        memory->mem_blocks[mb_index + index].data = (unsigned char*)&(memory->mem_pool8[index][0]);
        break;

      case 9:
        memory->mem_blocks[mb_index + index].data = (unsigned char*)&(memory->mem_pool9[index][0]);
        break;

      case 10:
        memory->mem_blocks[mb_index + index].data = (unsigned char*)&(memory->mem_pool10[index][0]);
        break;

      case 11:
        memory->mem_blocks[mb_index + index].data = (unsigned char*)&(memory->mem_pool11[index][0]);
        break;

      case 12:
        memory->mem_blocks[mb_index + index].data = (unsigned char*)&(memory->mem_pool12[index][0]);
        break;

      default:
        ;
        memory->mem_blocks[mb_index + index].data = NULL;   // pool copy
152 153

      }
154

155 156 157
      memory->mem_blocks[mb_index + index].pool_id = pool_index;
      list_add_tail_eurecom (&memory->mem_blocks[mb_index + index], &memory->mem_lists[pool_index]);
    }
158

159 160
    mb_index += pool_sizes[pool_index];
  }
161

roux's avatar
roux committed
162 163 164 165
#ifdef MEMBLOCK_BIG_LOCK
  if (pthread_mutex_unlock(&mtex)) abort();
#endif

166 167 168 169 170 171 172
  return 0;
}

//-----------------------------------------------------------------------------
void           *
pool_buffer_clean (void *arg)
{
173
  //-----------------------------------------------------------------------------
174 175 176 177
  return 0;
}
//-----------------------------------------------------------------------------
void
178
free_mem_block (mem_block_t * leP, const char* caller)
179
{
180
  //-----------------------------------------------------------------------------
181 182

  if (!(leP)) {
183
    LOG_W (RLC,"[MEM_MNGT][FREE] WARNING FREE NULL MEM_BLOCK\n");
184 185
    return;
  }
186

roux's avatar
roux committed
187 188 189 190
#ifdef MEMBLOCK_BIG_LOCK
  if (pthread_mutex_lock(&mtex)) abort();
#endif

191
#ifdef DEBUG_MEM_MNGT_FREE
192
  LOG_D (RLC,"[MEM_MNGT][FREE] free_mem_block() %p pool: %d\n", leP, leP->pool_id);
193 194
#endif
#ifdef DEBUG_MEM_MNGT_ALLOC
195
  check_free_mem_block (leP);
196
#endif
197 198 199

  if (leP->pool_id <= MEM_MNGT_POOL_ID_COPY) {
    list_add_tail_eurecom (leP, &mem_block_var.mem_lists[leP->pool_id]);
200
#ifdef DEBUG_MEM_MNGT_ALLOC
201
    counters[leP->pool_id] -= 1;
202
    LGO_D (RLC,"[%s][MEM_MNGT][INFO] after pool[%2d] freed: counters = {%2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d}\n",
203 204 205 206
        caller, leP->pool_id,
        counters[0],counters[1],counters[2],counters[3],counters[4],
        counters[5],counters[6],counters[7],counters[8],counters[9],
        counters[10],counters[11]);
207
#endif
208 209
    leP = NULL;                 // this prevent from freeing the block twice
  } else {
210
    LOG_E (RLC,"[MEM_MNGT][FREE] ERROR free_mem_block() unknown pool_id : %d\n", leP->pool_id);
211
  }
roux's avatar
roux committed
212 213 214 215

#ifdef MEMBLOCK_BIG_LOCK
  if (pthread_mutex_unlock(&mtex)) abort();
#endif
216 217 218 219
}

//-----------------------------------------------------------------------------
mem_block_t      *
220
get_free_mem_block (uint32_t sizeP, const char* caller)
221
{
222
  //-----------------------------------------------------------------------------
223 224 225 226
  mem_block_t      *le = NULL;
  int             pool_selected;
  int             size;

227
  if (sizeP > MEM_MNGT_MB12_BLOCK_SIZE) {
228
    LOG_E (RLC,"[MEM_MNGT][ERROR][FATAL] size requested %d out of bounds\n", sizeP);
229
    display_mem_load ();
230
    AssertFatal(1==0,"get_free_mem_block size requested out of bounds");
231 232
    return NULL;
  }
roux's avatar
roux committed
233 234 235 236 237

#ifdef MEMBLOCK_BIG_LOCK
  if (pthread_mutex_lock(&mtex)) abort();
#endif

238 239 240 241 242 243 244 245 246 247 248 249
  size = sizeP >> 6;
  pool_selected = 0;

  while ((size)) {
    pool_selected += 1;
    size = size >> 1;
  }

  // pool is selected according to the size requested, now get a block
  // if no block is available pick one in an other pool
  do {
    if ((le = list_remove_head (&mem_block_var.mem_lists[pool_selected]))) {
250
#ifdef DEBUG_MEM_MNGT_ALLOC
251
      counters[pool_selected] += 1;
252
      LOG_D (RLC,"[%s][MEM_MNGT][INFO] after pool[%2d] allocated: counters = {%2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d}\n",
253 254 255 256 257
          caller,
          pool_selected,
          counters[0],counters[1],counters[2],counters[3],counters[4],
          counters[5],counters[6],counters[7],counters[8],counters[9],
          counters[10],counters[11]);
258 259
#endif
#ifdef DEBUG_MEM_MNGT_ALLOC_SIZE
260
      LOG_D (RLC,"[MEM_MNGT][INFO] ALLOC MEM_BLOCK SIZE %d bytes pool %d (%p)\n", sizeP, pool_selected,le);
261
#endif
roux's avatar
roux committed
262

263 264
      AssertFatal(le->pool_id == pool_selected, "Unexpected pool ID!");

roux's avatar
roux committed
265 266 267 268
#ifdef MEMBLOCK_BIG_LOCK
  if (pthread_mutex_unlock(&mtex)) abort();
#endif

269 270
      return le;
    }
271 272

#ifdef DEBUG_MEM_MNGT_ALLOC
273
    LOG_E (RLC,"[MEM_MNGT][ERROR][MINOR] memory pool %d is empty trying next pool alloc count = %d\n", pool_selected, counters[pool_selected]);
274 275
    //    display_mem_load ();
    //    check_mem_area ((void *)&mem_block_var);
276
#endif
277 278
  } while (pool_selected++ < 12);

279
  LOG_E(PHY, "[MEM_MNGT][ERROR][FATAL] failed allocating MEM_BLOCK size %d byes (pool_selected=%d size=%d)\n", sizeP, pool_selected, size);
280 281 282
//  display_mem_load();
//  AssertFatal(1==0,"get_free_mem_block failed");
  LOG_E(MAC,"[MEM_MNGT][ERROR][FATAL] get_free_mem_block failed!!!\n");
roux's avatar
roux committed
283 284 285 286
#ifdef MEMBLOCK_BIG_LOCK
  if (pthread_mutex_unlock(&mtex)) abort();
#endif

287 288 289 290 291 292 293
  return NULL;
};

//-----------------------------------------------------------------------------
mem_block_t      *
get_free_copy_mem_block (void)
{
294
  //-----------------------------------------------------------------------------
295 296
  mem_block_t      *le;

roux's avatar
roux committed
297 298 299 300
#ifdef MEMBLOCK_BIG_LOCK
  AssertFatal(0, "This function is not handled properly but not used anywhere. FIXME?\n");
#endif

301 302
  if ((le = list_remove_head (&mem_block_var.mem_lists[MEM_MNGT_POOL_ID_COPY]))) {
#ifdef DEBUG_MEM_MNGT_ALLOC_SIZE
303
    LOG_D (RLC,"[MEM_MNGT][INFO] ALLOC COPY MEM BLOCK (%p)\n",le);
304 305 306
#endif
#ifdef DEBUG_MEM_MNGT_ALLOC
      counters[MEM_MNGT_POOL_ID_COPY] += 1;
307
      LOG_D (RLC,"[MEM_MNGT][INFO] pool counters = {%2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d}\n",
308 309 310
          counters[0],counters[1],counters[2],counters[3],counters[4],
          counters[5],counters[6],counters[7],counters[8],counters[9],
          counters[10],counters[11]);
311 312 313
#endif
    return le;
  } else {
314
    LOG_E (RLC,"[MEM_MNGT][ERROR] POOL COPY IS EMPTY\n");
315 316 317 318 319
    //#ifdef DEBUG_MEM_MNGT_ALLOC
    check_mem_area ();
    //    break_point ();
    //#endif

320
    AssertFatal(1==0,"mem pool is empty");
321 322 323 324 325 326 327 328
    return NULL;
  }
}

//-----------------------------------------------------------------------------
mem_block_t      *
copy_mem_block (mem_block_t * leP, mem_block_t * destP)
{
329
  //-----------------------------------------------------------------------------
330

roux's avatar
roux committed
331 332 333 334
#ifdef MEMBLOCK_BIG_LOCK
  AssertFatal(0, "This function is not handled properly but not used anywhere. FIXME?\n");
#endif

335 336 337
  if ((destP != NULL) && (leP != NULL) && (destP->pool_id == MEM_MNGT_POOL_ID_COPY)) {
    destP->data = leP->data;
  } else {
338
    LOG_E (RLC,"[MEM_MNGT][COPY] copy_mem_block() pool dest src or dest is NULL\n");
339
  }
340

341 342 343 344 345 346 347
  return destP;
}

//-----------------------------------------------------------------------------
void
display_mem_load (void)
{
348
  //-----------------------------------------------------------------------------
roux's avatar
roux committed
349 350 351
#ifdef MEMBLOCK_BIG_LOCK
  /* this function does not need to be protected, do nothing */
#endif
352 353 354

  mem_pool       *memory = (mem_pool *) &mem_block_var;

355
  LOG_D (RLC,"POOL 0 (%d elements of %d Bytes): ", MEM_MNGT_MB0_NB_BLOCKS, MEM_MNGT_MB0_BLOCK_SIZE);
356
  list_display (&memory->mem_lists[MEM_MNGT_POOL_ID0]);
357
  LOG_D (RLC,"POOL 1 (%d elements of %d Bytes): ", MEM_MNGT_MB1_NB_BLOCKS, MEM_MNGT_MB1_BLOCK_SIZE);
358
  list_display (&memory->mem_lists[MEM_MNGT_POOL_ID1]);
359
  LOG_D (RLC,"POOL 2 (%d elements of %d Bytes): ", MEM_MNGT_MB2_NB_BLOCKS, MEM_MNGT_MB2_BLOCK_SIZE);
360
  list_display (&memory->mem_lists[MEM_MNGT_POOL_ID2]);
361
  LOG_D (RLC,"POOL 3 (%d elements of %d Bytes): ", MEM_MNGT_MB3_NB_BLOCKS, MEM_MNGT_MB3_BLOCK_SIZE);
362
  list_display (&memory->mem_lists[MEM_MNGT_POOL_ID3]);
363
  LOG_D (RLC,"POOL 4 (%d elements of %d Bytes): ", MEM_MNGT_MB4_NB_BLOCKS, MEM_MNGT_MB4_BLOCK_SIZE);
364
  list_display (&memory->mem_lists[MEM_MNGT_POOL_ID4]);
365
  LOG_D (RLC,"POOL 5 (%d elements of %d Bytes): ", MEM_MNGT_MB5_NB_BLOCKS, MEM_MNGT_MB5_BLOCK_SIZE);
366
  list_display (&memory->mem_lists[MEM_MNGT_POOL_ID5]);
367
  LOG_D (RLC,"POOL 6 (%d elements of %d Bytes): ", MEM_MNGT_MB6_NB_BLOCKS, MEM_MNGT_MB6_BLOCK_SIZE);
368
  list_display (&memory->mem_lists[MEM_MNGT_POOL_ID6]);
369
  LOG_D (RLC,"POOL 7 (%d elements of %d Bytes): ", MEM_MNGT_MB7_NB_BLOCKS, MEM_MNGT_MB7_BLOCK_SIZE);
370
  list_display (&memory->mem_lists[MEM_MNGT_POOL_ID7]);
371
  LOG_D (RLC,"POOL 8 (%d elements of %d Bytes): ", MEM_MNGT_MB8_NB_BLOCKS, MEM_MNGT_MB8_BLOCK_SIZE);
372
  list_display (&memory->mem_lists[MEM_MNGT_POOL_ID8]);
373
  LOG_D (RLC,"POOL 9 (%d elements of %d Bytes): ", MEM_MNGT_MB9_NB_BLOCKS, MEM_MNGT_MB9_BLOCK_SIZE);
374
  list_display (&memory->mem_lists[MEM_MNGT_POOL_ID9]);
375
  LOG_D (RLC,"POOL 10 (%d elements of %d Bytes): ", MEM_MNGT_MB10_NB_BLOCKS, MEM_MNGT_MB10_BLOCK_SIZE);
376
  list_display (&memory->mem_lists[MEM_MNGT_POOL_ID10]);
377
  LOG_D (RLC,"POOL 11 (%d elements of %d Bytes): ", MEM_MNGT_MB11_NB_BLOCKS, MEM_MNGT_MB11_BLOCK_SIZE);
378
  list_display (&memory->mem_lists[MEM_MNGT_POOL_ID11]);
379
  LOG_D (RLC,"POOL 12 (%d elements of %d Bytes): ", MEM_MNGT_MB12_NB_BLOCKS, MEM_MNGT_MB12_BLOCK_SIZE);
380
  list_display (&memory->mem_lists[MEM_MNGT_POOL_ID12]);
381
  LOG_D (RLC,"POOL C (%d elements): ", MEM_MNGT_MBCOPY_NB_BLOCKS);
382 383 384 385 386 387 388
  list_display (&memory->mem_lists[MEM_MNGT_POOL_ID_COPY]);
}

//-----------------------------------------------------------------------------
void
check_mem_area (void)
{
389
  //-----------------------------------------------------------------------------
390 391 392
  int             index, mb_index;
  mem_pool       *memory = (mem_pool *) &mem_block_var;

roux's avatar
roux committed
393 394 395 396
#ifdef MEMBLOCK_BIG_LOCK
  AssertFatal(0, "This function is not handled properly but not used anywhere. FIXME?\n");
#endif

397
  for (index = 0; index < MEM_MNGT_MB0_NB_BLOCKS; index++) {
398
    if ((memory->mem_blocks[index].data != (unsigned char*)&(memory->mem_pool0[index][0])) && (memory->mem_blocks[index].pool_id != MEM_MNGT_POOL_ID0)) {
399
      LOG_D (RLC,"[MEM] ERROR POOL0 block index %d\n", index);
400 401
    }
  }
402

403
  mb_index = MEM_MNGT_MB0_NB_BLOCKS;
404

405
  for (index = 0; index < MEM_MNGT_MB1_NB_BLOCKS; index++) {
406 407
    if ((memory->mem_blocks[mb_index + index].data != (unsigned char*)&(memory->mem_pool1[index][0]))
        && (memory->mem_blocks[mb_index + index].pool_id != MEM_MNGT_POOL_ID1)) {
408
      LOG_D (RLC,"[MEM] ERROR POOL1 block index %d\n", index);
409 410
    }
  }
411

412
  mb_index += MEM_MNGT_MB1_NB_BLOCKS;
413

414
  for (index = 0; index < MEM_MNGT_MB2_NB_BLOCKS; index++) {
415 416
    if ((memory->mem_blocks[mb_index + index].data != (unsigned char*)&(memory->mem_pool2[index][0]))
        && (memory->mem_blocks[mb_index + index].pool_id != MEM_MNGT_POOL_ID2)) {
417
      LOG_D (RLC,"[MEM] ERROR POOL2 block index %d\n", index);
418 419
    }
  }
420

421
  mb_index += MEM_MNGT_MB2_NB_BLOCKS;
422

423
  for (index = 0; index < MEM_MNGT_MB3_NB_BLOCKS; index++) {
424 425
    if ((memory->mem_blocks[mb_index + index].data != (unsigned char*)&(memory->mem_pool3[index][0]))
        && (memory->mem_blocks[mb_index + index].pool_id != MEM_MNGT_POOL_ID3)) {
426
      LOG_D (RLC,"[MEM] ERROR POOL3 block index %d\n", index);
427 428
    }
  }
429

430
  mb_index += MEM_MNGT_MB3_NB_BLOCKS;
431

432
  for (index = 0; index < MEM_MNGT_MB4_NB_BLOCKS; index++) {
433 434
    if ((memory->mem_blocks[mb_index + index].data != (unsigned char*)&(memory->mem_pool4[index][0]))
        && (memory->mem_blocks[mb_index + index].pool_id != MEM_MNGT_POOL_ID4)) {
435
      LOG_D (RLC,"[MEM] ERROR POOL4 block index %d\n", index);
436 437
    }
  }
438

439
  mb_index += MEM_MNGT_MB4_NB_BLOCKS;
440

441
  for (index = 0; index < MEM_MNGT_MB5_NB_BLOCKS; index++) {
442 443
    if ((memory->mem_blocks[mb_index + index].data != (unsigned char*)&(memory->mem_pool5[index][0]))
        && (memory->mem_blocks[mb_index + index].pool_id != MEM_MNGT_POOL_ID5)) {
444
      LOG_D (RLC,"[MEM] ERROR POOL5 block index %d\n", index);
445 446
    }
  }
447

448
  mb_index += MEM_MNGT_MB5_NB_BLOCKS;
449

450
  for (index = 0; index < MEM_MNGT_MB6_NB_BLOCKS; index++) {
451 452
    if ((memory->mem_blocks[mb_index + index].data != (unsigned char*)&(memory->mem_pool6[index][0]))
        && (memory->mem_blocks[mb_index + index].pool_id != MEM_MNGT_POOL_ID6)) {
453
      LOG_D (RLC,"[MEM] ERROR POOL6 block index %d\n", index);
454 455
    }
  }
456

457
  mb_index += MEM_MNGT_MB6_NB_BLOCKS;
458

459
  for (index = 0; index < MEM_MNGT_MB7_NB_BLOCKS; index++) {
460 461
    if ((memory->mem_blocks[mb_index + index].data != (unsigned char*)&(memory->mem_pool7[index][0]))
        && (memory->mem_blocks[mb_index + index].pool_id != MEM_MNGT_POOL_ID7)) {
462
      LOG_D (RLC,"[MEM] ERROR POOL7 block index %d\n", index);
463 464
    }
  }
465

466
  mb_index += MEM_MNGT_MB7_NB_BLOCKS;
467

468
  for (index = 0; index < MEM_MNGT_MB8_NB_BLOCKS; index++) {
469 470
    if ((memory->mem_blocks[mb_index + index].data != (unsigned char*)&(memory->mem_pool8[index][0]))
        && (memory->mem_blocks[mb_index + index].pool_id != MEM_MNGT_POOL_ID8)) {
471
      LOG_D (RLC,"[MEM] ERROR POOL8 block index %d\n", index);
472 473
    }
  }
474

475
  mb_index += MEM_MNGT_MB8_NB_BLOCKS;
476

477
  for (index = 0; index < MEM_MNGT_MB9_NB_BLOCKS; index++) {
478 479
    if ((memory->mem_blocks[mb_index + index].data != (unsigned char*)&(memory->mem_pool9[index][0]))
        && (memory->mem_blocks[mb_index + index].pool_id != MEM_MNGT_POOL_ID9)) {
480
      LOG_D (RLC,"[MEM] ERROR POOL9 block index %d\n", index);
481 482
    }
  }
483

484
  mb_index += MEM_MNGT_MB9_NB_BLOCKS;
485

486
  for (index = mb_index; index < MEM_MNGT_MB10_NB_BLOCKS; index++) {
487 488
    if ((memory->mem_blocks[mb_index + index].data != (unsigned char*)&(memory->mem_pool10[index][0]))
        && (memory->mem_blocks[mb_index + index].pool_id != MEM_MNGT_POOL_ID10)) {
489
      LOG_D (RLC,"[MEM] ERROR POOL10 block index %d\n", index);
490
    }
491
  }
492

493
  mb_index += MEM_MNGT_MB10_NB_BLOCKS;
494

495
  for (index = mb_index; index < MEM_MNGT_MB11_NB_BLOCKS; index++) {
496 497
    if ((memory->mem_blocks[mb_index + index].data != (unsigned char*)&(memory->mem_pool11[index][0]))
        && (memory->mem_blocks[mb_index + index].pool_id != MEM_MNGT_POOL_ID11)) {
498
      LOG_D (RLC,"[MEM] ERROR POOL11 block index %d\n", index);
499
    }
500
  }
501

502
  mb_index += MEM_MNGT_MB11_NB_BLOCKS;
503

504
  for (index = mb_index; index < MEM_MNGT_MB12_NB_BLOCKS; index++) {
505 506
    if ((memory->mem_blocks[mb_index + index].data != (unsigned char*)&(memory->mem_pool12[index][0]))
        && (memory->mem_blocks[mb_index + index].pool_id != MEM_MNGT_POOL_ID12)) {
507
      LOG_D (RLC,"[MEM] ERROR POOL12 block index %d\n", index);
508
    }
509
  }
510

511
  mb_index += MEM_MNGT_MB12_NB_BLOCKS;
512

513
  for (index = mb_index; index < MEM_MNGT_NB_ELEMENTS; index++) {
514
    if ((memory->mem_blocks[index].data != NULL) && (memory->mem_blocks[index].pool_id != MEM_MNGT_POOL_ID_COPY)) {
515
      LOG_D (RLC,"[MEM] ERROR POOL COPY block index %d\n", index);
516
    }
517 518 519 520 521 522 523
  }
}

//-----------------------------------------------------------------------------
void
check_free_mem_block (mem_block_t * leP)
{
524
  //-----------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
525
  ptrdiff_t             block_index;
526

roux's avatar
roux committed
527 528 529 530
#ifdef MEMBLOCK_BIG_LOCK
  /* this function does not SEEM TO need to be protected, do nothing (for the moment) */
#endif

531 532 533 534 535
  if ((leP >= mem_block_var.mem_blocks) && (leP <= &mem_block_var.mem_blocks[MEM_MNGT_NB_ELEMENTS-1])) {
    // the pointer is inside the memory region
    block_index = leP - mem_block_var.mem_blocks;
    // block_index is now: 0 <= block_index < MEM_MNGT_NB_ELEMENTS

536
    if (block_index < MEM_MNGT_MB0_NB_BLOCKS) {
537
      if ((leP->data != (unsigned char*)mem_block_var.mem_pool0[block_index]) && (leP->pool_id != MEM_MNGT_POOL_ID0)) {
538
        LOG_D (RLC,"[MEM][ERROR][FATAL] free mem block is corrupted\n");
539
      }
540

541 542
      return;
    }
543

544 545 546 547
    block_index -= MEM_MNGT_MB0_NB_BLOCKS;

    if (block_index < MEM_MNGT_MB1_NB_BLOCKS) {
      if ((leP->data != (unsigned char*)mem_block_var.mem_pool1[block_index]) && (leP->pool_id != MEM_MNGT_POOL_ID1)) {
548
        LOG_D (RLC,"[MEM][ERROR][FATAL] free mem block is corrupted\n");
549
      }
550

551 552
      return;
    }
553

554 555 556 557
    block_index -= MEM_MNGT_MB1_NB_BLOCKS;

    if (block_index < MEM_MNGT_MB2_NB_BLOCKS) {
      if ((leP->data != (unsigned char*)mem_block_var.mem_pool2[block_index]) && (leP->pool_id != MEM_MNGT_POOL_ID2)) {
558
        LOG_D (RLC,"[MEM][ERROR][FATAL] free mem block is corrupted\n");
559
      }
560

561 562
      return;
    }
563

564 565 566 567
    block_index -= MEM_MNGT_MB2_NB_BLOCKS;

    if (block_index < MEM_MNGT_MB3_NB_BLOCKS) {
      if ((leP->data != (unsigned char*)mem_block_var.mem_pool3[block_index]) && (leP->pool_id != MEM_MNGT_POOL_ID3)) {
568
        LOG_D (RLC,"[MEM][ERROR][FATAL] free mem block is corrupted\n");
569
      }
570

571 572
      return;
    }
573

574 575 576 577
    block_index -= MEM_MNGT_MB3_NB_BLOCKS;

    if (block_index < MEM_MNGT_MB4_NB_BLOCKS) {
      if ((leP->data != (unsigned char*)mem_block_var.mem_pool4[block_index]) && (leP->pool_id != MEM_MNGT_POOL_ID4)) {
578
        LOG_D (RLC,"[MEM][ERROR][FATAL] free mem block is corrupted\n");
579
      }
580

581 582
      return;
    }
583

584 585 586 587
    block_index -= MEM_MNGT_MB4_NB_BLOCKS;

    if (block_index < MEM_MNGT_MB5_NB_BLOCKS) {
      if ((leP->data != (unsigned char*)mem_block_var.mem_pool5[block_index]) && (leP->pool_id != MEM_MNGT_POOL_ID5)) {
588
        LOG_D (RLC,"[MEM][ERROR][FATAL] free mem block is corrupted\n");
589
      }
590

591 592
      return;
    }
593

594 595 596 597
    block_index -= MEM_MNGT_MB5_NB_BLOCKS;

    if (block_index < MEM_MNGT_MB6_NB_BLOCKS) {
      if ((leP->data != (unsigned char*)mem_block_var.mem_pool6[block_index]) && (leP->pool_id != MEM_MNGT_POOL_ID6)) {
598
        LOG_D (RLC,"[MEM][ERROR][FATAL] free mem block is corrupted\n");
599
      }
600

601 602
      return;
    }
603

604 605 606 607
    block_index -= MEM_MNGT_MB6_NB_BLOCKS;

    if (block_index < MEM_MNGT_MB7_NB_BLOCKS) {
      if ((leP->data != (unsigned char*)mem_block_var.mem_pool7[block_index]) && (leP->pool_id != MEM_MNGT_POOL_ID7)) {
608
        LOG_D (RLC,"[MEM][ERROR][FATAL] free mem block is corrupted\n");
609
      }
610

611 612
      return;
    }
613

614 615 616 617
    block_index -= MEM_MNGT_MB7_NB_BLOCKS;

    if (block_index < MEM_MNGT_MB8_NB_BLOCKS) {
      if ((leP->data != (unsigned char*)mem_block_var.mem_pool8[block_index]) && (leP->pool_id != MEM_MNGT_POOL_ID8)) {
618
        LOG_D (RLC,"[MEM][ERROR][FATAL] free mem block is corrupted\n");
619
      }
620

621 622
      return;
    }
623

624 625 626 627
    block_index -= MEM_MNGT_MB8_NB_BLOCKS;

    if (block_index < MEM_MNGT_MB9_NB_BLOCKS) {
      if ((leP->data != (unsigned char*)mem_block_var.mem_pool9[block_index]) && (leP->pool_id != MEM_MNGT_POOL_ID9)) {
628
        LOG_D (RLC,"[MEM][ERROR][FATAL] free mem block is corrupted\n");
629
      }
630

631 632
      return;
    }
633

634 635 636 637
    block_index -= MEM_MNGT_MB9_NB_BLOCKS;

    if (block_index < MEM_MNGT_MB10_NB_BLOCKS) {
      if ((leP->data != (unsigned char*)mem_block_var.mem_pool10[block_index]) && (leP->pool_id != MEM_MNGT_POOL_ID10)) {
638
        LOG_D (RLC,"[MEM][ERROR][FATAL] free mem block is corrupted\n");
Cedric Roux's avatar
Cedric Roux committed
639
      }
640

641 642
      return;
    }
643

644 645 646 647
    block_index -= MEM_MNGT_MB10_NB_BLOCKS;

    if (block_index < MEM_MNGT_MB11_NB_BLOCKS) {
      if ((leP->data != (unsigned char*)mem_block_var.mem_pool11[block_index]) && (leP->pool_id != MEM_MNGT_POOL_ID11)) {
648
        LOG_D (RLC,"[MEM][ERROR][FATAL] free mem block is corrupted\n");
Cedric Roux's avatar
Cedric Roux committed
649
      }
650

651 652
      return;
    }
653

654 655 656 657
    block_index -= MEM_MNGT_MB11_NB_BLOCKS;

    if (block_index < MEM_MNGT_MB12_NB_BLOCKS) {
      if ((leP->data != (unsigned char*)mem_block_var.mem_pool12[block_index]) && (leP->pool_id != MEM_MNGT_POOL_ID12)) {
658
        LOG_D (RLC,"[MEM][ERROR][FATAL] free mem block is corrupted\n");
Cedric Roux's avatar
Cedric Roux committed
659
      }
660

661
      return;
Cedric Roux's avatar
Cedric Roux committed
662
    }
663
  } else {
664
    LOG_D (RLC,"[MEM][ERROR][FATAL] free mem block is corrupted\n");
665
  }
666 667

  // the block is ok
668
}