Author: das
Date: Sat Jan 31 18:27:02 2009
New Revision: 187961
URL: http://svn.freebsd.org/changeset/base/187961

Log:
  Add a function attribute called `__malloc_like', which informs gcc
  that the annotated function returns a pointer that doesn't alias any
  extant pointer. This results in a 50%+ speedup in microbenchmarks such
  as the following:
  
      char *cp = malloc(1), *buf = malloc(BUF);
      for (i = 0; i < BUF; i++) buf[i] = *cp;
  
  In real programs, your mileage will vary. Note that gcc already
  performs this optimization automatically for any function called
  `malloc', `calloc', `strdup', or `strndup' unless -fno-builtins is
  used.

Modified:
  head/include/stdlib.h
  head/include/string.h
  head/include/wchar.h
  head/sys/sys/cdefs.h
  head/sys/sys/malloc.h

Modified: head/include/stdlib.h
==============================================================================
--- head/include/stdlib.h       Sat Jan 31 18:06:34 2009        (r187960)
+++ head/include/stdlib.h       Sat Jan 31 18:27:02 2009        (r187961)
@@ -88,14 +88,14 @@ int  atoi(const char *);
 long    atol(const char *);
 void   *bsearch(const void *, const void *, size_t,
            size_t, int (*)(const void *, const void *));
-void   *calloc(size_t, size_t);
+void   *calloc(size_t, size_t) __malloc_like;
 div_t   div(int, int) __pure2;
 void    exit(int) __dead2;
 void    free(void *);
 char   *getenv(const char *);
 long    labs(long) __pure2;
 ldiv_t  ldiv(long, long) __pure2;
-void   *malloc(size_t);
+void   *malloc(size_t) __malloc_like;
 int     mblen(const char *, size_t);
 size_t  mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
 int     mbtowc(wchar_t * __restrict, const char * __restrict, size_t);

Modified: head/include/string.h
==============================================================================
--- head/include/string.h       Sat Jan 31 18:06:34 2009        (r187960)
+++ head/include/string.h       Sat Jan 31 18:27:02 2009        (r187961)
@@ -78,7 +78,7 @@ int    strcoll(const char *, const char *)
 char   *strcpy(char * __restrict, const char * __restrict);
 size_t  strcspn(const char *, const char *) __pure;
 #if __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
-char   *strdup(const char *);
+char   *strdup(const char *) __malloc_like;
 #endif
 char   *strerror(int);
 #if __POSIX_VISIBLE >= 200112
@@ -96,7 +96,7 @@ char  *strncat(char * __restrict, const c
 int     strncmp(const char *, const char *, size_t) __pure;
 char   *strncpy(char * __restrict, const char * __restrict, size_t);
 #if __BSD_VISIBLE
-char   *strndup(const char *, size_t);
+char   *strndup(const char *, size_t) __malloc_like;
 char   *strnstr(const char *, const char *, size_t) __pure;
 #endif
 char   *strpbrk(const char *, const char *) __pure;

Modified: head/include/wchar.h
==============================================================================
--- head/include/wchar.h        Sat Jan 31 18:06:34 2009        (r187960)
+++ head/include/wchar.h        Sat Jan 31 18:27:02 2009        (r187961)
@@ -214,7 +214,7 @@ int wcwidth(wchar_t);
 wchar_t        *fgetwln(struct __sFILE * __restrict, size_t * __restrict);
 size_t mbsnrtowcs(wchar_t * __restrict, const char ** __restrict, size_t,
            size_t, mbstate_t * __restrict);
-wchar_t        *wcsdup(const wchar_t *);
+wchar_t        *wcsdup(const wchar_t *) __malloc_like;
 size_t wcsnrtombs(char * __restrict, const wchar_t ** __restrict, size_t,
            size_t, mbstate_t * __restrict);
 size_t wcslcat(wchar_t *, const wchar_t *, size_t);

Modified: head/sys/sys/cdefs.h
==============================================================================
--- head/sys/sys/cdefs.h        Sat Jan 31 18:06:34 2009        (r187960)
+++ head/sys/sys/cdefs.h        Sat Jan 31 18:27:02 2009        (r187961)
@@ -221,8 +221,10 @@
 #endif
 
 #if __GNUC_PREREQ__(2, 96)
+#define        __malloc_like   __attribute__((__malloc__))
 #define        __pure          __attribute__((__pure__))
 #else
+#define        __malloc_like
 #define        __pure
 #endif
 

Modified: head/sys/sys/malloc.h
==============================================================================
--- head/sys/sys/malloc.h       Sat Jan 31 18:06:34 2009        (r187960)
+++ head/sys/sys/malloc.h       Sat Jan 31 18:27:02 2009        (r187961)
@@ -190,9 +190,9 @@ typedef void malloc_type_list_func_t(str
 void   contigfree(void *addr, unsigned long size, struct malloc_type *type);
 void   *contigmalloc(unsigned long size, struct malloc_type *type, int flags,
            vm_paddr_t low, vm_paddr_t high, unsigned long alignment,
-           unsigned long boundary);
+           unsigned long boundary) __malloc_like;
 void   free(void *addr, struct malloc_type *type);
-void   *malloc(unsigned long size, struct malloc_type *type, int flags);
+void   *malloc(unsigned long size, struct malloc_type *type, int flags) 
__malloc_like;
 void   malloc_init(void *);
 int    malloc_last_fail(void);
 void   malloc_type_allocated(struct malloc_type *type, unsigned long size);
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to