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
 

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)
 
static void auto_mem_destructor (void *arg)
 
static void auto_mem_key_init (void)
 
static struct auto_memget_auto_allocs (void)
 
static void set_auto_allocs (struct auto_mem *am)
 
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 pthread_key_t auto_mem_key
 
static pthread_once_t auto_mem_once = PTHREAD_ONCE_INIT
 

Macro Definition Documentation

◆ POSTGRES_ECPG_INTERNAL

#define POSTGRES_ECPG_INTERNAL

Definition at line 3 of file memory.c.

Function Documentation

◆ auto_mem_destructor()

static void auto_mem_destructor ( void *  arg)
static

Definition at line 75 of file memory.c.

76 {
77  (void) arg; /* keep the compiler quiet */
79 }
void ECPGfree_auto_mem(void)
Definition: memory.c:131
void * arg

References arg, and ECPGfree_auto_mem().

Referenced by auto_mem_key_init().

◆ auto_mem_key_init()

static void auto_mem_key_init ( void  )
static

Definition at line 82 of file memory.c.

83 {
84  pthread_key_create(&auto_mem_key, auto_mem_destructor);
85 }
static pthread_key_t auto_mem_key
Definition: memory.c:71
static void auto_mem_destructor(void *arg)
Definition: memory.c:75

References auto_mem_destructor(), and auto_mem_key.

Referenced by get_auto_allocs().

◆ ecpg_add_mem()

bool ecpg_add_mem ( void *  ptr,
int  lineno 
)

Definition at line 117 of file memory.c.

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 }
static struct auto_mem * get_auto_allocs(void)
Definition: memory.c:88
char * ecpg_alloc(long size, int lineno)
Definition: memory.c:19
static void set_auto_allocs(struct auto_mem *am)
Definition: memory.c:95
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
static pg_noinline void Size size
Definition: slab.c:607

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

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 101 of file memory.c.

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 }
bool ecpg_add_mem(void *ptr, int lineno)
Definition: memory.c:117
void ecpg_free(void *ptr)
Definition: memory.c:13

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

Referenced by ecpg_store_result(), and ECPGget_desc().

◆ ecpg_clear_auto_mem()

void ecpg_clear_auto_mem ( void  )

Definition at line 151 of file memory.c.

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 }

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, realloc, and size.

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 131 of file memory.c.

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 }

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

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

◆ get_auto_allocs()

static struct auto_mem* get_auto_allocs ( void  )
static

Definition at line 88 of file memory.c.

89 {
90  pthread_once(&auto_mem_once, auto_mem_key_init);
91  return (struct auto_mem *) pthread_getspecific(auto_mem_key);
92 }
static void auto_mem_key_init(void)
Definition: memory.c:82
static pthread_once_t auto_mem_once
Definition: memory.c:72
void * pthread_getspecific(pthread_key_t key)
Definition: pthread-win32.c:29

References auto_mem_key, auto_mem_key_init(), auto_mem_once, and pthread_getspecific().

Referenced by ecpg_add_mem(), ecpg_clear_auto_mem(), and ECPGfree_auto_mem().

◆ set_auto_allocs()

static void set_auto_allocs ( struct auto_mem am)
static

Definition at line 95 of file memory.c.

96 {
98 }
void pthread_setspecific(pthread_key_t key, void *val)
Definition: pthread-win32.c:24

References auto_mem_key, and pthread_setspecific().

Referenced by ecpg_add_mem(), ecpg_clear_auto_mem(), and ECPGfree_auto_mem().

Variable Documentation

◆ auto_mem_key

pthread_key_t auto_mem_key
static

Definition at line 71 of file memory.c.

Referenced by auto_mem_key_init(), get_auto_allocs(), and set_auto_allocs().

◆ auto_mem_once

pthread_once_t auto_mem_once = PTHREAD_ONCE_INIT
static

Definition at line 72 of file memory.c.

Referenced by get_auto_allocs().