getdns_strdup, getdns_bindata_(copy|destroy)

Restructure a bit to make that work too
This commit is contained in:
Willem Toorop 2013-12-09 00:05:18 +01:00
parent 70e5193b3f
commit c80cab6929
8 changed files with 102 additions and 101 deletions

View File

@ -74,7 +74,7 @@ static void cancel_dns_req(getdns_dns_req *);
static uint16_t *
create_default_namespaces(struct getdns_context *context)
{
uint16_t *result = GETDNS_XMALLOC(&context->my_mf, uint16_t, 2);
uint16_t *result = GETDNS_XMALLOC(context->my_mf, uint16_t, 2);
result[0] = GETDNS_CONTEXT_NAMESPACE_LOCALNAMES;
result[1] = GETDNS_CONTEXT_NAMESPACE_DNS;
return result;
@ -263,10 +263,10 @@ getdns_context_create_with_memory_functions(struct getdns_context ** context,
result->update_callback = NULL;
result->mf_arg = MF_PLAIN;
result->mf.pln.malloc = malloc;
result->mf.pln.realloc = realloc;
result->mf.pln.free = free;
result->mf.mf_arg = MF_PLAIN;
result->mf.mf.pln.malloc = malloc;
result->mf.mf.pln.realloc = realloc;
result->mf.mf.pln.free = free;
result->event_base_sync = event_base_new();
result->unbound_sync = ub_ctx_create_event(result->event_base_sync);
@ -338,7 +338,7 @@ getdns_context_destroy(struct getdns_context *context)
return;
}
if (context->namespaces)
GETDNS_FREE(&context->my_mf, context->namespaces);
GETDNS_FREE(context->my_mf, context->namespaces);
getdns_list_destroy(context->dns_root_servers);
getdns_list_destroy(context->suffix);
@ -353,7 +353,7 @@ getdns_context_destroy(struct getdns_context *context)
ldns_rbtree_free(context->outbound_requests);
GETDNS_FREE(&context->my_mf, context);
GETDNS_FREE(context->my_mf, context);
return;
} /* getdns_context_destroy */
@ -454,10 +454,10 @@ getdns_context_set_namespaces(struct getdns_context *context,
}
/** clean up old namespaces **/
GETDNS_FREE(&context->my_mf, context->namespaces);
GETDNS_FREE(context->my_mf, context->namespaces);
/** duplicate **/
context->namespaces = GETDNS_XMALLOC(&context->my_mf, uint16_t,
context->namespaces = GETDNS_XMALLOC(context->my_mf, uint16_t,
namespace_count);
memcpy(context->namespaces, namespaces,
namespace_count * sizeof(uint16_t));
@ -808,10 +808,10 @@ getdns_context_set_memory_functions(struct getdns_context *context,
if (!malloc || !realloc || !free)
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
context->mf_arg = MF_PLAIN;
context->mf.pln.malloc = malloc;
context->mf.pln.realloc = realloc;
context->mf.pln.free = free;
context->mf.mf_arg = MF_PLAIN;
context->mf.mf.pln.malloc = malloc;
context->mf.mf.pln.realloc = realloc;
context->mf.mf.pln.free = free;
dispatch_updated(context, GETDNS_CONTEXT_CODE_MEMORY_FUNCTIONS);
@ -883,7 +883,7 @@ getdns_context_cancel_request(struct getdns_context *context,
user_pointer = req->user_pointer;
/* clean up */
GETDNS_FREE(&context->my_mf, node);
GETDNS_FREE(context->my_mf, node);
dns_req_free(req);
/* fire callback */
@ -968,7 +968,7 @@ getdns_context_track_outbound_request(getdns_dns_req * req)
return GETDNS_RETURN_GENERIC_ERROR;
}
struct getdns_context *context = req->context;
ldns_rbnode_t *node = GETDNS_MALLOC(&context->my_mf, ldns_rbnode_t);
ldns_rbnode_t *node = GETDNS_MALLOC(context->my_mf, ldns_rbnode_t);
if (!node) {
return GETDNS_RETURN_GENERIC_ERROR;
}
@ -976,7 +976,7 @@ getdns_context_track_outbound_request(getdns_dns_req * req)
node->data = req;
if (!ldns_rbtree_insert(context->outbound_requests, node)) {
/* free the node */
GETDNS_FREE(&context->my_mf, node);
GETDNS_FREE(context->my_mf, node);
return GETDNS_RETURN_GENERIC_ERROR;
}
return GETDNS_RETURN_GOOD;
@ -992,16 +992,16 @@ getdns_context_clear_outbound_request(getdns_dns_req * req)
ldns_rbnode_t *node = ldns_rbtree_delete(context->outbound_requests,
&(req->trans_id));
if (node) {
GETDNS_FREE(&context->my_mf, node);
GETDNS_FREE(context->my_mf, node);
}
return GETDNS_RETURN_GOOD;
}
char *
getdns_strdup(void *(*malloc)(size_t), const char *s)
getdns_strdup(struct mem_funcs *mfs, const char *s)
{
size_t sz = strlen(s) + 1;
char *r = (char *)(*malloc)(sizeof(char) * sz);
char *r = GETDNS_XMALLOC(*mfs, char, sz);
if (r)
return memcpy(r, s, sz);
else
@ -1009,7 +1009,7 @@ getdns_strdup(void *(*malloc)(size_t), const char *s)
}
struct getdns_bindata *
getdns_bindata_copy(void *(*malloc)(size_t), void (*free)(void *),
getdns_bindata_copy(struct mem_funcs *mfs,
const struct getdns_bindata *src)
{
struct getdns_bindata *dst;
@ -1017,14 +1017,14 @@ getdns_bindata_copy(void *(*malloc)(size_t), void (*free)(void *),
if (!src)
return NULL;
dst = (struct getdns_bindata *)(*malloc)(sizeof(struct getdns_bindata));
dst = GETDNS_MALLOC(*mfs, struct getdns_bindata);
if (!dst)
return NULL;
dst->size = src->size;
dst->data = (uint8_t *)(*malloc)(sizeof(uint8_t) * src->size);
dst->data = GETDNS_XMALLOC(*mfs, uint8_t, src->size);
if (!dst->data) {
(*free)(dst);
GETDNS_FREE(*mfs, dst);
return NULL;
}
(void) memcpy(dst->data, src->data, src->size);
@ -1032,11 +1032,12 @@ getdns_bindata_copy(void *(*malloc)(size_t), void (*free)(void *),
}
void
getdns_bindata_destroy(void (*free)(void *), struct getdns_bindata *bindata)
getdns_bindata_destroy(struct mem_funcs *mfs,
struct getdns_bindata *bindata)
{
if (!bindata)
return;
(*free)(bindata->data);
(*free)(bindata);
GETDNS_FREE(*mfs, bindata->data);
GETDNS_FREE(*mfs, bindata);
}
/* getdns_context.c */

View File

@ -59,13 +59,9 @@ struct getdns_context {
uint8_t edns_do_bit;
getdns_update_callback update_callback;
void *mf_arg;
mem_funcs mf;
struct {
void *mf_arg;
mem_funcs mf;
} my_mf;
struct mem_funcs mf;
struct mem_funcs my_mf;
/* Event loop for sync requests */
struct event_base *event_base_sync;
@ -105,13 +101,14 @@ getdns_return_t getdns_context_clear_outbound_request(struct getdns_dns_req
getdns_return_t getdns_context_cancel_request(struct getdns_context *context,
getdns_transaction_t transaction_id, int fire_callback);
char *getdns_strdup(void *(*malloc)(size_t), const char *str);
char *getdns_strdup(struct mem_funcs *mfs, const char *str);
struct getdns_bindata *getdns_bindata_copy(
void *(*malloc)(size_t), void (*free)(void *),
struct mem_funcs *mfs,
const struct getdns_bindata *src);
void getdns_bindata_destroy(void (*free)(void *),
void getdns_bindata_destroy(
struct mem_funcs *mfs,
struct getdns_bindata *bindata);
#endif /* _GETDNS_CONTEXT_H_ */

View File

@ -63,8 +63,8 @@ getdns_dict_find(struct getdns_dict *dict, char *key, int addifnotfnd)
if (!item && addifnotfnd) {
/* tsearch will add a node automatically for us */
item = GETDNS_MALLOC(dict, struct getdns_dict_item);
item->node.key = getdns_strdup(dict->mf.pln.malloc, key);
item = GETDNS_MALLOC(dict->mf, struct getdns_dict_item);
item->node.key = getdns_strdup(&dict->mf, key);
item->data.n = 0;
ldns_rbtree_insert(&(dict->root), (ldns_rbnode_t *) item);
}
@ -84,7 +84,7 @@ getdns_dict_get_names(struct getdns_dict * dict, struct getdns_list ** answer)
return GETDNS_RETURN_NO_SUCH_DICT_NAME;
*answer = getdns_list_create_with_memory_functions(
dict->mf.pln.malloc, dict->mf.pln.realloc, dict->mf.pln.free);
dict->mf.mf.pln.malloc, dict->mf.mf.pln.realloc, dict->mf.mf.pln.free);
if (!*answer)
return GETDNS_RETURN_NO_SUCH_DICT_NAME;
@ -200,10 +200,10 @@ getdns_dict_create_with_memory_functions(void *(*malloc)(size_t),
if (!dict)
return NULL;
dict->mf_arg = MF_PLAIN;
dict->mf.pln.malloc = malloc;
dict->mf.pln.realloc = realloc;
dict->mf.pln.free = free;
dict->mf.mf_arg = MF_PLAIN;
dict->mf.mf.pln.malloc = malloc;
dict->mf.mf.pln.realloc = realloc;
dict->mf.mf.pln.free = free;
ldns_rbtree_init(&(dict->root),
(int (*)(const void *, const void *)) strcmp);
@ -217,8 +217,8 @@ getdns_dict_create_with_context(struct getdns_context *context)
{
if (context)
return getdns_dict_create_with_memory_functions(
context->mf.pln.malloc,
context->mf.pln.realloc, context->mf.pln.free);
context->mf.mf.pln.malloc,
context->mf.mf.pln.realloc, context->mf.mf.pln.free);
else
return getdns_dict_create_with_memory_functions(&malloc,
&realloc, &free);
@ -255,7 +255,9 @@ getdns_dict_copy(struct getdns_dict * srcdict, struct getdns_dict ** dstdict)
return GETDNS_RETURN_GOOD;
}
*dstdict = getdns_dict_create_with_memory_functions(
srcdict->mf.pln.malloc, srcdict->mf.pln.realloc, srcdict->mf.pln.free);
srcdict->mf.mf.pln.malloc,
srcdict->mf.mf.pln.realloc,
srcdict->mf.mf.pln.free);
if (!*dstdict)
return GETDNS_RETURN_NO_SUCH_DICT_NAME;
@ -309,7 +311,7 @@ getdns_dict_item_free(ldns_rbnode_t * node, void *arg)
switch (item->dtype) {
case t_bindata:
getdns_bindata_destroy(dict->mf.pln.free, item->data.bindata);
getdns_bindata_destroy(&dict->mf, item->data.bindata);
break;
case t_dict:
getdns_dict_destroy(item->data.dict);
@ -321,8 +323,8 @@ getdns_dict_item_free(ldns_rbnode_t * node, void *arg)
break;
}
if (item->node.key)
GETDNS_FREE(dict, (void *)item->node.key);
GETDNS_FREE(dict, item);
GETDNS_FREE(dict->mf, (void *)item->node.key);
GETDNS_FREE(dict->mf, item);
} /* getdns_dict_item_free */
/*---------------------------------------- getdns_dict_destroy */
@ -334,7 +336,7 @@ getdns_dict_destroy(struct getdns_dict *dict)
ldns_traverse_postorder(&(dict->root),
getdns_dict_item_free, dict);
GETDNS_FREE(dict, dict);
GETDNS_FREE(dict->mf, dict);
} /* getdns_dict_destroy */
/*---------------------------------------- getdns_dict_set_dict */
@ -400,14 +402,13 @@ getdns_dict_set_bindata(struct getdns_dict * dict, char *name,
if (!dict || !name || !child_bindata)
return GETDNS_RETURN_NO_SUCH_DICT_NAME;
newbindata = getdns_bindata_copy(
dict->mf.pln.malloc, dict->mf.pln.free, child_bindata);
newbindata = getdns_bindata_copy(&dict->mf, child_bindata);
if (!newbindata)
return GETDNS_RETURN_NO_SUCH_DICT_NAME;
item = getdns_dict_find(dict, name, 1);
if (!item) {
getdns_bindata_destroy(dict->mf.pln.free, newbindata);
getdns_bindata_destroy(&dict->mf, newbindata);
return GETDNS_RETURN_NO_SUCH_DICT_NAME;
}
item->dtype = t_bindata;

View File

@ -67,8 +67,7 @@ struct getdns_dict_item
struct getdns_dict
{
ldns_rbtree_t root;
void *mf_arg;
mem_funcs mf;
struct mem_funcs mf;
};
#endif

View File

@ -138,7 +138,7 @@ getdns_list_realloc(struct getdns_list *list)
if (!list)
return GETDNS_RETURN_GENERIC_ERROR;
newlist = GETDNS_XREALLOC(list, list->items,
newlist = GETDNS_XREALLOC(list->mf, list->items,
struct getdns_list_item,
list->numalloc + GETDNS_LIST_BLOCKSZ);
if (!newlist)
@ -165,7 +165,10 @@ getdns_list_copy(struct getdns_list * srclist, struct getdns_list ** dstlist)
return GETDNS_RETURN_GOOD;
}
*dstlist = getdns_list_create_with_memory_functions(
srclist->mf.pln.malloc, srclist->mf.pln.realloc, srclist->mf.pln.free);
srclist->mf.mf.pln.malloc,
srclist->mf.mf.pln.realloc,
srclist->mf.mf.pln.free
);
if (!dstlist)
return GETDNS_RETURN_NO_SUCH_LIST_ITEM;
@ -219,10 +222,10 @@ getdns_list_create_with_memory_functions(void *(*malloc)(size_t),
if (!list)
return NULL;
list->mf_arg = MF_PLAIN;
list->mf.pln.malloc = malloc;
list->mf.pln.realloc = realloc;
list->mf.pln.free = free;
list->mf.mf_arg = MF_PLAIN;
list->mf.mf.pln.malloc = malloc;
list->mf.mf.pln.realloc = realloc;
list->mf.mf.pln.free = free;
list->numalloc = 0;
list->numinuse = 0;
@ -240,8 +243,10 @@ getdns_list_create_with_context(struct getdns_context *context)
{
if (context)
return getdns_list_create_with_memory_functions(
context->mf.pln.malloc,
context->mf.pln.realloc, context->mf.pln.free);
context->mf.mf.pln.malloc,
context->mf.mf.pln.realloc,
context->mf.mf.pln.free
);
else
return getdns_list_create_with_memory_functions(malloc,
realloc, free);
@ -274,7 +279,7 @@ getdns_list_destroy(struct getdns_list *list)
break;
case t_bindata:
getdns_bindata_destroy(list->mf.pln.free,
getdns_bindata_destroy(&list->mf,
list->items[i].data.bindata);
break;
@ -284,8 +289,8 @@ getdns_list_destroy(struct getdns_list *list)
}
if (list->items)
GETDNS_FREE(list, list->items);
GETDNS_FREE(list, list);
GETDNS_FREE(list->mf, list->items);
GETDNS_FREE(list->mf, list);
} /* getdns_list_destroy */
/*---------------------------------------- getdns_list_add_item */
@ -377,8 +382,7 @@ getdns_list_set_bindata(struct getdns_list * list, size_t index,
if (index >= list->numinuse)
return GETDNS_RETURN_NO_SUCH_LIST_ITEM;
newbindata = getdns_bindata_copy(
list->mf.pln.malloc, list->mf.pln.free, child_bindata);
newbindata = getdns_bindata_copy(&list->mf, child_bindata);
if (!newbindata)
return GETDNS_RETURN_NO_SUCH_LIST_ITEM;

View File

@ -64,8 +64,7 @@ struct getdns_list
int numalloc;
int numinuse;
struct getdns_list_item *items;
void *mf_arg;
mem_funcs mf;
struct mem_funcs mf;
};
#endif

View File

@ -54,7 +54,7 @@ network_req_free(getdns_network_req * net_req)
if (net_req->result) {
ldns_pkt_free(net_req->result);
}
GETDNS_FREE(context, net_req);
GETDNS_FREE(context->mf, net_req);
}
getdns_network_req *
@ -64,7 +64,7 @@ network_req_new(getdns_dns_req * owner,
{
struct getdns_context *context = owner->context;
getdns_network_req *net_req = GETDNS_MALLOC( context
getdns_network_req *net_req = GETDNS_MALLOC( context->mf
, getdns_network_req);
if (!net_req) {
return NULL;
@ -117,7 +117,7 @@ dns_req_free(getdns_dns_req * req)
/* free strduped name */
free(req->name);
GETDNS_FREE(context, req);
GETDNS_FREE(context->mf, req);
}
/* create a new dns req to be submitted */
@ -132,7 +132,7 @@ dns_req_new(struct getdns_context *context,
getdns_return_t r;
uint32_t both = GETDNS_EXTENSION_FALSE;
result = GETDNS_MALLOC(context, getdns_dns_req);
result = GETDNS_MALLOC(context->mf, getdns_dns_req);
if (result == NULL) {
return NULL;
}

View File

@ -179,40 +179,40 @@ typedef struct getdns_dns_req
#define MF_PLAIN ((void *)&plain_mem_funcs_user_arg)
extern void *plain_mem_funcs_user_arg;
typedef union {
struct {
void *(*malloc)(size_t);
void *(*realloc)(void *, size_t);
void (*free)(void *);
} pln;
struct {
void *(*malloc)(void *userarg, size_t);
void *(*realloc)(void *userarg, void *, size_t);
void (*free)(void *userarg, void *);
} ext;
} mem_funcs;
struct mem_funcs {
void *mf_arg;
union {
struct {
void *(*malloc)(size_t);
void *(*realloc)(void *, size_t);
void (*free)(void *);
} pln;
struct {
void *(*malloc)(void *userarg, size_t);
void *(*realloc)(void *userarg, void *, size_t);
void (*free)(void *userarg, void *);
} ext;
} mf;
};
#define GETDNS_XMALLOC(obj, type, count) \
((!(obj)) ? ((type *)malloc((count)*sizeof(type))) \
: ((obj)->mf_arg == MF_PLAIN \
? ((type *)(*(obj)->mf.pln.malloc)( (count)*sizeof(type))) \
: ((type *)(*(obj)->mf.ext.malloc)((obj)->mf_arg, (count)*sizeof(type))) \
))
((obj).mf_arg == MF_PLAIN \
? ((type *)(*(obj).mf.pln.malloc)( (count)*sizeof(type))) \
: ((type *)(*(obj).mf.ext.malloc)((obj).mf_arg, (count)*sizeof(type))) \
)
#define GETDNS_XREALLOC(obj, ptr, type, count) \
((!(obj)) ? ((type *)realloc((ptr), (count)*sizeof(type))) \
: ((obj)->mf_arg == MF_PLAIN \
? ((type *)(*(obj)->mf.pln.realloc)( (ptr), (count)*sizeof(type))) \
: ((type *)(*(obj)->mf.ext.realloc)( (obj)->mf_arg \
, (ptr), (count)*sizeof(type))) \
))
((obj).mf_arg == MF_PLAIN \
? ((type *)(*(obj).mf.pln.realloc)( (ptr), (count)*sizeof(type))) \
: ((type *)(*(obj).mf.ext.realloc)( (obj).mf_arg \
, (ptr), (count)*sizeof(type))) \
)
#define GETDNS_FREE(obj, ptr) \
((!(obj)) ? free(ptr) \
: ((obj)->mf_arg == MF_PLAIN \
? ((*(obj)->mf.pln.free)( (ptr))) \
: ((*(obj)->mf.ext.free)((obj)->mf_arg, (ptr))) \
))
((obj).mf_arg == MF_PLAIN \
? ((*(obj).mf.pln.free)( (ptr))) \
: ((*(obj).mf.ext.free)((obj).mf_arg, (ptr))) \
)
#define GETDNS_MALLOC(obj, type) GETDNS_XMALLOC(obj, type, 1)
#define GETDNS_REALLOC(obj, ptr, type) GETDNS_XREALLOC(obj, ptr, type, 1);