@cryptotaxi247 / netdata-1 / commits / e2698bfae

strncpyz, snprintfz and vsnprintfz functions added.

Frederico Lamberti Pissarra committed May 9, 2016 at 00:25 UTC e2698bfae5680feebc81a371561b69c410473bcc
2 files changed +57 -11
src/common.c
+53 -11
@@ -582,8 +582,11 @@ uint32_t simple_hash(const char *name) {
582 // FNV-1a algorithm
583 while (*s) {
584 // multiply by the 32 bit FNV magic prime mod 2^32
585 - // gcc optimized
586 - hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
585 + // NOTE: No need to optimize with left shifts.
586 + // GCC will use imul instruction anyway.
587 + // Tested with 'gcc -O3 -S'
588 + //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
589 + hval *= 16777619;
590
591 // xor the bottom with the current octet
592 hval ^= (uint32_t)*s++;
@@ -618,9 +621,15 @@ uint32_t simple_hash(const char *name) {
621
622 void strreverse(char* begin, char* end)
623 {
621 - char aux;
622 - while (end > begin)
623 - aux = *end, *end-- = *begin, *begin++ = aux;
624 + char aux;
625 +
626 + while (end > begin)
627 + {
628 + // clearer code.
629 + aux = *end;
630 + *end-- = *begin;
631 + *begin++ = aux;
632 + }
633 }
634
635 char *mystrsep(char **ptr, char *s)
@@ -633,17 +642,22 @@ char *mystrsep(char **ptr, char *s)
642 char *trim(char *s)
643 {
644 // skip leading spaces
645 + // and 'comments' as well!?
646 while(*s && isspace(*s)) s++;
647 if(!*s || *s == '#') return NULL;
648
649 // skip tailing spaces
640 - long c = (long) strlen(s) - 1;
641 - while(c >= 0 && isspace(s[c])) {
642 - s[c] = '\0';
643 - c--;
650 + // this way is way faster. Writes only one NUL char.
651 + ssize_t l = strlen(s);
652 + if (--l >= 0)
653 + {
654 + char *p = s + l;
655 + while (p > s && isspace(*p)) p--;
656 + *++p = '\0';
657 }
645 - if(c < 0) return NULL;
658 +
659 if(!*s) return NULL;
660 +
661 return s;
662 }
663
@@ -710,7 +724,7 @@ int savememory(const char *filename, void *mem, unsigned long size)
724 {
725 char tmpfilename[FILENAME_MAX + 1];
726
713 - snprintf(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
727 + snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
728
729 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
730 if(fd < 0) {
@@ -762,3 +776,31 @@ pid_t gettid(void)
776 return syscall(SYS_gettid);
777 }
778
779 +char *strncpyz(char *dest, const char *src, size_t n)
780 +{
781 + char *p = dest;
782 +
783 + while (n--)
784 + *dest++ = *src++;
785 + *dest = '\0';
786 +
787 + return p;
788 +}
789 +
790 +int vsnprintfz(char *sout, size_t n, const char *fmt, va_list args)
791 +{
792 + int size;
793 +
794 + size = vsnprintf(sout, n, fmt, args);
795 + sout[size] = '\0';
796 + return size;
797 +}
798 +
799 +int snprintfz(char *sout, size_t n, const char *fmt, ...)
800 +{
801 + va_list args;
802 +
803 + va_start(args, fmt);
804 + return vsnprintfz(sout, n, fmt, args);
805 + // args will vanish!
806 +}
src/common.h
+4
@@ -1,3 +1,4 @@
1 +#include <stdarg.h>
2 #include <sys/time.h>
3 #include <sys/resource.h>
4
@@ -22,6 +23,9 @@ extern uint32_t simple_hash(const char *name);
23 extern void strreverse(char* begin, char* end);
24 extern char *mystrsep(char **ptr, char *s);
25 extern char *trim(char *s);
26 +extern char *strncpyz(char *dest, const char *src, size_t n);
27 +extern int vsnprintfz(char *sout, size_t n, const char *fmt, va_list args);
28 +extern int snprintfz(char *sout, size_t n, const char *fmt, ...);
29
30 extern void *mymmap(const char *filename, size_t size, int flags, int ksm);
31 extern int savememory(const char *filename, void *mem, unsigned long size);