aboutsummaryrefslogtreecommitdiffstats
path: root/src/mem.c
blob: 8a7271d4044772c5091ab232977540087720b028 (plain) (blame)
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
/*
 * Calcurse - text-based organizer
 *
 * Copyright (c) 2004-2011 calcurse Development Team <misc@calcurse.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer in the documentation and/or other
 *        materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Send your feedback or comments to : misc@calcurse.org
 * Calcurse home page : http://calcurse.org
 *
 */

#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include "calcurse.h"

enum {
  BLK_STATE,
  BLK_SIZE,
  BLK_ID,
  EXTRA_SPACE_START
};

#define EXTRA_SPACE_END    1
#define EXTRA_SPACE        EXTRA_SPACE_START + EXTRA_SPACE_END

#define MAGIC_ALLOC        0xda
#define MAGIC_FREE         0xdf

struct mem_blk {
  unsigned id, size;
  const char *pos;
  struct mem_blk *next;
};

struct mem_stats {
  unsigned ncall, nalloc, nfree;
  struct mem_blk *blk;
};

static struct mem_stats mstats;


static unsigned
stats_add_blk (size_t size, const char *pos)
{
  struct mem_blk *o, **i;

  o = malloc (sizeof (*o));
  EXIT_IF (o == NULL, _("could not allocate memory to store block info"));

  mstats.ncall++;

  o->pos = pos;
  o->size = (unsigned)size;
  o->next = 0;

  for (i = &mstats.blk; *i; i = &(*i)->next)
    ;
  o->id = mstats.ncall;
  *i = o;

  return o->id;
}

static void
stats_del_blk (unsigned id)
{
  struct mem_blk *o, **i;

  i = &mstats.blk;
  for (o = mstats.blk; o; o = o->next)
    {
      if (o->id == id)
        {
          *i = o->next;
          free (o);
          return;
        }
      i = &o->next;
    }

  EXIT (_("Block not found"));
  /* NOTREACHED */
}

void *
xmalloc (size_t size)
{
  void *p;

  EXIT_IF (size == 0, _("xmalloc: zero size"));
  p = malloc (size);
  EXIT_IF (p == NULL, _("xmalloc: out of memory"));

  return p;
}

void *
xcalloc (size_t nmemb, size_t size)
{
  void *p;

  EXIT_IF (nmemb == 0 || size == 0, _("xcalloc: zero size"));
  EXIT_IF (SIZE_MAX / nmemb < size, _("xcalloc: overflow"));
  p = calloc (nmemb, size);
  EXIT_IF (p == NULL, _("xcalloc: out of memory"));

  return p;
}

void *
xrealloc (void *ptr, size_t nmemb, size_t size)
{
  void *new_ptr;
  size_t new_size;

  new_size = nmemb * size;
  EXIT_IF (new_size == 0, _("xrealloc: zero size"));
  EXIT_IF (SIZE_MAX / nmemb < size, _("xrealloc: overflow"));
  new_ptr = realloc (ptr, new_size);
  EXIT_IF (new_ptr == NULL, _("xrealloc: out of memory"));

  return new_ptr;
}

char *
xstrdup (const char *str)
{
  size_t len;
  char *cp;

  len = strlen (str) + 1;
  cp = xmalloc (len);

  return strncpy (cp, str, len);
}

void
xfree (void *p)
{
  EXIT_IF (p == NULL, _("xfree: null pointer"));
  free (p);
}

void *
dbg_malloc (size_t size, const char *pos)
{
  unsigned *buf;

   if (size == 0)
    return NULL;

  size = EXTRA_SPACE + (size + sizeof (unsigned) - 1) / sizeof (unsigned);
  buf = xmalloc (size * sizeof (unsigned));

  buf[BLK_STATE] = MAGIC_ALLOC;             /* state of the block */
  buf[BLK_SIZE] = size;                     /* size of the block */
  buf[BLK_ID] = stats_add_blk (size, pos);  /* identify a block by its id */
  buf[size - 1] = buf[BLK_ID];              /* mark at end of block */

  mstats.nalloc += size;

  return (void *)(buf + EXTRA_SPACE_START);
}

void *
dbg_calloc (size_t nmemb, size_t size, const char *pos)
{
  void *buf;

  if (!nmemb || !size)
    return NULL;

  EXIT_IF (nmemb > SIZE_MAX / size, _("overflow at %s"), pos);

  size *= nmemb;
  if ((buf = dbg_malloc (size, pos)) == NULL)
    return NULL;

  memset (buf, 0, size);

  return buf;
}

void *
dbg_realloc (void *ptr, size_t nmemb, size_t size, const char *pos)
{
  unsigned *buf, old_size, new_size, cpy_size;

  if (ptr == NULL)
    return NULL;

  new_size = nmemb *size;
  if (new_size == 0)
    return NULL;

  EXIT_IF (nmemb > SIZE_MAX / size, _("overflow at %s"), pos);

  if ((buf = dbg_malloc (new_size, pos)) == NULL)
    return NULL;

  old_size = *((unsigned *)ptr - EXTRA_SPACE_START + BLK_SIZE);
  cpy_size = (old_size > new_size) ? new_size : old_size;
  memmove (buf, ptr, cpy_size);

  mem_free (ptr);

  return (void *)buf;
}

char *
dbg_strdup (const char *s, const char *pos)
{
  size_t size;
  char *buf;

  if (s == NULL)
    return NULL;

  size = strlen (s);
  if ((buf = dbg_malloc (size + 1, pos)) == NULL)
    return NULL;

  return strncpy (buf, s, size + 1);
}

void
dbg_free (void *ptr, const char *pos)
{
  unsigned *buf, size;

  EXIT_IF (ptr == NULL, _("dbg_free: null pointer at %s"), pos);

  buf = (unsigned *)ptr - EXTRA_SPACE_START;
  size = buf[BLK_SIZE];

  EXIT_IF (buf[BLK_STATE] == MAGIC_FREE,
           _("block seems already freed at %s"), pos);
  EXIT_IF (buf[BLK_STATE] != MAGIC_ALLOC,
           _("corrupt block header at %s"), pos);
  EXIT_IF (buf[size - 1] != buf[BLK_ID],
           _("corrupt block end at %s, (end = %u, should be %d)"), pos,
           buf[size - 1], buf[BLK_ID]);

  buf[0] = MAGIC_FREE;

  stats_del_blk (buf[BLK_ID]);

  free (buf);
  mstats.nfree += size;
}


#ifdef CALCURSE_MEMORY_DEBUG

static void
dump_block_info (struct mem_blk *blk)
{
  if (blk == NULL)
    return;

  puts (_("---==== MEMORY BLOCK ====----------------\n"));
  printf (_("            id: %u\n"), blk->id);
  printf (_("          size: %u\n"), blk->size);
  printf (_("  allocated in: %s\n"), blk->pos);
  puts (_("-----------------------------------------\n"));
}

void
mem_stats (void)
{
  putchar ('\n');
  puts (_("+------------------------------+\n"));
  puts (_("| calcurse memory usage report |\n"));
  puts (_("+------------------------------+\n"));
  printf (_("  number of calls: %u\n"), mstats.ncall);
  printf (_(" allocated blocks: %u\n"), mstats.nalloc);
  printf (_("   unfreed blocks: %u\n"), mstats.nalloc - mstats.nfree);
  putchar ("\n");

  if (mstats.nfree < mstats.nalloc)
    {
      struct mem_blk *blk;

      for (blk = mstats.blk; blk; blk = blk->next)
        dump_block_info (blk);
    }
}

#endif /* CALCURSE_MEMORY_DEBUG */