File: cpl_xmemory.c

package info (click to toggle)
cpl 6.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 18,764 kB
  • sloc: ansic: 111,368; sh: 14,549; makefile: 626
file content (992 lines) | stat: -rw-r--r-- 30,702 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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
/* $Id: cpl_xmemory.c,v 1.41 2011/08/18 07:58:32 llundin Exp $
 *
 * This file is part of the ESO Common Pipeline Library
 * Copyright (C) 2001-2008 European Southern Observatory
 *
 * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * $Author: llundin $
 * $Date: 2011/08/18 07:58:32 $
 * $Revision: 1.41 $
 * $Name: cpl-6_1_1 $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/*-----------------------------------------------------------------------------
                                   Includes
 -----------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <assert.h>

#include "cpl_xmemory.h"

/* The below doxygen has been inactivated by removing the '**' comment. */

/*----------------------------------------------------------------------------*/
/*
 * @defgroup    cpl_xmemory     POSIX-compatible extended memory handling
 *
 * cpl_xmemory is a small and efficient module offering memory extension
 * capabitilies to ANSI C programs running on POSIX-compliant systems. It
 * offers several useful features such as memory leak detection, protection for
 * free on NULL or unallocated pointers.
 * This module has been tested on a number of
 * current Unix * flavours and is reported to work fine.
 * The current limitation is the limited number of pointers it can handle at
 * the same time - and its absence of thread-safety.
 * 
 */
/*----------------------------------------------------------------------------*/
/**@{*/

/*-----------------------------------------------------------------------------
                                Defines
 -----------------------------------------------------------------------------*/

#ifndef inline
#define inline /* inline */
#endif

#define CPL_XSTRINGIFY(TOSTRING) #TOSTRING
#define CPL_STRINGIFY(TOSTRING) CPL_XSTRINGIFY(TOSTRING)

/*
  This symbol defines the level of usage of the memory module.

  0   Use the memory system calls.
  1   Use the memory system calls, but abort() if they are not succesfull
  2   Fully use the memory functions
*/

/* Initial number of entries in memory table */
/* If this number is big, the size of the memory table can become
   problematic.
 */
/* Use a prime number to reduce risk of hashing clashes */
#ifndef CPL_XMEMORY_MAXPTRS
#error "CPL_XMEMORY_MAXPTRS is not defined"
#endif

#if CPL_XMEMORY_MAXPTRS <= 0
#error "CPL_XMEMORY_MAXPTRS must be positive"
#endif

#ifndef CPL_XMEMORY_RESIZE_THRESHOLD
#define CPL_XMEMORY_RESIZE_THRESHOLD 0.9
#endif

#ifndef CPL_XMEMORY_RESIZE_FACTOR
#define CPL_XMEMORY_RESIZE_FACTOR 2
#endif

/* A very simple hash */
#define PTR_HASH(ptr) (((size_t)ptr) % cpl_xmemory_table_size)

#define CPL_XMEMORY_TYPE_FREE    0
#define CPL_XMEMORY_TYPE_MALLOC  1
#define CPL_XMEMORY_TYPE_CALLOC  2
#define CPL_XMEMORY_TYPE_REALLOC 3

/*-----------------------------------------------------------------------------
                        Private variables
 -----------------------------------------------------------------------------*/

static const char * cpl_xmemory_type[]
    = {"free", "malloc", "calloc", "realloc"};

/* Number of active cells */
static size_t       cpl_xmemory_ncells = 0;

/* Peak number of pointers ever seen for diagnostics */
static size_t       cpl_xmemory_max_cells = 0;

    /* Total allocated RAM in bytes */
static size_t       cpl_xmemory_alloc_ram = 0;

/* Peak allocation ever seen for diagnostics */
static size_t       cpl_xmemory_alloc_max = 0;

/* The current size of the xmemory table */
static size_t       cpl_xmemory_table_size = 0;

/* The default size of the xmemory table, when it is non-empty */
static size_t       cpl_xmemory_table_size_max = 0;

static int cpl_xmemory_fatal = 0;

/* Various infos about the pointers */
/* List of pointers */
static const void   ** cpl_xmemory_p_val;

/* Type of allocation */
/* - CPL_XMEMORY_TYPE_FREE means no allocation */
static unsigned char * cpl_xmemory_p_type;

/* Size of allocated memory [bytes] */
static size_t        * cpl_xmemory_p_size;

/*-----------------------------------------------------------------------------
                    Private function prototypes
 -----------------------------------------------------------------------------*/

static void cpl_xmemory_init(void);
static void cpl_xmemory_init_alloc(void);
static void cpl_xmemory_end(void);
static void cpl_xmemory_resize(void);
static void cpl_xmemory_addcell(size_t, const void *, size_t, unsigned char);
static void cpl_xmemory_remcell(size_t);
static size_t cpl_xmemory_findcell(const void *) CPL_ATTR_PURE;
static size_t cpl_xmemory_findfree(const void *);

/*-----------------------------------------------------------------------------
                            Function codes
 -----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    malloc() with failure check
  @param    size        Size (in bytes) to allocate.
  @return   1 newly allocated pointer (possibly NULL on zero bytes)
  @note Will abort() on failure

 */
/*----------------------------------------------------------------------------*/
inline
void * cpl_xmemory_malloc_count(size_t size)
{

    void * ptr = malloc(size);


    if (ptr != NULL) {

#       ifdef _OPENMP
#       pragma omp atomic
#       endif
        cpl_xmemory_ncells++;
        /* Remember peak allocation */
        /* FIXME: This non-thread-safe check is incorrect, but might still
           produce a useful estimate */
        if (cpl_xmemory_ncells > cpl_xmemory_max_cells)
            cpl_xmemory_max_cells = cpl_xmemory_ncells;

    } else if (size != 0) {

        fprintf(stderr, "cpl_xmemory fatal error: malloc(%lu) returned NULL:\n",
                (long unsigned)size);
        perror(NULL);
        cpl_xmemory_fatal = 1;
        cpl_xmemory_status(cpl_xmemory_table_size ? 2 : 1);
        assert(ptr != NULL); /* More informative, if enabled */
        abort();

    }

    return ptr;

}

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    calloc() with failure check
  @param    nmemb       Number of elements to allocate.
  @param    size        Size (in bytes) to allocate.
  @return   1 newly allocated pointer (possibly NULL on zero bytes)
  @note Will abort() on failure

 */
/*----------------------------------------------------------------------------*/
inline
void * cpl_xmemory_calloc_count(size_t nmemb, size_t size)
{

    void * ptr = calloc(nmemb, size);


    if (ptr != NULL) {

#       ifdef _OPENMP
#       pragma omp atomic
#       endif
        cpl_xmemory_ncells++;
        /* Remember peak allocation */
        /* FIXME: This non-thread-safe check is incorrect, but might still
           produce a useful estimate */
        if (cpl_xmemory_ncells > cpl_xmemory_max_cells)
            cpl_xmemory_max_cells = cpl_xmemory_ncells;

    } else if (size != 0 && nmemb != 0) {

        fprintf(stderr, "cpl_xmemory fatal error: calloc(%lu, %lu) returned "
                "NULL:\n", (long unsigned)nmemb, (long unsigned)size);
        perror(NULL);
        cpl_xmemory_fatal = 1;
        cpl_xmemory_status(cpl_xmemory_table_size ? 2 : 1);
        assert(ptr != NULL); /* More informative, if enabled */
        abort();

    }

    return ptr;

}


/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    realloc() with failure check
  @param    oldptr      Pointer to reallocate.
  @param    size        Size (in bytes) to reallocate.
  @return   1 newly allocated pointer (possibly NULL on zero bytes)
  @note Will abort() on failure

 */
/*----------------------------------------------------------------------------*/
inline
void * cpl_xmemory_realloc_count(void * oldptr, size_t size)
{

    void * ptr;

    if (oldptr == NULL) {
        /* This is just another way to do a malloc() */

        ptr = cpl_xmemory_malloc_count(size);

    } else if (cpl_xmemory_ncells == 0) {
        fprintf(stderr, "cpl_xmemory error: Ignoring realloc() to %lu bytes on "
                "unallocated pointer (%p)\n", (long unsigned)size, oldptr);
        ptr = NULL;
    } else {

        /* assert( oldptr != NULL ); */

        ptr = realloc(oldptr, size);

        if (ptr == NULL) {

            if (size != 0) {
                fprintf(stderr, "cpl_xmemory fatal error: realloc(%p, %lu) "
                        "returned NULL:\n", oldptr, (long unsigned)size);
                perror(NULL);
                cpl_xmemory_fatal = 1;
                cpl_xmemory_status(cpl_xmemory_table_size ? 2 : 1);
                assert(ptr != NULL); /* More informative, if enabled */
                abort();
            }

            /* This is just another way to do a free() */
#           ifdef _OPENMP
#           pragma omp atomic
#           endif
            cpl_xmemory_ncells--;
        }
    }

    return ptr;
}

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Free memory.
  @param    ptr Pointer to free.
  @return   void
  @see free()

 */
/*----------------------------------------------------------------------------*/
void cpl_xmemory_free_count(void * ptr)
{

    if (ptr != NULL) {
        if (cpl_xmemory_ncells == 0) {
            fprintf(stderr, "cpl_xmemory error: Ignoring free() on "
                    "unallocated pointer (%p)\n", ptr);
        } else {
            free(ptr);

#           ifdef _OPENMP
#           pragma omp atomic
#           endif
            cpl_xmemory_ncells--;

        }
    }

    return;
}

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Allocate memory.
  @param    size        Size (in bytes) to allocate.
  @return   1 newly allocated pointer.

 */
/*----------------------------------------------------------------------------*/
void * cpl_xmemory_malloc(size_t size)
{
    void * ptr;
    size_t pos;

    if (size == 0) {
        /* In this case it is allowed to return NULL
            - do that and save space in the table */
        return NULL;
    }

    ptr = cpl_xmemory_malloc_count(size);

    if (cpl_xmemory_ncells
        >= cpl_xmemory_table_size * CPL_XMEMORY_RESIZE_THRESHOLD) {

        /* Initialize table if needed */
        if (cpl_xmemory_table_size == 0) {
            cpl_xmemory_init();
        }

        /* Memory table is too full, resize */
        cpl_xmemory_resize();
    }

    /* Add cell into general table */
    pos = cpl_xmemory_findfree(ptr);
    cpl_xmemory_addcell(pos, ptr, size, CPL_XMEMORY_TYPE_MALLOC);

    return ptr;
}

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Allocate memory.
  @param    nmemb       Number of elements to allocate.
  @param    size        Size (in bytes) of each element.
  @return   1 newly allocated pointer.

 */
/*----------------------------------------------------------------------------*/
void * cpl_xmemory_calloc(size_t nmemb, size_t size)
{
    void * ptr;
    size_t pos;


    if (size == 0 || nmemb == 0) {
        /* In this case it is allowed to return NULL
            - do that and save space in the table */
        return NULL;
    }

    ptr = cpl_xmemory_calloc_count(nmemb, size);

    if (cpl_xmemory_ncells
        >= cpl_xmemory_table_size * CPL_XMEMORY_RESIZE_THRESHOLD) {

        /* Initialize table if needed */
        if (cpl_xmemory_table_size == 0) {
            cpl_xmemory_init();
        }

        /* Memory table is too full, resize */
        cpl_xmemory_resize();
    }

    /* Add cell into general table */
    pos = cpl_xmemory_findfree(ptr);
    cpl_xmemory_addcell(pos, ptr, nmemb * size, CPL_XMEMORY_TYPE_CALLOC);

    return ptr;
}

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Re-Allocate memory.
  @param    oldptr      Pointer to free.
  @param    size        Size (in bytes) to allocate.
  @return   1 newly allocated pointer.
 */
/*----------------------------------------------------------------------------*/
void * cpl_xmemory_realloc(void * oldptr, size_t size)
{
    void * ptr;
    size_t pos = cpl_xmemory_table_size; /* Avoid (false) uninit warning */

    if (size == 0) {
        /* In this case it is allowed to return NULL
            - do that and save space in the table */
        cpl_xmemory_free(oldptr);
        return NULL;
    }

    if (oldptr != NULL) {

        if (cpl_xmemory_table_size == 0) {
            fprintf(stderr, "cpl_xmemory error: Ignoring realloc() of %lu bytes"
                    " requested on unallocated pointer (%p)\n",
                    (unsigned long) size, oldptr);
            return NULL;
        }

        pos = cpl_xmemory_findcell(oldptr);

        if (pos == cpl_xmemory_table_size) {
            fprintf(stderr, "cpl_xmemory error: Ignoring realloc() of %lu bytes"
                    " requested on unallocated pointer (%p)\n",
                    (unsigned long) size, oldptr);
            return NULL;
        }

        if (size == cpl_xmemory_p_size[pos]) {
            /* No actual realloc() is needed, so just update the
               allocation type since the caller will expect the pointer
               to come from realloc(). */
            cpl_xmemory_p_type[pos] = CPL_XMEMORY_TYPE_REALLOC;
            return oldptr;
        }

        /* Remove cell from main table */
        cpl_xmemory_remcell(pos);

    }

    ptr = cpl_xmemory_realloc_count(oldptr, size);

    /* assert( ptr != NULL ); */

    if (ptr != oldptr) {

        if (cpl_xmemory_ncells >= cpl_xmemory_table_size
            * CPL_XMEMORY_RESIZE_THRESHOLD) {

            /* Initialize table if needed */
            if (cpl_xmemory_table_size == 0) {
                cpl_xmemory_init();
            }

            /* Memory table is too full, resize */
            cpl_xmemory_resize();
        }

        pos = cpl_xmemory_findfree(ptr);

    }

    /* Add cell into general table */
    cpl_xmemory_addcell(pos, ptr, size, CPL_XMEMORY_TYPE_REALLOC);

    return ptr;
}

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Free memory.
  @param    ptr         Pointer to free.
  @return   void
  @note Nothing is done on NULL
  @see free()

  Free the memory associated to a given pointer. Prints a warning on stderr
  if the requested pointer cannot be found in the memory table. If the pointer
  @em ptr is @c NULL, nothing is done and no error is set.

 */
/*----------------------------------------------------------------------------*/
void cpl_xmemory_free(void * ptr)
{

    size_t pos;

    /* Do nothing for a NULL pointer */
    if (ptr == NULL) return;

    if (cpl_xmemory_ncells == 0) {
        fprintf(stderr, "cpl_xmemory error: Ignoring free() on unallocated "
                "pointer (%p)\n", ptr);
        return;
    }

    /* assert( cpl_xmemory_table_size > 0 ); */

    pos = cpl_xmemory_findcell(ptr);

    if (pos == cpl_xmemory_table_size) {
        fprintf(stderr, "cpl_xmemory error: Ignoring free() on unallocated "
                "pointer (%p)\n", ptr);
        return;

    }

    /* free + below decrement copied from cpl_xmemory_free_count() */
    free(ptr);

    /* Decrement number of allocated pointers */
#   ifdef _OPENMP
#   pragma omp atomic
#   endif
    cpl_xmemory_ncells--;

    if (cpl_xmemory_ncells > 0) {
        /* Remove cell from main table */
        cpl_xmemory_remcell(pos);
    } else {
        /* There are no more active pointers, deallocate internal memory */
        cpl_xmemory_end();
    }

    return;
}

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Display memory status information.
  @param mode  Mode (0: system, 1: Xmemory-failure check, 2: Xmemory-full)
  @return   void

  This function is meant for debugging purposes, but it is recommended to
  call it at the end of every executable making use of the extended memory
  features.
 */
/*----------------------------------------------------------------------------*/
void cpl_xmemory_status(int mode)
{

    if (mode > 0)
        {
            const size_t rowsize =
                sizeof(void*) +
                sizeof(size_t)+
                sizeof(unsigned char);

            fprintf(stderr, "#----- Memory Diagnostics -----\n");

            if (mode == 2) {
                fprintf(stderr,
                        "Peak pointer usage [pointer]: %u\n"
                        "Peak memory allocation [B]:   %lu\n"
                        "Peak table size [pointer]:    %lu\n"
                        "Peak table size [B]:          %lu\n",
                        cpl_xmemory_max_cells,
                        (unsigned long)cpl_xmemory_alloc_max,
                        (unsigned long)cpl_xmemory_table_size_max,
                        (unsigned long)(cpl_xmemory_table_size_max*rowsize));
            } else {
                fprintf(stderr, "Maximum number of pointers"
#               ifdef _OPENMP
                        /* cpl_xmemory_max_cells is not updated thread-safely */
                        " (approximate)"
#               endif
                        ": %u\n", cpl_xmemory_max_cells);
            }

            fprintf(stderr, "#----- Memory Currently Allocated -----\n");

            fprintf(stderr, "Number of active pointers:    %u\n",
                    cpl_xmemory_ncells);

            if (mode == 2 && cpl_xmemory_ncells > 0) {
                size_t ii;
                size_t ntype[4] = {0, 0, 0, 0};

                /* assert( cpl_xmemory_table_size != 0 ); */

                fprintf(stderr,
                        "Memory allocation [B]:        %lu\n"
                        "Current table size [pointer]: %u\n"
                        "Current table size [B]:       %lu\n",
                        (unsigned long)cpl_xmemory_alloc_ram,
                        (unsigned)cpl_xmemory_table_size,
                        (unsigned long)(cpl_xmemory_table_size*rowsize));

                /* Do not print entire table on fatal error
                   - it is likely huge */
                if (cpl_xmemory_fatal == 0)
                    fprintf(stderr, "#- Active pointer details\n");
                for (ii=0; ii < cpl_xmemory_table_size; ii++) {
                    if (cpl_xmemory_p_type[ii] != CPL_XMEMORY_TYPE_FREE) {
                        ntype[cpl_xmemory_p_type[ii]]++;
                        if (cpl_xmemory_fatal == 0)
                            fprintf(stderr, "(%p) - %s() of %lu bytes\n",
                                    cpl_xmemory_p_val[ii],
                                    cpl_xmemory_type[cpl_xmemory_p_type[ii]],
                                    (unsigned long)cpl_xmemory_p_size[ii]);
                    }
                }
                fprintf(stderr, "#- Types of active pointers\n");
                fprintf(stderr, "%7s(): %u\n", cpl_xmemory_type[1],
                        (unsigned)ntype[1]);
                fprintf(stderr, "%7s(): %u\n", cpl_xmemory_type[2],
                        (unsigned)ntype[2]);
                fprintf(stderr, "%7s(): %u\n", cpl_xmemory_type[3],
                        (unsigned)ntype[3]);
            }
            if (cpl_xmemory_fatal != 0) (void)fflush(stderr);
        }

    return;
}

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Tell if there is still some memory allocated
  @param mode  Mode (0: system, 1: Xmemory-failure check, 2: Xmemory-full)
  @return   1 if the memory table is empty, 0 if no,
            -1 if the memory model is off
 */
/*----------------------------------------------------------------------------*/
int cpl_xmemory_is_empty(int mode)
{
    if (mode > 0) {
        /*
          assert(cpl_xmemory_ncells >  0 || cpl_xmemory_alloc_ram == 0);
          if (mode == 2)
          assert(cpl_xmemory_ncells == 0 || cpl_xmemory_alloc_ram  > 0);
        */
        return cpl_xmemory_ncells == 0 ? 1 : 0;
    } else {
        return -1;
    }
}

/**@}*/

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Initialize extended memory features.
  @return   void

  This function is implicitly called by the first of xmemory_alloc() and
  xmemory_free().
  It allocates the default number of memory cells.

 */
/*----------------------------------------------------------------------------*/
static void cpl_xmemory_init(void)
{

    if (CPL_XMEMORY_RESIZE_THRESHOLD > 1.0) {
        fprintf(stderr, "cpl_xmemory fatal error: Memory table resize threshold "
                CPL_XSTRINGIFY(CPL_XMEMORY_RESIZE_THRESHOLD) " must be less "
                "than or equal to 1, not "
                CPL_STRINGIFY(CPL_XMEMORY_RESIZE_THRESHOLD) "\n");
        assert(CPL_XMEMORY_RESIZE_THRESHOLD <= 1.0); /* More informative */

        abort();
    }

    if (CPL_XMEMORY_RESIZE_THRESHOLD <= 0.0) {
        fprintf(stderr, "cpl_xmemory fatal error: Memory table resize threshold "
                CPL_XSTRINGIFY(CPL_XMEMORY_RESIZE_THRESHOLD) " must be positive"
                ", not " CPL_STRINGIFY(CPL_XMEMORY_RESIZE_THRESHOLD) "\n");
        assert(CPL_XMEMORY_RESIZE_THRESHOLD > 0.0); /* More informative */
        abort();
    }

    if (CPL_XMEMORY_RESIZE_FACTOR <= 1.0) {
        fprintf(stderr, "cpl_xmemory fatal error: Memory table resize factor "
                CPL_XSTRINGIFY(CPL_XMEMORY_RESIZE_FACTOR) " must be greater "
                "than 1, not " CPL_STRINGIFY(CPL_XMEMORY_RESIZE_FACTOR) "\n");
        assert(CPL_XMEMORY_RESIZE_FACTOR > 1.0); /* More informative */
        abort();
    }

    cpl_xmemory_table_size = CPL_XMEMORY_MAXPTRS;

    cpl_xmemory_init_alloc();

    return;
}

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Allocate memory for memory features.
  @return   void
  @note This allocation is done directly with calloc()

 */
/*----------------------------------------------------------------------------*/
static void cpl_xmemory_init_alloc(void)
{

    {
        cpl_xmemory_p_val  = calloc(cpl_xmemory_table_size, sizeof(void*));
        cpl_xmemory_p_type = calloc(cpl_xmemory_table_size,
                                    sizeof(unsigned char));
        cpl_xmemory_p_size = malloc(cpl_xmemory_table_size * sizeof(size_t));

        /* FIXME: pragma omp atomic */
        if (cpl_xmemory_table_size > cpl_xmemory_table_size_max) {
            cpl_xmemory_table_size_max = cpl_xmemory_table_size;
        }

    }

    if (cpl_xmemory_p_val == NULL || cpl_xmemory_p_size == NULL ||
        cpl_xmemory_p_type == NULL) {
        /* The table could not be allocated */
        fprintf(stderr, "cpl_xmemory fatal error: calloc() of memory table "
                "failed with size %u:\n", cpl_xmemory_table_size);
        perror(NULL);
        cpl_xmemory_fatal = 1;
        cpl_xmemory_status(1); /* Mode-2 data is invalid! */
        assert(cpl_xmemory_p_val != NULL);  /* More informative, if enabled */
        assert(cpl_xmemory_p_size != NULL);
        assert(cpl_xmemory_p_type != NULL);
        abort();
    }

    return;
}

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Resize the pointer allocation table
  @return   void

 */
/*----------------------------------------------------------------------------*/
static void cpl_xmemory_resize(void)
{

    const void   ** p_val    = cpl_xmemory_p_val;
    size_t        * p_size   = cpl_xmemory_p_size;
    unsigned char * p_type   = cpl_xmemory_p_type;
    const size_t    old_size = cpl_xmemory_table_size;
    size_t          ii;

    assert( cpl_xmemory_table_size > 0 );

    assert( cpl_xmemory_ncells > 0 );

    cpl_xmemory_table_size = cpl_xmemory_table_size * CPL_XMEMORY_RESIZE_FACTOR;

    /* Ensure that the table size is odd, to reduce hashing clashes */
    cpl_xmemory_table_size |= 1;

    if (cpl_xmemory_table_size <= old_size) {
        /* The table could not be resized */
        fprintf(stderr, "cpl_xmemory fatal error: Memory table could not be "
                "resized, %u < %u:\n", cpl_xmemory_table_size, old_size);
        perror(NULL);
        cpl_xmemory_fatal = 1;
        cpl_xmemory_status(1); /* Mode-2 data is invalid! */
        assert(cpl_xmemory_table_size > old_size);  /* More informative */
        abort();
    }

    cpl_xmemory_init_alloc();

    /* Locate pointer in main table */
    /* #   pragma omp parallel for private(ii,pos) schedule(static) */
    for (ii = 0; ii < old_size; ii++) {
        if (p_type[ii]) {
            /* Old table has a pointer here */
            /* Find position in enlarged table */
            const size_t pos = cpl_xmemory_findfree(p_val[ii]);

            /* Duplicated from cpl_xmemory_addcell() */
            cpl_xmemory_p_val[pos] = p_val[ii];
            cpl_xmemory_p_size[pos] = p_size[ii];
            cpl_xmemory_p_type[pos] = p_type[ii];
        }
    }

    free((void*)p_val);
    free(p_size);
    free(p_type);

    return;
}

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Deinitialize extended memory features.
  @return   void

 */
/*----------------------------------------------------------------------------*/
inline
static void cpl_xmemory_end(void)
{

    assert( cpl_xmemory_ncells == 0 );

    cpl_xmemory_table_size = 0;

    cpl_xmemory_alloc_ram = 0;

    free(cpl_xmemory_p_val);
    free(cpl_xmemory_p_type);
    free(cpl_xmemory_p_size);

    return;
}

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Add allocation cell.
  @param    pos             Position in the table
  @param    pointer         Pointer value.
  @param    size            Pointer size.
  @param    type            Allocation type
  @return   void

  Add a memory cell in the xtended memory table to register that a new
  allocation took place.
  This call is not protected against illegal parameter values, so make sure
  the passed values are correct!
 */
/*----------------------------------------------------------------------------*/
inline
static void cpl_xmemory_addcell(size_t        pos,
                                const void  * pointer,
                                size_t        size,
                                unsigned char type)
{

    /* Store information */
    cpl_xmemory_p_val[pos] = pointer;
    cpl_xmemory_p_size[pos] = size;

    /* Allocation type */
    cpl_xmemory_p_type[pos] = type;

    cpl_xmemory_alloc_ram += size;

    /* Remember peak allocation */
    /* FIXME: pragma omp atomic */
    if (cpl_xmemory_alloc_ram > cpl_xmemory_alloc_max)
        cpl_xmemory_alloc_max = cpl_xmemory_alloc_ram;

    return;
}

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Find the cell of a given pointer
  @param    ptr       Xmemory-allocated pointer, i.e. not NULL
  @return   Index of cell with pointer, or cpl_xmemory_table_size when not found

 */
/*----------------------------------------------------------------------------*/
inline
static size_t cpl_xmemory_findcell(const void * ptr)
{
    const size_t pos = PTR_HASH(ptr);
    size_t       ii = pos;

    /* Locate pointer in main table */
    do {
        if (cpl_xmemory_p_val[ii] == ptr) return ii; /* Found */
        if (++ii == cpl_xmemory_table_size) ii = 0; /* Wrap around */
    } while (ii != pos);

    /* Not found */
    return cpl_xmemory_table_size;
}


/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Find the first free cell suitable for the pointer
  @param    ptr      Allocated pointer to keep in Xmemory
  @return   Index of cell with pointer
  @note This function will exit when the table is full

 */
/*----------------------------------------------------------------------------*/
inline
static size_t cpl_xmemory_findfree(const void * ptr)
{

    /* Find an available slot */
    const size_t pos = PTR_HASH(ptr);

    /* In comparison with the method of cpl_xmemory_findcell()
       this is faster when the table starts to be full */
    const void * ppos = memchr(cpl_xmemory_p_type+pos, CPL_XMEMORY_TYPE_FREE,
                               cpl_xmemory_table_size - pos);

    if (ppos == NULL) {
        ppos = memchr(cpl_xmemory_p_type, CPL_XMEMORY_TYPE_FREE, pos);

        if (ppos == NULL) {
            /* No available slot */
            fprintf(stderr, "cpl_xmemory internal, fatal error: "
                    "Could not find place for new pointer\n");
            cpl_xmemory_fatal = 1;
            cpl_xmemory_status(2);
            assert(ppos != NULL); /* More informative, if enabled */
            abort();
        }
    }

    return ((size_t)ppos - (size_t)cpl_xmemory_p_type);
}

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief    Remove the specified memory cell from the xtended memory table.
  @param    pos     Position of the pointer in the table.
  @return   void
  @note This call is not protected against illegal parameter values,
        so make sure the passed value is correct!

 */
/*----------------------------------------------------------------------------*/
inline
static void cpl_xmemory_remcell(size_t pos)
{
    /* Set pointer to NULL */
    cpl_xmemory_p_val[pos] = NULL;

    /* Set type to free */
    cpl_xmemory_p_type[pos] = CPL_XMEMORY_TYPE_FREE;

    cpl_xmemory_alloc_ram -= cpl_xmemory_p_size[pos];

    return;
}