~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/interfaces/ecpg/ecpglib/memory.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/memory.c,v 1.6 2004-12-30 09:36:37 meskes Exp $ */
 
2
 
 
3
#define POSTGRES_ECPG_INTERNAL
 
4
#include "postgres_fe.h"
 
5
 
 
6
#include "ecpgtype.h"
 
7
#include "ecpglib.h"
 
8
#include "ecpgerrno.h"
 
9
#include "extern.h"
 
10
 
 
11
void
 
12
ECPGfree(void *ptr)
 
13
{
 
14
        free(ptr);
 
15
}
 
16
 
 
17
char *
 
18
ECPGalloc(long size, int lineno)
 
19
{
 
20
        char       *new = (char *) calloc(1L, size);
 
21
 
 
22
        if (!new)
 
23
        {
 
24
                ECPGraise(lineno, ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
 
25
                return NULL;
 
26
        }
 
27
 
 
28
        memset(new, '\0', size);
 
29
        return (new);
 
30
}
 
31
 
 
32
char *
 
33
ECPGrealloc(void *ptr, long size, int lineno)
 
34
{
 
35
        char       *new = (char *) realloc(ptr, size);
 
36
 
 
37
        if (!new)
 
38
        {
 
39
                ECPGraise(lineno, ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
 
40
                return NULL;
 
41
        }
 
42
 
 
43
        return (new);
 
44
}
 
45
 
 
46
char *
 
47
ECPGstrdup(const char *string, int lineno)
 
48
{
 
49
        char       *new;
 
50
 
 
51
        if (string == NULL)
 
52
                return NULL;
 
53
        
 
54
        new = strdup(string);
 
55
        if (!new)
 
56
        {
 
57
                ECPGraise(lineno, ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
 
58
                return NULL;
 
59
        }
 
60
 
 
61
        return (new);
 
62
}
 
63
 
 
64
/* keep a list of memory we allocated for the user */
 
65
static struct auto_mem
 
66
{
 
67
        void       *pointer;
 
68
        struct auto_mem *next;
 
69
}       *auto_allocs = NULL;
 
70
 
 
71
void
 
72
ECPGadd_mem(void *ptr, int lineno)
 
73
{
 
74
        struct auto_mem *am = (struct auto_mem *) ECPGalloc(sizeof(struct auto_mem), lineno);
 
75
 
 
76
        am->pointer = ptr;
 
77
        am->next = auto_allocs;
 
78
        auto_allocs = am;
 
79
}
 
80
 
 
81
void
 
82
ECPGfree_auto_mem(void)
 
83
{
 
84
        struct auto_mem *am;
 
85
 
 
86
        /* free all memory we have allocated for the user */
 
87
        for (am = auto_allocs; am;)
 
88
        {
 
89
                struct auto_mem *act = am;
 
90
 
 
91
                am = am->next;
 
92
                ECPGfree(act->pointer);
 
93
                ECPGfree(act);
 
94
        }
 
95
 
 
96
        auto_allocs = NULL;
 
97
}
 
98
 
 
99
void
 
100
ECPGclear_auto_mem(void)
 
101
{
 
102
        struct auto_mem *am;
 
103
 
 
104
        /* free just our own structure */
 
105
        for (am = auto_allocs; am;)
 
106
        {
 
107
                struct auto_mem *act = am;
 
108
 
 
109
                am = am->next;
 
110
                ECPGfree(act);
 
111
        }
 
112
 
 
113
        auto_allocs = NULL;
 
114
}