diff options
author | Sree Harsha Totakura <totakura@in.tum.de> | 2012-12-27 13:26:49 +0000 |
---|---|---|
committer | Sree Harsha Totakura <totakura@in.tum.de> | 2012-12-27 13:26:49 +0000 |
commit | fe5c2f8037abd4b56448fe7f36103d817bfa5a0c (patch) | |
tree | 1223d4d37d8422428c4b40ccfa368015d5e67547 /src/testbed/gnunet-service-testbed.c | |
parent | 757758f550b09e431ab6e1e6ba7681d71ce3599f (diff) | |
download | gnunet-fe5c2f8037abd4b56448fe7f36103d817bfa5a0c.tar.gz gnunet-fe5c2f8037abd4b56448fe7f36103d817bfa5a0c.zip |
- use GNUNET_array_grow()
Diffstat (limited to 'src/testbed/gnunet-service-testbed.c')
-rw-r--r-- | src/testbed/gnunet-service-testbed.c | 88 |
1 files changed, 28 insertions, 60 deletions
diff --git a/src/testbed/gnunet-service-testbed.c b/src/testbed/gnunet-service-testbed.c index 17520a246..8cf4b4037 100644 --- a/src/testbed/gnunet-service-testbed.c +++ b/src/testbed/gnunet-service-testbed.c | |||
@@ -920,22 +920,22 @@ static uint64_t event_mask; | |||
920 | /** | 920 | /** |
921 | * The size of the host list | 921 | * The size of the host list |
922 | */ | 922 | */ |
923 | static uint32_t host_list_size; | 923 | static unsigned int host_list_size; |
924 | 924 | ||
925 | /** | 925 | /** |
926 | * The size of the route list | 926 | * The size of the route list |
927 | */ | 927 | */ |
928 | static uint32_t route_list_size; | 928 | static unsigned int route_list_size; |
929 | 929 | ||
930 | /** | 930 | /** |
931 | * The size of directly linked neighbours list | 931 | * The size of directly linked neighbours list |
932 | */ | 932 | */ |
933 | static uint32_t slave_list_size; | 933 | static unsigned int slave_list_size; |
934 | 934 | ||
935 | /** | 935 | /** |
936 | * The size of the peer list | 936 | * The size of the peer list |
937 | */ | 937 | */ |
938 | static uint32_t peer_list_size; | 938 | static unsigned int peer_list_size; |
939 | 939 | ||
940 | /*********/ | 940 | /*********/ |
941 | /* Tasks */ | 941 | /* Tasks */ |
@@ -1024,21 +1024,27 @@ queue_message (struct GNUNET_SERVER_Client *client, | |||
1024 | 1024 | ||
1025 | 1025 | ||
1026 | /** | 1026 | /** |
1027 | * Similar to GNUNET_realloc; however clears tail part of newly allocated memory | 1027 | * Similar to GNUNET_array_grow(); however instead of calling GNUNET_array_grow() |
1028 | * several times we call it only once. The array is also made to grow in steps | ||
1029 | * of LIST_GROW_STEP. | ||
1028 | * | 1030 | * |
1029 | * @param ptr the memory block to realloc | 1031 | * @param ptr the array pointer to grow |
1030 | * @param size the size of ptr | 1032 | * @param size the size of array |
1031 | * @param new_size the size to which ptr has to be realloc'ed | 1033 | * @param accommodate_size the size which the array has to accommdate; after |
1032 | * @return the newly reallocated memory block | 1034 | * this call the array will be big enough to accommdate sizes upto |
1035 | * accommodate_size | ||
1033 | */ | 1036 | */ |
1034 | static void * | 1037 | #define array_grow_large_enough(ptr, size, accommodate_size) \ |
1035 | TESTBED_realloc (void *ptr, size_t size, size_t new_size) | 1038 | do \ |
1036 | { | 1039 | { \ |
1037 | ptr = GNUNET_realloc (ptr, new_size); | 1040 | unsigned int growth_size; \ |
1038 | if (new_size > size) | 1041 | GNUNET_assert (size <= accommodate_size); \ |
1039 | (void) memset (ptr + size, 0, new_size - size); | 1042 | growth_size = size; \ |
1040 | return ptr; | 1043 | while (growth_size <= accommodate_size) \ |
1041 | } | 1044 | growth_size += LIST_GROW_STEP; \ |
1045 | GNUNET_array_grow (ptr, size, growth_size); \ | ||
1046 | GNUNET_assert (size > accommodate_size); \ | ||
1047 | } while (0) | ||
1042 | 1048 | ||
1043 | 1049 | ||
1044 | /** | 1050 | /** |
@@ -1052,20 +1058,10 @@ static int | |||
1052 | host_list_add (struct GNUNET_TESTBED_Host *host) | 1058 | host_list_add (struct GNUNET_TESTBED_Host *host) |
1053 | { | 1059 | { |
1054 | uint32_t host_id; | 1060 | uint32_t host_id; |
1055 | uint32_t orig_size; | ||
1056 | 1061 | ||
1057 | host_id = GNUNET_TESTBED_host_get_id_ (host); | 1062 | host_id = GNUNET_TESTBED_host_get_id_ (host); |
1058 | orig_size = host_list_size; | ||
1059 | if (host_list_size <= host_id) | 1063 | if (host_list_size <= host_id) |
1060 | { | 1064 | array_grow_large_enough (host_list, host_list_size, host_id); |
1061 | while (host_list_size <= host_id) | ||
1062 | host_list_size += LIST_GROW_STEP; | ||
1063 | host_list = | ||
1064 | TESTBED_realloc (host_list, | ||
1065 | sizeof (struct GNUNET_TESTBED_Host *) * orig_size, | ||
1066 | sizeof (struct GNUNET_TESTBED_Host *) | ||
1067 | * host_list_size); | ||
1068 | } | ||
1069 | if (NULL != host_list[host_id]) | 1065 | if (NULL != host_list[host_id]) |
1070 | { | 1066 | { |
1071 | LOG_DEBUG ("A host with id: %u already exists\n", host_id); | 1067 | LOG_DEBUG ("A host with id: %u already exists\n", host_id); |
@@ -1084,18 +1080,8 @@ host_list_add (struct GNUNET_TESTBED_Host *host) | |||
1084 | static void | 1080 | static void |
1085 | route_list_add (struct Route *route) | 1081 | route_list_add (struct Route *route) |
1086 | { | 1082 | { |
1087 | uint32_t orig_size; | ||
1088 | |||
1089 | orig_size = route_list_size; | ||
1090 | if (route->dest >= route_list_size) | 1083 | if (route->dest >= route_list_size) |
1091 | { | 1084 | array_grow_large_enough (route_list, route_list_size, route->dest); |
1092 | while (route->dest >= route_list_size) | ||
1093 | route_list_size += LIST_GROW_STEP; | ||
1094 | route_list = | ||
1095 | TESTBED_realloc (route_list, | ||
1096 | sizeof (struct Route *) * orig_size, | ||
1097 | sizeof (struct Route *) * route_list_size); | ||
1098 | } | ||
1099 | GNUNET_assert (NULL == route_list[route->dest]); | 1085 | GNUNET_assert (NULL == route_list[route->dest]); |
1100 | route_list[route->dest] = route; | 1086 | route_list[route->dest] = route; |
1101 | } | 1087 | } |
@@ -1109,17 +1095,8 @@ route_list_add (struct Route *route) | |||
1109 | static void | 1095 | static void |
1110 | slave_list_add (struct Slave *slave) | 1096 | slave_list_add (struct Slave *slave) |
1111 | { | 1097 | { |
1112 | uint32_t orig_size; | ||
1113 | |||
1114 | orig_size = slave_list_size; | ||
1115 | if (slave->host_id >= slave_list_size) | 1098 | if (slave->host_id >= slave_list_size) |
1116 | { | 1099 | array_grow_large_enough (slave_list, slave_list_size, slave->host_id); |
1117 | while (slave->host_id >= slave_list_size) | ||
1118 | slave_list_size += LIST_GROW_STEP; | ||
1119 | slave_list = | ||
1120 | TESTBED_realloc (slave_list, sizeof (struct Slave *) * orig_size, | ||
1121 | sizeof (struct Slave *) * slave_list_size); | ||
1122 | } | ||
1123 | GNUNET_assert (NULL == slave_list[slave->host_id]); | 1100 | GNUNET_assert (NULL == slave_list[slave->host_id]); |
1124 | slave_list[slave->host_id] = slave; | 1101 | slave_list[slave->host_id] = slave; |
1125 | } | 1102 | } |
@@ -1133,17 +1110,8 @@ slave_list_add (struct Slave *slave) | |||
1133 | static void | 1110 | static void |
1134 | peer_list_add (struct Peer *peer) | 1111 | peer_list_add (struct Peer *peer) |
1135 | { | 1112 | { |
1136 | uint32_t orig_size; | ||
1137 | |||
1138 | orig_size = peer_list_size; | ||
1139 | if (peer->id >= peer_list_size) | 1113 | if (peer->id >= peer_list_size) |
1140 | { | 1114 | array_grow_large_enough (peer_list, peer_list_size, peer->id); |
1141 | while (peer->id >= peer_list_size) | ||
1142 | peer_list_size += LIST_GROW_STEP; | ||
1143 | peer_list = | ||
1144 | TESTBED_realloc (peer_list, sizeof (struct Peer *) * orig_size, | ||
1145 | sizeof (struct Peer *) * peer_list_size); | ||
1146 | } | ||
1147 | GNUNET_assert (NULL == peer_list[peer->id]); | 1115 | GNUNET_assert (NULL == peer_list[peer->id]); |
1148 | peer_list[peer->id] = peer; | 1116 | peer_list[peer->id] = peer; |
1149 | } | 1117 | } |
@@ -1157,8 +1125,8 @@ peer_list_add (struct Peer *peer) | |||
1157 | static void | 1125 | static void |
1158 | peer_list_remove (struct Peer *peer) | 1126 | peer_list_remove (struct Peer *peer) |
1159 | { | 1127 | { |
1128 | unsigned int orig_size; | ||
1160 | uint32_t id; | 1129 | uint32_t id; |
1161 | uint32_t orig_size; | ||
1162 | 1130 | ||
1163 | peer_list[peer->id] = NULL; | 1131 | peer_list[peer->id] = NULL; |
1164 | orig_size = peer_list_size; | 1132 | orig_size = peer_list_size; |