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

Re: MINTLIB Diff needed



Dirk Stadler <dirk@lstm.uni-erlangen.de> writes:

|> Hello people,
|> some weeks ago, someone posted a diff concerning the memory allocation
|> routines of the Mintlib.

Do you mean this one?  It makes it possible for free() to return
memory blocks back to the system, so that the process size can shrink.
This was part of my patches for emacs, but i'm using this all the
time, not only for emacs.  Maybe this should be included in the
official version?  (I think i should first add some comments...
Comments? What's this? :-)


diff -ur orig/lib.h ./lib.h
--- orig/lib.h	Wed Mar  2 19:07:58 1994
+++ ./lib.h	Wed Mar  2 19:49:54 1994
@@ -75,10 +75,13 @@
 	long valid;
 #define VAL_FREE  0xf4ee0abcL
 #define VAL_ALLOC 0xa11c0abcL
+#define VAL_BORDER 0xb04d0abcL
 
 	struct mem_chunk *next;
 	unsigned long size;
 	};
+#define ALLOC_SIZE(ch) (*(long *) ((char *) (ch) + sizeof (*(ch))))
+#define BORDER_EXTRA ((sizeof (struct mem_chunk) + sizeof (long) + 7) & ~7)
 
 /* linked list of free blocks */
 
diff -ur orig/malloc.c ./malloc.c
--- orig/malloc.c	Fri Sep 17 20:10:18 1993
+++ ./malloc.c	Fri Sep 24 22:31:08 1993
@@ -12,6 +12,7 @@
 #include <string.h>
 #include <assert.h>
 #include <unistd.h>
+#include <osbind.h>
 #include "lib.h"
 
 extern long _stksize;
@@ -54,7 +55,7 @@
   p = &_mchunk_free_list;
   q = _mchunk_free_list.next;
 
-  while ((q != NULL) && (q->size < n))
+  while ((q != NULL) && (q->size < n || q->valid == VAL_BORDER))
 	{
 	p = q;
 	q = q->next;
@@ -63,8 +64,13 @@
 /* if not enough memory, get more from the system */
   if (q == NULL) 
 	{
-	if (((!_split_mem) && (_heapbase != NULL)) || (n > MINHUNK))
+	if (((!_split_mem) && (_heapbase != NULL))
+	    || n + BORDER_EXTRA > MINHUNK)
+	  {
 		sz = n;
+		if (_heapbase == NULL)
+		  sz += BORDER_EXTRA;
+	  }
 	else {
 		sz = MINHUNK;
 		if (MINHUNK < MAXHUNK)
@@ -86,10 +92,24 @@
 	p = &_mchunk_free_list;
 	while (p->next != NULL && q > p->next)
 	  p = p->next;
+	if (_heapbase == NULL)
+	  {
+	    q->size = BORDER_EXTRA;
+	    sz -= BORDER_EXTRA;
+	    q->valid = VAL_BORDER;
+	    ALLOC_SIZE (q) = sz;
+	    q->next = (struct mem_chunk *) ((long) q + BORDER_EXTRA);
+	    q->next->next = p->next;
+	    p = p->next = q;
+	    q = q->next;
+	  }
+	else
+	  {
+	    q->next = p->next;
+	    p->next = q;
+	  }
 	q->size = sz;
-	q->next = p->next;
 	q->valid = VAL_FREE;
-	p->next = q;
 	}
 		
   if (q->size > n + sizeof(struct mem_chunk))
@@ -146,7 +166,7 @@
 
 /* merge after if possible */
   s = (struct mem_chunk * )(((long) r) + r->size);
-  if (q != NULL && s >= q) 
+  if (q != NULL && s >= q && q->valid != VAL_BORDER)
 	{
 	assert(s == q);
 	r->size += q->size;
@@ -162,6 +182,17 @@
     /* remember: r may be below &_mchunk_free_list in memory */
 	{
 	assert(s == r);
+	if (p->valid == VAL_BORDER)
+	  {
+	    if (ALLOC_SIZE (p) == r->size)
+	      {
+		o->next = r->next;
+		Mfree (p);
+	      }
+	    else
+	      p->next = r;
+	    return;
+	  }
 	p->size += r->size;
 	p->next = r->next;
 	r->size = 0;
@@ -175,6 +206,22 @@
 	  _stksize += p->size;
 	  o->next = p->next;	/* o is always != NULL here */
 	}
+	else if (o->valid == VAL_BORDER && ALLOC_SIZE (o) == p->size)
+	  {
+	    q = &_mchunk_free_list;
+	    s = q->next;
+	    while (s != NULL && s < o)
+	      {
+		q = s;
+		s = q->next;
+	      }
+	    if (s)
+	      {
+		assert (s == o);
+		q->next = p->next;
+		Mfree (o);
+	      }
+	  }
 	}
     else
         {
diff -ur orig/realloc.c ./realloc.c
--- orig/realloc.c	Sun May 23 14:27:18 1993
+++ ./realloc.c	Fri Mar 12 17:35:36 1993
@@ -60,7 +60,8 @@
 
     /* merge after if possible */
     s = (struct mem_chunk * )(((long) p) + p->size);
-    if (t != NULL && s >= t && p->size + t->size >= sz)
+    if (t != NULL && s >= t && p->size + t->size >= sz
+	&& t->valid != VAL_BORDER)
       {
       assert(s == t);
       p->size += t->size;

-- 
Andreas Schwab                                      "And now for something
schwab@issan.informatik.uni-dortmund.de              completely different"