Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp4219885imj; Tue, 12 Feb 2019 11:55:09 -0800 (PST) X-Google-Smtp-Source: AHgI3IYWSfOTgoW7mDusteiB8TriJpR9jVyD9+hpCqVvkivIMzW7ZesBmthbIFy0KIDUTa2ks6jF X-Received: by 2002:a17:902:6681:: with SMTP id e1mr5582274plk.98.1550001309170; Tue, 12 Feb 2019 11:55:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550001309; cv=none; d=google.com; s=arc-20160816; b=jW4XDS+W/g1WhLEru018NVAX0HDNp9yUKjLWN+QtHGt7Lmw9GasmJ69zboOGaV5uV1 Sp64vXCQvi/QOUdXBQpgfVDWntxpgFoQgS7aV9UMQNkrLdGbKCYm3ihRHsXUnYkcujyC qiK+DsXZjOcJfVj/poMJB6hMyiDPk7vH5IjUIkTyqzNeEG3OdkuHDRHTmDfU270TE5FO HugPcImE2u78VYF2+JXFCE3TrwQZHrFCXIex5Y1Id68XL/Rbnywh4BB6iIiHMynDeXk6 bSlhCS+/tXwggutahi7GwB5i99ybm+Ttj9d72eZfsr9/r/IP1PBFRJhZwSpnFjjU0dWv 9liQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=i3gc5E2H5s0Z0cU1Rtar6FigVqhI90HZ+Cw+llye4LQ=; b=mbIn1vNG47HYHF1DEAFBoHXdvNyGB3veq6q4ECQPXscJJZV3MNH5/9Q5/EZCwgsC2c yEuv/jU8QgIH9TCYyQrUdN5/NbRdMPnkcIS6ELHOBoPYU7nlN3QshGfi89wJuJ3EeyDP jY7wrKYnvQ/Sb8+eWV2USDvLHAcv9l4YMX9G2htK/zKYWVvotutyr3uMFRnhXeRo97HD yUxN8THFWVm7ULhi5IcobNP/eR+LkfIL62IDLi39l+oESN6eh/6oTGZSnIe5Wa84v1ef IFK9BCEMMoXSyLh5pZD1UFX10wPBNTxLhG9yp3+syt/Tb8/P/FS0fMjGnMVm9NPntDGv p8tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=JyRhdG2M; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c24si546491pgj.60.2019.02.12.11.54.53; Tue, 12 Feb 2019 11:55:09 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=JyRhdG2M; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730360AbfBLSzJ (ORCPT + 99 others); Tue, 12 Feb 2019 13:55:09 -0500 Received: from mail-pl1-f194.google.com ([209.85.214.194]:36562 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727230AbfBLSzI (ORCPT ); Tue, 12 Feb 2019 13:55:08 -0500 Received: by mail-pl1-f194.google.com with SMTP id g9so1734355plo.3 for ; Tue, 12 Feb 2019 10:55:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=i3gc5E2H5s0Z0cU1Rtar6FigVqhI90HZ+Cw+llye4LQ=; b=JyRhdG2M++y73EuFdk+qw423DPq0SZe7Zi2/PDKmsnH9A7eaWIK85CJC0eKLQr6nPS LBuGVSzhwg2Ho+xwQIMvxrqZpseMPXz1WxogW65pFWNh3XFyXdPeWRLJPMgxgEL/RBL8 IrtTNaIku3F0KI+hkcTSeAf0StsujnhoejyjnUs/BA5GnYJEKxUbXDZKlwVKXtEDRWfn ouMuHpMZnWTmVATRGZ71Cd5l1MVDbBwU/g2nu7JhJHz4reQTGD7Zcg8TRHqwK5LBcExZ Fw2IkW/20g1hWYpxHAsCrKmBvK003LRlw5YRFMfodVthvom4cIlpDNJOGvBsg8sWBhSN Uu0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=i3gc5E2H5s0Z0cU1Rtar6FigVqhI90HZ+Cw+llye4LQ=; b=bqkIPVvJytfe2PGd+ZniNa4K3r/jQDvRg6zXeaayUroTMz4ap3S7IaspAz6FKkjTYH LnkDjSk95NlPQlJowLMwOFdAaP798549QKRIjus8d2TN1Rc57QYwAZxamAxH2NC+LYax F/Y1kKH47peZZ0UZD7JJB7VLX8IA175ojr8QWUwWyWv9D/zlyD0+kf8ynqK389MCAgOB ekDKODc00ghp5+7CZim8KZznR4u1K4huBg1RwFLKjJU30ONBH5cJEpn0j7cajZVy2TwQ pYsX49WiU/drN15sWQp5lKGbHgJDr/GoYSYsMvdGAyWaB1z8TCgReEgrP8/9n10Keepq 1FYA== X-Gm-Message-State: AHQUAuaLDE8yzzzt0JUDKSGLhn3JTtbJZA2VtSGrTR24fkzr6UAG8zok 8eNWSs1MIEcKr2zAGRLXjMDljs6cg0vF+A== X-Received: by 2002:a17:902:c5:: with SMTP id a63mr5452013pla.267.1549997708301; Tue, 12 Feb 2019 10:55:08 -0800 (PST) Received: from localhost.localdomain ([43.224.245.179]) by smtp.gmail.com with ESMTPSA id q187sm601003pfb.128.2019.02.12.10.55.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 12 Feb 2019 10:55:07 -0800 (PST) From: Xiang Xiao X-Google-Original-From: Xiang Xiao To: gregkh@linuxfoundation.org, alexander.shishkin@linux.intel.com, andriy.shevchenko@linux.intel.com, linux-kernel@vger.kernel.org Cc: Xiang Xiao Subject: [v2] lib/string: add memrchr function Date: Wed, 13 Feb 2019 02:54:43 +0800 Message-Id: <1549997683-27986-1-git-send-email-xiaoxiang@xiaomi.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1549994809-27479-1-git-send-email-xiaoxiang@xiaomi.com> References: <1549994809-27479-1-git-send-email-xiaoxiang@xiaomi.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Here is the detailed description for memrchr: void *memrchr(const void *s, int c, size_t n); The memrchr() function is like the memchr() function, except that it searches backward from the end of the n bytes pointed to by s instead of forward from the beginning. The memrchr() functions return a pointer to the matching byte or NULL if the character does not occur in the given memory area. Signed-off-by: Xiang Xiao --- include/linux/string.h | 1 + lib/string.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index 7927b87..f380f4b 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -167,6 +167,7 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt) memcpy(dst, src, cnt); } #endif +void *memrchr(const void *s, int c, size_t n); void *memchr_inv(const void *s, int c, size_t n); char *strreplace(char *s, char old, char new); diff --git a/lib/string.c b/lib/string.c index 38e4ca0..92914f6 100644 --- a/lib/string.c +++ b/lib/string.c @@ -964,6 +964,27 @@ void *memchr(const void *s, int c, size_t n) EXPORT_SYMBOL(memchr); #endif +/** + * memrchr - Find a character in an area of memory. + * @s: The memory area + * @c: The byte to search for + * @n: The size of the area. + * + * returns the address of the last occurrence of @c, or %NULL + * if @c is not found + */ +void *memrchr(const void *s, int c, size_t n) +{ + const unsigned char *p = s + n; + + while (n-- != 0) { + if ((unsigned char)c == *--p) + return (void *)p; + } + return NULL; +} +EXPORT_SYMBOL(memrchr); + static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes) { while (bytes) { -- 2.7.4