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

« back to all changes in this revision

Viewing changes to src/util/memory.c

  • 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
/* Memory allocation manager */
 
2
/* $Id: memory.c,v 1.15 2003/12/21 14:56:56 zas Exp $ */
 
3
 
 
4
#ifdef HAVE_CONFIG_H
 
5
#include "config.h"
 
6
#endif
 
7
 
 
8
#include <stdlib.h>
 
9
#include <unistd.h>
 
10
 
 
11
#include "elinks.h"
 
12
 
 
13
#include "util/error.h"
 
14
#include "util/memory.h"
 
15
 
 
16
 
 
17
#if !defined(LEAK_DEBUG) && !defined(FASTMEM)
 
18
 
 
19
static int alloc_try = 0;
 
20
 
 
21
static int
 
22
patience(unsigned char *of)
 
23
{
 
24
        ++alloc_try;
 
25
        if (alloc_try < ALLOC_MAXTRIES) {
 
26
                ERROR("Out of memory (%s returned NULL): retry #%d,"
 
27
                        " I still exercise my patience and retry tirelessly.",
 
28
                        of, alloc_try);
 
29
                sleep(ALLOC_DELAY);
 
30
                return alloc_try;
 
31
        }
 
32
 
 
33
#ifdef CRASH_IF_ALLOC_MAXTRIES
 
34
        INTERNAL("Out of memory (%s returned NULL) after %d tries,"
 
35
                " I give up. See ya on the other side.",
 
36
                of, alloc_try);
 
37
#else
 
38
        ERROR("Out of memory (%s returned NULL) after %d tries,"
 
39
                " I give up and try to continue. Pray for me, please.",
 
40
                of, alloc_try);
 
41
#endif
 
42
        alloc_try = 0;
 
43
        return 0;
 
44
}
 
45
 
 
46
void *
 
47
mem_alloc(size_t size)
 
48
{
 
49
        if (size)
 
50
                do {
 
51
                        void *p = malloc(size);
 
52
 
 
53
                        if (p) return p;
 
54
                } while (patience("malloc"));
 
55
 
 
56
        return NULL;
 
57
}
 
58
 
 
59
void *
 
60
mem_calloc(size_t count, size_t eltsize)
 
61
{
 
62
        if (eltsize && count)
 
63
                do {
 
64
                        void *p = calloc(count, eltsize);
 
65
 
 
66
                        if (p) return p;
 
67
                } while (patience("calloc"));
 
68
 
 
69
        return NULL;
 
70
}
 
71
 
 
72
void
 
73
mem_free(void *p)
 
74
{
 
75
        if (!p) {
 
76
                INTERNAL("mem_free(NULL)");
 
77
                return;
 
78
        }
 
79
 
 
80
        free(p);
 
81
}
 
82
 
 
83
void *
 
84
mem_realloc(void *p, size_t size)
 
85
{
 
86
        if (!p) return mem_alloc(size);
 
87
 
 
88
        if (size)
 
89
                do {
 
90
                        void *p2 = realloc(p, size);
 
91
 
 
92
                        if (p2) return p2;
 
93
                } while (patience("realloc"));
 
94
        else
 
95
                mem_free(p);
 
96
 
 
97
        return NULL;
 
98
}
 
99
 
 
100
#endif