A valid array that is not allocated will have a size of 0.
Passing these to the function should not result in an error.
If the size is 0, then there is nothing to copy even though array is NULL
This is all fine.
Update the documentation comments to be more explicit on NULL in the parameters.
#ifndef _di_f_memory_array_append_
f_status_t f_memory_array_append(const void * const source, const size_t width, void ** const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size) {
#ifndef _di_level_0_parameter_checking_
- if (!source) return F_status_set_error(F_parameter);
if (!width) return F_status_set_error(F_parameter);
if (!array) return F_status_set_error(F_parameter);
if (!used) return F_status_set_error(F_parameter);
if (!size) return F_status_set_error(F_parameter);
#endif // _di_level_0_parameter_checking_
+ if (!source) return F_data_not;
if (*used >= F_number_t_size_unsigned_d) return F_status_set_error(F_array_too_large);
{
#ifndef _di_f_memory_array_append_all_
f_status_t f_memory_array_append_all(const void * const sources, const f_number_unsigned_t amount, const size_t width, void ** const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size) {
#ifndef _di_level_0_parameter_checking_
- if (!sources) return F_status_set_error(F_parameter);
if (!width) return F_status_set_error(F_parameter);
if (!array) return F_status_set_error(F_parameter);
if (!used) return F_status_set_error(F_parameter);
if (!size) return F_status_set_error(F_parameter);
+
+ // Sources might be un-allocated, so this is not actually an error if amount is 0.
+ if (!sources && amount) return F_status_set_error(F_parameter);
#endif // _di_level_0_parameter_checking_
if (!amount) return F_data_not;
* @param width
* The size of the structure represented by array.
* The word "width" is used due to conflicts of already using "length" and "size".
+ *
* Must not be 0.
* @param array
* The structure.array to resize.
+ *
+ * Must not be NULL.
* @param used
* The structure.used.
+ *
+ * Must not be NULL.
* @param size
* The structure.size.
*
+ * Must not be NULL.
+ *
* @return
* F_okay on success.
*
* If the simple type that is "array" requires additional memory manipulation on allocation or de-allocation, then do not use this function.
*
* @param source
- * The source structure to copy from.
+ * A pointer to the source structure to copy from.
+ *
+ * May be NULL, which means source is not allocated.
* @param width
* The size of the structure represented by array.
* The word "width" is used due to conflicts of already using "length" and "size".
+ *
* Must not be 0.
* @param array
* The structure.array destination.
* ...
* f_memory_array_append(source, width, (void **) &arr_pounter, used, size);
* }
+ *
+ * Must not be NULL.
* @param used
* The structure.used destination.
+ *
+ * Must not be NULL.
* @param size
* The structure.size destination.
*
+ * Must not be NULL.
+ *
* @return
* F_okay on success.
+ * F_data_not on success but source is NULL.
*
* F_array_too_large (with error bit) if the new array length is too large.
* F_memory_not (with error bit) on out of memory.
* Be careful that the resulting multiplication does not overflow f_number_unsigned_t.
*
* @param sources
- * The source structure to copy from.
+ * A pointer to the source structure to copy from.
* This is generally a structure.array value.
+ *
+ * May be NULL when amount is 0, which means sources is not allocated.
+ * May not be NULL when amount is greater than 0.
* @param amount
* The total length of the sources to copy.
* The is generally the structure.used value.
* @param width
* The size of the structure represented by array.
* The word "width" is used due to conflicts of already using "length" and "size".
+ *
* Must not be 0.
* @param array
* The structure.array destination.
* }
* @param used
* The structure.used destination.
+ *
+ * Must not be NULL.
* @param size
* The structure.size destination.
*
+ * Must not be NULL.
+ *
* @return
* F_okay on success.
* F_data_not if amount is 0.
* @param width
* The size of the structure represented by array.
* The word "width" is used due to conflicts of already using "length" and "size".
+ *
* Must not be 0.
* @param array
* The structure.array to resize.
+ *
+ * Must not be NULL.
* @param used
* The structure.used.
+ *
+ * Must not be NULL.
* @param size
* The structure.size.
*
+ * Must not be NULL.
+ *
* @return
* F_okay on success.
*
* @param width
* The size of the structure represented by array.
* The word "width" is used due to conflicts of already using "length" and "size".
+ *
* Must not be 0.
* @param array
* The structure.array to resize.
+ *
+ * Must not be NULL.
* @param used
* The structure.used.
+ *
+ * Must not be NULL.
* @param size
* The structure.size.
*
+ * Must not be NULL.
+ *
* @return
* F_okay on success.
*
*
* @param step
* The allocation step to use.
- * Must be greater than 0.
+ *
+ * Must not be 0.
* @param width
* The size of the structure represented by array.
* The word "width" is used due to conflicts of already using "length" and "size".
+ *
* Must not be 0.
* @param array
* The structure.array to resize.
+ *
+ * Must not be NULL.
* @param used
* The structure.used.
+ *
+ * Must not be NULL.
* @param size
* The structure.size.
*
+ * Must not be NULL.
+ *
* @return
* F_okay on success.
* F_data_not on success, but there is no reason to increase size (used + 1 <= size).
* @param width
* The size of the structure represented by array.
* The word "width" is used due to conflicts of already using "length" and "size".
+ *
* Must not be 0.
* @param array
* The structure.array to resize.
+ *
+ * Must not be NULL.
* @param used
* The structure.used.
+ *
+ * Must not be NULL.
* @param size
* The structure.size.
*
+ * Must not be NULL.
+ *
* @return
* F_okay on success.
* F_data_not on success, but there is no reason to increase size (used + amount <= size).
* @param width
* The size of the structure represented by array.
* The word "width" is used due to conflicts of already using "length" and "size".
+ *
* Must not be 0.
* @param array
* The structure.array to resize.
+ *
+ * Must not be NULL.
* @param used
* The structure.used.
+ *
+ * Must not be NULL.
* @param size
* The structure.size.
*
+ * Must not be NULL.
+ *
* @return
* F_okay on success.
*
const int number = 1;
test_memory_array_t data = test_memory_array_t_initialize;
+ // In this case, the usual parameter checking must not return F_parameter error when source is NULL.
{
- const f_status_t status = f_memory_array_append((void *) &number, sizeof(int), 0, 0, 0);
+ const f_status_t status = f_memory_array_append(0, sizeof(int), (void **) &data.array, &data.used, &data.size);
+
+ assert_int_equal(status, F_data_not);
+ assert_int_equal(data.used, 0);
+ assert_int_equal(data.size, 0);
+ }
+
+ {
+ const f_status_t status = f_memory_array_append(0, sizeof(int), 0, 0, 0);
assert_int_equal(status, F_status_set_error(F_parameter));
assert_int_equal(data.used, 0);
}
{
- const f_status_t status = f_memory_array_append((void *) &number, sizeof(int), 0, &data.used, &data.size);
+ const f_status_t status = f_memory_array_append((void *) &number, sizeof(int), 0, 0, 0);
assert_int_equal(status, F_status_set_error(F_parameter));
assert_int_equal(data.used, 0);
}
{
- const f_status_t status = f_memory_array_append((void *) &number, sizeof(int), (void **) &data.array, 0, &data.size);
+ const f_status_t status = f_memory_array_append((void *) &number, sizeof(int), 0, &data.used, &data.size);
assert_int_equal(status, F_status_set_error(F_parameter));
assert_int_equal(data.used, 0);
}
{
- const f_status_t status = f_memory_array_append((void *) &number, sizeof(int), (void **) &data.array, &data.used, 0);
+ const f_status_t status = f_memory_array_append((void *) &number, sizeof(int), (void **) &data.array, 0, &data.size);
assert_int_equal(status, F_status_set_error(F_parameter));
assert_int_equal(data.used, 0);
}
{
- const f_status_t status = f_memory_array_append((void *) &number, 0, (void **) &data.array, &data.used, &data.size);
+ const f_status_t status = f_memory_array_append((void *) &number, sizeof(int), (void **) &data.array, &data.used, 0);
assert_int_equal(status, F_status_set_error(F_parameter));
assert_int_equal(data.used, 0);
}
{
- const f_status_t status = f_memory_array_append(0, sizeof(int), (void **) &data.array, &data.used, &data.size);
+ const f_status_t status = f_memory_array_append((void *) &number, 0, (void **) &data.array, &data.used, &data.size);
assert_int_equal(status, F_status_set_error(F_parameter));
assert_int_equal(data.used, 0);
test_memory_array_t sources = { .array = sources_array, .used = 2, .size = 0 };
test_memory_array_t data = test_memory_array_t_initialize;
+ // In this case, the usual parameter checking must not return F_parameter error when sources is NULL and amount is 0.
+ {
+ const f_status_t status = f_memory_array_append_all(0, 0, sizeof(int), (void **) &data.array, &data.used, &data.size);
+
+ assert_int_equal(status, F_data_not);
+ assert_int_equal(data.used, 0);
+ assert_int_equal(data.size, 0);
+ }
+
+ {
+ const f_status_t status = f_memory_array_append_all(0, sources.used, sizeof(int), (void **) &data.array, &data.used, &data.size);
+
+ assert_int_equal(status, F_status_set_error(F_parameter));
+ assert_int_equal(data.used, 0);
+ assert_int_equal(data.size, 0);
+ }
+
+ {
+ const f_status_t status = f_memory_array_append_all(0, 0, sizeof(int), 0, 0, 0);
+
+ assert_int_equal(status, F_status_set_error(F_parameter));
+ assert_int_equal(data.used, 0);
+ assert_int_equal(data.size, 0);
+ }
+
{
const f_status_t status = f_memory_array_append_all((void *) sources.array, sources.used, sizeof(int), 0, 0, 0);