diff options
Diffstat (limited to 'drivers/unix')
| -rw-r--r-- | drivers/unix/memory_pool_static_malloc.cpp | 29 | ||||
| -rw-r--r-- | drivers/unix/thread_posix.cpp | 17 | ||||
| -rw-r--r-- | drivers/unix/thread_posix.h | 5 |
3 files changed, 31 insertions, 20 deletions
diff --git a/drivers/unix/memory_pool_static_malloc.cpp b/drivers/unix/memory_pool_static_malloc.cpp index 1a79272dc1..f89b55de12 100644 --- a/drivers/unix/memory_pool_static_malloc.cpp +++ b/drivers/unix/memory_pool_static_malloc.cpp @@ -48,7 +48,12 @@ void* MemoryPoolStaticMalloc::alloc(size_t p_bytes,const char *p_description) { #else - int total = p_bytes + DEFAULT_ALIGNMENT; + 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))); @@ -64,11 +69,18 @@ void* MemoryPoolStaticMalloc::_alloc(size_t p_bytes,const char *p_description) { MutexLock lock(mutex); #ifdef DEBUG_MEMORY_ENABLED - void *mem=malloc(p_bytes+sizeof(RingPtr)); /// add for size and ringlist + + 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 if (!mem) { - printf("**ERROR: out of memory while allocating %i bytes by %s?\n",(int) p_bytes, p_description); - printf("**ERROR: memory usage is %i\n", (int)get_total_usage()); + 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()); }; ERR_FAIL_COND_V(!mem,0); //out of memory, or unreasonable request @@ -129,7 +141,12 @@ void* MemoryPoolStaticMalloc::realloc(void *p_memory,size_t p_bytes) { if (!p_memory) return alloc(p_bytes); - int total = p_bytes + DEFAULT_ALIGNMENT; + 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; @@ -321,7 +338,7 @@ size_t MemoryPoolStaticMalloc::get_max_usage() { /* Most likely available only if memory debugger was compiled in */ int MemoryPoolStaticMalloc::get_alloc_count() { - return 0; + return total_pointers; } void * MemoryPoolStaticMalloc::get_alloc_ptr(int p_alloc_idx) { diff --git a/drivers/unix/thread_posix.cpp b/drivers/unix/thread_posix.cpp index bd33c81298..6ace64a923 100644 --- a/drivers/unix/thread_posix.cpp +++ b/drivers/unix/thread_posix.cpp @@ -81,9 +81,9 @@ void ThreadPosix::wait_to_finish_func_posix(Thread* p_thread) { tp->pthread=0; } -Error ThreadPosix::set_name(const String& p_name) { +Error ThreadPosix::set_name_func_posix(const String& p_name) { - ERR_FAIL_COND_V(pthread == 0, ERR_UNCONFIGURED); + pthread_t running_thread = pthread_self(); #ifdef PTHREAD_NO_RENAME return ERR_UNAVAILABLE; @@ -93,22 +93,15 @@ Error ThreadPosix::set_name(const String& p_name) { #ifdef PTHREAD_RENAME_SELF // check if thread is the same as caller - int caller = Thread::get_caller_ID(); - int self = get_ID(); - if (caller != self) { - ERR_EXPLAIN("On this platform, thread can only be renamed with calls from the threads to be renamed."); - ERR_FAIL_V(ERR_UNAVAILABLE); - return ERR_UNAVAILABLE; - }; int err = pthread_setname_np(p_name.utf8().get_data()); #else #ifdef PTHREAD_BSD_SET_NAME - pthread_set_name_np(pthread, p_name.utf8().get_data()); + pthread_set_name_np(running_thread, p_name.utf8().get_data()); int err = 0; // Open/FreeBSD ignore errors in this function #else - int err = pthread_setname_np(pthread, p_name.utf8().get_data()); + int err = pthread_setname_np(running_thread, p_name.utf8().get_data()); #endif // PTHREAD_BSD_SET_NAME #endif // PTHREAD_RENAME_SELF @@ -123,7 +116,7 @@ void ThreadPosix::make_default() { create_func=create_func_posix; get_thread_ID_func=get_thread_ID_func_posix; wait_to_finish_func=wait_to_finish_func_posix; - + set_name_func = set_name_func_posix; } ThreadPosix::ThreadPosix() { diff --git a/drivers/unix/thread_posix.h b/drivers/unix/thread_posix.h index 179d56d5bd..06a17c2ae6 100644 --- a/drivers/unix/thread_posix.h +++ b/drivers/unix/thread_posix.h @@ -55,13 +55,14 @@ class ThreadPosix : public Thread { static Thread* create_func_posix(ThreadCreateCallback p_callback,void *,const Settings&); static ID get_thread_ID_func_posix(); static void wait_to_finish_func_posix(Thread* p_thread); - + + static Error set_name_func_posix(const String& p_name); + ThreadPosix(); public: virtual ID get_ID() const; - Error set_name(const String& p_name); static void make_default(); |