IDZEBRA
2.2.8
dfa
imalloc.c
Go to the documentation of this file.
1
/* This file is part of the Zebra server.
2
Copyright (C) Index Data
3
4
Zebra is free software; you can redistribute it and/or modify it under
5
the terms of the GNU General Public License as published by the Free
6
Software Foundation; either version 2, or (at your option) any later
7
version.
8
9
Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10
WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18
*/
19
20
21
#if HAVE_CONFIG_H
22
#include <
config.h
>
23
#endif
24
#include <stdio.h>
25
#include <assert.h>
26
#include <stdlib.h>
27
28
#include <
idzebra/util.h
>
29
#include <yaz/xmalloc.h>
30
#include "
imalloc.h
"
31
32
#if MEMDEBUG
33
#define MAG1 0x8fe1
34
#define MAG2 0x91
35
#define MAG3 0xee
36
37
long
alloc = 0L;
38
long
max_alloc = 0L;
39
int
alloc_calls = 0;
40
int
free_calls = 0;
41
#endif
42
43
void
*
imalloc
(
size_t
size)
44
{
45
#if MEMDEBUG
46
size_t
words = (4*
sizeof
(unsigned) -1 + size)/
sizeof
(unsigned);
47
char
*p = (
char
*)xmalloc( words*
sizeof
(
unsigned
) );
48
if
( !p )
49
yaz_log (YLOG_FATAL,
"No memory: imalloc(%u); c/f %d/%d; %ld/%ld"
,
50
size, alloc_calls, free_calls, alloc, max_alloc );
51
*((
unsigned
*)p) = size;
52
((
unsigned
*)p)[1] = MAG1;
53
p +=
sizeof
(unsigned)*2;
54
size[(
unsigned
char
*) p] = MAG2;
55
size[(
unsigned
char
*) p+1] = MAG3;
56
if
( (alloc+=size) > max_alloc )
57
max_alloc = alloc;
58
++alloc_calls;
59
return
(
void
*) p;
60
#else
61
void
*p = (
void
*)xmalloc( size );
62
if
( !p )
63
yaz_log (YLOG_FATAL,
"Out of memory (imalloc)"
);
64
return
p;
65
#endif
66
}
67
68
void
*
icalloc
(
size_t
size)
69
{
70
#if MEMDEBUG
71
unsigned
words = (4*
sizeof
(unsigned) -1 + size)/
sizeof
(unsigned);
72
char
*p = (
char
*) xcalloc( words*
sizeof
(
unsigned
), 1 );
73
if
( !p )
74
yaz_log (YLOG_FATAL,
"No memory: icalloc(%u); c/f %d/%d; %ld/%ld"
,
75
size, alloc_calls, free_calls, alloc, max_alloc );
76
((
unsigned
*)p)[0] = size;
77
((
unsigned
*)p)[1] = MAG1;
78
p +=
sizeof
(unsigned)*2;
79
size[(
unsigned
char
*) p] = MAG2;
80
size[(
unsigned
char
*) p+1] = MAG3;
81
if
( (alloc+=size) > max_alloc )
82
max_alloc = alloc;
83
++alloc_calls;
84
return
(
void
*)p;
85
#else
86
void
*p = (
void
*) xcalloc( size, 1 );
87
if
( !p )
88
yaz_log (YLOG_FATAL,
"Out of memory (icalloc)"
);
89
return
p;
90
#endif
91
}
92
93
void
ifree
(
void
*p)
94
{
95
#if MEMDEBUG
96
size_t
size;
97
if
( !p )
98
return
;
99
++free_calls;
100
size = (-2)[(
unsigned
*) p];
101
if
( (-1)[(
unsigned
*) p] != MAG1 )
102
yaz_log (YLOG_FATAL,
"Internal: ifree(%u) magic 1 corrupted"
, size );
103
if
( size[(
unsigned
char
*) p] != MAG2 )
104
yaz_log (YLOG_FATAL,
"Internal: ifree(%u) magic 2 corrupted"
, size );
105
if
( (size+1)[(
unsigned
char
*) p] != MAG3 )
106
yaz_log (YLOG_FATAL,
"Internal: ifree(%u) magic 3 corrupted"
, size );
107
alloc -= size;
108
if
( alloc < 0L )
109
yaz_log (YLOG_FATAL,
"Internal: ifree(%u) negative alloc."
, size );
110
xfree( (
unsigned
*) p-2 );
111
#else
112
xfree (p);
113
#endif
114
}
115
116
#if MEMDEBUG
117
void
imemstat (
void
)
118
{
119
fprintf( stdout,
"imalloc: calls malloc/free %d/%d, "
,
120
alloc_calls, free_calls );
121
if
( alloc )
122
fprintf( stdout,
"memory cur/max %ld/%ld : unreleased"
,
123
alloc, max_alloc );
124
else
125
fprintf( stdout,
"memory max %ld"
, max_alloc );
126
fputc(
'\n'
, stdout );
127
}
128
#endif
129
/*
130
* Local variables:
131
* c-basic-offset: 4
132
* c-file-style: "Stroustrup"
133
* indent-tabs-mode: nil
134
* End:
135
* vim: shiftwidth=4 tabstop=8 expandtab
136
*/
137
config.h
ifree
void ifree(void *p)
Definition
imalloc.c:93
imalloc
void * imalloc(size_t size)
Definition
imalloc.c:43
icalloc
void * icalloc(size_t size)
Definition
imalloc.c:68
imalloc.h
util.h
Generated by
1.9.8