| 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 |
{ |
| 25 |
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL); |
| 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 |
{ |
| 39 |
ecpg_raise(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 |
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 |
{ |
| 57 |
ecpg_raise(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 |
struct auto_mem |
| 66 |
{ |
| 67 |
void *pointer; |
| 68 |
struct auto_mem *next; |
| 69 |
}; |
| 70 |
|
| 71 |
static pthread_key_t auto_mem_key; |
| 72 |
static pthread_once_t auto_mem_once = PTHREAD_ONCE_INIT; |
| 73 |
|
| 74 |
static void |
| 75 |
auto_mem_destructor(void *arg) |
| 76 |
{ |
| 77 |
(void) arg; /* keep the compiler quiet */ |
| 78 |
ECPGfree_auto_mem(); |
| 79 |
} |
| 80 |
|
| 81 |
static void |
| 82 |
auto_mem_key_init(void) |
| 83 |
{ |
| 84 |
pthread_key_create(&auto_mem_key, auto_mem_destructor); |
| 85 |
} |
| 86 |
|
| 87 |
static struct auto_mem * |
| 88 |
get_auto_allocs(void) |
| 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 |
| 95 |
set_auto_allocs(struct auto_mem *am) |
| 96 |
{ |
| 97 |
pthread_setspecific(auto_mem_key, am); |
| 98 |
} |
| 99 |
|
| 100 |
char * |
| 101 |
ecpg_auto_alloc(long size, int lineno) |
| 102 |
{ |
| 103 |
void *ptr = 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 |
| 131 |
ECPGfree_auto_mem(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 |
| 151 |
ecpg_clear_auto_mem(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 |
} |