From 3e9b703ebcdf4167707d76b6bbdb752eb63de437 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 19 Sep 2020 14:57:53 +0800 Subject: [PATCH] add hgl_align_realloc --- inc/hgl/platform/os/MSWindows.h | 8 +++++++- inc/hgl/type/List.cpp | 24 ++++++++++++------------ inc/hgl/type/Queue.cpp | 8 ++++---- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/inc/hgl/platform/os/MSWindows.h b/inc/hgl/platform/os/MSWindows.h index b070911..707965c 100644 --- a/inc/hgl/platform/os/MSWindows.h +++ b/inc/hgl/platform/os/MSWindows.h @@ -59,11 +59,17 @@ using os_char =wchar_t; #define hgl_free _aligned_free template -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)); } +template +inline T *hgl_align_realloc(T *ptr,size_t n) +{ + return (T *)_aligned_realloc(ptr,n*sizeof(T),alignof(T)); +} + #define OS_EXTERNAL_H using ExternalModulePointer =HMODULE; #define pi_get GetProcAddress diff --git a/inc/hgl/type/List.cpp b/inc/hgl/type/List.cpp index 7a80302..ea0e757 100644 --- a/inc/hgl/type/List.cpp +++ b/inc/hgl/type/List.cpp @@ -80,7 +80,7 @@ namespace hgl { count=1; max_count=1; - items=hgl_aligned_malloc(1); + items=hgl_align_malloc(1); return items; } @@ -88,7 +88,7 @@ namespace hgl { max_count=power_to_2(count+1); - items=(T *)hgl_realloc(items,max_count*sizeof(T)); + items=(T *)hgl_align_realloc(items,max_count); ++count; return(items+(count-1)); @@ -107,13 +107,13 @@ namespace hgl { count=0; max_count=1; - items=hgl_aligned_malloc(1); + items=hgl_align_malloc(1); } else { max_count=power_to_2(count+1); - items=(T *)hgl_realloc(items,max_count*sizeof(T)); + items=(T *)hgl_align_realloc(items,max_count); } memcpy(items+count,&data,sizeof(T));//items[count]=data; @@ -136,13 +136,13 @@ namespace hgl { count=0; max_count=power_to_2(n); - items=hgl_aligned_malloc(max_count); + items=hgl_align_malloc(max_count); } else { max_count=power_to_2(count+n); - items=(T *)hgl_realloc(items,max_count*sizeof(T)); + items=(T *)hgl_align_realloc(items,max_count); } T *p=items; @@ -172,13 +172,13 @@ namespace hgl count=0; max_count=power_to_2(n); - items=hgl_aligned_malloc(max_count); + items=hgl_align_malloc(max_count); } else { max_count=power_to_2(count+n); - items=(T *)hgl_realloc(items,max_count*sizeof(T)); + items=(T *)hgl_align_realloc(items,max_count); } memcpy(items+count,data,n*sizeof(T)); @@ -396,13 +396,13 @@ namespace hgl { max_count=1; - items=hgl_aligned_malloc(max_count); + items=hgl_align_malloc(max_count); } else { max_count=power_to_2(count+1); - items=(T *)hgl_realloc(items,max_count*sizeof(T)); + items=(T *)hgl_align_realloc(items,max_count); } memmove(items+index+1,items+index,(count-index)*sizeof(T)); @@ -447,9 +447,9 @@ namespace hgl max_count=power_to_2(new_count); if(!items) - items=hgl_aligned_malloc(max_count); + items=hgl_align_malloc(max_count); else - items=(T *)hgl_realloc(items,max_count*sizeof(T)); + items=(T *)hgl_align_realloc(items,max_count); } template diff --git a/inc/hgl/type/Queue.cpp b/inc/hgl/type/Queue.cpp index 3c08ac7..a04b973 100644 --- a/inc/hgl/type/Queue.cpp +++ b/inc/hgl/type/Queue.cpp @@ -16,7 +16,7 @@ namespace hgl { max_count=m; - items=hgl_aligned_malloc(max_count); + items=hgl_align_malloc(max_count); } else max_count=0; @@ -41,14 +41,14 @@ namespace hgl { alloc_count=power_to_2(count); - items=(T *)hgl_realloc(items,alloc_count*sizeof(T)); + items=(T *)hgl_align_realloc(items,alloc_count); } } else { alloc_count=power_to_2(count); - items=hgl_aligned_malloc(alloc_count); + items=hgl_align_malloc(alloc_count); } return(true); @@ -243,7 +243,7 @@ namespace hgl else alloc_count=max_count; - items=hgl_aligned_malloc(alloc_count); + items=hgl_align_malloc(alloc_count); memcpy(items,ori.items,alloc_count*sizeof(T)); }