Skip to content

Commit

Permalink
Merge pull request #640 from openvenues/fix_aligned_resize
Browse files Browse the repository at this point in the history
Fix aligned resize
  • Loading branch information
albarrentine authored Aug 21, 2023
2 parents 1fe1f0a + 330bd2e commit 8f2066b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef enum {
} name##_t; \
\
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) { \
return NULL; \
Expand Down Expand Up @@ -62,7 +62,7 @@ typedef enum {
matrix->m = m; \
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) { \
free(matrix); \
return NULL; \
Expand All @@ -86,7 +86,7 @@ typedef enum {
if (self == NULL) return; \
\
if (self->values != NULL) { \
_aligned_free(self->values); \
aligned_free(self->values); \
} \
\
free(self); \
Expand Down Expand Up @@ -118,7 +118,7 @@ typedef enum {
if (self == NULL) return false; \
\
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) { \
return false; \
} \
Expand Down
27 changes: 18 additions & 9 deletions src/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@

#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
#include <malloc.h>
static inline void *aligned_malloc(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
#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;
int ret = posix_memalign(&p, alignment, size);
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 *))) {
return NULL;
Expand All @@ -25,17 +34,17 @@ static inline void *_aligned_realloc(void *p, size_t size, size_t alignment)
return NULL;
}

void *p1 = _aligned_malloc(size, alignment);
void *p1 = aligned_malloc(new_size, alignment);
if (p1 == NULL) {
free(p);
return NULL;
}

memcpy(p1, p, size);
memcpy(p1, p, old_size);
free(p);
return p1;
}
static inline void _aligned_free(void *p)
static inline void aligned_free(void *p)
{
free(p);
}
Expand Down Expand Up @@ -71,7 +80,7 @@ static inline void _aligned_free(void *p)
name *array = malloc(sizeof(name)); \
if (array == NULL) return NULL; \
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; \
array->m = size; \
return array; \
Expand All @@ -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) { \
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; \
array->a = ptr; \
array->m = size; \
Expand Down Expand Up @@ -152,7 +161,7 @@ static inline void _aligned_free(void *p)
} \
static inline void name##_destroy_aligned(name *array) { \
if (array == NULL) return; \
if (array->a != NULL) _aligned_free(array->a); \
if (array->a != NULL) aligned_free(array->a); \
free(array); \
}

Expand All @@ -174,7 +183,7 @@ static inline void _aligned_free(void *p)
free_func(array->a[i]); \
} \
} \
_aligned_free(array->a); \
aligned_free(array->a); \
free(array); \
}

Expand Down

0 comments on commit 8f2066b

Please sign in to comment.