[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [47046] trunk/blender/intern/guardedalloc/ intern/mallocn.c: style cleanup: our own malloc code

Campbell Barton ideasman42 at gmail.com
Sat May 26 15:36:12 CEST 2012


Revision: 47046
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=47046
Author:   campbellbarton
Date:     2012-05-26 13:36:12 +0000 (Sat, 26 May 2012)
Log Message:
-----------
style cleanup: our own malloc code

Modified Paths:
--------------
    trunk/blender/intern/guardedalloc/intern/mallocn.c

Modified: trunk/blender/intern/guardedalloc/intern/mallocn.c
===================================================================
--- trunk/blender/intern/guardedalloc/intern/mallocn.c	2012-05-26 13:31:57 UTC (rev 47045)
+++ trunk/blender/intern/guardedalloc/intern/mallocn.c	2012-05-26 13:36:12 UTC (rev 47046)
@@ -37,23 +37,23 @@
  */
 
 #include <stdlib.h>
-#include <string.h>	/* memcpy */
+#include <string.h> /* memcpy */
 #include <stdarg.h>
 #include <sys/types.h>
 /* Blame Microsoft for LLP64 and no inttypes.h, quick workaround needed: */
 #if defined(WIN64)
-#define SIZET_FORMAT "%I64u"
-#define SIZET_ARG(a) ((unsigned long long)(a))
+#  define SIZET_FORMAT "%I64u"
+#  define SIZET_ARG(a) ((unsigned long long)(a))
 #else
-#define SIZET_FORMAT "%lu"
-#define SIZET_ARG(a) ((unsigned long)(a))
+#  define SIZET_FORMAT "%lu"
+#  define SIZET_ARG(a) ((unsigned long)(a))
 #endif
 
 /* mmap exception */
 #if defined(WIN32)
-#include "mmap_win.h"
+#  include "mmap_win.h"
 #else
-#include <sys/mman.h>
+#  include <sys/mman.h>
 #endif
 
 #include "MEM_guardedalloc.h"
@@ -79,25 +79,23 @@
 /* Data definition                                                       */
 /* --------------------------------------------------------------------- */
 /* all memory chunks are put in linked lists */
-typedef struct localLink
-{
-	struct localLink *next,*prev;
+typedef struct localLink {
+	struct localLink *next, *prev;
 } localLink;
 
-typedef struct localListBase 
-{
+typedef struct localListBase {
 	void *first, *last;
 } localListBase;
 
-	/* note: keep this struct aligned (e.g., irix/gcc) - Hos */
+/* note: keep this struct aligned (e.g., irix/gcc) - Hos */
 typedef struct MemHead {
 	int tag1;
 	size_t len;
-	struct MemHead *next,*prev;
-	const char * name;
-	const char * nextname;
+	struct MemHead *next, *prev;
+	const char *name;
+	const char *nextname;
 	int tag2;
-	int mmap;	/* if true, memory was mmapped */
+	int mmap;  /* if true, memory was mmapped */
 #ifdef DEBUG_MEMCOUNTER
 	int _count;
 #endif
@@ -123,9 +121,9 @@
 /* --------------------------------------------------------------------- */
 
 #ifdef __BIG_ENDIAN__
-#  define MAKE_ID(a,b,c,d) ( (int)(a)<<24 | (int)(b)<<16 | (c)<<8 | (d) )
+#  define MAKE_ID(a, b, c, d) ((int)(a) << 24 | (int)(b) << 16 | (c) << 8 | (d))
 #else
-#  define MAKE_ID(a,b,c,d) ( (int)(d)<<24 | (int)(c)<<16 | (b)<<8 | (a) )
+#  define MAKE_ID(a, b, c, d) ((int)(d) << 24 | (int)(c) << 16 | (b) << 8 | (a))
 #endif
 
 #define MEMTAG1 MAKE_ID('M', 'E', 'M', 'O')
@@ -133,15 +131,15 @@
 #define MEMTAG3 MAKE_ID('O', 'C', 'K', '!')
 #define MEMFREE MAKE_ID('F', 'R', 'E', 'E')
 
-#define MEMNEXT(x) ((MemHead *)(((char *) x) - ((char *) & (((MemHead *)0)->next))))
+#define MEMNEXT(x) ((MemHead *)(((char *) x) - ((char *) &(((MemHead *)0)->next))))
 	
 /* --------------------------------------------------------------------- */
 /* vars                                                                  */
 /* --------------------------------------------------------------------- */
 	
 
-static volatile int totblock= 0;
-static volatile uintptr_t mem_in_use= 0, mmap_in_use= 0, peak_mem = 0;
+static volatile int totblock = 0;
+static volatile uintptr_t mem_in_use = 0, mmap_in_use = 0, peak_mem = 0;
 
 static volatile struct localListBase _membase;
 static volatile struct localListBase *membase = &_membase;
@@ -149,7 +147,7 @@
 static void (*thread_lock_callback)(void) = NULL;
 static void (*thread_unlock_callback)(void) = NULL;
 
-static int malloc_debug_memset= 0;
+static int malloc_debug_memset = 0;
 
 #ifdef malloc
 #undef malloc
@@ -195,8 +193,8 @@
 
 int MEM_check_memory_integrity(void)
 {
-	const char* err_val = NULL;
-	MemHead* listend;
+	const char *err_val = NULL;
+	MemHead *listend;
 	/* check_memlist starts from the front, and runs until it finds
 	 * the requested chunk. For this test, that's the last one. */
 	listend = membase->last;
@@ -221,32 +219,34 @@
 
 void MEM_set_memory_debug(void)
 {
-	malloc_debug_memset= 1;
+	malloc_debug_memset = 1;
 }
 
 size_t MEM_allocN_len(void *vmemh)
 {
 	if (vmemh) {
-		MemHead *memh= vmemh;
+		MemHead *memh = vmemh;
 	
 		memh--;
 		return memh->len;
-	} else
+	}
+	else {
 		return 0;
+	}
 }
 
 void *MEM_dupallocN(void *vmemh)
 {
-	void *newp= NULL;
+	void *newp = NULL;
 	
 	if (vmemh) {
-		MemHead *memh= vmemh;
+		MemHead *memh = vmemh;
 		memh--;
 		
 		if (memh->mmap)
-			newp= MEM_mapallocN(memh->len, "dupli_mapalloc");
+			newp = MEM_mapallocN(memh->len, "dupli_mapalloc");
 		else
-			newp= MEM_mallocN(memh->len, "dupli_alloc");
+			newp = MEM_mallocN(memh->len, "dupli_alloc");
 
 		if (newp == NULL) return NULL;
 
@@ -258,13 +258,13 @@
 
 void *MEM_reallocN(void *vmemh, size_t len)
 {
-	void *newp= NULL;
+	void *newp = NULL;
 	
 	if (vmemh) {
-		MemHead *memh= vmemh;
+		MemHead *memh = vmemh;
 		memh--;
 
-		newp= MEM_mallocN(len, memh->name);
+		newp = MEM_mallocN(len, memh->name);
 		if (newp) {
 			if (len < memh->len)
 				memcpy(newp, vmemh, len);
@@ -292,7 +292,7 @@
 	memt = (MemTail *)(((char *) memh) + sizeof(MemHead) + len);
 	memt->tag3 = MEMTAG3;
 	
-	addtail(membase,&memh->next);
+	addtail(membase, &memh->next);
 	if (memh->next) memh->nextname = MEMNEXT(memh->next)->name;
 	
 	totblock++;
@@ -307,20 +307,20 @@
 
 	mem_lock_thread();
 
-	len = (len + 3 ) & ~3; 	/* allocate in units of 4 */
+	len = (len + 3) & ~3;   /* allocate in units of 4 */
 	
-	memh= (MemHead *)malloc(len+sizeof(MemHead)+sizeof(MemTail));
+	memh = (MemHead *)malloc(len + sizeof(MemHead) + sizeof(MemTail));
 
 	if (memh) {
 		make_memhead_header(memh, len, str);
 		mem_unlock_thread();
 		if (malloc_debug_memset && len)
-			memset(memh+1, 255, len);
+			memset(memh + 1, 255, len);
 
 #ifdef DEBUG_MEMCOUNTER
-		if (_mallocn_count==DEBUG_MEMCOUNTER_ERROR_VAL)
+		if (_mallocn_count == DEBUG_MEMCOUNTER_ERROR_VAL)
 			memcount_raise(__func__);
-		memh->_count= _mallocn_count++;
+		memh->_count = _mallocn_count++;
 #endif
 		return (++memh);
 	}
@@ -335,17 +335,17 @@
 
 	mem_lock_thread();
 
-	len = (len + 3 ) & ~3; 	/* allocate in units of 4 */
+	len = (len + 3) & ~3;   /* allocate in units of 4 */
 
-	memh= (MemHead *)calloc(len+sizeof(MemHead)+sizeof(MemTail),1);
+	memh = (MemHead *)calloc(len + sizeof(MemHead) + sizeof(MemTail), 1);
 
 	if (memh) {
 		make_memhead_header(memh, len, str);
 		mem_unlock_thread();
 #ifdef DEBUG_MEMCOUNTER
-		if (_mallocn_count==DEBUG_MEMCOUNTER_ERROR_VAL)
+		if (_mallocn_count == DEBUG_MEMCOUNTER_ERROR_VAL)
 			memcount_raise(__func__);
-		memh->_count= _mallocn_count++;
+		memh->_count = _mallocn_count++;
 #endif
 		return (++memh);
 	}
@@ -361,21 +361,21 @@
 
 	mem_lock_thread();
 	
-	len = (len + 3 ) & ~3; 	/* allocate in units of 4 */
+	len = (len + 3) & ~3;   /* allocate in units of 4 */
 
-	memh= mmap(NULL, len+sizeof(MemHead)+sizeof(MemTail),
-			PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
+	memh = mmap(NULL, len + sizeof(MemHead) + sizeof(MemTail),
+	            PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
 
-	if (memh!=(MemHead *)-1) {
+	if (memh != (MemHead *)-1) {
 		make_memhead_header(memh, len, str);
-		memh->mmap= 1;
+		memh->mmap = 1;
 		mmap_in_use += len;
 		peak_mem = mmap_in_use > peak_mem ? mmap_in_use : peak_mem;
 		mem_unlock_thread();
 #ifdef DEBUG_MEMCOUNTER
-		if (_mallocn_count==DEBUG_MEMCOUNTER_ERROR_VAL)
+		if (_mallocn_count == DEBUG_MEMCOUNTER_ERROR_VAL)
 			memcount_raise(__func__);
-		memh->_count= _mallocn_count++;
+		memh->_count = _mallocn_count++;
 #endif
 		return (++memh);
 	}
@@ -395,16 +395,16 @@
 
 static int compare_name(const void *p1, const void *p2)
 {
-	const MemPrintBlock *pb1= (const MemPrintBlock*)p1;
-	const MemPrintBlock *pb2= (const MemPrintBlock*)p2;
+	const MemPrintBlock *pb1 = (const MemPrintBlock *)p1;
+	const MemPrintBlock *pb2 = (const MemPrintBlock *)p2;
 
 	return strcmp(pb1->name, pb2->name);
 }
 
 static int compare_len(const void *p1, const void *p2)
 {
-	const MemPrintBlock *pb1= (const MemPrintBlock*)p1;
-	const MemPrintBlock *pb2= (const MemPrintBlock*)p2;
+	const MemPrintBlock *pb1 = (const MemPrintBlock *)p1;
+	const MemPrintBlock *pb2 = (const MemPrintBlock *)p2;
 
 	if (pb1->len < pb2->len)
 		return 1;
@@ -423,30 +423,30 @@
 	mem_lock_thread();
 
 	/* put memory blocks into array */
-	printblock= malloc(sizeof(MemPrintBlock)*totblock);
+	printblock = malloc(sizeof(MemPrintBlock) * totblock);
 
-	pb= printblock;
-	totpb= 0;
+	pb = printblock;
+	totpb = 0;
 
 	membl = membase->first;
 	if (membl) membl = MEMNEXT(membl);
 
 	while (membl) {
-		pb->name= membl->name;
-		pb->len= membl->len;
-		pb->items= 1;
+		pb->name = membl->name;
+		pb->len = membl->len;
+		pb->items = 1;
 
 		totpb++;
 		pb++;
 
 		if (membl->next)
-			membl= MEMNEXT(membl->next);
+			membl = MEMNEXT(membl->next);
 		else break;
 	}
 
 	/* sort by name and add together blocks with the same name */
 	qsort(printblock, totpb, sizeof(MemPrintBlock), compare_name);
-	for (a = 0, b=0; a<totpb; a++) {
+	for (a = 0, b = 0; a < totpb; a++) {
 		if (a == b) {
 			continue;
 		}
@@ -459,14 +459,14 @@
 			memcpy(&printblock[b], &printblock[a], sizeof(MemPrintBlock));
 		}
 	}
-	totpb= b+1;
+	totpb = b + 1;
 
 	/* sort by length and print */
 	qsort(printblock, totpb, sizeof(MemPrintBlock), compare_len);
-	printf("\ntotal memory len: %.3f MB\n", (double)mem_in_use/(double)(1024*1024));
+	printf("\ntotal memory len: %.3f MB\n", (double)mem_in_use / (double)(1024 * 1024));
 	printf(" ITEMS TOTAL-MiB AVERAGE-KiB TYPE\n");
-	for (a = 0, pb=printblock; a<totpb; a++, pb++)
-		printf("%6d (%8.3f  %8.3f) %s\n", pb->items, (double)pb->len/(double)(1024*1024), (double)pb->len/1024.0/(double)pb->items, pb->name);
+	for (a = 0, pb = printblock; a < totpb; a++, pb++)
+		printf("%6d (%8.3f  %8.3f) %s\n", pb->items, (double)pb->len / (double)(1024 * 1024), (double)pb->len / 1024.0 / (double)pb->items, pb->name);
 
 	free(printblock);
 	
@@ -478,7 +478,7 @@
 }
 
 /* Prints in python syntax for easy */
-static void MEM_printmemlist_internal( int pydict )
+static void MEM_printmemlist_internal(int pydict)
 {
 	MemHead *membl;
 
@@ -493,42 +493,43 @@
 	}
 	while (membl) {
 		if (pydict) {
-			fprintf(stderr, "{'len':" SIZET_FORMAT ", 'name':'''%s''', 'pointer':'%p'},\\\n", SIZET_ARG(membl->len), membl->name, (void *)(membl+1));
-		} else {
+			fprintf(stderr, "{'len':" SIZET_FORMAT ", 'name':'''%s''', 'pointer':'%p'},\\\n", SIZET_ARG(membl->len), membl->name, (void *)(membl + 1));
+		}
+		else {
 #ifdef DEBUG_MEMCOUNTER

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list