void
os_free_aligned(void *ptr);
+void *
+os_realloc_aligned(void *ptr, size_t oldsize, size_t newsize, size_t alignemnt);
+
#ifdef __cplusplus
}
#endif
#endif
-
/**
* Add two size_t values with integer overflow check.
* TODO: leverage __builtin_add_overflow where available
}
+#if defined(HAVE_POSIX_MEMALIGN)
+
+static inline void *
+os_malloc_aligned(size_t size, size_t alignment)
+{
+ void *ptr;
+ alignment = (alignment + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
+ if(posix_memalign(&ptr, alignment, size) != 0)
+ return NULL;
+ return ptr;
+}
+
+#define os_free_aligned(_ptr) free(_ptr)
+
+#else
+
/**
* Return memory on given byte alignment
*/
os_free(realAddr);
}
}
+
+#endif
+
+/**
+ * Reallocate memeory, with alignment
+ */
+static inline void *
+os_realloc_aligned(void *ptr, size_t oldsize, size_t newsize, size_t alignment)
+{
+ const size_t copySize = MIN2(oldsize, newsize);
+ void *newBuf = os_malloc_aligned(newsize, alignment);
+ if (newBuf && ptr && copySize > 0) {
+ memcpy(newBuf, ptr, copySize);
+ }
+
+ os_free_aligned(ptr);
+ return newBuf;
+}
#define os_realloc( _old_ptr, _old_size, _new_size) \
realloc(_old_ptr, _new_size + 0*(_old_size))
-
-#if defined(HAVE_POSIX_MEMALIGN)
-
-static inline void *
-os_malloc_aligned(size_t size, size_t alignment)
-{
- void *ptr;
- alignment = (alignment + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
- if(posix_memalign(&ptr, alignment, size) != 0)
- return NULL;
- return ptr;
-}
-
-#define os_free_aligned(_ptr) free(_ptr)
-
-#elif DETECT_OS_WINDOWS
+#if DETECT_OS_WINDOWS
#include <malloc.h>
#define os_malloc_aligned(_size, _align) _aligned_malloc(_size, _align)
#define os_free_aligned(_ptr) _aligned_free(_ptr)
+#define os_realloc_aligned(_ptr, _oldsize, _newsize, _alignment) _aligned_realloc(_ptr, _newsize, _alignment)
#else
#define align_malloc(_size, _alignment) os_malloc_aligned(_size, _alignment)
#define align_free(_ptr) os_free_aligned(_ptr)
+#define align_realloc(_ptr, _oldsize, _newsize, _alignment) os_realloc_aligned(_ptr, _oldsize, _newsize, _alignment)
static inline void *
align_calloc(size_t size, unsigned long alignment)