diff options
Diffstat (limited to 'src/include/gnunet_common.h')
-rw-r--r-- | src/include/gnunet_common.h | 63 |
1 files changed, 51 insertions, 12 deletions
diff --git a/src/include/gnunet_common.h b/src/include/gnunet_common.h index 78aeb3de7..033a68894 100644 --- a/src/include/gnunet_common.h +++ b/src/include/gnunet_common.h | |||
@@ -1209,7 +1209,10 @@ GNUNET_is_zero_ (const void *a, | |||
1209 | * @param n number of elements in the array | 1209 | * @param n number of elements in the array |
1210 | * @param type name of the struct or union, i.e. pass 'struct Foo'. | 1210 | * @param type name of the struct or union, i.e. pass 'struct Foo'. |
1211 | */ | 1211 | */ |
1212 | #define GNUNET_new_array(n, type) (type *) GNUNET_malloc ((n) * sizeof(type)) | 1212 | #define GNUNET_new_array(n, type) ({ \ |
1213 | GNUNET_assert (SIZE_MAX / sizeof (type) >= n); \ | ||
1214 | (type *) GNUNET_malloc ((n) * sizeof(type)); \ | ||
1215 | }) | ||
1213 | 1216 | ||
1214 | /** | 1217 | /** |
1215 | * @ingroup memory | 1218 | * @ingroup memory |
@@ -1284,23 +1287,23 @@ GNUNET_is_zero_ (const void *a, | |||
1284 | * @ingroup memory | 1287 | * @ingroup memory |
1285 | * Wrapper around free. Frees the memory referred to by ptr. | 1288 | * Wrapper around free. Frees the memory referred to by ptr. |
1286 | * Note that it is generally better to free memory that was | 1289 | * Note that it is generally better to free memory that was |
1287 | * allocated with #GNUNET_array_grow using #GNUNET_array_grow(mem, size, 0) instead of #GNUNET_free. | 1290 | * allocated with #GNUNET_array_grow using #GNUNET_array_grow(mem, size, 0) instead of #GNUNET_free_nz. |
1288 | * | 1291 | * |
1289 | * @param ptr location where to free the memory. ptr must have | 1292 | * @param ptr location where to free the memory. ptr must have |
1290 | * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier. | 1293 | * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier. |
1291 | */ | 1294 | */ |
1292 | #define GNUNET_free(ptr) GNUNET_xfree_ (ptr, __FILE__, __LINE__) | 1295 | #define GNUNET_free_nz(ptr) GNUNET_xfree_ (ptr, __FILE__, __LINE__) |
1293 | 1296 | ||
1294 | /** | 1297 | /** |
1295 | * @ingroup memory | 1298 | * @ingroup memory |
1296 | * Wrapper around free. Frees the memory referred to by ptr and sets ptr to NULL. | 1299 | * Wrapper around free. Frees the memory referred to by ptr and sets ptr to NULL. |
1297 | * Note that it is generally better to free memory that was | 1300 | * Note that it is generally better to free memory that was |
1298 | * allocated with #GNUNET_array_grow using #GNUNET_array_grow(mem, size, 0) instead of #GNUNET_freez. | 1301 | * allocated with #GNUNET_array_grow using #GNUNET_array_grow(mem, size, 0) instead of #GNUNET_free. |
1299 | * | 1302 | * |
1300 | * @param ptr location where to free the memory. ptr must have | 1303 | * @param ptr location where to free the memory. ptr must have |
1301 | * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier. | 1304 | * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier. |
1302 | */ | 1305 | */ |
1303 | #define GNUNET_freez(ptr) do { \ | 1306 | #define GNUNET_free(ptr) do { \ |
1304 | GNUNET_xfree_ (ptr, __FILE__, __LINE__); \ | 1307 | GNUNET_xfree_ (ptr, __FILE__, __LINE__); \ |
1305 | ptr = NULL; \ | 1308 | ptr = NULL; \ |
1306 | } while (0) | 1309 | } while (0) |
@@ -1389,22 +1392,58 @@ GNUNET_is_zero_ (const void *a, | |||
1389 | 1392 | ||
1390 | /** | 1393 | /** |
1391 | * @ingroup memory | 1394 | * @ingroup memory |
1392 | * Append an element to a list (growing the list by one). | 1395 | * Append an element to an array (growing the array by one). |
1393 | * | 1396 | * |
1394 | * @param arr base-pointer of the vector, may be NULL if size is 0; | 1397 | * @param arr base-pointer of the vector, may be NULL if @a len is 0; |
1395 | * will be updated to reflect the new address. The TYPE of | 1398 | * will be updated to reflect the new address. The TYPE of |
1396 | * arr is important since size is the number of elements and | 1399 | * arr is important since size is the number of elements and |
1397 | * not the size in bytes | 1400 | * not the size in bytes |
1398 | * @param size the number of elements in the existing vector (number | 1401 | * @param len the number of elements in the existing vector (number |
1399 | * of elements to copy over), will be updated with the new | 1402 | * of elements to copy over), will be updated with the new |
1400 | * array size | 1403 | * array length |
1401 | * @param element the element that will be appended to the array | 1404 | * @param element the element that will be appended to the array |
1402 | */ | 1405 | */ |
1403 | #define GNUNET_array_append(arr, size, element) \ | 1406 | #define GNUNET_array_append(arr, len, element) \ |
1407 | do \ | ||
1408 | { \ | ||
1409 | GNUNET_assert ((len) + 1 > (len)); \ | ||
1410 | GNUNET_array_grow (arr, len, len + 1); \ | ||
1411 | (arr) [len - 1] = element; \ | ||
1412 | } while (0) | ||
1413 | |||
1414 | |||
1415 | /** | ||
1416 | * @ingroup memory | ||
1417 | * Append @a arr2 to @a arr1 (growing @a arr1 | ||
1418 | * as needed). The @a arr2 array is left unchanged. Naturally | ||
1419 | * this function performs a shallow copy. Both arrays must have | ||
1420 | * the same type for their elements. | ||
1421 | * | ||
1422 | * @param arr1 base-pointer of the vector, may be NULL if @a len is 0; | ||
1423 | * will be updated to reflect the new address. The TYPE of | ||
1424 | * arr is important since size is the number of elements and | ||
1425 | * not the size in bytes | ||
1426 | * @param len1 the number of elements in the existing vector (number | ||
1427 | * of elements to copy over), will be updated with the new | ||
1428 | * array size | ||
1429 | * @param arr2 base-pointer a second array to concatenate, may be NULL if @a len2 is 0; | ||
1430 | * will be updated to reflect the new address. The TYPE of | ||
1431 | * arr is important since size is the number of elements and | ||
1432 | * not the size in bytes | ||
1433 | * @param len the number of elements in the existing vector (number | ||
1434 | * of elements to copy over), will be updated with the new | ||
1435 | * array size | ||
1436 | |||
1437 | */ | ||
1438 | #define GNUNET_array_concatenate(arr1, len1, arr2, len2) \ | ||
1404 | do \ | 1439 | do \ |
1405 | { \ | 1440 | { \ |
1406 | GNUNET_array_grow (arr, size, size + 1); \ | 1441 | const typeof (*arr2) * _a1 = (arr1); \ |
1407 | (arr) [size - 1] = element; \ | 1442 | const typeof (*arr1) * _a2 = (arr2); \ |
1443 | GNUNET_assert ((len1) + (len2) >= (len1)); \ | ||
1444 | GNUNET_assert (SIZE_MAX / sizeof (*_a1) >= ((len1) + (len2))); \ | ||
1445 | GNUNET_array_grow (arr1, len1, (len1) + (len2)); \ | ||
1446 | memcpy (&(arr1) [(len1) - (len2)], _a2, (len2) * sizeof (*arr1)); \ | ||
1408 | } while (0) | 1447 | } while (0) |
1409 | 1448 | ||
1410 | /** | 1449 | /** |