add hgl_align_realloc

This commit is contained in:
2020-09-19 14:57:53 +08:00
parent 2aadf9005c
commit 3e9b703ebc
3 changed files with 23 additions and 17 deletions

View File

@@ -59,11 +59,17 @@ using os_char =wchar_t;
#define hgl_free _aligned_free #define hgl_free _aligned_free
template<typename T> template<typename T>
inline T *hgl_aligned_malloc(size_t n) inline T *hgl_align_malloc(size_t n)
{ {
return (T *)_aligned_malloc(n*sizeof(T),alignof(T)); return (T *)_aligned_malloc(n*sizeof(T),alignof(T));
} }
template<typename T>
inline T *hgl_align_realloc(T *ptr,size_t n)
{
return (T *)_aligned_realloc(ptr,n*sizeof(T),alignof(T));
}
#define OS_EXTERNAL_H <winbase.h> #define OS_EXTERNAL_H <winbase.h>
using ExternalModulePointer =HMODULE; using ExternalModulePointer =HMODULE;
#define pi_get GetProcAddress #define pi_get GetProcAddress

View File

@@ -80,7 +80,7 @@ namespace hgl
{ {
count=1; count=1;
max_count=1; max_count=1;
items=hgl_aligned_malloc<T>(1); items=hgl_align_malloc<T>(1);
return items; return items;
} }
@@ -88,7 +88,7 @@ namespace hgl
{ {
max_count=power_to_2(count+1); max_count=power_to_2(count+1);
items=(T *)hgl_realloc(items,max_count*sizeof(T)); items=(T *)hgl_align_realloc<T>(items,max_count);
++count; ++count;
return(items+(count-1)); return(items+(count-1));
@@ -107,13 +107,13 @@ namespace hgl
{ {
count=0; count=0;
max_count=1; max_count=1;
items=hgl_aligned_malloc<T>(1); items=hgl_align_malloc<T>(1);
} }
else else
{ {
max_count=power_to_2(count+1); max_count=power_to_2(count+1);
items=(T *)hgl_realloc(items,max_count*sizeof(T)); items=(T *)hgl_align_realloc<T>(items,max_count);
} }
memcpy(items+count,&data,sizeof(T));//items[count]=data; memcpy(items+count,&data,sizeof(T));//items[count]=data;
@@ -136,13 +136,13 @@ namespace hgl
{ {
count=0; count=0;
max_count=power_to_2(n); max_count=power_to_2(n);
items=hgl_aligned_malloc<T>(max_count); items=hgl_align_malloc<T>(max_count);
} }
else else
{ {
max_count=power_to_2(count+n); max_count=power_to_2(count+n);
items=(T *)hgl_realloc(items,max_count*sizeof(T)); items=(T *)hgl_align_realloc<T>(items,max_count);
} }
T *p=items; T *p=items;
@@ -172,13 +172,13 @@ namespace hgl
count=0; count=0;
max_count=power_to_2(n); max_count=power_to_2(n);
items=hgl_aligned_malloc<T>(max_count); items=hgl_align_malloc<T>(max_count);
} }
else else
{ {
max_count=power_to_2(count+n); max_count=power_to_2(count+n);
items=(T *)hgl_realloc(items,max_count*sizeof(T)); items=(T *)hgl_align_realloc<T>(items,max_count);
} }
memcpy(items+count,data,n*sizeof(T)); memcpy(items+count,data,n*sizeof(T));
@@ -396,13 +396,13 @@ namespace hgl
{ {
max_count=1; max_count=1;
items=hgl_aligned_malloc<T>(max_count); items=hgl_align_malloc<T>(max_count);
} }
else else
{ {
max_count=power_to_2(count+1); max_count=power_to_2(count+1);
items=(T *)hgl_realloc(items,max_count*sizeof(T)); items=(T *)hgl_align_realloc<T>(items,max_count);
} }
memmove(items+index+1,items+index,(count-index)*sizeof(T)); memmove(items+index+1,items+index,(count-index)*sizeof(T));
@@ -447,9 +447,9 @@ namespace hgl
max_count=power_to_2(new_count); max_count=power_to_2(new_count);
if(!items) if(!items)
items=hgl_aligned_malloc<T>(max_count); items=hgl_align_malloc<T>(max_count);
else else
items=(T *)hgl_realloc(items,max_count*sizeof(T)); items=(T *)hgl_align_realloc<T>(items,max_count);
} }
template<typename T> template<typename T>

View File

@@ -16,7 +16,7 @@ namespace hgl
{ {
max_count=m; max_count=m;
items=hgl_aligned_malloc<T>(max_count); items=hgl_align_malloc<T>(max_count);
} }
else max_count=0; else max_count=0;
@@ -41,14 +41,14 @@ namespace hgl
{ {
alloc_count=power_to_2(count); alloc_count=power_to_2(count);
items=(T *)hgl_realloc(items,alloc_count*sizeof(T)); items=(T *)hgl_align_realloc<T>(items,alloc_count);
} }
} }
else else
{ {
alloc_count=power_to_2(count); alloc_count=power_to_2(count);
items=hgl_aligned_malloc<T>(alloc_count); items=hgl_align_malloc<T>(alloc_count);
} }
return(true); return(true);
@@ -243,7 +243,7 @@ namespace hgl
else else
alloc_count=max_count; alloc_count=max_count;
items=hgl_aligned_malloc<T>(alloc_count); items=hgl_align_malloc<T>(alloc_count);
memcpy(items,ori.items,alloc_count*sizeof(T)); memcpy(items,ori.items,alloc_count*sizeof(T));
} }