PostgreSQL Source Code  git master
memory.c File Reference
#include "postgres_fe.h"
#include "ecpg-pthread-win32.h"
#include "ecpgerrno.h"
#include "ecpglib.h"
#include "ecpglib_extern.h"
#include "ecpgtype.h"
Include dependency graph for memory.c:

Go to the source code of this file.

Data Structures

struct  auto_mem
 

Macros

#define POSTGRES_ECPG_INTERNAL
 
#define get_auto_allocs()   (auto_allocs)
 
#define set_auto_allocs(am)   do { auto_allocs = (am); } while(0)
 

Functions

void ecpg_free (void *ptr)
 
char * ecpg_alloc (long size, int lineno)
 
char * ecpg_realloc (void *ptr, long size, int lineno)
 
char * ecpg_strdup (const char *string, int lineno)
 
char * ecpg_auto_alloc (long size, int lineno)
 
bool ecpg_add_mem (void *ptr, int lineno)
 
void ECPGfree_auto_mem (void)
 
void ecpg_clear_auto_mem (void)
 

Variables

static struct auto_memauto_allocs = NULL
 

Macro Definition Documentation

◆ get_auto_allocs

#define get_auto_allocs ( )    (auto_allocs)

Definition at line 103 of file memory.c.

◆ POSTGRES_ECPG_INTERNAL

#define POSTGRES_ECPG_INTERNAL

Definition at line 3 of file memory.c.

◆ set_auto_allocs

#define set_auto_allocs (   am)    do { auto_allocs = (am); } while(0)

Definition at line 104 of file memory.c.

Function Documentation

◆ ecpg_add_mem()

bool ecpg_add_mem ( void *  ptr,
int  lineno 
)

Definition at line 124 of file memory.c.

125 {
126  struct auto_mem *am = (struct auto_mem *) ecpg_alloc(sizeof(struct auto_mem), lineno);
127 
128  if (!am)
129  return false;
130 
131  am->pointer = ptr;
132  am->next = get_auto_allocs();
133  set_auto_allocs(am);
134  return true;
135 }
#define set_auto_allocs(am)
Definition: memory.c:104
#define get_auto_allocs()
Definition: memory.c:103
char * ecpg_alloc(long size, int lineno)
Definition: memory.c:19
void * pointer
Definition: memory.c:67
struct auto_mem * next
Definition: memory.c:68

References ecpg_alloc(), get_auto_allocs, auto_mem::next, auto_mem::pointer, and set_auto_allocs.

Referenced by ecpg_auto_alloc().

◆ ecpg_alloc()

char* ecpg_alloc ( long  size,
int  lineno 
)

Definition at line 19 of file memory.c.

20 {
21  char *new = (char *) calloc(1L, size);
22 
23  if (!new)
24  {
26  return NULL;
27  }
28 
29  return new;
30 }
#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 calloc(a, b)
Definition: header.h:55

References calloc, ECPG_OUT_OF_MEMORY, ecpg_raise(), and ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY.

Referenced by AddStmtToCache(), convert_bytea_to_string(), deallocate_one(), ecpg_add_mem(), ecpg_auto_alloc(), ecpg_build_compat_sqlda(), ecpg_build_native_sqlda(), ecpg_build_params(), ecpg_do_prologue(), ecpg_get_data(), ecpg_is_type_an_array(), ecpg_register_prepared_stmt(), ecpg_store_input(), ECPGallocate_desc(), ECPGconnect(), ECPGset_desc(), insert_tobeinserted(), prepare_common(), print_param_value(), quote_postgres(), replace_variables(), and store_input_from_desc().

◆ ecpg_auto_alloc()

char* ecpg_auto_alloc ( long  size,
int  lineno 
)

Definition at line 108 of file memory.c.

109 {
110  void *ptr = (void *) ecpg_alloc(size, lineno);
111 
112  if (!ptr)
113  return NULL;
114 
115  if (!ecpg_add_mem(ptr, lineno))
116  {
117  ecpg_free(ptr);
118  return NULL;
119  }
120  return ptr;
121 }
bool ecpg_add_mem(void *ptr, int lineno)
Definition: memory.c:124
void ecpg_free(void *ptr)
Definition: memory.c:13

References ecpg_add_mem(), ecpg_alloc(), and ecpg_free().

Referenced by ecpg_store_result(), and ECPGget_desc().

◆ ecpg_clear_auto_mem()

void ecpg_clear_auto_mem ( void  )

Definition at line 158 of file memory.c.

159 {
160  struct auto_mem *am = get_auto_allocs();
161 
162  /* only free our own structure */
163  if (am)
164  {
165  do
166  {
167  struct auto_mem *act = am;
168 
169  am = am->next;
170  ecpg_free(act);
171  } while (am);
172  set_auto_allocs(NULL);
173  }
174 }

References ecpg_free(), get_auto_allocs, auto_mem::next, and set_auto_allocs.

Referenced by ecpg_do_prologue(), and ECPGconnect().

◆ ecpg_free()

◆ ecpg_realloc()

char* ecpg_realloc ( void *  ptr,
long  size,
int  lineno 
)

Definition at line 33 of file memory.c.

34 {
35  char *new = (char *) realloc(ptr, size);
36 
37  if (!new)
38  {
40  return NULL;
41  }
42 
43  return new;
44 }
#define realloc(a, b)
Definition: header.h:60

References ECPG_OUT_OF_MEMORY, ecpg_raise(), ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, and realloc.

Referenced by ecpg_build_params(), and ecpg_store_input().

◆ ecpg_strdup()

char* ecpg_strdup ( const char *  string,
int  lineno 
)

Definition at line 47 of file memory.c.

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 }

References ECPG_OUT_OF_MEMORY, ecpg_raise(), and ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY.

Referenced by AddStmtToCache(), ecpg_auto_prepare(), ecpg_do_prologue(), ecpg_register_prepared_stmt(), ecpg_store_input(), ECPGconnect(), ECPGget_desc(), and prepare_common().

◆ ECPGfree_auto_mem()

void ECPGfree_auto_mem ( void  )

Definition at line 138 of file memory.c.

139 {
140  struct auto_mem *am = get_auto_allocs();
141 
142  /* free all memory we have allocated for the user */
143  if (am)
144  {
145  do
146  {
147  struct auto_mem *act = am;
148 
149  am = am->next;
150  ecpg_free(act->pointer);
151  ecpg_free(act);
152  } while (am);
153  set_auto_allocs(NULL);
154  }
155 }

References ecpg_free(), get_auto_allocs, auto_mem::next, auto_mem::pointer, and set_auto_allocs.

Referenced by ecpg_raise(), ecpg_raise_backend(), ECPGset_var(), and main().

Variable Documentation

◆ auto_allocs

struct auto_mem* auto_allocs = NULL
static

Definition at line 101 of file memory.c.