godot/drivers/unix/memory_pool_static_malloc.cpp

421 lines
10 KiB
C++
Raw Normal View History

2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* memory_pool_static_malloc.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 01:10:30 +00:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "memory_pool_static_malloc.h"
#include "error_macros.h"
#include "os/copymem.h"
#include "os/memory.h"
2014-02-10 01:10:30 +00:00
#include "os/os.h"
#include <stdio.h>
#include <stdlib.h>
2014-02-10 01:10:30 +00:00
/**
* NOTE NOTE NOTE NOTE
* in debug mode, this prepends the memory size to the allocated block
* so BE CAREFUL!
*/
void *MemoryPoolStaticMalloc::alloc(size_t p_bytes, const char *p_description) {
2014-02-10 01:10:30 +00:00
#if DEFAULT_ALIGNMENT == 1
2014-02-10 01:10:30 +00:00
return _alloc(p_bytes, p_description);
2014-02-10 01:10:30 +00:00
#else
2014-02-10 01:10:30 +00:00
size_t total;
#if defined(_add_overflow)
if (_add_overflow(p_bytes, DEFAULT_ALIGNMENT, &total)) return NULL;
#else
total = p_bytes + DEFAULT_ALIGNMENT;
#endif
uint8_t *ptr = (uint8_t *)_alloc(total, p_description);
ERR_FAIL_COND_V(!ptr, ptr);
int ofs = (DEFAULT_ALIGNMENT - ((uintptr_t)ptr & (DEFAULT_ALIGNMENT - 1)));
ptr[ofs - 1] = ofs;
return (void *)(ptr + ofs);
#endif
2014-02-10 01:10:30 +00:00
};
void *MemoryPoolStaticMalloc::_alloc(size_t p_bytes, const char *p_description) {
ERR_FAIL_COND_V(p_bytes == 0, 0);
2014-02-10 01:10:30 +00:00
MutexLock lock(mutex);
2014-02-10 01:10:30 +00:00
#ifdef DEBUG_MEMORY_ENABLED
size_t total;
#if defined(_add_overflow)
if (_add_overflow(p_bytes, sizeof(RingPtr), &total)) return NULL;
#else
total = p_bytes + sizeof(RingPtr);
#endif
void *mem = malloc(total); /// add for size and ringlist
2014-02-10 01:10:30 +00:00
if (!mem) {
printf("**ERROR: out of memory while allocating %lu bytes by %s?\n", (unsigned long)p_bytes, p_description);
printf("**ERROR: memory usage is %lu\n", (unsigned long)get_total_usage());
2014-02-10 01:10:30 +00:00
};
ERR_FAIL_COND_V(!mem, 0); //out of memory, or unreasonable request
2014-02-10 01:10:30 +00:00
/* setup the ringlist element */
RingPtr *ringptr = (RingPtr *)mem;
2014-02-10 01:10:30 +00:00
/* setup the ringlist element data (description and size ) */
2014-02-10 01:10:30 +00:00
ringptr->size = p_bytes;
ringptr->descr = p_description;
2014-02-10 01:10:30 +00:00
if (ringlist) { /* existing ringlist */
2014-02-10 01:10:30 +00:00
/* assign next */
ringptr->next = ringlist->next;
ringlist->next = ringptr;
/* assign prev */
ringptr->prev = ringlist;
ringptr->next->prev = ringptr;
} else { /* non existing ringlist */
ringptr->next = ringptr;
ringptr->prev = ringptr;
ringlist = ringptr;
2014-02-10 01:10:30 +00:00
}
total_mem += p_bytes;
2014-02-10 01:10:30 +00:00
/* update statistics */
if (total_mem > max_mem)
2014-02-10 01:10:30 +00:00
max_mem = total_mem;
2014-02-10 01:10:30 +00:00
total_pointers++;
2014-02-10 01:10:30 +00:00
if (total_pointers > max_pointers)
max_pointers = total_pointers;
2014-02-10 01:10:30 +00:00
return ringptr + 1; /* return memory after ringptr */
#else
void *mem = malloc(p_bytes);
ERR_FAIL_COND_V(!mem, 0); //out of memory, or unreasonable request
return mem;
#endif
2014-02-10 01:10:30 +00:00
}
void *MemoryPoolStaticMalloc::realloc(void *p_memory, size_t p_bytes) {
2014-02-10 01:10:30 +00:00
#if DEFAULT_ALIGNMENT == 1
return _realloc(p_memory, p_bytes);
#else
if (!p_memory)
return alloc(p_bytes);
size_t total;
#if defined(_add_overflow)
if (_add_overflow(p_bytes, DEFAULT_ALIGNMENT, &total)) return NULL;
#else
total = p_bytes + DEFAULT_ALIGNMENT;
#endif
uint8_t *mem = (uint8_t *)p_memory;
int ofs = *(mem - 1);
mem = mem - ofs;
uint8_t *ptr = (uint8_t *)_realloc(mem, total);
ERR_FAIL_COND_V(ptr == NULL, NULL);
int new_ofs = (DEFAULT_ALIGNMENT - ((uintptr_t)ptr & (DEFAULT_ALIGNMENT - 1)));
if (new_ofs != ofs) {
//printf("realloc moving %i bytes\n", p_bytes);
movemem((ptr + new_ofs), (ptr + ofs), p_bytes);
ptr[new_ofs - 1] = new_ofs;
};
return ptr + new_ofs;
#endif
2014-02-10 01:10:30 +00:00
};
void *MemoryPoolStaticMalloc::_realloc(void *p_memory, size_t p_bytes) {
if (p_memory == NULL) {
return alloc(p_bytes);
2014-02-10 01:10:30 +00:00
}
if (p_bytes == 0) {
2014-02-10 01:10:30 +00:00
this->free(p_memory);
ERR_FAIL_COND_V(p_bytes < 0, NULL);
2014-02-10 01:10:30 +00:00
return NULL;
}
2014-02-10 01:10:30 +00:00
MutexLock lock(mutex);
2014-02-10 01:10:30 +00:00
#ifdef DEBUG_MEMORY_ENABLED
RingPtr *ringptr = (RingPtr *)p_memory;
2014-02-10 01:10:30 +00:00
ringptr--; /* go back an element to find the tingptr */
2014-02-10 01:10:30 +00:00
bool single_element = (ringptr->next == ringptr) && (ringptr->prev == ringptr);
bool is_list = (ringlist == ringptr);
RingPtr *new_ringptr = (RingPtr *)::realloc(ringptr, p_bytes + sizeof(RingPtr));
ERR_FAIL_COND_V(new_ringptr == 0, NULL); /// reallocation failed
2014-02-10 01:10:30 +00:00
/* actualize mem used */
total_mem -= new_ringptr->size;
new_ringptr->size = p_bytes;
total_mem += new_ringptr->size;
if (total_mem > max_mem) //update statistics
2014-02-10 01:10:30 +00:00
max_mem = total_mem;
if (new_ringptr == ringptr)
2014-02-10 01:10:30 +00:00
return ringptr + 1; // block didn't move, don't do anything
2014-02-10 01:10:30 +00:00
if (single_element) {
new_ringptr->next = new_ringptr;
new_ringptr->prev = new_ringptr;
2014-02-10 01:10:30 +00:00
} else {
new_ringptr->next->prev = new_ringptr;
new_ringptr->prev->next = new_ringptr;
2014-02-10 01:10:30 +00:00
}
2014-02-10 01:10:30 +00:00
if (is_list)
ringlist = new_ringptr;
2014-02-10 01:10:30 +00:00
return new_ringptr + 1;
2014-02-10 01:10:30 +00:00
#else
return ::realloc(p_memory, p_bytes);
2014-02-10 01:10:30 +00:00
#endif
}
void MemoryPoolStaticMalloc::free(void *p_ptr) {
ERR_FAIL_COND(!MemoryPoolStatic::get_singleton());
2014-02-10 01:10:30 +00:00
#if DEFAULT_ALIGNMENT == 1
2014-02-10 01:10:30 +00:00
_free(p_ptr);
#else
2014-02-10 01:10:30 +00:00
uint8_t *mem = (uint8_t *)p_ptr;
int ofs = *(mem - 1);
mem = mem - ofs;
2014-02-10 01:10:30 +00:00
_free(mem);
#endif
2014-02-10 01:10:30 +00:00
};
void MemoryPoolStaticMalloc::_free(void *p_ptr) {
2014-02-10 01:10:30 +00:00
MutexLock lock(mutex);
2014-02-10 01:10:30 +00:00
#ifdef DEBUG_MEMORY_ENABLED
if (p_ptr == 0) {
2014-02-10 01:10:30 +00:00
printf("**ERROR: STATIC ALLOC: Attempted free of NULL pointer.\n");
return;
};
RingPtr *ringptr = (RingPtr *)p_ptr;
2014-02-10 01:10:30 +00:00
ringptr--; /* go back an element to find the ringptr */
2014-02-10 01:10:30 +00:00
#if 0
{ // check for existing memory on free.
RingPtr *p = ringlist;
bool found=false;
if (ringlist) {
do {
if (p==ringptr) {
found=true;
break;
}
p=p->next;
} while (p!=ringlist);
}
if (!found) {
printf("**ERROR: STATIC ALLOC: Attempted free of unknown pointer at %p\n",(ringptr+1));
return;
}
}
#endif
2014-02-10 01:10:30 +00:00
/* proceed to erase */
2014-02-10 01:10:30 +00:00
bool single_element = (ringptr->next == ringptr) && (ringptr->prev == ringptr);
bool is_list = (ringlist == ringptr);
2014-02-10 01:10:30 +00:00
if (single_element) {
/* just get rid of it */
ringlist = 0;
2014-02-10 01:10:30 +00:00
} else {
/* auto-remove from ringlist */
if (is_list)
ringlist = ringptr->next;
2014-02-10 01:10:30 +00:00
ringptr->prev->next = ringptr->next;
ringptr->next->prev = ringptr->prev;
}
2014-02-10 01:10:30 +00:00
total_mem -= ringptr->size;
total_pointers--;
// catch more errors
zeromem(ringptr, sizeof(RingPtr) + ringptr->size);
2014-02-10 01:10:30 +00:00
::free(ringptr); //just free that pointer
2014-02-10 01:10:30 +00:00
#else
ERR_FAIL_COND(p_ptr == 0);
2014-02-10 01:10:30 +00:00
::free(p_ptr);
#endif
}
size_t MemoryPoolStaticMalloc::get_available_mem() const {
return 0xffffffff;
}
size_t MemoryPoolStaticMalloc::get_total_usage() {
2014-02-10 01:10:30 +00:00
#ifdef DEBUG_MEMORY_ENABLED
2014-02-10 01:10:30 +00:00
return total_mem;
#else
2014-02-10 01:10:30 +00:00
return 0;
#endif
}
size_t MemoryPoolStaticMalloc::get_max_usage() {
return max_mem;
}
2014-02-10 01:10:30 +00:00
/* Most likely available only if memory debugger was compiled in */
int MemoryPoolStaticMalloc::get_alloc_count() {
2016-01-24 05:00:52 +00:00
return total_pointers;
2014-02-10 01:10:30 +00:00
}
void *MemoryPoolStaticMalloc::get_alloc_ptr(int p_alloc_idx) {
2014-02-10 01:10:30 +00:00
return 0;
}
const char *MemoryPoolStaticMalloc::get_alloc_description(int p_alloc_idx) {
2014-02-10 01:10:30 +00:00
return "";
}
size_t MemoryPoolStaticMalloc::get_alloc_size(int p_alloc_idx) {
2014-02-10 01:10:30 +00:00
return 0;
}
void MemoryPoolStaticMalloc::dump_mem_to_file(const char *p_file) {
2014-02-10 01:10:30 +00:00
#ifdef DEBUG_MEMORY_ENABLED
ERR_FAIL_COND(!ringlist); /** WTF BUG !? */
2014-02-10 01:10:30 +00:00
RingPtr *p = ringlist;
FILE *f = fopen(p_file, "wb");
2014-02-10 01:10:30 +00:00
do {
fprintf(f, "%p-%i-%s\n", p + 1, (int)p->size, (p->descr ? p->descr : ""));
p = p->next;
} while (p != ringlist);
2014-02-10 01:10:30 +00:00
fclose(f);
#endif
2014-02-10 01:10:30 +00:00
}
MemoryPoolStaticMalloc::MemoryPoolStaticMalloc() {
#ifdef DEBUG_MEMORY_ENABLED
total_mem = 0;
total_pointers = 0;
ringlist = 0;
max_mem = 0;
max_pointers = 0;
2014-02-10 01:10:30 +00:00
#endif
mutex = NULL;
2014-02-10 01:10:30 +00:00
#ifndef NO_THREADS
mutex = Mutex::create(); // at this point, this should work
2014-02-10 01:10:30 +00:00
#endif
}
MemoryPoolStaticMalloc::~MemoryPoolStaticMalloc() {
Mutex *old_mutex = mutex;
mutex = NULL;
2014-02-10 01:10:30 +00:00
if (old_mutex)
memdelete(old_mutex);
2014-02-10 01:10:30 +00:00
#ifdef DEBUG_MEMORY_ENABLED
if (OS::get_singleton()->is_stdout_verbose()) {
if (total_mem > 0) {
2014-02-10 01:10:30 +00:00
printf("**ERROR: STATIC ALLOC: ** MEMORY LEAKS DETECTED **\n");
printf("**ERROR: STATIC ALLOC: %i bytes of memory in use at exit.\n", (int)total_mem);
2014-02-10 01:10:30 +00:00
if (1) {
2014-02-10 01:10:30 +00:00
printf("**ERROR: STATIC ALLOC: Following is the list of leaked allocations: \n");
ERR_FAIL_COND(!ringlist); /** WTF BUG !? */
2014-02-10 01:10:30 +00:00
RingPtr *p = ringlist;
do {
printf("\t%p - %i bytes - %s\n", (RingPtr *)(p + 1), (int)p->size, (p->descr ? p->descr : ""));
p = p->next;
} while (p != ringlist);
2014-02-10 01:10:30 +00:00
printf("**ERROR: STATIC ALLOC: End of Report.\n");
};
printf("mem - max %i, pointers %i, leaks %i.\n", (int)max_mem, max_pointers, (int)total_mem);
2014-02-10 01:10:30 +00:00
} else {
printf("INFO: mem - max %i, pointers %i, no leaks.\n", (int)max_mem, max_pointers);
2014-02-10 01:10:30 +00:00
}
}
2014-02-10 01:10:30 +00:00
#endif
}