Make Blender malloc wrapper be 64 bit ready.

This commit is contained in:
Guillermo S. Romero
2010-04-29 17:34:40 +00:00
parent f63de81223
commit 1fc7ea774f
2 changed files with 24 additions and 17 deletions

View File

@@ -89,18 +89,18 @@ extern "C" {
* allocated block, the old one is freed. this is not as optimized
* as a system realloc but just makes a new allocation and copies
* over from existing memory. */
void *MEM_reallocN(void *vmemh, unsigned int len);
void *MEM_reallocN(void *vmemh, size_t len);
/**
* Allocate a block of memory of size len, with tag name str. The
* memory is cleared. The name must be static, because only a
* pointer to it is stored ! */
void *MEM_callocN(unsigned int len, const char * str);
void *MEM_callocN(size_t len, const char * str);
/** Allocate a block of memory of size len, with tag name str. The
* name must be a static, because only a pointer to it is stored !
* */
void *MEM_mallocN(unsigned int len, const char * str);
void *MEM_mallocN(size_t len, const char * str);
/** Same as callocN, clears memory and uses mmap (disk cached) if supported.
Can be free'd with MEM_freeN as usual.

View File

@@ -36,13 +36,20 @@
#include <stdlib.h>
#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))
#else
#define SIZET_FORMAT "%lu"
#define SIZET_ARG(a) ((unsigned long)(a))
#endif
/* mmap exception */
#if defined(WIN32)
#include <sys/types.h>
#include "mmap_win.h"
#else
#include <sys/types.h>
#include <sys/mman.h>
#endif
@@ -82,7 +89,7 @@ typedef struct localListBase
/* note: keep this struct aligned (e.g., irix/gcc) - Hos */
typedef struct MemHead {
int tag1;
unsigned int len;
size_t len;
struct MemHead *next,*prev;
const char * name;
const char * nextname;
@@ -245,7 +252,7 @@ void *MEM_dupallocN(void *vmemh)
return newp;
}
void *MEM_reallocN(void *vmemh, unsigned int len)
void *MEM_reallocN(void *vmemh, size_t len)
{
void *newp= NULL;
@@ -267,7 +274,7 @@ void *MEM_reallocN(void *vmemh, unsigned int len)
return newp;
}
static void make_memhead_header(MemHead *memh, unsigned int len, const char *str)
static void make_memhead_header(MemHead *memh, size_t len, const char *str)
{
MemTail *memt;
@@ -288,7 +295,7 @@ static void make_memhead_header(MemHead *memh, unsigned int len, const char *str
mem_in_use += len;
}
void *MEM_mallocN(unsigned int len, const char *str)
void *MEM_mallocN(size_t len, const char *str)
{
MemHead *memh;
@@ -312,11 +319,11 @@ void *MEM_mallocN(unsigned int len, const char *str)
return (++memh);
}
mem_unlock_thread();
print_error("Malloc returns nill: len=%d in %s, total %u\n",len, str, mem_in_use);
print_error("Malloc returns null: len=" SIZET_FORMAT " in %s, total %u\n", SIZET_ARG(len), str, mem_in_use);
return NULL;
}
void *MEM_callocN(unsigned int len, const char *str)
void *MEM_callocN(size_t len, const char *str)
{
MemHead *memh;
@@ -337,12 +344,12 @@ void *MEM_callocN(unsigned int len, const char *str)
return (++memh);
}
mem_unlock_thread();
print_error("Calloc returns nill: len=%d in %s, total %u\n",len, str, mem_in_use);
print_error("Calloc returns null: len=" SIZET_FORMAT " in %s, total %u\n", SIZET_ARG(len), str, mem_in_use);
return 0;
}
/* note; mmap returns zero'd memory */
void *MEM_mapallocN(unsigned int len, const char *str)
void *MEM_mapallocN(size_t len, const char *str)
{
MemHead *memh;
@@ -380,7 +387,7 @@ void *MEM_mapallocN(unsigned int len, const char *str)
}
else {
mem_unlock_thread();
print_error("Mapalloc returns nill, fallback to regular malloc: len=%d in %s, total %u\n",len, str, mmap_in_use);
print_error("Mapalloc returns null, fallback to regular malloc: len=" SIZET_FORMAT " in %s, total %u\n", SIZET_ARG(len), str, mmap_in_use);
return MEM_callocN(len, str);
}
}
@@ -492,12 +499,12 @@ static void MEM_printmemlist_internal( int pydict )
}
while(membl) {
if (pydict) {
fprintf(stderr, "{'len':%i, 'name':'''%s''', 'pointer':'%p'},\\\n", membl->len, membl->name, membl+1);
fprintf(stderr, "{'len':" SIZET_FORMAT ", 'name':'''%s''', 'pointer':'%p'},\\\n", SIZET_ARG(membl->len), membl->name, membl+1);
} else {
#ifdef DEBUG_MEMCOUNTER
print_error("%s len: %d %p, count: %d\n",membl->name,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: %d %p\n",membl->name,membl->len, membl+1);
print_error("%s len: " SIZET_FORMAT " %p\n", membl->name, SIZET_ARG(membl->len), membl+1);
#endif
}
if(membl->next)