[fix] memcpy in aligned vector/matrix resize needs to know the original size. Since this is an internal static function and does not affect client APIs, changing it to aligned_malloc, aligned_free, and aligned_resize, which takes the original pointer size as well as the new. On Windows it simply passes the pointer, new size, and alignment through _aligned_realloc, whereas on other platforms only the aligned_malloc is for new_size bytes and uses old_size bytes for memcpy
This commit is contained in:
@@ -33,7 +33,7 @@ typedef enum {
|
|||||||
} name##_t; \
|
} name##_t; \
|
||||||
\
|
\
|
||||||
static name##_t *name##_new(size_t m, size_t n) { \
|
static name##_t *name##_new(size_t m, size_t n) { \
|
||||||
name##_t *matrix = malloc(sizeof(name##_t)); \
|
name##_t *matrix = malloc(sizeof(name##_t)); \
|
||||||
\
|
\
|
||||||
if (matrix == NULL) { \
|
if (matrix == NULL) { \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
@@ -62,7 +62,7 @@ typedef enum {
|
|||||||
matrix->m = m; \
|
matrix->m = m; \
|
||||||
matrix->n = n; \
|
matrix->n = n; \
|
||||||
\
|
\
|
||||||
matrix->values = _aligned_malloc(sizeof(type) * m * n, alignment); \
|
matrix->values = aligned_malloc(sizeof(type) * m * n, alignment); \
|
||||||
if (matrix->values == NULL) { \
|
if (matrix->values == NULL) { \
|
||||||
free(matrix); \
|
free(matrix); \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
@@ -86,7 +86,7 @@ typedef enum {
|
|||||||
if (self == NULL) return; \
|
if (self == NULL) return; \
|
||||||
\
|
\
|
||||||
if (self->values != NULL) { \
|
if (self->values != NULL) { \
|
||||||
_aligned_free(self->values); \
|
aligned_free(self->values); \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
free(self); \
|
free(self); \
|
||||||
@@ -118,7 +118,7 @@ typedef enum {
|
|||||||
if (self == NULL) return false; \
|
if (self == NULL) return false; \
|
||||||
\
|
\
|
||||||
if (m * n > (self->m * self->n)) { \
|
if (m * n > (self->m * self->n)) { \
|
||||||
type *ptr = _aligned_realloc(self->values, sizeof(type) * m * n, alignment); \
|
type *ptr = aligned_resize(self->values, sizeof(type) * self->m * self->n, sizeof(type) * m * n, alignment); \
|
||||||
if (ptr == NULL) { \
|
if (ptr == NULL) { \
|
||||||
return false; \
|
return false; \
|
||||||
} \
|
} \
|
||||||
|
|||||||
27
src/vector.h
27
src/vector.h
@@ -7,15 +7,24 @@
|
|||||||
|
|
||||||
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
|
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
|
static inline void *aligned_alloc(size_t size, size_t alignment) {
|
||||||
|
return _aligned_malloc(size, alignment);
|
||||||
|
}
|
||||||
|
static inline void *aligned_resize(void *p, size_t old_size, size_t new_size, size_t alignment) {
|
||||||
|
return _aligned_realloc(p, new_size, alignment);
|
||||||
|
}
|
||||||
|
static inline void aligned_free(void *p) {
|
||||||
|
_aligned_free(p);
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
static inline void *_aligned_malloc(size_t size, size_t alignment)
|
static inline void *aligned_malloc(size_t size, size_t alignment)
|
||||||
{
|
{
|
||||||
void *p;
|
void *p;
|
||||||
int ret = posix_memalign(&p, alignment, size);
|
int ret = posix_memalign(&p, alignment, size);
|
||||||
return (ret == 0) ? p : NULL;
|
return (ret == 0) ? p : NULL;
|
||||||
}
|
}
|
||||||
static inline void *_aligned_realloc(void *p, size_t size, size_t alignment)
|
static inline void *aligned_resize(void *p, size_t old_size, size_t new_size, size_t alignment)
|
||||||
{
|
{
|
||||||
if ((alignment == 0) || ((alignment & (alignment - 1)) != 0) || (alignment < sizeof(void *))) {
|
if ((alignment == 0) || ((alignment & (alignment - 1)) != 0) || (alignment < sizeof(void *))) {
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -25,17 +34,17 @@ static inline void *_aligned_realloc(void *p, size_t size, size_t alignment)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *p1 = _aligned_malloc(size, alignment);
|
void *p1 = aligned_malloc(new_size, alignment);
|
||||||
if (p1 == NULL) {
|
if (p1 == NULL) {
|
||||||
free(p);
|
free(p);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(p1, p, size);
|
memcpy(p1, p, old_size);
|
||||||
free(p);
|
free(p);
|
||||||
return p1;
|
return p1;
|
||||||
}
|
}
|
||||||
static inline void _aligned_free(void *p)
|
static inline void aligned_free(void *p)
|
||||||
{
|
{
|
||||||
free(p);
|
free(p);
|
||||||
}
|
}
|
||||||
@@ -71,7 +80,7 @@ static inline void _aligned_free(void *p)
|
|||||||
name *array = malloc(sizeof(name)); \
|
name *array = malloc(sizeof(name)); \
|
||||||
if (array == NULL) return NULL; \
|
if (array == NULL) return NULL; \
|
||||||
array->n = array->m = 0; \
|
array->n = array->m = 0; \
|
||||||
array->a = _aligned_malloc(size * sizeof(type), alignment); \
|
array->a = aligned_malloc(size * sizeof(type), alignment); \
|
||||||
if (array->a == NULL) return NULL; \
|
if (array->a == NULL) return NULL; \
|
||||||
array->m = size; \
|
array->m = size; \
|
||||||
return array; \
|
return array; \
|
||||||
@@ -86,7 +95,7 @@ static inline void _aligned_free(void *p)
|
|||||||
} \
|
} \
|
||||||
static inline bool name##_resize_aligned(name *array, size_t size, size_t alignment) { \
|
static inline bool name##_resize_aligned(name *array, size_t size, size_t alignment) { \
|
||||||
if (size <= array->m) return true; \
|
if (size <= array->m) return true; \
|
||||||
type *ptr = _aligned_realloc(array->a, sizeof(type) * size, alignment); \
|
type *ptr = aligned_resize(array->a, sizeof(type) * array->m, sizeof(type) * size, alignment); \
|
||||||
if (ptr == NULL) return false; \
|
if (ptr == NULL) return false; \
|
||||||
array->a = ptr; \
|
array->a = ptr; \
|
||||||
array->m = size; \
|
array->m = size; \
|
||||||
@@ -152,7 +161,7 @@ static inline void _aligned_free(void *p)
|
|||||||
} \
|
} \
|
||||||
static inline void name##_destroy_aligned(name *array) { \
|
static inline void name##_destroy_aligned(name *array) { \
|
||||||
if (array == NULL) return; \
|
if (array == NULL) return; \
|
||||||
if (array->a != NULL) _aligned_free(array->a); \
|
if (array->a != NULL) aligned_free(array->a); \
|
||||||
free(array); \
|
free(array); \
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,7 +183,7 @@ static inline void _aligned_free(void *p)
|
|||||||
free_func(array->a[i]); \
|
free_func(array->a[i]); \
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
_aligned_free(array->a); \
|
aligned_free(array->a); \
|
||||||
free(array); \
|
free(array); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user