File: k2mem.c

package info (click to toggle)
k2pdfopt 2.51+ds-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,480 kB
  • sloc: ansic: 81,694; cpp: 5,829; makefile: 5
file content (129 lines) | stat: -rw-r--r-- 3,642 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
/*
** k2mem.c      Functions to handle k2pdfopt memory allocation.
**
** Copyright (C) 2014  http://willus.com
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
*/

#include "k2pdfopt.h"

/*
** mem_index... controls which memory allocactions get a protective margin
** around them.  Only used for debugging.  Set to 999 if not debugging.
*/
#if (WILLUSDEBUGX & 0x100000)
static int mem_index_min = 0;
static int mem_index_max = 50;
static int wmsize[16384];
static int wmindex[16384];
static unsigned char *wmptr[16384];
static int nm;
#endif


void willus_dmem_alloc_warn(int index,void **ptr,int size,char *funcname,int exitcode)

    {
#if (WILLUSDEBUGX & 0x100000)
    if (index>=mem_index_min && index<=mem_index_max)
        {
        unsigned char *ptr1;
        void *x;
        willus_mem_alloc_warn((void **)&ptr1,size+2048,funcname,exitcode);
        if (nm>=16384)
            {
            printf("Memory debug out of slots.\n");
            exit(10);
            }
        wmsize[nm]=size;
        wmindex[nm]=index;
        memset(ptr1,211,size+2048);
        ptr1 += 1024;
        x=(void *)ptr1;
        (*ptr) = x;
        wmptr[nm]=(*ptr);
        nm++;
        }
    else
#endif
        willus_mem_alloc_warn(ptr,size,funcname,exitcode);
    }


void willus_dmem_free(int index,double **ptr,char *funcname)

    {
    if ((*ptr)==NULL)
        return;
#if (WILLUSDEBUGX & 0x100000)
    if (index>=mem_index_min && index<=mem_index_max)
        { 
        double *x;
        unsigned char *ptr1;
        int i,size;
        x=(*ptr);
        ptr1=(unsigned char *)x;
        for (i=0;i<nm;i++)
            if (ptr1==wmptr[i])
                break;
        if (i>=nm)
            {
            printf("Memory debug, index %d pointer not found!\n",index);
            exit(10);
            }
        size=wmsize[i];
        if (i<nm-1)
            {
            memmove(&wmptr[i],&wmptr[i+1],sizeof(unsigned char *)*(nm-1-i));
            memmove(&wmsize[i],&wmsize[i+1],sizeof(int)*(nm-1-i));
            memmove(&wmindex[i],&wmindex[i+1],sizeof(int)*(nm-1-i));
            }
        nm--;
        ptr1 -= 1024;
        for (i=0;i<1024;i++)
            if (ptr1[i]!=211 || ptr1[i+size+1024]!=211)
                {
                printf("Corrupt memory write on index %d!\n",index);
                exit(10);
                }
        x=(double *)ptr1;
        willus_mem_free(&x,funcname);
        (*ptr)=NULL;
        }
    else
#endif
        willus_mem_free(ptr,funcname);
    }



#if (WILLUSDEBUGX & 0x100000)
void willus_dmem_check(void)

    {
    if (nm>0)
        {
        int i;

        printf("%d un-freed memory allocations!\n",nm);
        for (i=0;i<nm;i++)
            printf("Index[%d] = %d, size= %d\n",i,wmindex[i],wmsize[i]);
        exit(10);
        }
    else
        printf("willus_dmem_check:  All memory correctly released.\n");
    }
#endif