From babd622f7ab9e8d3cb4004700fcf2f9dadd4bd4b Mon Sep 17 00:00:00 2001 From: Niels Provos Date: Sun, 9 Sep 2007 03:10:16 +0000 Subject: [PATCH] make allocating array members in event_rpcgen more efficient, but doubling the size of available slots every time we run out. svn:r421 --- ChangeLog | 1 + event_rpcgen.py | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index f947d757..9cadd717 100644 --- a/ChangeLog +++ b/ChangeLog @@ -5,3 +5,4 @@ Changes in current version: o Add a "Date" header to HTTP responses, as required by HTTP 1.1. o Support specifying the local address of an evhttp_connection using set_local_address o Fix a memory leak in which failed HTTP connections whould not free the request object + o Make adding of array members in event_rpcgen more efficient, but doubling memory allocation diff --git a/event_rpcgen.py b/event_rpcgen.py index 54a1878a..07fdf383 100755 --- a/event_rpcgen.py +++ b/event_rpcgen.py @@ -930,9 +930,15 @@ class EntryArray(Entry): self._struct.Name(), name, self._struct.Name()), '{', ' msg->%s_length++;' % name, - ' msg->%s_data = (struct %s**)realloc(msg->%s_data, ' - ' msg->%s_length * sizeof(struct %s*));' % ( + ' if (msg->%s_length >= msg->%s_num_allocated) { ' % (name, name), + ' if (!msg->%s_num_allocated) ' % name, + ' msg->%s_num_allocated = 1; ' % name, + ' else ', + ' msg->%s_num_allocated <<= 1; ' % name, + ' msg->%s_data = (struct %s**)realloc(msg->%s_data, ' + ' msg->%s_num_allocated * sizeof(struct %s*));' % ( name, self._refname, name, name, self._refname ), + ' }', ' if (msg->%s_data == NULL)' % name, ' return (NULL);', ' msg->%s_data[msg->%s_length - 1] = %s_new();' % ( @@ -1005,6 +1011,7 @@ class EntryArray(Entry): ' %s->%s_data = NULL;' % (structname, self.Name()), ' %s->%s_set = 0;' % (structname, self.Name()), ' %s->%s_length = 0;' % (structname, self.Name()), + ' %s->%s_num_allocated = 0;' % (structname, self.Name()), '}' ] @@ -1012,7 +1019,8 @@ class EntryArray(Entry): def CodeNew(self, name): code = ['%s->%s_data = NULL;' % (name, self._name), - '%s->%s_length = 0;' % (name, self._name)] + '%s->%s_length = 0;' % (name, self._name), + '%s->%s_num_allocated = 0;' % (name, self._name)] code.extend(Entry.CodeNew(self, name)) return code @@ -1028,6 +1036,7 @@ class EntryArray(Entry): ' free(%s->%s_data);' % (name, self._name), ' %s->%s_data = NULL;' % (name, self._name), ' %s->%s_length = 0;' % (name, self._name), + ' %s->%s_num_allocated = 0;' % (name, self._name), '}' ] @@ -1035,7 +1044,8 @@ class EntryArray(Entry): def Declaration(self): dcl = ['struct %s **%s_data;' % (self._refname, self._name), - 'int %s_length;' % self._name] + 'int %s_length;' % self._name, + 'int %s_num_allocated;' % self._name ] return dcl -- 2.40.0