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 
6 #include "ecpg-pthread-win32.h"
7 #include "ecpgerrno.h"
8 #include "ecpglib.h"
9 #include "ecpglib_extern.h"
10 #include "ecpgtype.h"
11 
12 void
13 ecpg_free(void *ptr)
14 {
15  free(ptr);
16 }
17 
18 char *
19 ecpg_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 
32 char *
33 ecpg_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 char *
47 ecpg_strdup(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  {
58  return NULL;
59  }
60 
61  return new;
62 }
63 
64 /* keep a list of memory we allocated for the user */
65 struct auto_mem
66 {
67  void *pointer;
68  struct auto_mem *next;
69 };
70 
72 static pthread_once_t auto_mem_once = PTHREAD_ONCE_INIT;
73 
74 static void
76 {
77  (void) arg; /* keep the compiler quiet */
79 }
80 
81 static void
83 {
84  pthread_key_create(&auto_mem_key, auto_mem_destructor);
85 }
86 
87 static struct auto_mem *
89 {
90  pthread_once(&auto_mem_once, auto_mem_key_init);
91  return (struct auto_mem *) pthread_getspecific(auto_mem_key);
92 }
93 
94 static void
96 {
98 }
99 
100 char *
101 ecpg_auto_alloc(long size, int lineno)
102 {
103  void *ptr = (void *) ecpg_alloc(size, lineno);
104 
105  if (!ptr)
106  return NULL;
107 
108  if (!ecpg_add_mem(ptr, lineno))
109  {
110  ecpg_free(ptr);
111  return NULL;
112  }
113  return ptr;
114 }
115 
116 bool
117 ecpg_add_mem(void *ptr, int lineno)
118 {
119  struct auto_mem *am = (struct auto_mem *) ecpg_alloc(sizeof(struct auto_mem), lineno);
120 
121  if (!am)
122  return false;
123 
124  am->pointer = ptr;
125  am->next = get_auto_allocs();
126  set_auto_allocs(am);
127  return true;
128 }
129 
130 void
132 {
133  struct auto_mem *am = get_auto_allocs();
134 
135  /* free all memory we have allocated for the user */
136  if (am)
137  {
138  do
139  {
140  struct auto_mem *act = am;
141 
142  am = am->next;
143  ecpg_free(act->pointer);
144  ecpg_free(act);
145  } while (am);
146  set_auto_allocs(NULL);
147  }
148 }
149 
150 void
152 {
153  struct auto_mem *am = get_auto_allocs();
154 
155  /* only free our own structure */
156  if (am)
157  {
158  do
159  {
160  struct auto_mem *act = am;
161 
162  am = am->next;
163  ecpg_free(act);
164  } while (am);
165  set_auto_allocs(NULL);
166  }
167 }
#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
char * ecpg_auto_alloc(long size, int lineno)
Definition: memory.c:101
void ECPGfree_auto_mem(void)
Definition: memory.c:131
bool ecpg_add_mem(void *ptr, int lineno)
Definition: memory.c:117
static struct auto_mem * get_auto_allocs(void)
Definition: memory.c:88
void ecpg_clear_auto_mem(void)
Definition: memory.c:151
static void auto_mem_key_init(void)
Definition: memory.c:82
static pthread_key_t auto_mem_key
Definition: memory.c:71
char * ecpg_alloc(long size, int lineno)
Definition: memory.c:19
static void auto_mem_destructor(void *arg)
Definition: memory.c:75
char * ecpg_strdup(const char *string, int lineno)
Definition: memory.c:47
char * ecpg_realloc(void *ptr, long size, int lineno)
Definition: memory.c:33
static pthread_once_t auto_mem_once
Definition: memory.c:72
void ecpg_free(void *ptr)
Definition: memory.c:13
static void set_auto_allocs(struct auto_mem *am)
Definition: memory.c:95
void * arg
void * pthread_getspecific(pthread_key_t key)
Definition: pthread-win32.c:29
void pthread_setspecific(pthread_key_t key, void *val)
Definition: pthread-win32.c:24
ULONG pthread_key_t
Definition: pthread-win32.h:7
int pthread_once_t
Definition: pthread-win32.h:18
static pg_noinline void Size size
Definition: slab.c:607
void * pointer
Definition: memory.c:67
struct auto_mem * next
Definition: memory.c:68