~ubuntu-branches/ubuntu/feisty/elinks/feisty-updates

« back to all changes in this revision

Viewing changes to src/util/memory.h

  • Committer: Bazaar Package Importer
  • Author(s): Peter Gervai
  • Date: 2004-01-21 22:13:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040121221345-ju33hai1yhhqt6kn
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: memory.h,v 1.17 2003/09/28 23:44:32 jonas Exp $ */
 
2
 
 
3
#ifndef EL__UTIL_MEMORY_H
 
4
#define EL__UTIL_MEMORY_H
 
5
 
 
6
/* If defined, we'll crash if ALLOC_MAXTRIES is attained,
 
7
 * if not defined, we'll try to continue. */
 
8
/* #define CRASH_IF_ALLOC_MAXTRIES */
 
9
/* Max. number of retry in case of memory allocation failure. */
 
10
#define ALLOC_MAXTRIES 3
 
11
/* Delay in seconds between each alloc try. */
 
12
#define ALLOC_DELAY 1
 
13
 
 
14
#define fmem_alloc(x) mem_alloc(x)
 
15
#define fmem_free(x) mem_free(x)
 
16
 
 
17
 
 
18
#ifdef LEAK_DEBUG
 
19
 
 
20
#include "util/memdebug.h"
 
21
 
 
22
#define mem_alloc(x) debug_mem_alloc(__FILE__, __LINE__, x)
 
23
#define mem_calloc(x, y) debug_mem_calloc(__FILE__, __LINE__, x, y)
 
24
#define mem_free(x) debug_mem_free(__FILE__, __LINE__, x)
 
25
#define mem_realloc(x, y) debug_mem_realloc(__FILE__, __LINE__, x, y)
 
26
 
 
27
#else
 
28
 
 
29
#ifndef FASTMEM
 
30
 
 
31
/* Cygwin wants some size_t definition here... let's try to make it happy
 
32
 * then. Hrmpf. */
 
33
#include <sys/types.h>
 
34
#include <stddef.h>
 
35
 
 
36
void *mem_alloc(size_t);
 
37
void *mem_calloc(size_t, size_t);
 
38
void mem_free(void *);
 
39
void *mem_realloc(void *, size_t);
 
40
 
 
41
#else
 
42
 
 
43
# include <stdlib.h>
 
44
 
 
45
/* TODO: For enhanced portability, checks at configure time:
 
46
 * malloc(0) -> NULL
 
47
 * realloc(NULL, 0) -> NULL
 
48
 * realloc(p, 0) <-> free(p)
 
49
 * realloc(NULL, n) <-> malloc(n)
 
50
 * Some old implementations may not respect these rules.
 
51
 * For these we need some replacement functions.
 
52
 * This should not be an issue on most modern systems.
 
53
 */
 
54
 
 
55
# define mem_alloc(size) malloc(size)
 
56
# define mem_calloc(count, size) calloc(count, size)
 
57
# define mem_free(p) free(p)
 
58
# define mem_realloc(p, size) realloc(p, size)
 
59
 
 
60
 
 
61
/* fmem_* functions should be use for allocation and freeing of memory
 
62
 * inside a function.
 
63
 * See alloca(3) manpage. */
 
64
 
 
65
#undef fmem_alloc
 
66
#undef fmem_free
 
67
 
 
68
#ifdef HAVE_ALLOCA
 
69
 
 
70
#ifdef HAVE_ALLOCA_H
 
71
#include <alloca.h>
 
72
#endif
 
73
#define fmem_alloc(x) alloca(x)
 
74
#define fmem_free(x)
 
75
 
 
76
#else /* HAVE_ALLOCA */
 
77
 
 
78
#define fmem_alloc(x) mem_alloc(x)
 
79
#define fmem_free(x) mem_free(x)
 
80
 
 
81
#endif /* HAVE_ALLOCA */
 
82
 
 
83
#endif /* FASTMEM */
 
84
 
 
85
#endif /* LEAK_DEBUG */
 
86
 
 
87
 
 
88
/* Granular memory allocation. */
 
89
 
 
90
/* The ``old'' style granularity. XXX: Must be power of 2 */
 
91
#define ALLOC_GR 0x100
 
92
 
 
93
#include <string.h> /* for memset() */
 
94
 
 
95
/* The granularity used by the aligned memory functions below must be a mask
 
96
 * with all bits set from but not including the most significant bit and down.
 
97
 * So if an alignment of 256 is wanted use 0xFF. */
 
98
 
 
99
#define ALIGN_MEMORY_SIZE(x, gr) (((x) + (gr)) & ~(gr))
 
100
 
 
101
static inline void *
 
102
mem_align_alloc__(
 
103
#ifdef LEAK_DEBUG
 
104
                  unsigned char *file, int line,
 
105
#endif
 
106
                  void **ptr, size_t old, size_t new, size_t objsize, int mask)
 
107
{
 
108
        size_t newsize = ALIGN_MEMORY_SIZE(new, mask);
 
109
        size_t oldsize = ALIGN_MEMORY_SIZE(old, mask);
 
110
 
 
111
        if (newsize > oldsize) {
 
112
                unsigned char *data;
 
113
 
 
114
                newsize *= objsize;
 
115
                oldsize *= objsize;
 
116
 
 
117
#ifdef LEAK_DEBUG
 
118
                data = debug_mem_realloc(file, line, *ptr, newsize);
 
119
#else
 
120
                data = mem_realloc(*ptr, newsize);
 
121
#endif
 
122
                if (!data) return NULL;
 
123
 
 
124
                *ptr = (void *)data;
 
125
                memset(&data[oldsize], 0, newsize - oldsize);
 
126
        }
 
127
 
 
128
        return *ptr;
 
129
}
 
130
 
 
131
#ifdef LEAK_DEBUG
 
132
#define mem_align_alloc(ptr, old, new, objsize, mask) \
 
133
        mem_align_alloc__(__FILE__, __LINE__, (void **)ptr, old, new, objsize, mask)
 
134
#else
 
135
#define mem_align_alloc(ptr, old, new, objsize, mask) \
 
136
        mem_align_alloc__((void **)ptr, old, new, objsize, mask)
 
137
#endif
 
138
 
 
139
#endif