3 #define POSTGRES_ECPG_INTERNAL
4 #include "postgres_fe.h"
6 #include "ecpg-pthread-win32.h"
19 ecpg_alloc(long size
, int lineno
)
21 char *new = (char *) calloc(1L, size
);
25 ecpg_raise(lineno
, ECPG_OUT_OF_MEMORY
, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
, NULL
);
33 ecpg_realloc(void *ptr
, long size
, int lineno
)
35 char *new = (char *) realloc(ptr
, size
);
39 ecpg_raise(lineno
, ECPG_OUT_OF_MEMORY
, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
, NULL
);
47 ecpg_strdup(const char *string
, int lineno
)
57 ecpg_raise(lineno
, ECPG_OUT_OF_MEMORY
, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
, NULL
);
64 /* keep a list of memory we allocated for the user */
68 struct auto_mem
*next
;
71 #ifdef ENABLE_THREAD_SAFETY
72 static pthread_key_t auto_mem_key
;
73 static pthread_once_t auto_mem_once
= PTHREAD_ONCE_INIT
;
76 auto_mem_destructor(void *arg
)
82 auto_mem_key_init(void)
84 pthread_key_create(&auto_mem_key
, auto_mem_destructor
);
87 static struct auto_mem
*
90 pthread_once(&auto_mem_once
, auto_mem_key_init
);
91 return (struct auto_mem
*) pthread_getspecific(auto_mem_key
);
95 set_auto_allocs(struct auto_mem
* am
)
97 pthread_setspecific(auto_mem_key
, am
);
100 static struct auto_mem
*auto_allocs
= NULL
;
102 #define get_auto_allocs() (auto_allocs)
103 #define set_auto_allocs(am) do { auto_allocs = (am); } while(0)
107 ecpg_add_mem(void *ptr
, int lineno
)
109 struct auto_mem
*am
= (struct auto_mem
*) ecpg_alloc(sizeof(struct auto_mem
), lineno
);
112 am
->next
= get_auto_allocs();
117 ECPGfree_auto_mem(void)
119 struct auto_mem
*am
= get_auto_allocs();
121 /* free all memory we have allocated for the user */
126 struct auto_mem
*act
= am
;
129 ecpg_free(act
->pointer
);
132 set_auto_allocs(NULL
);
137 ecpg_clear_auto_mem(void)
139 struct auto_mem
*am
= get_auto_allocs();
141 /* only free our own structure */
146 struct auto_mem
*act
= am
;
151 set_auto_allocs(NULL
);