summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Curley <charlescurley@users.noreply.github.com>2018-01-19 14:51:23 -0700
committerPierre GRANDIN <pgrandin@users.noreply.github.com>2018-01-19 13:51:23 -0800
commit852c5e3066af3827fa41013189e2a8f3e6b78397 (patch)
tree7e9133c46e35a15ae4e0b441f578d0baa7067a19
parent495acaddf11c603bd9c56ffdf5c987c49b6b36f8 (diff)
downloadnavit-852c5e3066af3827fa41013189e2a8f3e6b78397.tar.gz
fix:navit:navigation.c:distance_set_last (#373)
* fix:navit:navigation.c:distance_set_last Added a test so that the array subscript is never less than the lower bound. Note: there is no test for the upper bound. Also, I saw no reason to test for i == 0 right after the code initialized it to zero. modified: navit/navigation.c * Once more with feeling. Get rid of the function distance_set_last () entirely and use compile-time maths to do the work. fix:navit:navigation.c modified: navit/navigation.c * Added a comment on this method of calculating the number of elements in an array (#define SIZE_OF_ARRAY_DISTANCES). comment:navit:navigation.c modified: navit/navigation.c
-rw-r--r--navit/navigation.c37
1 files changed, 20 insertions, 17 deletions
diff --git a/navit/navigation.c b/navit/navigation.c
index cc1b47bc2..a31ee8459 100644
--- a/navit/navigation.c
+++ b/navit/navigation.c
@@ -194,8 +194,25 @@ struct navigation {
/** @brief Set of simplified distance values that are easy to be pronounced.
* Used for the 'vocabulary_distances' configuration.
*/
-int distances[]={1,2,3,4,5,10,25,50,75,100,150,200,250,300,400,500,750,-1};
+/**
+ * This method of calculating the number of elements in an array
+ * (#define SIZE_OF_ARRAY_DISTANCES, below) will work for most modern
+ * processors. It may cause problems on a few obscure processors, none
+ * of which are likely candidates for navit anyway.
+ *
+ * It works like so: Modern processors simply stuff the elements of an
+ * int array into memory one after the other, with no gaps. Some older
+ * processors might not do so, due to memory alignment issues. This
+ * method does not take such gaps into account. For more discussion,
+ * see https://github.com/navit-gps/navit/pull/373
+ *
+ * So if you are on an oddball processor and start getting really odd
+ * values for distances, this might be the reason. Good luck!
+ */
+const int distances[]={1,2,3,4,5,10,25,50,75,100,150,200,250,300,400,500,750};
+#define SIZE_OF_ARRAY_DISTANCES (sizeof (distances)/sizeof (int))
+#define LAST_DISTANCE (SIZE_OF_ARRAY_DISTANCES - 1)
/* Allowed values for navigation_maneuver.merge_or_exit
* The numeric values are chosen in such a way that they can be interpreted as flags:
@@ -965,20 +982,6 @@ round_distance(int dist)
return dist*10000;
}
-/** @brief Returns the last element of the simplified numbers containing a distance value.
-* @return value with the highest distance.
-*/
-static int
-distance_set_last(void)
-{
- static int i=0;
- if (i == 0) {
- while (distances[i] > 0)
- i++;
- }
- return distances[i-1];
-}
-
/** @brief Restricts the distance value to a simple set of pronounceable numbers.
* @param dist The distance to be processed
* @return distance Simplified distance value
@@ -987,14 +990,14 @@ static int
round_distance_reduced( int dist )
{
int factor = 1;
- if (dist > distance_set_last())
+ if (dist > distances[LAST_DISTANCE])
{
dist=(dist+500)/1000;
factor = 1000;
}
int i=0,d=0,m=0;
- while (distances[i] > 0) {
+ while (i < SIZE_OF_ARRAY_DISTANCES) {
if (!i || abs(distances[i]-dist) <= d) {
d=abs(distances[i]-dist);
m=i;