From 6b47d9ffaf8478c75cdf9c76ecc872288e68a23b Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sun, 23 Oct 2011 22:18:38 +0200 Subject: [PATCH] all files: use restrict key word for all pointer arguments Limit the effects of pointer aliasing and aiding caching optimizations. http://cellperformance.beyond3d.com/articles/2006/05/demystifying-the-restrict-keyword.html Signed-off-by: Sami Kerola --- configure.ac | 3 ++- src/dhcpd-pools.h | 20 ++++++++++---------- src/getdata.c | 8 ++++---- src/other.c | 6 +++--- src/output.c | 20 ++++++++++---------- src/sort.c | 8 ++++---- 6 files changed, 33 insertions(+), 32 deletions(-) diff --git a/configure.ac b/configure.ac index 89d285e..845b56c 100644 --- a/configure.ac +++ b/configure.ac @@ -14,7 +14,8 @@ AC_GNU_SOURCE # Checks for programs AC_PROG_AWK -AC_PROG_CC +AC_PROG_CC_C99 +AC_C_RESTRICT AC_PROG_CXX AC_PROG_MAKE_SET AC_PROG_LIBTOOL diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index 26b6ce3..e1b7db2 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -145,9 +145,9 @@ struct macaddr_t *macaddr; /* Function prototypes */ int prepare_memory(void); int parse_leases(void); -void parse_config(int, char *, struct shared_network_t *) +void parse_config(int, const char *__restrict, struct shared_network_t *__restrict) __attribute__ ((nonnull(2, 3))); -int nth_field(int n, char *dest, const char *src) +int nth_field(int n, char *__restrict dest, const char *__restrict src) __attribute__ ((nonnull(2, 3))) #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) __attribute__ ((__hot__)) @@ -155,7 +155,7 @@ int nth_field(int n, char *dest, const char *src) ; int prepare_data(void); int do_counting(void); -void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges) +void flip_ranges(struct range_t *__restrict ranges, struct range_t *__restrict tmp_ranges) __attribute__ ((nonnull(1, 2))); /* support functions */ void *safe_malloc(const size_t size) @@ -166,15 +166,15 @@ void *safe_malloc(const size_t size) #endif #endif ; -void *safe_realloc(void *ptr, const size_t size); -char *safe_strdup(const char *str) __attribute__ ((nonnull(1))); -int xstrstr(char *a, char *b, int len); +void *safe_realloc(void *__restrict ptr, const size_t size); +char *safe_strdup(const char *__restrict str) __attribute__ ((nonnull(1))); +int xstrstr(char *__restrict a, char *__restrict b, int len); void print_version(void) __attribute__ ((noreturn)); void usage(int status) __attribute__ ((noreturn)); /* qsort required functions... */ /* ...for ranges and... */ -int intcomp(const void *x, const void *y) __attribute__ ((nonnull(1, 2))); -int rangecomp(const void *r1, const void *r2) +int intcomp(const void *__restrict x, const void *__restrict y) __attribute__ ((nonnull(1, 2))); +int rangecomp(const void *__restrict r1, const void *__restrict r2) __attribute__ ((nonnull(1, 2))); /* sort function pointer and functions */ int sort_name(void); @@ -187,9 +187,9 @@ unsigned long int ret_touched(struct range_t r); unsigned long int ret_tc(struct range_t r); unsigned long int ret_tcperc(struct range_t r); void field_selector(char c); -int get_order(struct range_t *left, struct range_t *right) +int get_order(struct range_t *__restrict left, struct range_t *__restrict right) __attribute__ ((nonnull(1, 2))); -void mergesort_ranges(struct range_t *orig, int size, struct range_t *temp) +void mergesort_ranges(struct range_t *__restrict orig, int size, struct range_t *__restrict temp) __attribute__ ((nonnull(1, 3))); /* output function pointer and functions */ int (*output_analysis) (void); diff --git a/src/getdata.c b/src/getdata.c index 2946ee0..d034d86 100644 --- a/src/getdata.c +++ b/src/getdata.c @@ -198,7 +198,7 @@ int parse_leases(void) * first field is 1 and not 0 like C programs should have. Question of * semantics, send mail to author if this annoys. All performance boosts for * this function are well come. */ -int nth_field(int n, char *dest, const char *src) +int nth_field(int n, char *restrict dest, const char *restrict src) { int i, j = 0, wordn = 0, len; @@ -225,7 +225,7 @@ int nth_field(int n, char *dest, const char *src) } /* dhcpd.conf interesting words */ -int is_interesting_config_clause(char *s) +int is_interesting_config_clause(char *restrict s) { if (strstr(s, "range")) { return 3; @@ -239,8 +239,8 @@ int is_interesting_config_clause(char *s) } /* FIXME: This spagetti monster function need to be rewrote at least ones. */ -void parse_config(int is_include, char *config_file, - struct shared_network_t *shared_p) +void parse_config(int is_include, const char *restrict config_file, + struct shared_network_t *restrict shared_p) { FILE *dhcpd_config; int newclause = true, argument = false, comment = diff --git a/src/other.c b/src/other.c index c287ff3..391c623 100644 --- a/src/other.c +++ b/src/other.c @@ -82,7 +82,7 @@ int #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) __attribute__ ((hot)) #endif - xstrstr(char *a, char *b, int len) + xstrstr(char *restrict a, char *restrict b, int len) { int i; /* two spaces are very common in lease file, after them @@ -105,7 +105,7 @@ int } /* Simple strdup wrapper */ -char *safe_strdup(const char *str) +char *safe_strdup(const char *restrict str) { char *ret = strdup(str); @@ -114,7 +114,7 @@ char *safe_strdup(const char *str) return ret; } -void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges) +void flip_ranges(struct range_t *restrict ranges, struct range_t *restrict tmp_ranges) { unsigned int i = num_ranges - 1, j; diff --git a/src/output.c b/src/output.c index 6f4474f..4ef5e66 100644 --- a/src/output.c +++ b/src/output.c @@ -313,7 +313,7 @@ int output_xml(void) return 0; } -void html_header(FILE * f) +void html_header(FILE *restrict f) { char outstr[200]; struct tm *tmp; @@ -387,7 +387,7 @@ void html_header(FILE * f) fprintf(f, "The lease file mtime: %s", outstr); } -void html_footer(FILE * f) +void html_footer(FILE *restrict f) { fprintf(f, "


\n"); fprintf(f, "
\n"); @@ -403,44 +403,44 @@ void html_footer(FILE * f) fprintf(f, "\n"); } -void newrow(FILE * f) +void newrow(FILE *restrict f) { fprintf(f, "\n"); } -void endrow(FILE * f) +void endrow(FILE *restrict f) { fprintf(f, "\n\n"); } -void output_line(FILE * f, char *type, char *class, char *text) +void output_line(FILE *restrict f, char *restrict type, char *restrict class, char *restrict text) { fprintf(f, " <%s class=%s>%s\n", type, class, text, type); } -void output_long(FILE * f, char *type, unsigned long unlong) +void output_long(FILE *restrict f, char *restrict type, unsigned long unlong) { fprintf(f, " <%s class=ralign>%lu\n", type, unlong, type); } -void output_float(FILE * f, char *type, float fl) +void output_float(FILE * f, char *restrict type, float fl) { fprintf(f, " <%s class=ralign>%.3f\n", type, fl, type); } -void table_start(FILE * f) +void table_start(FILE *restrict f) { fprintf(f, "\n"); } -void table_end(FILE * f) +void table_end(FILE *restrict f) { fprintf(f, "
\n"); } -void newsection(FILE * f, char *title) +void newsection(FILE *restrict f, char *restrict title) { newrow(f); output_line(f, "td", "calign", " "); diff --git a/src/sort.c b/src/sort.c index b5c6fb6..e956608 100644 --- a/src/sort.c +++ b/src/sort.c @@ -44,7 +44,7 @@ #include "dhcpd-pools.h" /* Sort functions for range sorting */ -int intcomp(const void *x, const void *y) +int intcomp(const void *restrict x, const void *restrict y) { if (*(uint32_t *) x < *(uint32_t *) y) return -1; @@ -54,7 +54,7 @@ int intcomp(const void *x, const void *y) return 0; } -int rangecomp(const void *r1, const void *r2) +int rangecomp(const void *restrict r1, const void *restrict r2) { if ((((struct range_t *)r1)->first_ip) < (((struct range_t *)r2)->first_ip)) @@ -139,7 +139,7 @@ void field_selector(char c) } /* Needed to support multiple key sorting. */ -int get_order(struct range_t *left, struct range_t *right) +int get_order(struct range_t *restrict left, struct range_t *restrict right) { int i, len, ret; unsigned long int lint, rint; @@ -178,7 +178,7 @@ int get_order(struct range_t *left, struct range_t *right) return (0); } -void mergesort_ranges(struct range_t *orig, int size, struct range_t *temp) +void mergesort_ranges(struct range_t *restrict orig, int size, struct range_t *restrict temp) { int left, right, i; struct range_t hold;