/* allocate storage if needed */
if (arrsize == 0 && *(void **) var == NULL)
{
- void *mem = (void *) ecpg_alloc(offset * ntuples, lineno);
+ void *mem = (void *) ecpg_auto_alloc(offset * ntuples, lineno);
if (!mem)
return false;
*(void **) var = mem;
- ecpg_add_mem(mem, lineno);
var = mem;
}
/* allocate storage if needed */
if (data_var.ind_arrsize == 0 && data_var.ind_value == NULL)
{
- void *mem = (void *) ecpg_alloc(data_var.ind_offset * ntuples, lineno);
+ void *mem = (void *) ecpg_auto_alloc(data_var.ind_offset * ntuples, lineno);
if (!mem)
return false;
*(void **) data_var.ind_pointer = mem;
- ecpg_add_mem(mem, lineno);
data_var.ind_value = mem;
}
}
ecpg_log("ecpg_store_result on line %d: allocating memory for %d tuples\n", stmt->lineno, ntuples);
- var->value = (char *) ecpg_alloc(len, stmt->lineno);
+ var->value = (char *) ecpg_auto_alloc(len, stmt->lineno);
if (!var->value)
return false;
*((char **) var->pointer) = var->value;
- ecpg_add_mem(var->value, stmt->lineno);
}
/* allocate indicator variable if needed */
{
int len = var->ind_offset * ntuples;
- var->ind_value = (char *) ecpg_alloc(len, stmt->lineno);
+ var->ind_value = (char *) ecpg_auto_alloc(len, stmt->lineno);
if (!var->ind_value)
return false;
*((char **) var->ind_pointer) = var->ind_value;
- ecpg_add_mem(var->ind_value, stmt->lineno);
}
/* fill the variable with the tuple(s) */
/* Here are some methods used by the lib. */
-/* Returns a pointer to a string containing a simple type name. */
-void ecpg_add_mem(void *ptr, int lineno);
+bool ecpg_add_mem(void *ptr, int lineno);
bool ecpg_get_data(const PGresult *, int, int, int, enum ECPGttype type,
enum ECPGttype, char *, char *, long, long, long,
#endif
struct connection *ecpg_get_connection(const char *);
char *ecpg_alloc(long, int);
+char *ecpg_auto_alloc(long, int);
char *ecpg_realloc(void *, long, int);
void ecpg_free(void *);
bool ecpg_init(const struct connection *, const char *, const int);
#define set_auto_allocs(am) do { auto_allocs = (am); } while(0)
#endif
-void
+char *
+ecpg_auto_alloc(long size, int lineno)
+{
+ void *ptr = (void *) ecpg_alloc(size, lineno);
+
+ if (!ptr)
+ return NULL;
+
+ if (!ecpg_add_mem(ptr, lineno))
+ {
+ ecpg_free(ptr);
+ return NULL;
+ }
+ return ptr;
+}
+
+bool
ecpg_add_mem(void *ptr, int lineno)
{
struct auto_mem *am = (struct auto_mem *) ecpg_alloc(sizeof(struct auto_mem), lineno);
+ if (!am)
+ return false;
+
am->pointer = ptr;
am->next = get_auto_allocs();
set_auto_allocs(am);
+ return true;
}
void