Skip to content

Commit

Permalink
Use defines for memory operations to simplify memory management on re…
Browse files Browse the repository at this point in the history
…stricted hardware
  • Loading branch information
Pro committed Jul 21, 2017
1 parent b72f2c5 commit 4cea3ad
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 54 deletions.
70 changes: 35 additions & 35 deletions libmdnsd/mdnsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int gettimeofday(struct timeval * tp, struct timezone * tzp)

#if defined(__MINGW32__)
static char *my_strdup(const char *s) {
char *p = (char *)malloc(strlen(s) + 1);
char *p = (char *)MDNSD_malloc(strlen(s) + 1);
if(p) { strcpy(p, s); }
return p;
}
Expand Down Expand Up @@ -310,7 +310,7 @@ static void _u_push(mdns_daemon_t *d, mdns_record_t *r, int id, unsigned long in
{
struct unicast *u;

u = (struct unicast *)calloc(1, sizeof(struct unicast));
u = (struct unicast *)MDNSD_calloc(1, sizeof(struct unicast));
u->r = r;
u->id = id;
u->to = to;
Expand Down Expand Up @@ -361,8 +361,8 @@ static void _q_done(mdns_daemon_t *d, struct query *q)
cur->next = q->next;
}

free(q->name);
free(q);
MDNSD_free(q->name);
MDNSD_free(q);
}

/* buh-bye, remove from hash and free */
Expand All @@ -378,10 +378,10 @@ static void _r_done(mdns_daemon_t *d, mdns_record_t *r)
if (cur)
cur->next = r->next;
}
free(r->rr.name);
free(r->rr.rdata);
free(r->rr.rdname);
free(r);
MDNSD_free(r->rr.name);
MDNSD_free(r->rr.rdata);
MDNSD_free(r->rr.rdname);
MDNSD_free(r);
}

/* Call the answer function with this cached entry */
Expand Down Expand Up @@ -417,10 +417,10 @@ static void _c_expire(mdns_daemon_t *d, struct cached **list)
if (cur->q)
_q_answer(d, cur);

free(cur->rr.name);
free(cur->rr.rdata);
free(cur->rr.rdname);
free(cur);
MDNSD_free(cur->rr.name);
MDNSD_free(cur->rr.rdata);
MDNSD_free(cur->rr.rdname);
MDNSD_free(cur);
} else {
last = cur;
}
Expand Down Expand Up @@ -470,19 +470,19 @@ static int _cache(mdns_daemon_t *d, struct resource *r)
* XXX: The c->rr.ttl is a hack for now, BAD SPEC, start
* retrying just after half-waypoint, then expire
*/
c = (struct cached *)calloc(1, sizeof(struct cached));
c = (struct cached *)MDNSD_calloc(1, sizeof(struct cached));
c->rr.name = STRDUP(r->name);
c->rr.type = r->type;
c->rr.ttl = (unsigned long int)d->now.tv_sec + (r->ttl / 2) + 8;
c->rr.rdlen = r->rdlength;
if (r->rdlength && !r->rdata) {
MDNSD_LOG_ERROR("rdlength is %d but rdata is NULL for domain name %s, type: %d, ttl: %ld", r->rdlength, r->name, r->type, r->ttl);
free(c->rr.name);
free(c);
MDNSD_free(c->rr.name);
MDNSD_free(c);
return 1;
}
if (r->rdlength) {
c->rr.rdata = (unsigned char *)malloc(r->rdlength);
c->rr.rdata = (unsigned char *)MDNSD_malloc(r->rdlength);
memcpy(c->rr.rdata, r->rdata, r->rdlength);
} else {
c->rr.rdata = NULL;
Expand Down Expand Up @@ -570,7 +570,7 @@ mdns_daemon_t *mdnsd_new(int clazz, int frame)
{
mdns_daemon_t *d;

d = (mdns_daemon_t *)calloc(1, sizeof(struct mdns_daemon));
d = (mdns_daemon_t *)MDNSD_calloc(1, sizeof(struct mdns_daemon));
gettimeofday(&d->now, 0);
d->expireall = (unsigned long int)(d->now.tv_sec + GC);
d->clazz = clazz;
Expand Down Expand Up @@ -617,10 +617,10 @@ void mdnsd_free(mdns_daemon_t *d)
struct cached* cur = d->cache[i];
while (cur) {
struct cached* next = cur->next;
free(cur->rr.name);
free(cur->rr.rdata);
free(cur->rr.rdname);
free(cur);
MDNSD_free(cur->rr.name);
MDNSD_free(cur->rr.rdata);
MDNSD_free(cur->rr.rdname);
MDNSD_free(cur);
cur = next;
}
}
Expand All @@ -630,19 +630,19 @@ void mdnsd_free(mdns_daemon_t *d)
struct query* curq = NULL;
while (cur) {
struct mdns_record* next = cur->next;
free(cur->rr.name);
free(cur->rr.rdata);
free(cur->rr.rdname);
free(cur);
MDNSD_free(cur->rr.name);
MDNSD_free(cur->rr.rdata);
MDNSD_free(cur->rr.rdname);
MDNSD_free(cur);
cur = next;
}


curq = d->queries[i];
while (curq) {
struct query* next = curq->next;
free(curq->name);
free(curq);
MDNSD_free(curq->name);
MDNSD_free(curq);
curq = next;
}

Expand All @@ -652,12 +652,12 @@ void mdnsd_free(mdns_daemon_t *d)
struct unicast *u = d->uanswers;
while (u) {
struct unicast *next = u->next;
free(u);
MDNSD_free(u);
u=next;
}
}

free(d);
MDNSD_free(d);
}


Expand Down Expand Up @@ -789,7 +789,7 @@ int mdnsd_out(mdns_daemon_t *d, struct message *m, unsigned long int *ip, unsign
message_an(m, u->r->rr.name, u->r->rr.type, (unsigned short int)d->clazz, u->r->rr.ttl);
u->r->last_sent = d->now;
_a_copy(m, &u->r->rr);
free(u);
MDNSD_free(u);

return 1;
}
Expand Down Expand Up @@ -1042,7 +1042,7 @@ void mdnsd_query(mdns_daemon_t *d, const char *host, int type, int (*answer)(mdn
if (!answer)
return;

q = (struct query *)calloc(1, sizeof(struct query));
q = (struct query *)MDNSD_calloc(1, sizeof(struct query));
q->name = STRDUP(host);
q->type = type;
q->next = d->queries[i];
Expand Down Expand Up @@ -1089,7 +1089,7 @@ mdns_record_t *mdnsd_shared(mdns_daemon_t *d, const char *host, unsigned short i
int i = _namehash(host) % SPRIME;
mdns_record_t *r;

r = (struct mdns_record *)calloc(1, sizeof(struct mdns_record));
r = (struct mdns_record *)MDNSD_calloc(1, sizeof(struct mdns_record));
r->rr.name = STRDUP(host);
r->rr.type = type;
r->rr.ttl = ttl;
Expand Down Expand Up @@ -1146,16 +1146,16 @@ void mdnsd_done(mdns_daemon_t *d, mdns_record_t *r)

void mdnsd_set_raw(mdns_daemon_t *d, mdns_record_t *r, const char *data, unsigned short int len)
{
free(r->rr.rdata);
r->rr.rdata = (unsigned char *)malloc(len);
MDNSD_free(r->rr.rdata);
r->rr.rdata = (unsigned char *)MDNSD_malloc(len);
memcpy(r->rr.rdata, data, len);
r->rr.rdlen = len;
_r_publish(d, r);
}

void mdnsd_set_host(mdns_daemon_t *d, mdns_record_t *r, const char *name)
{
free(r->rr.rdname);
MDNSD_free(r->rr.rdname);
r->rr.rdname = STRDUP(name);
_r_publish(d, r);
}
Expand Down
5 changes: 5 additions & 0 deletions libmdnsd/mdnsd_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#define _DARWIN_C_SOURCE 1
#endif

#define MDNSD_free(ptr) free(ptr)
#define MDNSD_malloc(size) malloc(size)
#define MDNSD_calloc(num, size) calloc(num, size)
#define MDNSD_realloc(ptr, size) realloc(ptr, size)

#define MDNSD_LOGLEVEL ${MDNSD_LOGLEVEL}

/**
Expand Down
4 changes: 2 additions & 2 deletions libmdnsd/sdtxt.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ unsigned char *sd2txt(xht_t *h, int *len)
xht_walk(h, _sd2txt_count, (void *)len);
if (!*len) {
*len = 1;
buf = (unsigned char *)malloc(1);
buf = (unsigned char *)MDNSD_malloc(1);
*buf = 0;
return buf;
}

raw = buf = (unsigned char *)malloc((size_t)(*len));
raw = buf = (unsigned char *)MDNSD_malloc((size_t)(*len));
xht_walk(h, _sd2txt_write, &buf);

return raw;
Expand Down
32 changes: 16 additions & 16 deletions libmdnsd/xht.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ xht_t *xht_new(int prime)
{
xht_t *xnew;

xnew = (xht_t *)malloc(sizeof(struct xht));
xnew = (xht_t *)MDNSD_malloc(sizeof(struct xht));
xnew->prime = prime;
xnew->zen = (xhn_t *)calloc(1, (sizeof(struct xhn) * (size_t)prime)); /* array of xhn_t size of prime */
xnew->zen = (xhn_t *)MDNSD_calloc(1, (sizeof(struct xhn) * (size_t)prime)); /* array of xhn_t size of prime */

return xnew;
}
Expand All @@ -77,24 +77,24 @@ static xhn_t *_xht_set(xht_t *h, char *key, void *val, char flag)
/* if none, make a new one, link into this index */
if (n == NULL) {
if (h->zen != NULL) {
n = (xhn_t *)malloc(sizeof(struct xhn));
n = (xhn_t *)MDNSD_malloc(sizeof(struct xhn));
n->next = NULL;
n->next = h->zen[i].next;
h->zen[i].next = n;
}
} else if (n->flag) {
/* When flag is set, we manage their mem and free em first */
free((void *)n->key);
free(n->val);
MDNSD_free((void *)n->key);
MDNSD_free(n->val);
}

if (n != NULL) {
n->flag = flag;
n->key = key;
n->val = val;
} else {
free(key);
free(val);
MDNSD_free(key);
MDNSD_free(val);
}

return n;
Expand All @@ -114,10 +114,10 @@ void xht_store(xht_t *h, char *key, int klen, void *val, int vlen)
if (h == 0 || key == 0 || klen == 0)
return;

ckey = (char *)malloc((size_t)klen + 1);
ckey = (char *)MDNSD_malloc((size_t)klen + 1);
memcpy(ckey, key, (size_t)klen);
ckey[klen] = '\0';
cval = (char *)malloc((size_t)vlen + 1);
cval = (char *)MDNSD_malloc((size_t)vlen + 1);
memcpy(cval, val, (size_t)vlen);
cval[vlen] = '\0'; /* convenience, in case it was a string too */
_xht_set(h, ckey, cval, 1);
Expand Down Expand Up @@ -147,22 +147,22 @@ void xht_free(xht_t *h)
if ((n = (&h->zen[i])) == NULL)
continue;
if (n->flag) {
free((void *)n->key);
free(n->val);
MDNSD_free((void *)n->key);
MDNSD_free(n->val);
}
for (n = (&h->zen[i])->next; n != 0;) {
f = n->next;
if (n->flag) {
free((void *)n->key);
free(n->val);
MDNSD_free((void *)n->key);
MDNSD_free(n->val);
}
free(n);
MDNSD_free(n);
n = f;
}
}

free(h->zen);
free(h);
MDNSD_free(h->zen);
MDNSD_free(h);
}

void xht_walk(xht_t *h, xht_walker w, void *arg)
Expand Down
2 changes: 1 addition & 1 deletion mdnsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ int main(int argc, char *argv[])
packet = sd2txt(h, &len);
xht_free(h);
mdnsd_set_raw(d, r, (char *)packet, len);
free(packet);
MDNSD_free(packet);

// example for getting a previously published record:
{
Expand Down

0 comments on commit 4cea3ad

Please sign in to comment.