[utils] adding aligned malloc/free/realloc in vector.h and matrix.h, fixing bug in matrix_copy
This commit is contained in:
56
src/matrix.h
56
src/matrix.h
@@ -45,6 +45,26 @@
|
|||||||
\
|
\
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
|
static name##_t *name##_new_aligned(size_t m, size_t n, size_t alignment) { \
|
||||||
|
name##_t *matrix = malloc(sizeof(name##_t)); \
|
||||||
|
\
|
||||||
|
if (matrix == NULL) { \
|
||||||
|
return NULL; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
matrix->m = m; \
|
||||||
|
matrix->n = n; \
|
||||||
|
\
|
||||||
|
matrix->values = _aligned_malloc(sizeof(type) * m * n, alignment); \
|
||||||
|
if (matrix->values == NULL) { \
|
||||||
|
free(matrix); \
|
||||||
|
return NULL; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
return matrix; \
|
||||||
|
\
|
||||||
|
} \
|
||||||
|
\
|
||||||
static void name##_destroy(name##_t *self) { \
|
static void name##_destroy(name##_t *self) { \
|
||||||
if (self == NULL) return; \
|
if (self == NULL) return; \
|
||||||
\
|
\
|
||||||
@@ -55,6 +75,16 @@
|
|||||||
free(self); \
|
free(self); \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
|
static void name##_destroy_aligned(name##_t *self) { \
|
||||||
|
if (self == NULL) return; \
|
||||||
|
\
|
||||||
|
if (self->values != NULL) { \
|
||||||
|
_aligned_free(self->values); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
free(self); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
static inline void name##_zero(name##_t *self) { \
|
static inline void name##_zero(name##_t *self) { \
|
||||||
memset(self->values, 0, self->m * self->n * sizeof(type)); \
|
memset(self->values, 0, self->m * self->n * sizeof(type)); \
|
||||||
} \
|
} \
|
||||||
@@ -64,10 +94,30 @@
|
|||||||
if (self == NULL) return false; \
|
if (self == NULL) return false; \
|
||||||
\
|
\
|
||||||
if (m * n > (self->m * self->n)) { \
|
if (m * n > (self->m * self->n)) { \
|
||||||
self->values = realloc(self->values, sizeof(type) * m * n); \
|
type *ptr = realloc(self->values, sizeof(type) * m * n); \
|
||||||
if (self->values == NULL) { \
|
if (ptr == NULL) { \
|
||||||
return false; \
|
return false; \
|
||||||
} \
|
} \
|
||||||
|
self->values = ptr; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
self->m = m; \
|
||||||
|
self->n = n; \
|
||||||
|
\
|
||||||
|
name##_zero(self); \
|
||||||
|
\
|
||||||
|
return true; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
static inline bool name##_resize_aligned(name##_t *self, size_t m, size_t n, size_t alignment) { \
|
||||||
|
if (self == NULL) return false; \
|
||||||
|
\
|
||||||
|
if (m * n > (self->m * self->n)) { \
|
||||||
|
type *ptr = _aligned_realloc(self->values, sizeof(type) * m * n, alignment); \
|
||||||
|
if (ptr == NULL) { \
|
||||||
|
return false; \
|
||||||
|
} \
|
||||||
|
self->values = ptr; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
self->m = m; \
|
self->m = m; \
|
||||||
@@ -90,7 +140,7 @@
|
|||||||
if (self->m != other->m || self->n != other->n) { \
|
if (self->m != other->m || self->n != other->n) { \
|
||||||
return false; \
|
return false; \
|
||||||
} \
|
} \
|
||||||
size_t num_values = self->n * self->n; \
|
size_t num_values = self->m * self->n; \
|
||||||
\
|
\
|
||||||
memcpy(other->values, self->values, num_values * sizeof(type)); \
|
memcpy(other->values, self->values, num_values * sizeof(type)); \
|
||||||
return true; \
|
return true; \
|
||||||
|
|||||||
54
src/vector.h
54
src/vector.h
@@ -13,7 +13,35 @@ 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 : 0;
|
return (ret == 0) ? p : NULL;
|
||||||
|
}
|
||||||
|
static inline void *_aligned_realloc(void *p, size_t size, size_t alignment)
|
||||||
|
{
|
||||||
|
if ((alignment == 0) || ((alignment & (alignment - 1)) != 0) || (alignment < sizeof(void *))) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *rp = realloc(p, size);
|
||||||
|
|
||||||
|
/* If realloc result is not already at an aligned boundary,
|
||||||
|
_aligned_malloc a new block and copy the contents of the realloc'd
|
||||||
|
pointer to the aligned block, free the realloc'd pointer and return
|
||||||
|
the aligned pointer.
|
||||||
|
*/
|
||||||
|
if ( ((size_t)rp & (alignment - 1)) != 0) {
|
||||||
|
void *p1 = _aligned_malloc(size, alignment);
|
||||||
|
if (p1 != NULL) {
|
||||||
|
memcpy(p1, rp, size);
|
||||||
|
}
|
||||||
|
free(rp);
|
||||||
|
rp = p1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rp;
|
||||||
}
|
}
|
||||||
static inline void _aligned_free(void *p)
|
static inline void _aligned_free(void *p)
|
||||||
{
|
{
|
||||||
@@ -58,6 +86,14 @@ static inline void _aligned_free(void *p)
|
|||||||
array->m = size; \
|
array->m = size; \
|
||||||
return true; \
|
return true; \
|
||||||
} \
|
} \
|
||||||
|
static inline bool name##_resize_aligned(name *array, size_t size, size_t alignment) { \
|
||||||
|
if (size <= array->m) return true; \
|
||||||
|
type *ptr = _aligned_realloc(array->a, sizeof(type) * size, alignment); \
|
||||||
|
if (ptr == NULL) return false; \
|
||||||
|
array->a = ptr; \
|
||||||
|
array->m = size; \
|
||||||
|
return true; \
|
||||||
|
} \
|
||||||
static inline void name##_push(name *array, type value) { \
|
static inline void name##_push(name *array, type value) { \
|
||||||
if (array->n == array->m) { \
|
if (array->n == array->m) { \
|
||||||
size_t size = array->m ? array->m << 1 : 2; \
|
size_t size = array->m ? array->m << 1 : 2; \
|
||||||
@@ -105,9 +141,13 @@ static inline void _aligned_free(void *p)
|
|||||||
if (array == NULL) return; \
|
if (array == NULL) return; \
|
||||||
if (array->a != NULL) free(array->a); \
|
if (array->a != NULL) free(array->a); \
|
||||||
free(array); \
|
free(array); \
|
||||||
|
} \
|
||||||
|
static inline void name##_destroy_aligned(name *array) { \
|
||||||
|
if (array == NULL) return; \
|
||||||
|
if (array->a != NULL) _aligned_free(array->a); \
|
||||||
|
free(array); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#define __VECTOR_DESTROY_FREE_DATA(name, type, free_func) \
|
#define __VECTOR_DESTROY_FREE_DATA(name, type, free_func) \
|
||||||
static inline void name##_destroy(name *array) { \
|
static inline void name##_destroy(name *array) { \
|
||||||
if (array == NULL) return; \
|
if (array == NULL) return; \
|
||||||
@@ -118,6 +158,16 @@ static inline void _aligned_free(void *p)
|
|||||||
} \
|
} \
|
||||||
free(array->a); \
|
free(array->a); \
|
||||||
free(array); \
|
free(array); \
|
||||||
|
} \
|
||||||
|
static inline void name##_destroy_aligned(name *array) { \
|
||||||
|
if (array == NULL) return; \
|
||||||
|
if (array->a != NULL) { \
|
||||||
|
for (size_t i = 0; i < array->n; i++) { \
|
||||||
|
free_func(array->a[i]); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
_aligned_free(array->a); \
|
||||||
|
free(array); \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define VECTOR_INIT(name, type) \
|
#define VECTOR_INIT(name, type) \
|
||||||
|
|||||||
Reference in New Issue
Block a user