PostgreSQL Source Code git master
memory.c
Go to the documentation of this file.
1/* src/interfaces/ecpg/ecpglib/memory.c */
2
3#define POSTGRES_ECPG_INTERNAL
4#include "postgres_fe.h"
5
7#include "ecpgerrno.h"
8#include "ecpglib.h"
9#include "ecpglib_extern.h"
10#include "ecpgtype.h"
11
12void
13ecpg_free(void *ptr)
14{
15 free(ptr);
16}
17
18char *
19ecpg_alloc(long size, int lineno)
20{
21 char *new = (char *) calloc(1L, size);
22
23 if (!new)
24 {
26 return NULL;
27 }
28
29 return new;
30}
31
32char *
33ecpg_realloc(void *ptr, long size, int lineno)
34{
35 char *new = (char *) realloc(ptr, size);
36
37 if (!new)
38 {
40 return NULL;
41 }
42
43 return new;
44}
45
46/*
47 * Wrapper for strdup(), with NULL in input treated as a correct case.
48 *
49 * "alloc_failed" can be optionally specified by the caller to check for
50 * allocation failures. The caller is responsible for its initialization,
51 * as ecpg_strdup() may be called repeatedly across multiple allocations.
52 */
53char *
54ecpg_strdup(const char *string, int lineno, bool *alloc_failed)
55{
56 char *new;
57
58 if (string == NULL)
59 return NULL;
60
61 new = strdup(string);
62 if (!new)
63 {
64 if (alloc_failed)
65 *alloc_failed = true;
67 return NULL;
68 }
69
70 return new;
71}
72
73/* keep a list of memory we allocated for the user */
75{
76 void *pointer;
77 struct auto_mem *next;
78};
79
81static pthread_once_t auto_mem_once = PTHREAD_ONCE_INIT;
82
83static void
85{
86 (void) arg; /* keep the compiler quiet */
88}
89
90static void
92{
93 pthread_key_create(&auto_mem_key, auto_mem_destructor);
94}
95
96static struct auto_mem *
98{
99 pthread_once(&auto_mem_once, auto_mem_key_init);
100 return (struct auto_mem *) pthread_getspecific(auto_mem_key);
101}
102
103static void
105{
107}
108
109char *
110ecpg_auto_alloc(long size, int lineno)
111{
112 void *ptr = ecpg_alloc(size, lineno);
113
114 if (!ptr)
115 return NULL;
116
117 if (!ecpg_add_mem(ptr, lineno))
118 {
119 ecpg_free(ptr);
120 return NULL;
121 }
122 return ptr;
123}
124
125bool
126ecpg_add_mem(void *ptr, int lineno)
127{
128 struct auto_mem *am = (struct auto_mem *) ecpg_alloc(sizeof(struct auto_mem), lineno);
129
130 if (!am)
131 return false;
132
133 am->pointer = ptr;
134 am->next = get_auto_allocs();
135 set_auto_allocs(am);
136 return true;
137}
138
139void
141{
142 struct auto_mem *am = get_auto_allocs();
143
144 /* free all memory we have allocated for the user */
145 if (am)
146 {
147 do
148 {
149 struct auto_mem *act = am;
150
151 am = am->next;
152 ecpg_free(act->pointer);
153 ecpg_free(act);
154 } while (am);
155 set_auto_allocs(NULL);
156 }
157}
158
159void
161{
162 struct auto_mem *am = get_auto_allocs();
163
164 /* only free our own structure */
165 if (am)
166 {
167 do
168 {
169 struct auto_mem *act = am;
170
171 am = am->next;
172 ecpg_free(act);
173 } while (am);
174 set_auto_allocs(NULL);
175 }
176}
#define ECPG_OUT_OF_MEMORY
Definition: ecpgerrno.h:15
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
#define realloc(a, b)
Definition: header.h:60
#define calloc(a, b)
Definition: header.h:55
#define free(a)
Definition: header.h:65
static struct auto_mem * get_auto_allocs(void)
Definition: memory.c:97
char * ecpg_strdup(const char *string, int lineno, bool *alloc_failed)
Definition: memory.c:54
char * ecpg_alloc(long size, int lineno)
Definition: memory.c:19
void ECPGfree_auto_mem(void)
Definition: memory.c:140
bool ecpg_add_mem(void *ptr, int lineno)
Definition: memory.c:126
char * ecpg_auto_alloc(long size, int lineno)
Definition: memory.c:110
void ecpg_clear_auto_mem(void)
Definition: memory.c:160
static void auto_mem_key_init(void)
Definition: memory.c:91
static pthread_key_t auto_mem_key
Definition: memory.c:80
static void auto_mem_destructor(void *arg)
Definition: memory.c:84
char * ecpg_realloc(void *ptr, long size, int lineno)
Definition: memory.c:33
static pthread_once_t auto_mem_once
Definition: memory.c:81
void ecpg_free(void *ptr)
Definition: memory.c:13
static void set_auto_allocs(struct auto_mem *am)
Definition: memory.c:104
void * arg
void pthread_setspecific(pthread_key_t key, void *val)
Definition: pthread-win32.c:24
void * pthread_getspecific(pthread_key_t key)
Definition: pthread-win32.c:29
ULONG pthread_key_t
Definition: pthread-win32.h:7
int pthread_once_t
Definition: pthread-win32.h:18
void * pointer
Definition: memory.c:76
struct auto_mem * next
Definition: memory.c:77