#endif
#ifndef _di_f_memory_new_
- f_return_status f_memory_new(void **pointer, const f_memory_size_t size, const f_memory_length length) {
+ f_return_status f_memory_new(void **pointer, const size_t size, const size_t length) {
#ifndef _di_level_0_parameter_checking_
if (size <= 0) return F_status_set_error(F_parameter);
- if (length < 0) return F_status_set_error(F_parameter);
+ if (length <= 0) return F_status_set_error(F_parameter);
if (!pointer) return F_status_set_error(F_parameter);
#endif // _di_level_0_parameter_checking_
// Some people use malloc(size * length) to produce the same results.
// This has been observed to sometimes causes an increase in L1/L2 cache misses (0.02% L1 increase, 0.01% L2 increase).
- *pointer = calloc(size, length);
+ *pointer = calloc(length, size);
if (*pointer) {
}
#endif // _di_f_memory_new_
+#ifndef _di_f_memory_new_aligned_
+ f_return_status f_memory_new_aligned(void **pointer, const size_t alignment, const size_t length) {
+ #ifndef _di_level_0_parameter_checking_
+ if (alignment <= 0) return F_status_set_error(F_parameter);
+ if (length <= 0) return F_status_set_error(F_parameter);
+ if (!pointer) return F_status_set_error(F_parameter);
+ #endif // _di_level_0_parameter_checking_
+
+ // prevent double-allocations.
+ if (*pointer || length == 0) return F_none;
+
+ const int result = posix_memalign(pointer, alignment, length);
+
+ if (result) {
+ if (result == EINVAL) return F_status_set_error(F_parameter);
+
+ return F_status_set_error(F_memory_allocation);
+ }
+
+ // uint8_t * is of a data size size of 1, casting it to uint8_t should result in a single-length increment.
+ // this is done to avoid problems with (void *) having arithmetic issues.
+ memset(((uint8_t *) *pointer), 0, length);
+
+ return F_none;
+ }
+#endif // _di_f_memory_new_aligned_
+
#if !(defined(_di_f_memory_delete_) || defined(_f_memory_FORCE_secure_memory_))
- f_return_status f_memory_delete(void **pointer, const f_memory_size_t size, const f_memory_length length) {
+ f_return_status f_memory_delete(void **pointer, const size_t size, const size_t length) {
#ifndef _di_level_0_parameter_checking_
if (!pointer) return F_status_set_error(F_parameter);
#endif // _di_level_0_parameter_checking_
#endif // !(defined(_di_f_memory_delete_) || defined(_f_memory_FORCE_secure_memory_))
#if !(defined(_di_f_memory_destroy_) || defined(_f_memory_FORCE_fast_memory_))
- f_return_status f_memory_destroy(void **pointer, const f_memory_size_t size, const f_memory_length length) {
+ f_return_status f_memory_destroy(void **pointer, const size_t size, const size_t length) {
#ifndef _di_level_0_parameter_checking_
if (length < 0) return F_status_set_error(F_parameter);
if (size <= 0) return F_status_set_error(F_parameter);
#endif // !(defined(_di_f_memory_destroy_) || defined(_f_memory_FORCE_fast_memory_))
#if !(defined(_di_f_memory_resize_) || defined(_f_memory_FORCE_secure_memory_))
- f_return_status f_memory_resize(void **pointer, const f_memory_size_t size, const f_memory_length old_length, const f_memory_length new_length) {
+ f_return_status f_memory_resize(void **pointer, const size_t size, const size_t old_length, const size_t new_length) {
#ifndef _di_level_0_parameter_checking_
if (size <= 0) return F_status_set_error(F_parameter);
if (old_length < 0) return F_status_set_error(F_parameter);
#endif // !(defined(_di_f_memory_resize_) || defined(_f_memory_FORCE_secure_memory_))
#if !(defined(_di_f_memory_adjust_) || defined(_f_memory_FORCE_fast_memory_))
- f_return_status f_memory_adjust(void **pointer, const f_memory_size_t size, const f_memory_length old_length, const f_memory_length new_length) {
+ f_return_status f_memory_adjust(void **pointer, const size_t size, const size_t old_length, const size_t new_length) {
#ifndef _di_level_0_parameter_checking_
if (size <= 0) return F_status_set_error(F_parameter);
if (old_length < 0) return F_status_set_error(F_parameter);
#endif // _di_f_memory_default_allocation_step_
/**
- * Memory types.
- */
-#ifndef _di_f_memory_types_
- #define f_memory_size_t size_t
- #define f_memory_length size_t
-#endif // _di_f_memory_types_
-
-/**
* Create some dynamically allocated array of some length.
*
* @param pointer
* @see memset()
*/
#ifndef _di_f_memory_new_
- extern f_return_status f_memory_new(void **pointer, const f_memory_size_t size, const f_memory_length length);
+ extern f_return_status f_memory_new(void **pointer, const size_t size, const size_t length);
#endif // _di_f_memory_new_
/**
+ * Create some dynamically allocated array of some length, guaranteeing aligned memory.
+ *
+ * @param pointer
+ * A pointer that will be updated to the address of the newly allocated memory.
+ * @param alignment
+ * The size of the alignment, such as sizeof(void *).
+ * Must be greater than 0.
+ * @param length
+ * The total number of blocks to be allocated.
+ * Must be greater than 0.
+ * Must be a multiple of alignment.
+ *
+ * @return
+ * F_none on success.
+ * F_memory_allocation (with error bit) on allocation error.
+ * F_parameter (with error bit) if a parameter is invalid.
+ *
+ * @see posix_memalign()
+ * @see memset()
+ */
+#ifndef _di_f_memory_new_aligned_
+ extern f_return_status f_memory_new_aligned(void **pointer, const size_t alignment, const size_t length);
+#endif // _di_f_memory_new_aligned_
+
+/**
* Delete dynamically allocated data.
*
* Will not change any of the data to 0 prior to deallocation.
* @see free()
*/
#if !(defined(_di_f_memory_delete_) || defined(_f_memory_FORCE_secure_memory_))
- extern f_return_status f_memory_delete(void **pointer, const f_memory_size_t size, const f_memory_length length);
+ extern f_return_status f_memory_delete(void **pointer, const size_t size, const size_t length);
#endif // !(defined(_di_f_memory_delete_) || defined(_f_memory_FORCE_secure_memory_))
/**
* @see memset()
*/
#if !(defined(_di_f_memory_destroy_) || defined(_f_memory_FORCE_fast_memory_))
- extern f_return_status f_memory_destroy(void **pointer, const f_memory_size_t size, const f_memory_length length);
+ extern f_return_status f_memory_destroy(void **pointer, const size_t size, const size_t length);
#endif // !(defined(_di_f_memory_destroy_) || defined(_f_memory_FORCE_fast_memory_))
/**
* @see realloc()
*/
#if !(defined(_di_f_memory_resize_) || defined(_f_memory_FORCE_secure_memory_))
- extern f_return_status f_memory_resize(void **pointer, const f_memory_size_t size, const f_memory_length old_length, const f_memory_length new_length);
+ extern f_return_status f_memory_resize(void **pointer, const size_t size, const size_t old_length, const size_t new_length);
#endif // !(defined(_di_f_memory_resize_) || defined(_f_memory_FORCE_secure_memory_))
/**
* @see realloc()
*/
#if !(defined(_di_f_memory_adjust_) || defined(_f_memory_FORCE_fast_memory_))
- extern f_return_status f_memory_adjust(void **pointer, const f_memory_size_t size, const f_memory_length old_length, const f_memory_length new_length);
+ extern f_return_status f_memory_adjust(void **pointer, const size_t size, const size_t old_length, const size_t new_length);
#endif // _di_f_memory_adjust_
#ifdef __cplusplus