File: ecs_mem.c

package info (click to toggle)
code-saturne 5.3.2%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 76,868 kB
  • sloc: ansic: 338,582; f90: 118,487; python: 65,227; makefile: 4,429; cpp: 3,826; xml: 3,078; sh: 1,205; lex: 170; yacc: 100
file content (853 lines) | stat: -rw-r--r-- 23,010 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
/*============================================================================
 * Base memory allocation wrappers with optional tracing
 *============================================================================*/

/*
  This file is part of Code_Saturne, a general-purpose CFD tool.

  Copyright (C) 1998-2018 EDF S.A.

  This program is free software; you can redistribute it and/or modify it under
  the terms of the GNU General Public License as published by the Free Software
  Foundation; either version 2 of the License, or (at your option) any later
  version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
  Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/*----------------------------------------------------------------------------*/

#include "ecs_def.h"

/*
 * Standard C library headers
 */

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*
 * Optional library and ECS headers
 */

#include "ecs_def.h"
#include "ecs_mem.h"
#include "ecs_mem_usage.h"

/*----------------------------------------------------------------------------*/

BEGIN_C_DECLS

/*-----------------------------------------------------------------------------
 * Local type definitions
 *----------------------------------------------------------------------------*/

#ifndef DOXYGEN_SHOULD_SKIP_THIS

/*
 * Structure defining an allocated memory block (for memory tracing)
 */

struct _ecs_mem_block_t {

  void    *p_bloc;  /* Allocated memory block start adress */
  size_t   size;    /* Allocated memory block length */

};

#endif /* DOXYGEN_SHOULD_SKIP_THIS */

/*-----------------------------------------------------------------------------
 * Local macro documentation
 *----------------------------------------------------------------------------*/

/*! \fn ECS_MALLOC(_ptr, _ni, _type)
 * \brief Allocate memory for _ni elements of type _type.
 *
 * This macro calls ecs_mem_malloc(), automatically setting the
 * allocated variable name and source file name and line arguments.
 *
 * \param [out] _ptr  pointer to allocated memory.
 * \param [in]  _ni   number of elements.
 * \param [in]  _type element type.
 */

/*! \fn ECS_REALLOC(_ptr, _ni, _type)
 * \brief Reallocate memory for _ni elements of type _type.
 *
 * This macro calls ecs_mem_realloc(), automatically setting the
 * allocated variable name and source file name and line arguments.
 *
 * \param [in, out] _ptr  pointer to allocated memory.
 * \param [in]      _ni   number of elements.
 * \param [in]      _type element type.
 */

/*! \fn ECS_FREE(_ptr)
 * \brief Free allocated memory.
 *
 * This macro calls ecs_mem_free(), automatically setting the
 * allocated variable name and source file name and line arguments.
 *
 * The freed pointer is set to NULL to avoid accidental reuse.
 *
 * \param [in, out] _ptr  pointer to allocated memory.
 */

/*-----------------------------------------------------------------------------
 * Local macro definitions
 *----------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------
 * Local function prototypes
 *----------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------
 * Local static variable definitions
 *----------------------------------------------------------------------------*/

static int  _ecs_mem_global_initialized = 0;

static FILE *_ecs_mem_global_file = NULL;

static struct _ecs_mem_block_t  *_ecs_mem_global_block_array = NULL;

static unsigned long  _ecs_mem_global_block_nbr = 0 ;
static unsigned long  _ecs_mem_global_block_max = 512 ;

static size_t  _ecs_mem_global_alloc_cur = 0;
static size_t  _ecs_mem_global_alloc_max = 0;

static size_t  _ecs_mem_global_n_allocs = 0;
static size_t  _ecs_mem_global_n_reallocs = 0;
static size_t  _ecs_mem_global_n_frees = 0;

/*-----------------------------------------------------------------------------
 * Local function definitions
 *----------------------------------------------------------------------------*/

/*
 * Given a character string representing a file name, returns
 * pointer to that part of the string corresponding to the base name.
 *
 * parameters:
 *   file_name:      <-- full name of source file.
 *
 * return:
 *   pointer to part of file name corresponding to base name.
 */

static const char *
_ecs_mem_basename(const char  *file_name)
{
  int i;

  if (file_name == NULL)
    return NULL;

  for (i = strlen(file_name) - 1;
       i > 0 && file_name[i] != ECS_PATH_SEP;
       i--);

  if (file_name[i] == ECS_PATH_SEP)
    i++;

  return (file_name + i);
}

/*
 * Determines values associated with an array representing a
 * long integer
 *
 * parameters:
 *   counter: <-- counter to update.
 *   value:   --> counter values in output unit [out].
 *   unit:    --> counter value unit : ' ', 'k', 'm', 'g', 't', or 'p'
 *                for bytes, Kilobytes, Megabytes, Gigabytes, Terabytes,
 *                or Petabytes [out].
 */

static void
_ecs_mem_size_val(const size_t    counter,
                  unsigned long   value[2],
                  char           *unit)
{
  int i;
  size_t _counter[2];
  const char units[] = {' ', 'k', 'm', 'g', 't', 'p', 'e'};

  for (i = 0, _counter[0] = counter, _counter[1] = 0;
       _counter[0] >= 1024 && i < 6;
       i++) {
    _counter[1] = _counter[0] % 1024;
    _counter[0] /= 1024;
  }

  value[0] = _counter[0];
  value[1] = _counter[1];
  *unit = units[i];
}

/*
 * Memory usage summary.
 */

static void _ecs_mem_summary(FILE  *f)
{
  char unit;
  unsigned long value[2];
  size_t mem_usage;

  if (f == NULL)
    return;

  fprintf(f, "\n\n");
  fprintf(f,
          "Memory allocation summary\n"
          "-------------------------\n\n");

  /* Available memory usage information */

  _ecs_mem_size_val(_ecs_mem_global_alloc_cur, value, &unit);
  fprintf(f,
          "Theoretical current allocated memory:   %8lu.%lu %cB\n",
          value[0], value[1], unit);

  _ecs_mem_size_val(_ecs_mem_global_alloc_max, value, &unit);
  fprintf(f,
          "Theoretical maximum allocated memory:   %8lu.%lu %cB\n",
          value[0], value[1], unit);

  fprintf(f,
          "\n"
          "Number of allocations:   %lu\n"
          "          reallocations: %lu\n"
          "          frees:         %lu\n\n",
          (unsigned long)_ecs_mem_global_n_allocs,
          (unsigned long)_ecs_mem_global_n_reallocs,
          (unsigned long)_ecs_mem_global_n_frees);

  if (ecs_mem_usage_initialized() == 1) {

    /* Maximum measured memory */

    mem_usage = ecs_mem_usage_max_pr_size();
    if (mem_usage > 0) {
      fprintf(f,
              "Maximum program memory measure:  %8lu kB\n",
              (unsigned long)mem_usage);
    }

    /* Current measured memory */

    mem_usage = ecs_mem_usage_pr_size();
    if (mem_usage > 0)
      fprintf(f,
              "Current program memory measure:   %8lu kB\n",
              (unsigned long)mem_usage);
  }

}

/*
 * Return the _ecs_mem_block structure corresponding to a given
 * allocated block.
 *
 * parameters:
 *   p_in: <-- allocated block's start adress.
 *
 * returns:
 *   corresponding _ecs_mem_block structure.
 */

static struct _ecs_mem_block_t *
_ecs_mem_block_info(const void *p_get)
{
  struct _ecs_mem_block_t  *pinfo = NULL;
  unsigned long idx;

  if (_ecs_mem_global_block_array != NULL) {

    for (idx = _ecs_mem_global_block_nbr - 1;
         idx > 0 && (_ecs_mem_global_block_array + idx)->p_bloc != p_get;
         idx--);

    if ((_ecs_mem_global_block_array + idx)->p_bloc != p_get) {
      _ecs_mem_summary(stderr);
      ecs_error(__FILE__, __LINE__, 0,
                _("Adress [%10p] does not correspond to "
                  "the beginning of an allocated block."),
                p_get);
    }
    else {
      pinfo = _ecs_mem_global_block_array + idx;
      assert(p_get == pinfo->p_bloc);
    }

  }

  return pinfo;
}

/*
 * Return the size of a given allocated block.
 *
 * parameters:
 *   p_in: <-- allocated block's start adress.
 *
 * returns:
 *   block size.
 */

static size_t
_ecs_mem_block_size(const void  *p_in)
{
  struct _ecs_mem_block_t *pinfo = _ecs_mem_block_info(p_in);

  if (pinfo != NULL)
    return pinfo->size;
  else
    return 0;
}

/*
 * Fill a _ecs_mem_block_t structure for an allocated pointer.
 */

static void
_ecs_mem_block_malloc(void          *p_new,
                      const size_t   size_new)
{
  struct _ecs_mem_block_t *pinfo;

  assert(size_new != 0);

  if (_ecs_mem_global_block_array == NULL)
    return;

  if (_ecs_mem_global_block_nbr >= _ecs_mem_global_block_max) {

    _ecs_mem_global_block_max *= 2;
    _ecs_mem_global_block_array
      = (struct _ecs_mem_block_t *) realloc(_ecs_mem_global_block_array,
                                            sizeof(struct _ecs_mem_block_t)
                                            * _ecs_mem_global_block_max);

    if (_ecs_mem_global_block_array == NULL) {
      _ecs_mem_summary(stderr);
      ecs_error(__FILE__, __LINE__, errno,
                _("Memory allocation failure"));
      return;
    }

  }

  _ecs_mem_global_block_nbr += 1;

  pinfo = _ecs_mem_global_block_array + _ecs_mem_global_block_nbr - 1;

  /* Start adress and size of allocated block */

  pinfo->p_bloc = p_new;
  pinfo->size   = size_new;
}

/*
 * Update a _ecs_mem_block_t structure for an reallocated pointer.
 */

static void
_ecs_mem_block_realloc(const void    *p_old,
                       void          *p_new,
                       size_t         size_new)
{
  struct _ecs_mem_block_t *pinfo;

  assert(size_new != 0);

  pinfo = _ecs_mem_block_info(p_old);

  if (pinfo != NULL) {
    pinfo->p_bloc = p_new;
    pinfo->size   = size_new;
  }
}

/*
 * Free a _ecs_mem_block_t structure for a freed pointer.
 */

static void
_ecs_mem_block_free(const void *p_free)
{
  struct _ecs_mem_block_t *pinfo, *pmove;
  unsigned long idx;

  if (_ecs_mem_global_block_array == NULL)
    return;

  for (idx = _ecs_mem_global_block_nbr - 1;
       idx > 0 && (_ecs_mem_global_block_array + idx)->p_bloc != p_free;
       idx--);

  if ((_ecs_mem_global_block_array + idx)->p_bloc != p_free) {
    _ecs_mem_summary(stderr);
    ecs_error(__FILE__, __LINE__, 0,
              _("Adress [%10p] does not correspond to "
                "the beginning of an allocated block."),
              p_free);
  }

  else {

    /* We move the contents of the array's final block to the position
       of the freed block, and shorten the array's useful part by one. */

    pinfo = _ecs_mem_global_block_array + idx;
    pmove = _ecs_mem_global_block_array + _ecs_mem_global_block_nbr - 1;
    pinfo->p_bloc = pmove->p_bloc;
    pinfo->size   = pmove->size;

    _ecs_mem_global_block_nbr -= 1;

  }
}

/*============================================================================
 * Public function definitions
 *============================================================================*/

/*!
 * \brief Initialize memory handling.
 *
 * This function should be called before any other ecs_mem_...()
 * function. To activate memory allocation logging, a logfile
 * name should be given as an argument. The resulting file will
 * be a regular, local file. If this file cannot be opened for
 * some reason, logging is silently de-activated.
 *
 * \param log_file_name name of optional log_file (if NULL, no log).
 */

void
ecs_mem_init(const char *log_file_name)
{
  size_t alloc_size;

  if (_ecs_mem_global_initialized == 1) {
    _ecs_mem_summary(stderr);
    ecs_error(__FILE__, __LINE__, 0,
              _("ecs_mem_init() has already been called"));
  }
  _ecs_mem_global_initialized = 1;

  alloc_size = sizeof(struct _ecs_mem_block_t) * _ecs_mem_global_block_max;

  _ecs_mem_global_block_array
    = malloc(sizeof(struct _ecs_mem_block_t) * _ecs_mem_global_block_max);

  if (_ecs_mem_global_block_array == NULL) {
    _ecs_mem_summary(stderr);
    ecs_error(__FILE__, __LINE__, errno,
              _("Failure to allocate \"%s\" (%lu bytes)"),
              "_ecs_mem_global_block_array", (unsigned long)alloc_size);
    return;
  }

  if (log_file_name != NULL) {

    _ecs_mem_global_file = fopen(log_file_name, "w");

    /*
      If the file could not be opened, we do not abort, as it is not
      absolutely necessary. We silently continue.
      (We could warn the user, but this would require either using
      ecs_printf(), which we prefer to keep independent of the ecs_mem_...()
      functions to avoid evental crossed definitions when user-defined, or
      "warning handling" similar to error handling, with a possibility
      of user-defined warning handlers, as we are not sure if the calling
      code uses stderr (especially in a distributed environment). This
      is probably not worth the bother.
    */

    if (_ecs_mem_global_file == NULL)
      fprintf(stderr,
              _("Failure to open memory log file \"%s\"\n"),
              log_file_name);

  }

  /* Log file header */

  if (_ecs_mem_global_file != NULL) {

    fprintf(_ecs_mem_global_file,
            "       :     FILE NAME              : LINE  :"
            "  POINTER NAME                          : N BYTES   :"
            " (+- N BYTES) : TOTAL BYTES  : [    ADRESS]\n"
            "-------:----------------------------:-------:"
            "----------------------------------------:-----------:"
            "-----------------------------:--------------");

  }

}

/*!
 * \brief End memory handling.
 *
 * This function should be called after all other ecs_mem_...()
 * functions. In case of memory allocation logging, it
 * writes final information to the log file and closes is.
 */

void ecs_mem_end(void)
{
  if (_ecs_mem_global_initialized == 0) {
    _ecs_mem_summary(stderr);
    ecs_error(__FILE__, __LINE__, 0,
              _("ecs_mem_end() called before ecs_mem_init()"));
  }
  _ecs_mem_global_initialized = 0;

  if (_ecs_mem_global_file != NULL) {

    unsigned long  non_free = 0;
    struct _ecs_mem_block_t  *pinfo;

    /* Memory usage summary */

    _ecs_mem_summary(_ecs_mem_global_file);

    /* List of non-freed pointers */

    if (_ecs_mem_global_block_array != NULL) {

      fprintf(_ecs_mem_global_file, "List of non freed pointers:\n");

      for (pinfo = _ecs_mem_global_block_array;
           pinfo < _ecs_mem_global_block_array + _ecs_mem_global_block_nbr;
           pinfo++) {

        fprintf(_ecs_mem_global_file,"[%10p]\n", pinfo->p_bloc);
        non_free++;

      }

      fprintf(_ecs_mem_global_file,
              "Number of non freed pointers remaining: %lu\n",
              non_free);

    }

    fclose(_ecs_mem_global_file);
  }

  /* Reset defaults in case of later initialization */

  if (_ecs_mem_global_block_array != NULL) {
    free(_ecs_mem_global_block_array);
    _ecs_mem_global_block_array = NULL;
  }

  _ecs_mem_global_block_nbr   = 0 ;
  _ecs_mem_global_block_max   = 512 ;

  _ecs_mem_global_alloc_cur = 0;
  _ecs_mem_global_alloc_max = 0;

  _ecs_mem_global_n_allocs = 0;
  _ecs_mem_global_n_reallocs = 0;
  _ecs_mem_global_n_frees = 0;

}

/*!
 * \brief Indicates if ecs_mem_...() functions are initialized.
 *
 * \returns 1 if ecs_mem_init has been called, 0 otherwise.
 */

int
ecs_mem_initialized(void)
{
  return _ecs_mem_global_initialized;
}

/*!
 * \brief Allocate memory for ni elements of size bytes.
 *
 * This function calls malloc(), but adds tracing capabilities, and
 * automatically calls the ecs_error() errorhandler if it fails to
 * allocate the required memory.
 *
 * \param [in] ni        number of elements.
 * \param [in] size      element size.
 * \param [in] var_name  allocated variable name string.
 * \param [in] file_name name of calling source file.
 * \param [in] line_num  line number in calling source file.
 *
 * \returns pointer to allocated memory.
 */

void *
ecs_mem_malloc(size_t       ni,
               size_t       size,
               const char  *var_name,
               const char  *file_name,
               int          line_num)
{
  void       *p_loc;
  size_t      alloc_size = ni * size;

  if (ni == 0)
    return NULL;

  /* Allocate memory and check return */

  p_loc = malloc(alloc_size);

  if (p_loc == NULL) {
    _ecs_mem_summary(stderr);
    ecs_error(file_name, line_num, errno,
              _("Failure to allocate \"%s\" (%lu bytes)"),
              var_name, (unsigned long)alloc_size);
    return NULL;
  }
  else if (_ecs_mem_global_initialized == 0)
    return p_loc;

  /* Memory allocation counting */

  _ecs_mem_global_alloc_cur += alloc_size;

  if (_ecs_mem_global_alloc_max < _ecs_mem_global_alloc_cur)
    _ecs_mem_global_alloc_max = _ecs_mem_global_alloc_cur;

  if (_ecs_mem_global_file != NULL) {
    fprintf(_ecs_mem_global_file, "\n  alloc: %-27s:%6d : %-39s: %9lu",
            _ecs_mem_basename(file_name), line_num,
            var_name, (unsigned long)alloc_size);
    fprintf(_ecs_mem_global_file, " : (+%9lu) : %12lu : [%10p]",
            (unsigned long)alloc_size,
            (unsigned long)_ecs_mem_global_alloc_cur,
            p_loc);
    fflush(_ecs_mem_global_file);
  }

  _ecs_mem_block_malloc(p_loc, alloc_size);

  _ecs_mem_global_n_allocs += 1;

  /* Return pointer to allocated memory */

  return p_loc;
}

/*!
 * \brief Reallocate memory for ni elements of size bytes.
 *
 * This function calls realloc(), but adds tracing capabilities, and
 * automatically calls the ecs_error() errorhandler if it fails to
 * allocate the required memory.
 *
 * \param [in] ptr       pointer to previous memory location
 *                       (if NULL, ecs_alloc() called).
 * \param [in] ni        number of elements.
 * \param [in] size      element size.
 * \param [in] var_name  allocated variable name string.
 * \param [in] file_name name of calling source file.
 * \param [in] line_num  line number in calling source file.
 *
 * \returns pointer to reallocated memory.
 */

void *
ecs_mem_realloc(void        *ptr,
                size_t       ni,
                size_t       size,
                const char  *var_name,
                const char  *file_name,
                int          line_num)
{
  void      *p_loc;

  long size_diff;
  size_t old_size;
  size_t new_size = ni * size;

  /*
    Behave as ecs_malloc() if the previous pointer is equal to NULL.
    Note that the operation will then appear as a first allocation
    ('alloc') in the _ecs_mem_global_file trace file.
  */

  if (ptr == NULL)
    return ecs_mem_malloc(ni,
                          size,
                          var_name,
                          file_name,
                          line_num);

  /* If the old size equals the new size, nothing needs to be done. */

  old_size = _ecs_mem_block_size(ptr);

  if (new_size == old_size)
    return ptr;

  /*
    We may also simply free memory. Note that in this case, the operation
    appears as 'free' in the _ecs_mem_global_file trace file.
  */

  else if (ni == 0)
    return ecs_mem_free(ptr,
                        var_name,
                        file_name,
                        line_num);

  /* In the final case, we have a true reallocation */

  else {

    size_diff = new_size - old_size;

    p_loc = realloc(ptr, new_size);

    if (p_loc == NULL) {
      _ecs_mem_summary(stderr);
      ecs_error(file_name, line_num, errno,
                _("Failure to reallocate \"%s\" (%lu bytes)"),
                var_name, (unsigned long)new_size);
      return NULL;
    }
    else if (_ecs_mem_global_initialized == 0)
      return p_loc;

    _ecs_mem_global_alloc_cur += size_diff;

    if (size_diff > 0) {
      if (_ecs_mem_global_alloc_max < _ecs_mem_global_alloc_cur)
        _ecs_mem_global_alloc_max = _ecs_mem_global_alloc_cur;
    }

    if (_ecs_mem_global_file != NULL) {
      char sgn = (size_diff > 0) ? '+' : '-';
      fprintf(_ecs_mem_global_file, "\nrealloc: %-27s:%6d : %-39s: %9lu",
              _ecs_mem_basename(file_name), line_num,
              var_name, (unsigned long)new_size);
      fprintf(_ecs_mem_global_file, " : (%c%9lu) : %12lu : [%10p]",
              sgn,
              (unsigned long) ((size_diff > 0) ? size_diff : -size_diff),
              (unsigned long)_ecs_mem_global_alloc_cur,
              p_loc);
      fflush(_ecs_mem_global_file);
    }

    _ecs_mem_block_realloc(ptr, p_loc, new_size);

    _ecs_mem_global_n_reallocs += 1;

    return p_loc;
  }

}

/*!
 * \brief Free allocated memory.
 *
 * This function calls free(), but adds tracing capabilities, and
 * automatically calls the ecs_error() errorhandler if it fails to
 * free the corresponding memory. In case of a NULL pointer argument,
 * the function simply returns.
 *
 * \param [in] ptr       pointer to previous memory location
 *                       (if NULL, ecs_alloc() called).
 * \param [in] var_name  allocated variable name string
 * \param [in] file_name name of calling source file
 * \param [in] line_num  line number in calling source file
 *
 * \returns NULL pointer.
 */

void *
ecs_mem_free(void        *ptr,
             const char  *var_name,
             const char  *file_name,
             int          line_num)
{
  size_t  size_info;

  /* NULL pointer case (non-allocated location) */

  if (ptr == NULL)
    return NULL;

  /* General case (free allocated memory) */

  if (_ecs_mem_global_initialized != 0) {

    size_info = _ecs_mem_block_size(ptr);

    _ecs_mem_global_alloc_cur -= size_info;

    if (_ecs_mem_global_file != NULL) {
      fprintf(_ecs_mem_global_file,"\n   free: %-27s:%6d : %-39s: %9lu",
              _ecs_mem_basename(file_name), line_num,
              var_name, (unsigned long)size_info);
      fprintf(_ecs_mem_global_file, " : (-%9lu) : %12lu : [%10p]",
              (unsigned long)size_info,
              (unsigned long)_ecs_mem_global_alloc_cur,
              ptr);
      fflush(_ecs_mem_global_file);
    }

    _ecs_mem_block_free(ptr);

    _ecs_mem_global_n_frees += 1;
  }

  free(ptr);

  return NULL;
}

/*!
 * \brief Return current theoretical dynamic memory allocated.
 *
 * \return current memory handled through ecs_mem_...() (in kB).
 */

size_t
ecs_mem_size_current(void)
{
  return (_ecs_mem_global_alloc_cur / 1024);
}

/*!
 * \brief Return maximum theoretical dynamic memory allocated.
 *
 * \return maximum memory handled through ecs_mem_...() (in kB).
 */

size_t
ecs_mem_size_max(void)
{
  return (_ecs_mem_global_alloc_max / 1024);
}

/*----------------------------------------------------------------------------*/

END_C_DECLS