瀏覽代碼

-malloc real_used/initial size accounting fixes
(*realloc shrink accounting fix; initial size does not contain the
overhead anymore)
- DBG_F_MALLOC typo and warning fixes

Andrei Pelinescu-Onciul 20 年之前
父節點
當前提交
01653e28a6
共有 3 個文件被更改,包括 21 次插入10 次删除
  1. 1 1
      Makefile.defs
  2. 13 5
      mem/f_malloc.c
  3. 7 4
      mem/q_malloc.c

+ 1 - 1
Makefile.defs

@@ -59,7 +59,7 @@ MAIN_NAME=ser
 VERSION = 0
 PATCHLEVEL = 9
 SUBLEVEL = 5
-EXTRAVERSION = -pre4
+EXTRAVERSION = -pre5
 
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")

+ 13 - 5
mem/f_malloc.c

@@ -33,6 +33,8 @@
  *               memory blocks (64 bits machine & size >=2^32) 
  *              GET_HASH s/</<=/ (avoids waste of 1 hash cell)   (andrei)
  *  2004-11-10  support for > 4Gb mem., switched to long (andrei)
+ *  2005-12-12  fixed realloc shrink real_used accounting (andrei)
+ *              fixed initial size (andrei)
  */
 
 
@@ -188,6 +190,12 @@ struct fm_block* fm_malloc_init(char* address, unsigned long size)
 	
 	/* make address and size multiple of 8*/
 	start=(char*)ROUNDUP((unsigned long) address);
+	DBG("fm_malloc_init: F_OPTIMIZE=%lu, /ROUNDTO=%lu\n",
+			F_MALLOC_OPTIMIZE, F_MALLOC_OPTIMIZE/ROUNDTO);
+	DBG("fm_malloc_init: F_HASH_SIZE=%lu, fm_block size=%lu\n",
+			F_HASH_SIZE, (long)sizeof(struct fm_block));
+	DBG("fm_malloc_init(%p, %lu), start=%p\n", address, size, start);
+
 	if (size<start-address) return 0;
 	size-=(start-address);
 	if (size <(MIN_FRAG_SIZE+FRAG_OVERHEAD)) return 0;
@@ -204,12 +212,12 @@ struct fm_block* fm_malloc_init(char* address, unsigned long size)
 	end=start+size;
 	qm=(struct fm_block*)start;
 	memset(qm, 0, sizeof(struct fm_block));
-	size-=init_overhead;
 	qm->size=size;
 #ifdef DBG_F_MALLOC
 	qm->real_used=init_overhead;
 	qm->max_real_used=qm->real_used;
 #endif
+	size-=init_overhead;
 	
 	qm->first_frag=(struct fm_frag*)(start+ROUNDUP(sizeof(struct fm_block)));
 	qm->last_frag=(struct fm_frag*)(end-sizeof(struct fm_frag));
@@ -387,7 +395,7 @@ void* fm_realloc(struct fm_block* qm, void* p, unsigned long size)
 #ifdef DBG_F_MALLOC
 		DBG("fm_realloc: shrinking from %lu to %lu\n", f->size, size);
 		fm_split_frag(qm, f, size, file, "frag. from fm_realloc", line);
-		qm->real_used-=(orig_size-f->size);
+		qm->real_used-=(orig_size-f->size-FRAG_OVERHEAD);
 		qm->used-=(orig_size-f->size);
 #else
 		fm_split_frag(qm, f, size);
@@ -503,9 +511,9 @@ void fm_status(struct fm_block* qm)
 				size+=f->size,f=f->u.nxt_free,i++,j++){
 			if (!FRAG_WAS_USED(f)){
 				unused++;
-#ifdef DBG_FM_MALLOC
-				LOG(memlog, "unused fragm.: hash = %3d, fragment %x,"
-							" address %x size %lu, created from %s: %s(%d)\n",
+#ifdef DBG_F_MALLOC
+				LOG(memlog, "unused fragm.: hash = %3d, fragment %p,"
+							" address %p size %lu, created from %s: %s(%ld)\n",
 						    h, f, (char*)f+sizeof(struct fm_frag), f->size,
 							f->file, f->func, f->line);
 #endif

+ 7 - 4
mem/q_malloc.c

@@ -34,6 +34,8 @@
  *               memory blocks (64 bits machine & size>=2^32) (andrei)
  *              GET_HASH s/</<=/ (avoids waste of 1 hash cell) (andrei)
  *  2004-11-10  support for > 4Gb mem., switched to long (andrei)
+ *  2005-12-12  fixed realloc shrink real_used & used accounting;
+ *              fixed initial size (andrei)
  */
 
 
@@ -215,10 +217,10 @@ struct qm_block* qm_malloc_init(char* address, unsigned long size)
 	end=start+size;
 	qm=(struct qm_block*)start;
 	memset(qm, 0, sizeof(struct qm_block));
-	size-=init_overhead;
 	qm->size=size;
 	qm->real_used=init_overhead;
 	qm->max_real_used=qm->real_used;
+	size-=init_overhead;
 	
 	qm->first_frag=(struct qm_frag*)(start+ROUNDUP(sizeof(struct qm_block)));
 	qm->last_frag_end=(struct qm_frag_end*)(end-sizeof(struct qm_frag_end));
@@ -547,6 +549,7 @@ void* qm_realloc(struct qm_block* qm, void* p, unsigned long size)
 	/* find first acceptable size */
 	size=ROUNDUP(size);
 	if (f->size > size){
+		orig_size=f->size;
 		/* shrink */
 #ifdef DBG_QM_MALLOC
 		DBG("qm_realloc: shrinking from %lu to %lu\n", f->size, size);
@@ -555,9 +558,9 @@ void* qm_realloc(struct qm_block* qm, void* p, unsigned long size)
 #else
 		if(split_frag(qm, f, size)!=0){
 #endif
-			/* update used sizes */
-			qm->real_used-=(f->size-size);
-			qm->used-=(f->size-size);
+			/* update used sizes: freed the spitted frag */
+			qm->real_used-=(orig_size-f->size-FRAG_OVERHEAD);
+			qm->used-=(orig_size-f->size);
 		}
 		
 	}else if (f->size < size){