Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1224191pxu; Mon, 23 Nov 2020 15:10:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJyAffuWBminRXNPsM/gZuIhpmiUEvhvlv+G+/9DFMGEGB5FPYhLS2PC70Qc3sXhYDJt/uDZ X-Received: by 2002:a17:906:b53:: with SMTP id v19mr1708597ejg.250.1606173047105; Mon, 23 Nov 2020 15:10:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606173047; cv=none; d=google.com; s=arc-20160816; b=Z/EvFUFOSQjRLAXqeb5PRjCEvDQs9716l3gQSc9Qe2VX0Cl1QtXKgwKpX4smPELThb Bd8Vk/rfLVtC4P3Zhe2zhCLazBYw3wPj4t2LTBbfhZRiXMUW6qBMdjQv89EV6CoLMCJv ZB2MchfG5Z7avzgfvSUW780ygP5PG525n2t8m2Qqc69skifmBdKUSb98d+eqv1YP43/b MknfTSSkePgRcsBgPdf9WJeAG/hJuLgJdp1HLmJRW4sEOB4tkKlhaGrKeiKdmjyz4x17 Tzbhse9EL33DSgWphVpdlf+vyquGGwMWyfNPfC42vD1cjbWVzj8hY37hWRuMHfFUoxrG Poqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=1o7hqx9VzV3mb2SlQrBWTAviyYTUHP2AT+Zvnv901bM=; b=l9rGZ2PatFjKd4SbqpEbiM4WyfZNfPdn0imSgU+tf8qrctaLn1lOcJb0o4L8TlTRUh jrCWHMFV2JV9fK1nwxZwQda7fe53aJ+qGfYEg6DK+tamc5mN/7/d96c796KXztV/2K68 C3eUnv3pevvQA/cqY7PCQ03DhMaU5dFKP9/i4YaAT6fV9m0QpM9fAZCvv/Hq8mnmOieL 6+p7Ha8T/kWhpbHpre5YzCPhh5VRdhiSbiG1j1/vHURYLr80QkHpc+Ga1OTmQkgLWfCA o38pvZklxVO/8o6zuJmPJyjEn+Jf6DrdlySWzG0gVjgngPUCgiWYD101WCrb0RCDm7vP Mruw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q9si7661173edn.33.2020.11.23.15.10.24; Mon, 23 Nov 2020 15:10:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387575AbgKWXFu convert rfc822-to-8bit (ORCPT + 99 others); Mon, 23 Nov 2020 18:05:50 -0500 Received: from us-smtp-delivery-44.mimecast.com ([207.211.30.44]:25937 "EHLO us-smtp-delivery-44.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732902AbgKWXFt (ORCPT ); Mon, 23 Nov 2020 18:05:49 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-97-qEOhRQjBPNa2TqYnEdrPXA-1; Mon, 23 Nov 2020 18:05:42 -0500 X-MC-Unique: qEOhRQjBPNa2TqYnEdrPXA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F2D7C10866A0; Mon, 23 Nov 2020 23:05:39 +0000 (UTC) Received: from krava.redhat.com (unknown [10.40.195.242]) by smtp.corp.redhat.com (Postfix) with ESMTP id C2B0B5D9CA; Mon, 23 Nov 2020 23:05:36 +0000 (UTC) From: Jiri Olsa To: Arnaldo Carvalho de Melo Cc: lkml , Peter Zijlstra , Ingo Molnar , Mark Rutland , Namhyung Kim , Alexander Shishkin , Michael Petlan , Song Liu , Ian Rogers , Stephane Eranian , Alexey Budankov , Andi Kleen , Adrian Hunter Subject: [PATCH 05/25] tools lib: Adopt memchr_inv() from kernel Date: Tue, 24 Nov 2020 00:04:52 +0100 Message-Id: <20201123230512.2097312-6-jolsa@kernel.org> In-Reply-To: <20201123230512.2097312-1-jolsa@kernel.org> References: <20201123230512.2097312-1-jolsa@kernel.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jolsa@kernel.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kernel.org Content-Transfer-Encoding: 8BIT Content-Type: text/plain; charset=WINDOWS-1252 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We'll use it to check for undefined/zero data. Signed-off-by: Jiri Olsa --- tools/include/linux/string.h | 1 + tools/lib/string.c | 58 ++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/tools/include/linux/string.h b/tools/include/linux/string.h index 5e9e781905ed..db5c99318c79 100644 --- a/tools/include/linux/string.h +++ b/tools/include/linux/string.h @@ -46,4 +46,5 @@ extern char * __must_check skip_spaces(const char *); extern char *strim(char *); +extern void *memchr_inv(const void *start, int c, size_t bytes); #endif /* _TOOLS_LINUX_STRING_H_ */ diff --git a/tools/lib/string.c b/tools/lib/string.c index f645343815de..8b6892f959ab 100644 --- a/tools/lib/string.c +++ b/tools/lib/string.c @@ -168,3 +168,61 @@ char *strreplace(char *s, char old, char new) *s = new; return s; } + +static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes) +{ + while (bytes) { + if (*start != value) + return (void *)start; + start++; + bytes--; + } + return NULL; +} + +/** + * memchr_inv - Find an unmatching character in an area of memory. + * @start: The memory area + * @c: Find a character other than c + * @bytes: The size of the area. + * + * returns the address of the first character other than @c, or %NULL + * if the whole buffer contains just @c. + */ +void *memchr_inv(const void *start, int c, size_t bytes) +{ + u8 value = c; + u64 value64; + unsigned int words, prefix; + + if (bytes <= 16) + return check_bytes8(start, value, bytes); + + value64 = value; + value64 |= value64 << 8; + value64 |= value64 << 16; + value64 |= value64 << 32; + + prefix = (unsigned long)start % 8; + if (prefix) { + u8 *r; + + prefix = 8 - prefix; + r = check_bytes8(start, value, prefix); + if (r) + return r; + start += prefix; + bytes -= prefix; + } + + words = bytes / 8; + + while (words) { + if (*(u64 *)start != value64) + return check_bytes8(start, value, 8); + start += 8; + words--; + } + + return check_bytes8(start, value, bytes % 8); +} -- 2.26.2