Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2012-05-26 17:36:12 +0400
committerCampbell Barton <ideasman42@gmail.com>2012-05-26 17:36:12 +0400
commit88e646b0d4f832c011d44cfcc1261ecaa9122b7e (patch)
tree6d4e5d56c70ca34cb16439ca47bc1afd5c39e123 /intern/guardedalloc
parented98de33984bd95fb17323a32343effe84c047bc (diff)
style cleanup: our own malloc code
Diffstat (limited to 'intern/guardedalloc')
-rw-r--r--intern/guardedalloc/intern/mallocn.c252
1 files changed, 129 insertions, 123 deletions
diff --git a/intern/guardedalloc/intern/mallocn.c b/intern/guardedalloc/intern/mallocn.c
index f40bed16d75..9c2f5f331b0 100644
--- a/intern/guardedalloc/intern/mallocn.c
+++ b/intern/guardedalloc/intern/mallocn.c
@@ -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 @@ static void memcount_raise(const char *name)
/* 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 @@ static const char *check_memlist(MemHead *memh);
/* --------------------------------------------------------------------- */
#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 @@ static const char *check_memlist(MemHead *memh);
#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 (*error_callback)(const char *) = NULL;
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 @@ static void mem_unlock_thread(void)
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_lock_callback(void (*lock)(void), void (*unlock)(void))
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_dupallocN(void *vmemh)
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 @@ static void make_memhead_header(MemHead *memh, size_t len, const char *str)
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 @@ void *MEM_mallocN(size_t len, const char *str)
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 @@ void *MEM_callocN(size_t len, const char *str)
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 @@ void *MEM_mapallocN(size_t len, const char *str)
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 @@ typedef struct MemPrintBlock {
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 @@ void MEM_printmemlist_stats(void)
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 @@ void MEM_printmemlist_stats(void)
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 @@ void MEM_printmemlist_stats(void)
}
/* 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 @@ static void MEM_printmemlist_internal( int pydict )
}
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
- print_error("%s len: " SIZET_FORMAT " %p, count: %d\n", membl->name, SIZET_ARG(membl->len), membl+1, membl->_count);
+ print_error("%s len: " SIZET_FORMAT " %p, count: %d\n", membl->name, SIZET_ARG(membl->len), membl + 1, membl->_count);
#else
- print_error("%s len: " SIZET_FORMAT " %p\n", membl->name, SIZET_ARG(membl->len), membl+1);
+ print_error("%s len: " SIZET_FORMAT " %p\n", membl->name, SIZET_ARG(membl->len), membl + 1);
#endif
}
if (membl->next)
- membl= MEMNEXT(membl->next);
+ membl = MEMNEXT(membl->next);
else break;
}
if (pydict) {
fprintf(stderr, "]\n\n");
fprintf(stderr,
-"mb_userinfo = {}\n"
-"totmem = 0\n"
-"for mb_item in membase:\n"
-"\tmb_item_user_size = mb_userinfo.setdefault(mb_item['name'], [0,0])\n"
-"\tmb_item_user_size[0] += 1 # Add a user\n"
-"\tmb_item_user_size[1] += mb_item['len'] # Increment the size\n"
-"\ttotmem += mb_item['len']\n"
-"print '(membase) items:', len(membase), '| unique-names:', len(mb_userinfo), '| total-mem:', totmem\n"
-"mb_userinfo_sort = mb_userinfo.items()\n"
-"for sort_name, sort_func in (('size', lambda a: -a[1][1]), ('users', lambda a: -a[1][0]), ('name', lambda a: a[0])):\n"
-"\tprint '\\nSorting by:', sort_name\n"
-"\tmb_userinfo_sort.sort(key = sort_func)\n"
-"\tfor item in mb_userinfo_sort:\n"
-"\t\tprint 'name:%%s, users:%%i, len:%%i' %% (item[0], item[1][0], item[1][1])\n"
- );
+ "mb_userinfo = {}\n"
+ "totmem = 0\n"
+ "for mb_item in membase:\n"
+ "\tmb_item_user_size = mb_userinfo.setdefault(mb_item['name'], [0,0])\n"
+ "\tmb_item_user_size[0] += 1 # Add a user\n"
+ "\tmb_item_user_size[1] += mb_item['len'] # Increment the size\n"
+ "\ttotmem += mb_item['len']\n"
+ "print '(membase) items:', len(membase), '| unique-names:', len(mb_userinfo), '| total-mem:', totmem\n"
+ "mb_userinfo_sort = mb_userinfo.items()\n"
+ "for sort_name, sort_func in (('size', lambda a: -a[1][1]), ('users', lambda a: -a[1][0]), ('name', lambda a: a[0])):\n"
+ "\tprint '\\nSorting by:', sort_name\n"
+ "\tmb_userinfo_sort.sort(key = sort_func)\n"
+ "\tfor item in mb_userinfo_sort:\n"
+ "\t\tprint 'name:%%s, users:%%i, len:%%i' %% (item[0], item[1][0], item[1][1])\n"
+ );
}
mem_unlock_thread();
}
-void MEM_callbackmemlist(void (*func)(void*)) {
+void MEM_callbackmemlist(void (*func)(void *)) {
MemHead *membl;
mem_lock_thread();
@@ -537,9 +538,9 @@ void MEM_callbackmemlist(void (*func)(void*)) {
if (membl) membl = MEMNEXT(membl);
while (membl) {
- func(membl+1);
+ func(membl + 1);
if (membl->next)
- membl= MEMNEXT(membl->next);
+ membl = MEMNEXT(membl->next);
else break;
}
@@ -555,13 +556,13 @@ short MEM_testN(void *vmemh) {
if (membl) membl = MEMNEXT(membl);
while (membl) {
- if (vmemh == membl+1) {
+ if (vmemh == membl + 1) {
mem_unlock_thread();
return 1;
}
if (membl->next)
- membl= MEMNEXT(membl->next);
+ membl = MEMNEXT(membl->next);
else break;
}
@@ -571,42 +572,42 @@ short MEM_testN(void *vmemh) {
return 0;
}
-void MEM_printmemlist( void ) {
+void MEM_printmemlist(void) {
MEM_printmemlist_internal(0);
}
-void MEM_printmemlist_pydict( void ) {
+void MEM_printmemlist_pydict(void) {
MEM_printmemlist_internal(1);
}
-short MEM_freeN(void *vmemh) /* anders compileertie niet meer */
+short MEM_freeN(void *vmemh) /* anders compileertie niet meer */
{
short error = 0;
MemTail *memt;
- MemHead *memh= vmemh;
+ MemHead *memh = vmemh;
const char *name;
if (memh == NULL) {
- MemorY_ErroR("free","attempt to free NULL pointer");
+ MemorY_ErroR("free", "attempt to free NULL pointer");
/* print_error(err_stream, "%d\n", (memh+4000)->tag1); */
return(-1);
}
- if (sizeof(intptr_t)==8) {
+ if (sizeof(intptr_t) == 8) {
if (((intptr_t) memh) & 0x7) {
- MemorY_ErroR("free","attempt to free illegal pointer");
+ MemorY_ErroR("free", "attempt to free illegal pointer");
return(-1);
}
}
else {
if (((intptr_t) memh) & 0x3) {
- MemorY_ErroR("free","attempt to free illegal pointer");
+ MemorY_ErroR("free", "attempt to free illegal pointer");
return(-1);
}
}
memh--;
if (memh->tag1 == MEMFREE && memh->tag2 == MEMFREE) {
- MemorY_ErroR(memh->name,"double free");
+ MemorY_ErroR(memh->name, "double free");
return(-1);
}
@@ -626,18 +627,19 @@ short MEM_freeN(void *vmemh) /* anders compileertie niet meer */
return(0);
}
error = 2;
- MemorY_ErroR(memh->name,"end corrupt");
+ MemorY_ErroR(memh->name, "end corrupt");
name = check_memlist(memh);
if (name != NULL) {
- if (name != memh->name) MemorY_ErroR(name,"is also corrupt");
+ if (name != memh->name) MemorY_ErroR(name, "is also corrupt");
}
- } else{
+ }
+ else {
error = -1;
name = check_memlist(memh);
if (name == NULL)
- MemorY_ErroR("free","pointer not in memlist");
+ MemorY_ErroR("free", "pointer not in memlist");
else
- MemorY_ErroR(name,"error in header");
+ MemorY_ErroR(name, "error in header");
}
totblock--;
@@ -654,7 +656,7 @@ short MEM_freeN(void *vmemh) /* anders compileertie niet meer */
static void addtail(volatile localListBase *listbase, void *vlink)
{
- struct localLink *link= vlink;
+ struct localLink *link = vlink;
if (link == NULL) return;
if (listbase == NULL) return;
@@ -669,7 +671,7 @@ static void addtail(volatile localListBase *listbase, void *vlink)
static void remlink(volatile localListBase *listbase, void *vlink)
{
- struct localLink *link= vlink;
+ struct localLink *link = vlink;
if (link == NULL) return;
if (listbase == NULL) return;
@@ -683,7 +685,7 @@ static void remlink(volatile localListBase *listbase, void *vlink)
static void rem_memblock(MemHead *memh)
{
- remlink(membase,&memh->next);
+ remlink(membase, &memh->next);
if (memh->prev) {
if (memh->next)
MEMNEXT(memh->prev)->nextname = MEMNEXT(memh->next)->name;
@@ -701,14 +703,14 @@ static void rem_memblock(MemHead *memh)
}
else {
if (malloc_debug_memset && memh->len)
- memset(memh+1, 255, memh->len);
+ memset(memh + 1, 255, memh->len);
free(memh);
}
}
static void MemorY_ErroR(const char *block, const char *error)
{
- print_error("Memoryblock %s: %s\n",block, error);
+ print_error("Memoryblock %s: %s\n", block, error);
#ifdef WITH_ASSERT_ABORT
abort();
@@ -717,7 +719,7 @@ static void MemorY_ErroR(const char *block, const char *error)
static const char *check_memlist(MemHead *memh)
{
- MemHead *forw,*back,*forwok,*backok;
+ MemHead *forw, *back, *forwok, *backok;
const char *name;
forw = membase->first;
@@ -779,20 +781,24 @@ static const char *check_memlist(MemHead *memh)
forwok->next = (MemHead *)&backok->next;
backok->prev = (MemHead *)&forwok->next;
forwok->nextname = backok->name;
- } else{
+ }
+ else {
forwok->next = NULL;
membase->last = (struct localLink *) &forwok->next;
}
- } else{
+ }
+ else {
if (backok) {
backok->prev = NULL;
membase->first = &backok->next;
- } else{
+ }
+ else {
membase->first = membase->last = NULL;
}
}
- } else{
- MemorY_ErroR(name,"Additional error in header");
+ }
+ else {
+ MemorY_ErroR(name, "Additional error in header");
return("Additional error in header");
}
@@ -822,7 +828,7 @@ uintptr_t MEM_get_memory_in_use(void)
uintptr_t _mem_in_use;
mem_lock_thread();
- _mem_in_use= mem_in_use;
+ _mem_in_use = mem_in_use;
mem_unlock_thread();
return _mem_in_use;
@@ -833,7 +839,7 @@ uintptr_t MEM_get_mapped_memory_in_use(void)
uintptr_t _mmap_in_use;
mem_lock_thread();
- _mmap_in_use= mmap_in_use;
+ _mmap_in_use = mmap_in_use;
mem_unlock_thread();
return _mmap_in_use;
@@ -844,7 +850,7 @@ int MEM_get_memory_blocks_in_use(void)
int _totblock;
mem_lock_thread();
- _totblock= totblock;
+ _totblock = totblock;
mem_unlock_thread();
return _totblock;
@@ -854,7 +860,7 @@ int MEM_get_memory_blocks_in_use(void)
const char *MEM_name_ptr(void *vmemh)
{
if (vmemh) {
- MemHead *memh= vmemh;
+ MemHead *memh = vmemh;
memh--;
return memh->name;
}