Finesse allocation of memory for "struct crec" cache entries.

These normally have enough space for a name of up to SMALLDNAME characters.
When used to hold /etc/hosts entries, they are allocated with just enough
bytes for the name held. When used to hold other configured stuff, (CNAMES
DS records. DHCP names etc), the name is replaced by a pointer to a string
held elsewhere, and F_NAMEP set. Hence only enough space to hold a char *
is needed, rather than SMALLDNAME bytes.
This commit is contained in:
Simon Kelley
2018-09-26 16:50:35 +01:00
parent 48b090cb5c
commit 3a610a007f
2 changed files with 13 additions and 11 deletions

View File

@@ -825,7 +825,7 @@ static void add_hosts_cname(struct crec *target)
for (a = daemon->cnames; a; a = a->next) for (a = daemon->cnames; a; a = a->next)
if (a->alias[1] != '*' && if (a->alias[1] != '*' &&
hostname_isequal(cache_get_name(target), a->target) && hostname_isequal(cache_get_name(target), a->target) &&
(crec = whine_malloc(sizeof(struct crec)))) (crec = whine_malloc(SIZEOF_POINTER_CREC)))
{ {
crec->flags = F_FORWARD | F_IMMORTAL | F_NAMEP | F_CONFIG | F_CNAME; crec->flags = F_FORWARD | F_IMMORTAL | F_NAMEP | F_CONFIG | F_CNAME;
crec->ttd = a->ttl; crec->ttd = a->ttl;
@@ -1029,8 +1029,7 @@ int read_hostsfile(char *filename, unsigned int index, int cache_size, struct cr
{ {
/* If set, add a version of the name with a default domain appended */ /* If set, add a version of the name with a default domain appended */
if (option_bool(OPT_EXPAND) && domain_suffix && !fqdn && if (option_bool(OPT_EXPAND) && domain_suffix && !fqdn &&
(cache = whine_malloc(sizeof(struct crec) + (cache = whine_malloc(SIZEOF_BARE_CREC + strlen(canon) + 2 + strlen(domain_suffix))))
strlen(canon)+2+strlen(domain_suffix)-SMALLDNAME)))
{ {
strcpy(cache->name.sname, canon); strcpy(cache->name.sname, canon);
strcat(cache->name.sname, "."); strcat(cache->name.sname, ".");
@@ -1040,7 +1039,7 @@ int read_hostsfile(char *filename, unsigned int index, int cache_size, struct cr
add_hosts_entry(cache, &addr, addrlen, index, rhash, hashsz); add_hosts_entry(cache, &addr, addrlen, index, rhash, hashsz);
name_count++; name_count++;
} }
if ((cache = whine_malloc(sizeof(struct crec) + strlen(canon)+1-SMALLDNAME))) if ((cache = whine_malloc(SIZEOF_BARE_CREC + strlen(canon) + 1)))
{ {
strcpy(cache->name.sname, canon); strcpy(cache->name.sname, canon);
cache->flags = flags; cache->flags = flags;
@@ -1113,7 +1112,7 @@ void cache_reload(void)
for (intr = daemon->int_names; intr; intr = intr->next) for (intr = daemon->int_names; intr; intr = intr->next)
if (a->alias[1] != '*' && if (a->alias[1] != '*' &&
hostname_isequal(a->target, intr->name) && hostname_isequal(a->target, intr->name) &&
((cache = whine_malloc(sizeof(struct crec))))) ((cache = whine_malloc(SIZEOF_POINTER_CREC))))
{ {
cache->flags = F_FORWARD | F_NAMEP | F_CNAME | F_IMMORTAL | F_CONFIG; cache->flags = F_FORWARD | F_NAMEP | F_CNAME | F_IMMORTAL | F_CONFIG;
cache->ttd = a->ttl; cache->ttd = a->ttl;
@@ -1128,7 +1127,7 @@ void cache_reload(void)
#ifdef HAVE_DNSSEC #ifdef HAVE_DNSSEC
for (ds = daemon->ds; ds; ds = ds->next) for (ds = daemon->ds; ds; ds = ds->next)
if ((cache = whine_malloc(sizeof(struct crec))) && if ((cache = whine_malloc(SIZEOF_POINTER_CREC)) &&
(cache->addr.ds.keydata = blockdata_alloc(ds->digest, ds->digestlen))) (cache->addr.ds.keydata = blockdata_alloc(ds->digest, ds->digestlen)))
{ {
cache->flags = F_FORWARD | F_IMMORTAL | F_DS | F_CONFIG | F_NAMEP; cache->flags = F_FORWARD | F_IMMORTAL | F_DS | F_CONFIG | F_NAMEP;
@@ -1155,7 +1154,7 @@ void cache_reload(void)
for (nl = hr->names; nl; nl = nl->next) for (nl = hr->names; nl; nl = nl->next)
{ {
if (hr->addr.s_addr != 0 && if (hr->addr.s_addr != 0 &&
(cache = whine_malloc(sizeof(struct crec)))) (cache = whine_malloc(SIZEOF_POINTER_CREC)))
{ {
cache->name.namep = nl->name; cache->name.namep = nl->name;
cache->ttd = hr->ttl; cache->ttd = hr->ttl;
@@ -1164,7 +1163,7 @@ void cache_reload(void)
} }
#ifdef HAVE_IPV6 #ifdef HAVE_IPV6
if (!IN6_IS_ADDR_UNSPECIFIED(&hr->addr6) && if (!IN6_IS_ADDR_UNSPECIFIED(&hr->addr6) &&
(cache = whine_malloc(sizeof(struct crec)))) (cache = whine_malloc(SIZEOF_POINTER_CREC)))
{ {
cache->name.namep = nl->name; cache->name.namep = nl->name;
cache->ttd = hr->ttl; cache->ttd = hr->ttl;
@@ -1241,7 +1240,7 @@ static void add_dhcp_cname(struct crec *target, time_t ttd)
if ((aliasc = dhcp_spare)) if ((aliasc = dhcp_spare))
dhcp_spare = dhcp_spare->next; dhcp_spare = dhcp_spare->next;
else /* need new one */ else /* need new one */
aliasc = whine_malloc(sizeof(struct crec)); aliasc = whine_malloc(SIZEOF_POINTER_CREC);
if (aliasc) if (aliasc)
{ {
@@ -1332,7 +1331,7 @@ void cache_add_dhcp_entry(char *host_name, int prot,
if ((crec = dhcp_spare)) if ((crec = dhcp_spare))
dhcp_spare = dhcp_spare->next; dhcp_spare = dhcp_spare->next;
else /* need new one */ else /* need new one */
crec = whine_malloc(sizeof(struct crec)); crec = whine_malloc(SIZEOF_POINTER_CREC);
if (crec) /* malloc may fail */ if (crec) /* malloc may fail */
{ {
@@ -1432,7 +1431,7 @@ static void make_non_terminals(struct crec *source)
} }
else else
#endif #endif
crecp = whine_malloc(sizeof(struct crec)); crecp = whine_malloc(SIZEOF_POINTER_CREC);
if (crecp) if (crecp)
{ {

View File

@@ -442,6 +442,9 @@ struct crec {
} name; } name;
}; };
#define SIZEOF_BARE_CREC (sizeof(struct crec) - SMALLDNAME)
#define SIZEOF_POINTER_CREC (sizeof(struct crec) + sizeof(char *) - SMALLDNAME)
#define F_IMMORTAL (1u<<0) #define F_IMMORTAL (1u<<0)
#define F_NAMEP (1u<<1) #define F_NAMEP (1u<<1)
#define F_REVERSE (1u<<2) #define F_REVERSE (1u<<2)