[Dnsmasq-discuss] [PATCH] Introduce whine_realloc

2022-07-18 Thread Petr Menšík

Hi,

After reading the code creating servers.bind reply, I made a change to 
use realloc call instead. It should allow saving some memory copying 
when it is possible to just extend allocated memory size. I have 
replaced  few other uses as well. It is not yet tested much, but should 
be safe enough.


It might also slightly improve performance handling sockets, because it 
reduces copying done on socket listener adding.


Just improvement without adding any other feature.

Cheers,

Petr

--
Petr Menšík
Software Engineer, RHEL
Red Hat, http://www.redhat.com/
PGP: DFCF908DB7C87E8E529925BC4931CA5B6C9FC5CB
From 49070b7d5ba1406cbcd1a854abf33f54d7c45e1a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= 
Date: Mon, 18 Jul 2022 13:30:07 +0200
Subject: [PATCH] Introduce whine_realloc

Move few patters with whine_malloc, if (successful) copy+free, to a new
whine_realloc. It should do the same thing, but with a help from OS it
can avoid unnecessary copy and free if allocation of more data after
current data is possible.

Added few setting remanining space to 0, because realloc does not use
calloc like whine_malloc does. There is no advantage of zeroing what we
will immediately overwrite. Zero only remaining space.
---
 src/cache.c|  4 +---
 src/dnsmasq.h  |  1 +
 src/lease.c|  8 +---
 src/poll.c |  9 +
 src/rrfilter.c | 16 ++--
 src/util.c | 10 ++
 6 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/src/cache.c b/src/cache.c
index a99d70d..8ed4740 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -1676,10 +1676,8 @@ int cache_make_stat(struct txt_record *t)
 	  {
 		/* expand buffer if necessary */
 		newlen = bytes_needed + 1 + bufflen - bytes_avail;
-		if (!(new = whine_malloc(newlen)))
+		if (!(new = whine_realloc(buff, newlen)))
 		  return 0;
-		memcpy(new, buff, bufflen);
-		free(buff);
 		p = new + (p - buff);
 		lenp = p - 1;
 		buff = new;
diff --git a/src/dnsmasq.h b/src/dnsmasq.h
index bfc0fd4..db8a26b 100644
--- a/src/dnsmasq.h
+++ b/src/dnsmasq.h
@@ -1394,6 +1394,7 @@ void *safe_malloc(size_t size);
 void safe_strncpy(char *dest, const char *src, size_t size);
 void safe_pipe(int *fd, int read_noblock);
 void *whine_malloc(size_t size);
+void *whine_realloc(void *ptr, size_t size);
 int sa_len(union mysockaddr *addr);
 int sockaddr_isequal(const union mysockaddr *s1, const union mysockaddr *s2);
 int hostname_order(const char *a, const char *b);
diff --git a/src/lease.c b/src/lease.c
index 81477d5..8a7b975 100644
--- a/src/lease.c
+++ b/src/lease.c
@@ -1180,17 +1180,11 @@ void lease_add_extradata(struct dhcp_lease *lease, unsigned char *data, unsigned
   if ((lease->extradata_size - lease->extradata_len) < (len + 1))
 {
   size_t newsz = lease->extradata_len + len + 100;
-  unsigned char *new = whine_malloc(newsz);
+  unsigned char *new = whine_realloc(lease->extradata, newsz);
   
   if (!new)
 	return;
   
-  if (lease->extradata)
-	{
-	  memcpy(new, lease->extradata, lease->extradata_len);
-	  free(lease->extradata);
-	}
-
   lease->extradata = new;
   lease->extradata_size = newsz;
 }
diff --git a/src/poll.c b/src/poll.c
index 29b33a0..0e5964d 100644
--- a/src/poll.c
+++ b/src/poll.c
@@ -105,14 +105,15 @@ void poll_listen(int fd, short event)
 
 	   arrsize = (arrsize == 0) ? 64 : arrsize * 2;
 
-	   if (!(new = whine_malloc(arrsize * sizeof(struct pollfd
+	   if (!(new = whine_realloc(pollfds, arrsize * sizeof(struct pollfd
 	 return;
 
 	   if (pollfds)
 	 {
-	   memcpy(new, pollfds, i * sizeof(struct pollfd));
-	   memcpy(&new[i+1], &pollfds[i], (nfds - i) * sizeof(struct pollfd));
-	   free(pollfds);
+	   memmove(&new[i+1], &new[i], (nfds - i) * sizeof(struct pollfd));
+	   /* clear remaining space with zeroes. */
+	   if (nfds+1 < arrsize)
+	 memset(new+nfds+1, 0, arrsize-nfds-1);
 	 }
 	   
 	   pollfds = new;
diff --git a/src/rrfilter.c b/src/rrfilter.c
index f02f5a5..42d9c21 100644
--- a/src/rrfilter.c
+++ b/src/rrfilter.c
@@ -159,7 +159,7 @@ static int check_rrs(unsigned char *p, struct dns_header *header, size_t plen, i
 /* mode may be remove EDNS0 or DNSSEC RRs or remove A or  from answer section. */
 size_t rrfilter(struct dns_header *header, size_t plen, int mode)
 {
-  static unsigned char **rrs;
+  static unsigned char **rrs = NULL;
   static int rr_sz = 0;
 
   unsigned char *p = (unsigned char *)(header+1);
@@ -339,15 +339,11 @@ int expand_workspace(unsigned char ***wkspc, int *szp, int new)
 return 0;
 
   new += 5;
-  
-  if (!(p = whine_malloc(new * sizeof(unsigned char *
-return 0;  
-  
-  if (old != 0 && *wkspc)
-{
-  memcpy(p, *wkspc, old * sizeof(unsigned char *));
-  free(*wkspc);
-}
+
+  if (!(p = whine_realloc(*wkspc, new * sizeof(unsigned char *
+return 0;
+
+  memset(p+old, 0, new-old);
   
   *wkspc = p;
   *szp = new;
diff --git a/src/util.c b/src/util.c
index 

Re: [Dnsmasq-discuss] patchwork

2022-07-18 Thread Petr Menšík

On 17. 07. 22 14:35, Geert Stappers via Dnsmasq-discuss wrote:

Previous-Subject: Re: [Dnsmasq-discuss] [PATCH] Create temporary leases for 
DHCPOFFER actions
In-Reply-To: 
On Fri, Jul 15, 2022 at 10:54:28PM +0200, Petr Menšík wrote:

On 7/13/22 19:20, Geert Stappers via Dnsmasq-discuss wrote:

On Fri, Jul 08, 2022 at 10:26:35PM +0200, Petr Menšík wrote:

   ... First two patches were already sent. I think I have sent
also following patches already, but were not able to find them.

To prevent that patches get lost,
advices the Monthly Posting to poke after eight days again.


An additional attempt to prevent that patches (and contributors) get
lost, am I now experimenting with "dnsmasq mailing list patch collection"

It is a git repository that is cloned from git://thekelleys.org.uk/dnsmasq.git
The master branch follows that repo, the other branches have patches
that I collected from this mailinglist. Currently that is the only
extra branch "y22w27", short for "year 2022, week 27". An upcoming
branch is "gcc12".  I'm in need for a (short) branch name for
Create temporary leases for DHCPOFFER actions
as Previous-Subject says / suggests.

Ideas for the branch name are still welcome.


I would prefer to have a feature branches. One mailing list thread, one 
branch for it. If it is named after author and number, it would be okay. 
Something simple to rebase over current master and track independent 
things independent.


Allows separate changes review and merging them in any order without 
extra work.



Webpage https://git.sr.ht/~stappers/dnsmasqmlpc has `git clone URL`
and links for further "browsing".
Example given: https://git.sr.ht/~stappers/dnsmasqmlpc/log/y22w27


Interesting. Do you generate those by hand
or are those generated by some tool?

By hand

  

I have thought about a tool, which would collect patches sent from
registered people and create a pull requests on gitlab or github, which
would show their status. And if possible mark them merged automagically. But
haven't found enough free time to play with that. Sort of external pull
requests, which can be linked to bugs and tracked progress, if any.

There is 'patchwork' http://jk.ozlabs.org/projects/patchwork/
documentation is at https://patchwork.readthedocs.io/en/latest/

My estimation is that we, dnsmasq project, can live without such tool.
On the other hand it would be nice to have such tool.
Of course, Simon is the only maintainer of the repository. Whatever he 
prefers is important. But I guess a tool could make changes tracking 
easier without forcing Simon to change his workflow.

Groeten
Geert Stappers


P.S.
If I have missed patches, just say so.
Seems great! Thanks for the reference. I knew there has to be multiple 
of projects with similar requirements. I will try to read a more about 
it and if it could be enabled as addition to current patches review 
workflow.


Thanks!
Petr

--
Petr Menšík
Software Engineer, RHEL
Red Hat, http://www.redhat.com/
PGP: DFCF908DB7C87E8E529925BC4931CA5B6C9FC5CB


___
Dnsmasq-discuss mailing list
Dnsmasq-discuss@lists.thekelleys.org.uk
https://lists.thekelleys.org.uk/cgi-bin/mailman/listinfo/dnsmasq-discuss