~ubuntu-branches/debian/wheezy/ghostscript/wheezy

« back to all changes in this revision

Viewing changes to base/memento.c

  • Committer: Package Import Robot
  • Author(s): Jonas Smedegaard
  • Date: 2012-02-10 19:27:35 UTC
  • mfrom: (1.2.7)
  • Revision ID: package-import@ubuntu.com-20120210192735-itmbfs6nfl8t9qm8
Tags: 9.05~dfsg-1
* New upstream release.
* Update copyright file:
  + Improve Copyright shortnames:
    - BSD → BSD-3-Clause
    - MIT~Open → NTP~Open
    - MIT~WSU → NTP~WSU
    - MIT~Lucent → NTP~Lucent
    - other-Adobe → BSD-3-Clause~Adobe
  + Fix merge double Files-Excluded, and list globbing before specific
    files.
  + Quote licenses in Comment fields.
  + Drop duplicate Comment fields.
  + Fix refer to license shortname AFPL~AFPL (not AFPL).
  + Extend copyright years.
  + Add new Files section, Apache-2.0 licensed (TrueType file, not
    linked code so not challenging GPL).
  + Drop a few Files sections and a License section.
* strip convenience library openjpeg from upstream source.
* Imported Upstream version 9.05~dfsg
* Drop patches 020110819 and 1001, applied upstream.
* Explicitly disable use of openjpeg: Now preferred if available but
  requires patching for ICC and CMYK support.
* Update symbols file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 * to speed the operation */
18
18
#undef MEMENTO_LEAKONLY
19
19
 
 
20
#ifndef MEMENTO_STACKTRACE_METHOD
 
21
#ifdef __GNUC__
 
22
#define MEMENTO_STACKTRACE_METHOD 1
 
23
#endif
 
24
#endif
 
25
 
20
26
/* Don't keep blocks around if they'd mean losing more than a quarter of
21
27
 * the freelist. */
22
28
#define MEMENTO_FREELIST_MAX_SINGLE_BLOCK (MEMENTO_FREELIST_MAX/4)
25
31
/* For GS we include malloc_.h. Anyone else would just include memento.h */
26
32
#include "malloc_.h"
27
33
 
 
34
#if defined(__linux__)
 
35
#define MEMENTO_HAS_FORK
 
36
#elif defined(__APPLE__) && defined(__MACH__)
 
37
#define MEMENTO_HAS_FORK
 
38
#endif
 
39
 
28
40
/* Define the underlying allocators, just in case */
29
41
void *MEMENTO_UNDERLYING_MALLOC(size_t);
30
42
void MEMENTO_UNDERLYING_FREE(void *);
47
59
    Memento_PostSize = 16
48
60
};
49
61
 
 
62
enum {
 
63
    Memento_Flag_OldBlock = 1
 
64
};
 
65
 
50
66
typedef struct Memento_BlkHeader Memento_BlkHeader;
51
67
 
52
68
struct Memento_BlkHeader
54
70
    size_t             rawsize;
55
71
    int                sequence;
56
72
    int                lastCheckedOK;
 
73
    int                flags;
57
74
    Memento_BlkHeader *next;
58
75
    char               preblk[Memento_PreSize];
59
76
};
211
228
 
212
229
    p = MEMBLK_TOBLK(b);
213
230
    i = b->rawsize;
 
231
    /* Attempt to speed this up by checking an (aligned) int at a time */
214
232
    do {
215
 
        if (*p++ != (char)MEMENTO_FREEFILL)
216
 
            break;
217
 
    } while (--i);
 
233
        if (((size_t)p) & 1) {
 
234
            if (*p++ != (char)MEMENTO_FREEFILL)
 
235
                break;
 
236
            i--;
 
237
            if (i == 0)
 
238
                break;
 
239
        }
 
240
        if ((i >= 2) && (((size_t)p) & 2)) {
 
241
            if (*(short *)p != (short)(MEMENTO_FREEFILL | (MEMENTO_FREEFILL<<8)))
 
242
                goto mismatch;
 
243
            p += 2;
 
244
            i -= 2;
 
245
            if (i == 0)
 
246
                break;
 
247
        }
 
248
        i -= 4;
 
249
        while (i >= 0) {
 
250
            if (*(int *)p != (MEMENTO_FREEFILL |
 
251
                              (MEMENTO_FREEFILL<<8) |
 
252
                              (MEMENTO_FREEFILL<<16) |
 
253
                              (MEMENTO_FREEFILL<<24)))
 
254
                goto mismatch;
 
255
            p += 4;
 
256
            i -= 4;
 
257
        }
 
258
        i += 4;
 
259
        if ((i >= 2) && (((size_t)p) & 2)) {
 
260
            if (*(short *)p != (short)(MEMENTO_FREEFILL | (MEMENTO_FREEFILL<<8)))
 
261
                goto mismatch;
 
262
            p += 2;
 
263
            i -= 2;
 
264
        }
 
265
mismatch:
 
266
        while (i) {
 
267
            if (*p++ != (char)MEMENTO_FREEFILL)
 
268
                break;
 
269
            i--;
 
270
        }
 
271
    } while (0);
218
272
    if (i) {
219
273
        data->freeCorrupt = 1;
220
274
        data->index       = b->rawsize-i;
240
294
        /* FAIL! Will have been reported to user earlier, so just exit. */
241
295
        return;
242
296
    }
 
297
    VALGRIND_MAKE_MEM_DEFINED(blks->tail, sizeof(*blks->tail));
243
298
    if (*blks->tail == head) {
244
299
        /* Removing the tail of the list */
245
300
        if (prev == NULL) {
338
393
}
339
394
 
340
395
static int Memento_listBlock(Memento_BlkHeader *b,
341
 
                              void              *arg)
 
396
                             void              *arg)
342
397
{
343
398
    int *counts = (int *)arg;
344
 
    fprintf(stderr, "    0x%x:(size=%d,num=%d)\n",
345
 
            MEMBLK_TOBLK(b), b->rawsize, b->sequence);
 
399
    fprintf(stderr, "    0x%p:(size=%d,num=%d)\n",
 
400
            MEMBLK_TOBLK(b), (int)b->rawsize, b->sequence);
346
401
    counts[0]++;
347
402
    counts[1]+= b->rawsize;
348
403
    return 0;
349
404
}
350
405
 
351
 
static void Memento_listBlocks(void) {
 
406
void Memento_listBlocks(void) {
352
407
    int counts[2];
353
408
    counts[0] = 0;
354
409
    counts[1] = 0;
358
413
    fprintf(stderr, "  Total size of blocks = %d\n", counts[1]);
359
414
}
360
415
 
 
416
static int Memento_listNewBlock(Memento_BlkHeader *b,
 
417
                                void              *arg)
 
418
{
 
419
    if (b->flags & Memento_Flag_OldBlock)
 
420
        return 0;
 
421
    b->flags |= Memento_Flag_OldBlock;
 
422
    return Memento_listBlock(b, arg);
 
423
}
 
424
 
 
425
void Memento_listNewBlocks(void) {
 
426
    int counts[2];
 
427
    counts[0] = 0;
 
428
    counts[1] = 0;
 
429
    fprintf(stderr, "Blocks allocated and still extant since last list:\n");
 
430
    Memento_appBlocks(&globals.used, Memento_listNewBlock, &counts[0]);
 
431
    fprintf(stderr, "  Total number of blocks = %d\n", counts[0]);
 
432
    fprintf(stderr, "  Total size of blocks = %d\n", counts[1]);
 
433
}
 
434
 
361
435
static void Memento_fin(void)
362
436
{
363
437
    Memento_checkAllMemory();
443
517
 
444
518
#ifdef MEMENTO_HAS_FORK
445
519
#include <unistd.h>
446
 
#include <sys/syslimits.h>
447
520
#include <sys/wait.h>
448
521
#include <signal.h>
 
522
#ifdef MEMENTO_STACKTRACE_METHOD
 
523
#if MEMENTO_STACKTRACE_METHOD == 1
 
524
#include <signal.h>
 
525
#endif
 
526
#endif
 
527
 
 
528
/* FIXME: Find some portable way of getting this */
 
529
/* MacOSX has 10240, Ubuntu seems to have 256 */
 
530
#define OPEN_MAX 10240
449
531
 
450
532
/* stashed_map[j] = i means that filedescriptor i-1 was duplicated to j */
451
533
int stashed_map[OPEN_MAX];
453
535
static void Memento_signal(void)
454
536
{
455
537
    fprintf(stderr, "SEGV after Memory squeezing @ %d\n", globals.squeezed);
 
538
 
 
539
#ifdef MEMENTO_STACKTRACE_METHOD
 
540
#if MEMENTO_STACKTRACE_METHOD == 1
 
541
    {
 
542
      void *array[100];
 
543
      size_t size;
 
544
 
 
545
      size = backtrace(array, 100);
 
546
      fprintf(stderr, "------------------------------------------------------------------------\n");
 
547
      fprintf(stderr, "Backtrace:\n");
 
548
      backtrace_symbols_fd(array, size, 2);
 
549
      fprintf(stderr, "------------------------------------------------------------------------\n");
 
550
    }
 
551
#endif
 
552
#endif
 
553
 
456
554
    exit(1);
457
555
}
458
556
 
502
600
}
503
601
#endif
504
602
 
 
603
int Memento_failThisEvent(void)
 
604
{
 
605
    if (!globals.inited)
 
606
        Memento_init();
 
607
 
 
608
    Memento_event();
 
609
 
 
610
    if ((globals.squeezing) && (!globals.squeezed))
 
611
        squeeze();
 
612
 
 
613
    return globals.failing;
 
614
}
 
615
 
505
616
void *Memento_malloc(size_t s)
506
617
{
507
618
    Memento_BlkHeader *memblk;
537
648
    memblk->rawsize       = s;
538
649
    memblk->sequence      = globals.sequence;
539
650
    memblk->lastCheckedOK = memblk->sequence;
 
651
    memblk->flags         = 0;
540
652
    Memento_addBlockHead(&globals.used, memblk, 0);
541
653
    return MEMBLK_TOBLK(memblk);
542
654
}
560
672
                     &data, memblk);
561
673
    if (!data.found) {
562
674
        /* Failure! */
563
 
        fprintf(stderr, "Attempt to %s block 0x%x(size=%d,num=%d) not on allocated list!\n",
 
675
        fprintf(stderr, "Attempt to %s block 0x%p(size=%d,num=%d) not on allocated list!\n",
564
676
                action, memblk, memblk->rawsize, memblk->sequence);
565
677
        Memento_breakpoint();
566
678
        return 1;
567
679
    } else if (data.preCorrupt || data.postCorrupt) {
568
 
        fprintf(stderr, "Block 0x%x(size=%d,num=%d) found to be corrupted on %s!\n",
 
680
        fprintf(stderr, "Block 0x%p(size=%d,num=%d) found to be corrupted on %s!\n",
569
681
                action, memblk->rawsize, memblk->sequence, action);
570
682
        if (data.preCorrupt) {
571
683
            fprintf(stderr, "Preguard corrupted\n");
695
807
            fprintf(stderr, "Allocated blocks:\n");
696
808
            data->found |= 2;
697
809
        }
698
 
        fprintf(stderr, "  Block 0x%x(size=%d,num=%d)",
 
810
        fprintf(stderr, "  Block 0x%p(size=%d,num=%d)",
699
811
                memblk, memblk->rawsize, memblk->sequence);
700
812
        if (data->preCorrupt) {
701
813
            fprintf(stderr, " Preguard ");
724
836
            fprintf(stderr, "Freed blocks:\n");
725
837
            data->found |= 4;
726
838
        }
727
 
        fprintf(stderr, "  0x%x(size=%d,num=%d) ",
 
839
        fprintf(stderr, "  0x%p(size=%d,num=%d) ",
728
840
                MEMBLK_TOBLK(memblk), memblk->rawsize, memblk->sequence);
729
841
        if (data->freeCorrupt) {
730
 
            fprintf(stderr, "index %d (address 0x%x) onwards ", data->index,
 
842
            fprintf(stderr, "index %d (address 0x%p) onwards ", data->index,
731
843
                    &((char *)MEMBLK_TOBLK(memblk))[data->index]);
732
844
            if (data->preCorrupt) {
733
845
                fprintf(stderr, "+ preguard ");
843
955
    data.flags = 0;
844
956
    Memento_appBlocks(&globals.used, Memento_containsAddr, &data);
845
957
    if (data.blk != NULL) {
846
 
        fprintf(stderr, "Address 0x%x is in %sallocated block 0x%x(size=%d,num=%d)\n",
 
958
        fprintf(stderr, "Address 0x%p is in %sallocated block 0x%p(size=%d,num=%d)\n",
847
959
                data.addr,
848
960
                (data.flags == 1 ? "" : (data.flags == 2 ?
849
961
                                         "preguard of " : "postguard of ")),
854
966
    data.flags = 0;
855
967
    Memento_appBlocks(&globals.free, Memento_containsAddr, &data);
856
968
    if (data.blk != NULL) {
857
 
        fprintf(stderr, "Address 0x%x is in %sfreed block 0x%x(size=%d,num=%d)\n",
 
969
        fprintf(stderr, "Address 0x%p is in %sfreed block 0x%p(size=%d,num=%d)\n",
858
970
                data.addr,
859
971
                (data.flags == 1 ? "" : (data.flags == 2 ?
860
972
                                         "preguard of " : "postguard of ")),
950
1062
    return MEMENTO_UNDERLYING_CALLOC(n, s);
951
1063
}
952
1064
 
 
1065
void (Memento_listBlocks)(void)
 
1066
{
 
1067
}
 
1068
 
 
1069
void (Memento_listNewBlocks)(void)
 
1070
{
 
1071
}
 
1072
 
953
1073
 
954
1074
#endif