aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2018-11-18 00:38:31 -0800
committerChris Robinson <[email protected]>2018-11-18 00:38:31 -0800
commitd7cc9b912b71b17d6cd1bb9726673b79a4c0173a (patch)
tree591ba44cad8cf3d7d6ec2303c8fab49e954f5168
parent38d6df9c1d10ac74af3454c67147dd21bb0a7bb8 (diff)
Use new/delete for ALCcontext objects
-rw-r--r--Alc/alc.cpp18
-rw-r--r--Alc/alcontext.h3
-rw-r--r--Alc/bformatdec.cpp6
-rw-r--r--common/almalloc.cpp6
-rw-r--r--common/almalloc.h23
5 files changed, 22 insertions, 34 deletions
diff --git a/Alc/alc.cpp b/Alc/alc.cpp
index 7b55f120..1d6d55e0 100644
--- a/Alc/alc.cpp
+++ b/Alc/alc.cpp
@@ -2791,9 +2791,7 @@ static void FreeContext(ALCcontext *context)
ALCdevice_DecRef(context->Device);
context->Device = nullptr;
- //Invalidate context
- memset(context, 0, sizeof(ALCcontext));
- al_free(context);
+ delete context;
}
/* ReleaseContext
@@ -3791,19 +3789,7 @@ ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCin
ATOMIC_STORE_SEQ(&device->LastError, ALC_NO_ERROR);
- if(device->Type == Playback && DefaultEffect.type != AL_EFFECT_NULL)
- ALContext = static_cast<ALCcontext*>(al_calloc(16,
- sizeof(ALCcontext)+sizeof(ALlistener)+sizeof(ALeffectslot)));
- else
- ALContext = static_cast<ALCcontext*>(al_calloc(16, sizeof(ALCcontext)+sizeof(ALlistener)));
- if(!ALContext)
- {
- almtx_unlock(&device->BackendLock);
-
- alcSetError(device, ALC_OUT_OF_MEMORY);
- ALCdevice_DecRef(device);
- return nullptr;
- }
+ ALContext = new ALCcontext{};
InitRef(&ALContext->ref, 1);
ALContext->DefaultSlot = nullptr;
diff --git a/Alc/alcontext.h b/Alc/alcontext.h
index 16a5e909..f04607a9 100644
--- a/Alc/alcontext.h
+++ b/Alc/alcontext.h
@@ -9,6 +9,7 @@
#include "atomic.h"
#include "vector.h"
#include "threads.h"
+#include "almalloc.h"
#include "alListener.h"
@@ -113,6 +114,8 @@ struct ALCcontext_struct {
ATOMIC(ALCcontext*) next;
ALlistener Listener;
+
+ DEF_NEWDEL(ALCcontext)
};
ALCcontext *GetContextRef(void);
diff --git a/Alc/bformatdec.cpp b/Alc/bformatdec.cpp
index afa25461..6202d7c1 100644
--- a/Alc/bformatdec.cpp
+++ b/Alc/bformatdec.cpp
@@ -164,8 +164,7 @@ struct BFormatDec {
ALsizei NumChannels;
ALboolean DualBand;
- void *operator new(size_t size) { return al_malloc(alignof(BFormatDec), size); }
- void operator delete(void *block) { al_free(block); }
+ DEF_NEWDEL(BFormatDec)
};
BFormatDec *bformatdec_alloc()
@@ -437,8 +436,7 @@ struct AmbiUpsampler {
ALfloat Gains[4][MAX_OUTPUT_CHANNELS][NUM_BANDS];
- void *operator new(size_t size) { return al_malloc(alignof(AmbiUpsampler), size); }
- void operator delete(void *block) { al_free(block); }
+ DEF_NEWDEL(AmbiUpsampler)
};
AmbiUpsampler *ambiup_alloc()
diff --git a/common/almalloc.cpp b/common/almalloc.cpp
index 1b90d1c0..6dcb6cfc 100644
--- a/common/almalloc.cpp
+++ b/common/almalloc.cpp
@@ -55,7 +55,7 @@ void *al_calloc(size_t alignment, size_t size)
return ret;
}
-void al_free(void *ptr)
+void al_free(void *ptr) noexcept
{
#if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
free(ptr);
@@ -73,7 +73,7 @@ void al_free(void *ptr)
#endif
}
-size_t al_get_page_size(void)
+size_t al_get_page_size(void) noexcept
{
static size_t psize = 0;
if(UNLIKELY(!psize))
@@ -100,7 +100,7 @@ size_t al_get_page_size(void)
return psize;
}
-int al_is_sane_alignment_allocator(void)
+int al_is_sane_alignment_allocator(void) noexcept
{
#if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN) || defined(HAVE__ALIGNED_MALLOC)
return 1;
diff --git a/common/almalloc.h b/common/almalloc.h
index a4297cf5..ad48db8b 100644
--- a/common/almalloc.h
+++ b/common/almalloc.h
@@ -3,28 +3,29 @@
#include <stddef.h>
-#ifdef __cplusplus
-extern "C" {
-#endif
-
/* Minimum alignment required by posix_memalign. */
#define DEF_ALIGN sizeof(void*)
void *al_malloc(size_t alignment, size_t size);
void *al_calloc(size_t alignment, size_t size);
-void al_free(void *ptr);
+void al_free(void *ptr) noexcept;
-size_t al_get_page_size(void);
+size_t al_get_page_size(void) noexcept;
/**
* Returns non-0 if the allocation function has direct alignment handling.
* Otherwise, the standard malloc is used with an over-allocation and pointer
* offset strategy.
*/
-int al_is_sane_alignment_allocator(void);
-
-#ifdef __cplusplus
-}
-#endif
+int al_is_sane_alignment_allocator(void) noexcept;
+
+#define DEF_NEWDEL(T) \
+ void *operator new(size_t size) \
+ { \
+ void *ret = al_malloc(alignof(T), size); \
+ if(!ret) throw std::bad_alloc(); \
+ return ret; \
+ } \
+ void operator delete(void *block) noexcept { al_free(block); }
#endif /* AL_MALLOC_H */