[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: dpkg 1.8.3.1, fixes sparc sigbus



On Sat, Jan 13, 2001 at 09:54:53AM -0500, Ben Collins wrote:
> 
> After this, today begins my quest to purge nfmalloc from dpkg using
> safer malloc/alloc/obstack functionality.
> 

Ok, here's the patch I am going to commit to the v1_8 branch. It pretty
much reduces nfmalloc down to setting up a linked list of malloc'd
pointers. If it wasn't for dselect, nfmalloc wouldn't be needed. The
only thing that calls reset_database() is dselect, which is the only
thing that calls nffreeall(). Using nffreeall() is the only advantage of
having nfmalloc in the first place.

This code simplifies things a lot, should make it slightly faster (less
function calls in nfmalloc.c), and probably wont affect memory usage at
all. I seriously doubt the previous code was saving more than a few
bytes, if not costing more bytes. We don't have a "union maxalign"
allocated for each nfmalloc() now, so I'm willing to bet there is a
memory savings, if anything.

Maybe once dselect is gone, nfmalloc can be gone too.

-- 
 -----------=======-=-======-=========-----------=====------------=-=------
/  Ben Collins  --  ...on that fantastic voyage...  --  Debian GNU/Linux   \
`  bcollins@debian.org  --  bcollins@openldap.org  --  bcollins@linux.com  '
 `---=========------=======-------------=-=-----=-===-======-------=--=---'
--- lib/mlib.c 	2001/01/11 07:18:46	1.12.2.1
+++ lib/mlib.c 	2001/01/13 23:11:38
@@ -38,27 +38,20 @@
 volatile int onerr_abort= 0;
 
 void *m_malloc(size_t amount) {
-#ifdef MDEBUG
-  unsigned short *r2, x;
-#endif
   void *r;
-  
+
   onerr_abort++;
-  r= malloc(amount);
-  if (!r) ohshite(_("malloc failed (%ld bytes)"),(long)amount);
+  r = malloc(amount);
+  if (!r) ohshite(_("malloc failed (%ld bytes)"), (long)amount);
   onerr_abort--;
-  
-#ifdef MDEBUG
-  r2= r; x= (unsigned short)amount ^ 0xf000;
-  while (amount >= 2) { *r2++= x; amount -= 2; }
-#endif
+
   return r;
 }
 
 void *m_realloc(void *r, size_t amount) {
   onerr_abort++;
-  r= realloc(r,amount);
-  if (!r) ohshite(_("realloc failed (%ld bytes)"),(long)amount);
+  r = realloc(r, amount);
+  if (!r) ohshite(_("realloc failed (%ld bytes)"), (long)amount);
   onerr_abort--;
 
   return r;
--- lib/nfmalloc.c 	2001/01/13 14:01:04	1.2.2.1
+++ lib/nfmalloc.c 	2001/01/13 23:11:38
@@ -26,73 +26,43 @@
 #include <dpkg.h>
 #include <dpkg-db.h>
 
-#define ABLOCKSIZE 65536
-#define UNIQUE      4096
+/* XXX: Do we want to create a static sized array of pieces to increase
+   speed, or keep it this way to save some bytes?  */
 
-/* XXX: This is a silly way to find the max alignment of a given storage
-   set. There must be a better way. In the mean time, do not touch this
-   union.  */
-union maxalign {
-  long l;
-  long double d;
-  void *pv;
-  char *pc;
-  union maxalign *ps;
-  void (*pf)(void);
+struct piece {
+  struct piece *next;
+  void *space;
 };
 
-static unsigned char *playground= 0;
-static long remaining= 0;
-static struct piece { struct piece *next; union maxalign space; } *pieces= 0;
+static struct piece *pieces = NULL;
 
+/* Free all malloc'd chunks in the list.  */
 void nffreeall(void) {
-  struct piece *a,*b;
-  for (a=pieces; a; a=b) { b=a->next; free(a); }
-  pieces= 0; remaining= 0;
+  struct piece *a, *b;
+  for (a = pieces; a; a = b) {
+    b = a->next;
+    free(a->space);
+    free(a);
+  }
+  pieces = NULL;
 }
 
-static void *nfmalloc_sysmalloc(size_t size) {
-  struct piece *alc;
-  alc= m_malloc(size + sizeof(struct piece));
-  alc->next= pieces; pieces= alc;
-  return &alc->space;
-}
-
-#ifndef MDEBUG
+/* malloc a new chunk and add it to our list.  */
 void *nfmalloc(size_t size) {
-#else
-static void *nfmalloc_r(size_t size) {
-#endif
-  const size_t alignment= sizeof(union maxalign);
-
-  size -= (size + alignment-1) % alignment;
-  size += alignment-1;
-  if (size > UNIQUE) return nfmalloc_sysmalloc(size);
-  remaining -= size;
-  if (remaining > 0) return playground -= size;
-  playground= (unsigned char*)nfmalloc_sysmalloc(ABLOCKSIZE) + ABLOCKSIZE - size;
-  remaining= ABLOCKSIZE-size;
-  return playground;
-}
+  struct piece *alc = m_malloc(sizeof(struct piece));
 
-#ifdef MDEBUG
-/* If we haven't switched off debugging we wrap nfmalloc in something
- * that fills the space with junk that may tell us what happened if
- * we dereference a wild pointer.  It's better than leaving it full of
- * nulls, anyway.
- */
-void *nfmalloc(size_t size) {
-  unsigned short *r, *r2, x;
-  r= nfmalloc_r(size); r2=r; x= (unsigned short)size;
-  while (size >= 2) { *r2++= x; size -= 2; }
-  return r;
+  alc->next = pieces;
+  pieces = alc;
+
+  alc->space = m_malloc(size);
+
+  return alc->space;
 }
-#endif
 
 char *nfstrsave(const char *string) {
-  char *r;
-  
-  r= nfmalloc(strlen(string)+1);
-  strcpy(r,string);
+  int i = strlen(string);
+  char *r = nfmalloc(i+1);
+  memcpy(r, string, i);
+  r[i] = '\0';
   return r;
 }

Reply to: