diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2024-10-23 21:39:09 +0200 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2024-10-23 21:39:47 +0200 |
commit | d214f12dee3a35d07c415e166eebb1210c2c48f5 (patch) | |
tree | fb9a1396bbeb25b7c5020c4c0391ea1675535666 | |
parent | 0a88f99b0ba31a81d9a35514c603178058811786 (diff) | |
download | gnunet-d214f12dee3a35d07c415e166eebb1210c2c48f5.tar.gz gnunet-d214f12dee3a35d07c415e166eebb1210c2c48f5.zip |
util: Removed GNUNET_xnew_array_* and friends. Fixes #9283
NEWS: Removed unsafe and unused functions for 2d/3d array allocation.
-rw-r--r-- | src/include/gnunet_common.h | 72 | ||||
-rw-r--r-- | src/lib/util/common_allocation.c | 53 | ||||
-rw-r--r-- | src/lib/util/test_common_allocation.c | 36 |
3 files changed, 0 insertions, 161 deletions
diff --git a/src/include/gnunet_common.h b/src/include/gnunet_common.h index f3ffa49f6..bf5d46856 100644 --- a/src/include/gnunet_common.h +++ b/src/include/gnunet_common.h | |||
@@ -1364,32 +1364,6 @@ GNUNET_is_zero_ (const void *a, | |||
1364 | 1364 | ||
1365 | /** | 1365 | /** |
1366 | * @ingroup memory | 1366 | * @ingroup memory |
1367 | * Allocate a size @a n times @a m array | ||
1368 | * with structs or unions of the given @a type. | ||
1369 | * | ||
1370 | * @param n size of the first dimension | ||
1371 | * @param m size of the second dimension | ||
1372 | * @param type name of the struct or union, i.e. pass 'struct Foo'. | ||
1373 | */ | ||
1374 | #define GNUNET_new_array_2d(n, m, type) \ | ||
1375 | (type **) GNUNET_xnew_array_2d_ (n, m, sizeof(type), __FILE__, __LINE__) | ||
1376 | |||
1377 | /** | ||
1378 | * @ingroup memory | ||
1379 | * Allocate a size @a n times @a m times @a o array | ||
1380 | * with structs or unions of the given @a type. | ||
1381 | * | ||
1382 | * @param n size of the first dimension | ||
1383 | * @param m size of the second dimension | ||
1384 | * @param o size of the third dimension | ||
1385 | * @param type name of the struct or union, i.e. pass 'struct Foo'. | ||
1386 | */ | ||
1387 | #define GNUNET_new_array_3d(n, m, o, type) \ | ||
1388 | (type ***) GNUNET_xnew_array_3d_ (n, m, o, sizeof(type), __FILE__, \ | ||
1389 | __LINE__) | ||
1390 | |||
1391 | /** | ||
1392 | * @ingroup memory | ||
1393 | * Wrapper around malloc. Allocates size bytes of memory. | 1367 | * Wrapper around malloc. Allocates size bytes of memory. |
1394 | * The memory will be zero'ed out. | 1368 | * The memory will be zero'ed out. |
1395 | * | 1369 | * |
@@ -1640,52 +1614,6 @@ GNUNET_xmalloc_ (size_t size, | |||
1640 | 1614 | ||
1641 | 1615 | ||
1642 | /** | 1616 | /** |
1643 | * Allocate memory for a two dimensional array in one block | ||
1644 | * and set up pointers. Aborts if no more memory is available. | ||
1645 | * Don't use GNUNET_xnew_array_2d_ directly. Use the | ||
1646 | * #GNUNET_new_array_2d macro. | ||
1647 | * The memory of the elements will be zero'ed out. | ||
1648 | * | ||
1649 | * @param n size of the first dimension | ||
1650 | * @param m size of the second dimension | ||
1651 | * @param elementSize size of a single element in bytes | ||
1652 | * @param filename where is this call being made (for debugging) | ||
1653 | * @param linenumber line where this call is being made (for debugging) | ||
1654 | * @return allocated memory, never NULL | ||
1655 | */ | ||
1656 | void ** | ||
1657 | GNUNET_xnew_array_2d_ (size_t n, | ||
1658 | size_t m, | ||
1659 | size_t elementSize, | ||
1660 | const char *filename, | ||
1661 | int linenumber); | ||
1662 | |||
1663 | |||
1664 | /** | ||
1665 | * Allocate memory for a three dimensional array in one block | ||
1666 | * and set up pointers. Aborts if no more memory is available. | ||
1667 | * Don't use GNUNET_xnew_array_3d_ directly. Use the | ||
1668 | * #GNUNET_new_array_3d macro. | ||
1669 | * The memory of the elements will be zero'ed out. | ||
1670 | * | ||
1671 | * @param n size of the first dimension | ||
1672 | * @param m size of the second dimension | ||
1673 | * @param o size of the third dimension | ||
1674 | * @param elementSize size of a single element in bytes | ||
1675 | * @param filename where is this call being made (for debugging) | ||
1676 | * @param linenumber line where this call is being made (for debugging) | ||
1677 | * @return allocated memory, never NULL | ||
1678 | */ | ||
1679 | void *** | ||
1680 | GNUNET_xnew_array_3d_ (size_t n, | ||
1681 | size_t m, | ||
1682 | size_t o, | ||
1683 | size_t elementSize, | ||
1684 | const char *filename, | ||
1685 | int linenumber); | ||
1686 | |||
1687 | |||
1688 | /** | ||
1689 | * Allocate and initialize memory. Checks the return value, aborts if no more | 1617 | * Allocate and initialize memory. Checks the return value, aborts if no more |
1690 | * memory is available. Don't use GNUNET_xmemdup_ directly. Use the | 1618 | * memory is available. Don't use GNUNET_xmemdup_ directly. Use the |
1691 | * #GNUNET_memdup macro. | 1619 | * #GNUNET_memdup macro. |
diff --git a/src/lib/util/common_allocation.c b/src/lib/util/common_allocation.c index afd701720..a2da20da4 100644 --- a/src/lib/util/common_allocation.c +++ b/src/lib/util/common_allocation.c | |||
@@ -69,59 +69,6 @@ GNUNET_xmalloc_ (size_t size, | |||
69 | } | 69 | } |
70 | 70 | ||
71 | 71 | ||
72 | void ** | ||
73 | GNUNET_xnew_array_2d_ (size_t n, | ||
74 | size_t m, | ||
75 | size_t elementSize, | ||
76 | const char *filename, | ||
77 | int linenumber) | ||
78 | { | ||
79 | /* use char pointer internally to avoid void pointer arithmetic warnings */ | ||
80 | char **ret = GNUNET_xmalloc_ (n * sizeof(void *) /* 1. dim header */ | ||
81 | + n * m * elementSize, /* element data */ | ||
82 | filename, | ||
83 | linenumber); | ||
84 | |||
85 | for (size_t i = 0; i < n; i++) | ||
86 | ret[i] = (char *) ret /* base address */ | ||
87 | + n * sizeof(void *) /* skip 1. dim header */ | ||
88 | + i * m * elementSize; /* skip to 2. dim row header */ | ||
89 | return (void **) ret; | ||
90 | } | ||
91 | |||
92 | |||
93 | void *** | ||
94 | GNUNET_xnew_array_3d_ (size_t n, | ||
95 | size_t m, | ||
96 | size_t o, | ||
97 | size_t elementSize, | ||
98 | const char *filename, | ||
99 | int linenumber) | ||
100 | { | ||
101 | /* use char pointer internally to avoid void pointer arithmetic warnings */ | ||
102 | char ***ret = GNUNET_xmalloc_ (n * sizeof(void **) /* 1. dim header */ | ||
103 | + n * m * sizeof(void *) /* 2. dim header */ | ||
104 | + n * m * o * elementSize, /* element data */ | ||
105 | filename, | ||
106 | linenumber); | ||
107 | |||
108 | for (size_t i = 0; i < n; i++) | ||
109 | { | ||
110 | /* need to cast to (char *) temporarily for byte level accuracy */ | ||
111 | ret[i] = (char **) ((char *) ret /* base address */ | ||
112 | + n * sizeof(void **) /* skip 1. dim header */ | ||
113 | + i * m * sizeof(void *)); /* skip to 2. dim header */ | ||
114 | for (size_t j = 0; j < m; j++) | ||
115 | ret[i][j] = (char *) ret /* base address */ | ||
116 | + n * sizeof(void **) /* skip 1. dim header */ | ||
117 | + n * m * sizeof(void *) /* skip 2. dim header */ | ||
118 | + i * m * o * elementSize /* skip to 2. dim part */ | ||
119 | + j * o * elementSize; /* skip to 3. dim row data */ | ||
120 | } | ||
121 | return (void ***) ret; | ||
122 | } | ||
123 | |||
124 | |||
125 | void * | 72 | void * |
126 | GNUNET_xmemdup_ (const void *buf, | 73 | GNUNET_xmemdup_ (const void *buf, |
127 | size_t size, | 74 | size_t size, |
diff --git a/src/lib/util/test_common_allocation.c b/src/lib/util/test_common_allocation.c index d4cc4bb58..a85aeccab 100644 --- a/src/lib/util/test_common_allocation.c +++ b/src/lib/util/test_common_allocation.c | |||
@@ -32,8 +32,6 @@ check (void) | |||
32 | { | 32 | { |
33 | #define MAX_TESTVAL 1024 | 33 | #define MAX_TESTVAL 1024 |
34 | char *ptrs[MAX_TESTVAL]; | 34 | char *ptrs[MAX_TESTVAL]; |
35 | unsigned int **a2; | ||
36 | char ***a3; | ||
37 | char *tmp; | 35 | char *tmp; |
38 | int i; | 36 | int i; |
39 | int j; | 37 | int j; |
@@ -98,40 +96,6 @@ check (void) | |||
98 | if (ptrs[0] != NULL) | 96 | if (ptrs[0] != NULL) |
99 | return 9; | 97 | return 9; |
100 | 98 | ||
101 | /* GNUNET_new_array_2d tests */ | ||
102 | a2 = GNUNET_new_array_2d (17, 22, unsigned int); | ||
103 | for (i = 0; i < 17; i++) | ||
104 | { | ||
105 | for (j = 0; j < 22; j++) | ||
106 | { | ||
107 | if (0 != a2[i][j]) | ||
108 | { | ||
109 | GNUNET_free (a2); | ||
110 | return 10; | ||
111 | } | ||
112 | a2[i][j] = i * 100 + j; | ||
113 | } | ||
114 | } | ||
115 | GNUNET_free (a2); | ||
116 | |||
117 | /* GNUNET_new_array_3d tests */ | ||
118 | a3 = GNUNET_new_array_3d (2, 3, 4, char); | ||
119 | for (i = 0; i < 2; i++) | ||
120 | { | ||
121 | for (j = 0; j < 3; j++) | ||
122 | { | ||
123 | for (k = 0; k < 4; k++) | ||
124 | { | ||
125 | if (0 != a3[i][j][k]) | ||
126 | { | ||
127 | GNUNET_free (a3); | ||
128 | return 11; | ||
129 | } | ||
130 | a3[i][j][k] = i * 100 + j * 10 + k; | ||
131 | } | ||
132 | } | ||
133 | } | ||
134 | GNUNET_free (a3); | ||
135 | return 0; | 99 | return 0; |
136 | } | 100 | } |
137 | 101 | ||