From patchwork Mon Apr 15 19:14:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13630494 Received: from mail-oo1-f42.google.com (mail-oo1-f42.google.com [209.85.161.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9B93D15575F for ; Mon, 15 Apr 2024 19:14:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713208498; cv=none; b=kKj0ZBKQ8QcfiaorczAjnK/xK0qtga1HmZsTC3aqB46FOecrlcqP6kE1MguwrAK4msDk8QFnXoVpbCrvOw/0QT+amX8zQ+856jAZeREf8jzqtP+rGDy7ghYhcZ/Jwkc6PBt9fs67ncVynP16pCJq4Qb2kBqIGWM4MmCB+gIfhYU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713208498; c=relaxed/simple; bh=Zg7Hu1KhKlj1KupyiJ94O57KVPpipYFIPIf5MgSTY94=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=RYc1SYS6mK4DF4WCyCzTdzyr1KdXLbZ3jfyk60oCfvYMMiOYGz+F2zr6yWMkxYztwSLxrqg7zKW41J8wVB+vzvhJP3+2ZFfOAlbyPQ/ykhQIkpCVy4dQq7YEq3oqrDOVHv7W864FzG9ipFNwkmIQUeWK70uhxF5/bg9bhHPiEhg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IHaSqIfU; arc=none smtp.client-ip=209.85.161.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IHaSqIfU" Received: by mail-oo1-f42.google.com with SMTP id 006d021491bc7-5a522ae3747so1601033eaf.1 for ; Mon, 15 Apr 2024 12:14:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713208496; x=1713813296; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=zdJvczNucGwJm49momo3YBbWLY1Ibf9EQ5Z5pwzZsIw=; b=IHaSqIfUhA8AnwomuKGRrGpsDA+SxWP0Qae9r2HmpB85Y/3OfFwB1JHGgSwfSIcqPb LjcCvFnDC9Y/smOw3IbUV1b9AEXkJ0WwyRtpCQO3FyEMDXptvg9Js2fCJlCq923vbCXp O2P5ZkwK0HXDunKUraFUTcuOGR98LuX5Gxm2qDrW1RiEHqwjJ5JFG+WKO7deA3BB139G JJqqMPmBsyqJBlKKa6u8lQS2624wPLU5t+SmceGntRQkthyZgM50Ik4ALVbjIvwg2nz8 4Ovi2M7dGFlV9fxc/lupEhk3vnBpKKn5oQRi00LD7cqZ2RTDI8mNBkQfcMt7QrGe3iDc gsPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713208496; x=1713813296; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zdJvczNucGwJm49momo3YBbWLY1Ibf9EQ5Z5pwzZsIw=; b=UF6ImmyG2Qu4XokwjGE11vqrX0ZQFX9+B0EhdBosSgVi1S3p1vzdHstkaA6C7CbawV VVHtnBRD8RmsBR0iGqkEPLmznI2DojBbg/Wb0OK9DjborGVRfHgaH3Iuo+UUqbK2mOqK yZ4aJd41Iny5SRjEozqIFBhrIXrwf5aGh/fcrpaOvblryeQp+u5TQxpBgVks18NJBYQ7 MZGBrY79c0EoyCajkk1tiU53WWIdOKV7/tVIVA2F8woyGKHzvf/jxcoegcK19sx3NIXY ZAGBWRxkl/DA92EnwSkHRdpmN5qD7GHoKX6aNkuXrBNSH4+9rmCBbHFKuETlbLda5v98 Gb6g== X-Gm-Message-State: AOJu0Yw3GVW8wrzdPTps0vDG968E3CgfiSkZXiUuGYN+byr3mzjaXYVI 5d3I4HzU+6dVKXBuWLMZAsH7a0QNxwF6fDEiQAZI1YpOG8/9s65ZepVTzQ== X-Google-Smtp-Source: AGHT+IExmQdSQ2RaE1SAFey1EXWF0PTGAeHwfyuFsbv3qB1YOh7uFiLDsh2d6JNjc5LYmVGq4NM7dA== X-Received: by 2002:a4a:ad4a:0:b0:5ac:6891:ce56 with SMTP id s10-20020a4aad4a000000b005ac6891ce56mr206566oon.2.1713208496621; Mon, 15 Apr 2024 12:14:56 -0700 (PDT) Received: from localhost.localdomain ([70.114.247.242]) by smtp.gmail.com with ESMTPSA id w8-20020a4ae9e8000000b005a586b0906esm2206526ooc.26.2024.04.15.12.14.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Apr 2024 12:14:56 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 1/4] util: Remove confusing static keyword use Date: Mon, 15 Apr 2024 14:14:33 -0500 Message-ID: <20240415191454.3259243-1-denkenz@gmail.com> X-Mailer: git-send-email 2.44.0 Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 '__n' is declared as static const which is confusing since it would imply that __n is only initialized once. This works out in the end since the macro used in a separate anonymous block, but some compilers (i.e. clang) will generate a symbol table entry for '__n'. This is not needed since the expression is always constant. Declare '__n' as a const size_t instead since that is the type produced by the sizeof() operation used by L_ARRAY_SIZE. While here, also declare '__i' as size_t to match '__n'. Fixes: 4a9f6388dd7f ("util: Add L_IN_SET macros") --- ell/util.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ell/util.h b/ell/util.h index 80b29b7bb6e3..b5d96754e8ae 100644 --- a/ell/util.h +++ b/ell/util.h @@ -349,8 +349,8 @@ inline __attribute__((always_inline)) void _l_close_cleanup(void *p) #define _L_IN_SET_CMP(val, type, cmp, ...) __extension__ ({ \ const type __v = (val); \ const typeof(__v) __elems[] = {__VA_ARGS__}; \ - unsigned int __i; \ - static const unsigned int __n = L_ARRAY_SIZE(__elems); \ + size_t __i; \ + size_t __n = L_ARRAY_SIZE(__elems); \ bool __r = false; \ for (__i = 0; __i < __n && !__r; __i++) \ __r = (cmp); \ From patchwork Mon Apr 15 19:14:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13630495 Received: from mail-oo1-f54.google.com (mail-oo1-f54.google.com [209.85.161.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C140219EA for ; Mon, 15 Apr 2024 19:14:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713208499; cv=none; b=nqijw67IrX1QjFY7d9W5nNZzdDQ2nsbQx3nVAmW0gsJ9xDbEtzz4UpggKppWVLiFWQXMZ54j5Sm/SnviYn2OzXZK+q5VnoqfcXPv4mN9IBmDG9r4gWrkwsd8aMqIMI9nZjgE/epofFM154Q0eU6RFV6J4Sp/BVHZsP1V/1iFQqE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713208499; c=relaxed/simple; bh=qFi+tH4pcqnIybS3Fso/M652fRG75zRQUMViz6aDYts=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZtlQzVc3NN2or3A5SjXBGtjdionDSv7ouklTjlIbvVjeaQJDXDDe6GPM2+C9y1J0FXdJe62Kr1naYUB9XLC57KI1SQfYATQz/ptKCPInijBVRsqSqVlv28sO7r73MpzXrBZBcI8JR48uzuF6gQIN7wfKbK8tNYLnvgwHR4a4GFk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=aKoJeC52; arc=none smtp.client-ip=209.85.161.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aKoJeC52" Received: by mail-oo1-f54.google.com with SMTP id 006d021491bc7-5aa691c2349so2483587eaf.1 for ; Mon, 15 Apr 2024 12:14:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713208497; x=1713813297; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kevpRA/R60NsZ0JLRUZI+rGbjyhOfw7i6iOyAoPTBg8=; b=aKoJeC52h9znPiC8ptpN33nkyORezs1/OUrEhYMSW2IelW7ol9IgLOYA2TgXec5gfP untcTnR9escTpb6kFsxfsdHgNVfPX5aSrJijTjIQs/XRMnIzQquZlTENy0SX5yiMw7Mk Fdig07qchUmYX6fAfILuYSCxgOaRc9q7ZUxTso1KqnGBhaIykwWG9hk7igUf9qvdYdsI kvgXGroeC0aYqBY21tLFfp2O3+uQKvC8Qa04o3Yiv5LpDKxhUa0EVRYH+kC3c7qtYtMU 5IHqoa4oZXiPZaCHyM+Ju3xbimyVnGEBkhpF25lXJjn6H1SNxn5GxKubONlrGk2jx6Cp ku9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713208497; x=1713813297; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kevpRA/R60NsZ0JLRUZI+rGbjyhOfw7i6iOyAoPTBg8=; b=r68FOSeT4IBYGjBvrFtJN4GHq2xy1i8QTk4ltIsp3PSpcsdQDEcdDdSFgztFXAX72t XQZZABqxzDGPi9LJsy4f4VNpVFigwdanU9/zZ6y8KlMXciRcC37DCQ2Um3fa3yIVMG3O EFqO+FsHb83ICxq6KFg7OHNO7N7VbFb7VQne63Rv7P4psoQnrdHWSTRjfkzYeh4lSWKi CgZ21xDGcnrIJaQ9qxHsD4/rGzNahkXk2X35NmvsrjvpN1sIh9GmJKqxeCO67BPihlhj hiiBo9XH82+vs09Ocn6tsIXHWR45Wo3GvOmhyLM3KvTsnqFcOS8OrJ9Py+PbpiaTlI+8 TIsQ== X-Gm-Message-State: AOJu0Ywqa7SMTRJMgfQOTuzCFELsDsx9BcudQi+I8BjuuBmVzDF+ec8G 0j+3M7BiEGVX1rNioQCn7oT7Gyz0HvRWqcfvcH6lQUkevLfJDwtiNXRhSQ== X-Google-Smtp-Source: AGHT+IHuA69rZCiwmTzDwulBJiEm+I2S0FUB56H90DBFBQIdO4r0aLtmHAEByZHmSyVA1sYD2SRF8g== X-Received: by 2002:a05:6820:209:b0:5aa:5206:30aa with SMTP id bw9-20020a056820020900b005aa520630aamr11924916oob.7.1713208497194; Mon, 15 Apr 2024 12:14:57 -0700 (PDT) Received: from localhost.localdomain ([70.114.247.242]) by smtp.gmail.com with ESMTPSA id w8-20020a4ae9e8000000b005a586b0906esm2206526ooc.26.2024.04.15.12.14.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Apr 2024 12:14:57 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 2/4] util: Add L_BIT_{SET|CLEAR|TEST} macros Date: Mon, 15 Apr 2024 14:14:34 -0500 Message-ID: <20240415191454.3259243-2-denkenz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240415191454.3259243-1-denkenz@gmail.com> References: <20240415191454.3259243-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 These macros support arbitrarily sized bitmaps using any integer data type. It is thus possible to write code like this: uint64_t bitmap = 0; uint8_t array[4] = {}; bool r; L_BIT_SET(&bitmap, 63); r = L_BIT_TEST(&bitmap, 0); L_BIT_SET(array, 25); r = L_BIT_TEST(array, 31); --- ell/util.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ell/util.h b/ell/util.h index b5d96754e8ae..53a932c771e6 100644 --- a/ell/util.h +++ b/ell/util.h @@ -366,6 +366,24 @@ inline __attribute__((always_inline)) void _l_close_cleanup(void *p) (__v && __elems[__i] && \ !strcmp(__v, __elems[__i])), ##__VA_ARGS__) +#define _L_BIT_TO_MASK(bits, nr) __extension__ ({ \ + const unsigned int shift = (nr) % (sizeof(*(bits)) * 8); \ + const typeof(*(bits)) mask = (typeof(*(bits)))1 << shift; \ + mask; \ +}) + +#define _L_BIT_TO_OFFSET(bits, nr) \ + (*((bits) + ((nr) / (sizeof(*(bits)) * 8)))) \ + +#define L_BIT_SET(bits, nr) \ + _L_BIT_TO_OFFSET(bits, nr) |= _L_BIT_TO_MASK(bits, nr) + +#define L_BIT_CLEAR(bits, nr) \ + _L_BIT_TO_OFFSET(bits, nr) &= ~_L_BIT_TO_MASK(bits, nr) + +#define L_BIT_TEST(bits, nr) \ + ((~_L_BIT_TO_OFFSET(bits, nr) & _L_BIT_TO_MASK(bits, nr)) == 0) + /* * Taken from https://github.com/chmike/cst_time_memcmp, adding a volatile to * ensure the compiler does not try to optimize the constant time behavior. From patchwork Mon Apr 15 19:14:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13630496 Received: from mail-ot1-f50.google.com (mail-ot1-f50.google.com [209.85.210.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE0D7155A27 for ; Mon, 15 Apr 2024 19:14:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713208500; cv=none; b=X7tS6E9TzMK6XXKe+DhEWzJJ9cDcrumcWum+bNZEQbJtOMxzYkttR6ujJLgTGkfcuPq1BZOoOu/Qvw+zUqz8pjGv+o3Q71brkkTwlJiB9mxQvvUY8R34oqeb6rpUanT5h69N3chQ+TUaGletM2KFzrXRHgz/KYvNJPU0OY2NyCg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713208500; c=relaxed/simple; bh=etGFJfjhqJnaN2K4N4QHrKF9ivp1CxuuGpLRqITRLcw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GRrqBF9eQ9W+epFH5XcLiglqFa9jW0hJSyfxUW/X3AiveGrVws/WTOUA0kKTFgr/lnHLWx9TMg8olptVrLVW48hrQ2fZ0p1mO5PE4o7Jdg/O9cH9YOmjgVzjeeCIJ7fJTTBfM32pPAx9qzqPBzmZIidy6LSBgqlUE4Zend6pyCM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=L+WF9ZIu; arc=none smtp.client-ip=209.85.210.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="L+WF9ZIu" Received: by mail-ot1-f50.google.com with SMTP id 46e09a7af769-6ea156bee62so1474613a34.0 for ; Mon, 15 Apr 2024 12:14:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713208498; x=1713813298; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=M11g0U3IY03yMnZK0FrhJjZCJ5lipYhsVqDVgp5+Bxo=; b=L+WF9ZIu8oBzLQz52M3vo4mRRlVYOhSF+146TgKwcVKMa/jX0zJQnmH29WTknK9g4y 8jPLBCRhXkmzhWu9vOcL41LnWutFNYTOlUeC2QtakM9G6nlQvVkCKvyfxivA3SF/Bm/M xQoJfwdxNlBIeZS4PTmc+rRO0sBvXqFJfqAtkrCO5QIPKCpDOhf6Z6WxdfbFYRYTKiNP uVq3VFFqNKhlH0BrqkjpZw9MT8kpDPScbK4MZRFY2B2jHI4IiHDnTDJNcaN0aXuKJNI0 UXR0g3yK/WbX6R6kARWczhD62fy4kjMEbRu6wVNmyO/bQVdoB9AglYqgOxHVxBrbTQOm p8/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713208498; x=1713813298; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M11g0U3IY03yMnZK0FrhJjZCJ5lipYhsVqDVgp5+Bxo=; b=oMOteLzdOcP1+y+fSaAtXkKH5NoMGggT9a6QuYImsSTnIAsYKEzFhSH0ZWM6WVnHp4 5laFihDggv1chGtthPqTdxiW6XUM6H4MMhDh1eT+wDB2cHehWrjsMCajieoCSJNe6396 roIRmzkaQ3OAyw/zqBu9Ay9JoN6iZkbuaCE/vv/WSwZDRuzI0oPj2zDyncOKVnBhRwId 6C86ob1JZh5ohBwJI/V2XE+7SBD+yGuu2EHd633cI5qcvn5ZRSGAd7A69JNDogWSDlDn BzIyxZikicMeFS0gPul4lDfTGuhi0UMvY20wGZ+mjGkKZx0+rVVLHmgmDIuujyxht6YM iUgQ== X-Gm-Message-State: AOJu0YwcNxFXtUNC+U9p+yIn3bozredGHsNnLyfHE0TcePXtkz1aWmOv cTx9C0j+awwFszUgO2mWOOB2QumCkP1TgTRtjE/sNyKY/+TW5Z4ZX01boQ== X-Google-Smtp-Source: AGHT+IEEljh5yKb4SlQkI6ZcW7q8hGvftTwq/tENdTpEepNdgvs/qwBm8iukhBGqFOkIsrdYexa2lw== X-Received: by 2002:a05:6870:6e0e:b0:22e:bd8d:c04d with SMTP id qt14-20020a0568706e0e00b0022ebd8dc04dmr180175oab.14.1713208497812; Mon, 15 Apr 2024 12:14:57 -0700 (PDT) Received: from localhost.localdomain ([70.114.247.242]) by smtp.gmail.com with ESMTPSA id w8-20020a4ae9e8000000b005a586b0906esm2206526ooc.26.2024.04.15.12.14.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Apr 2024 12:14:57 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 3/4] util: Add L_BITS_{SET|CLEAR} Date: Mon, 15 Apr 2024 14:14:35 -0500 Message-ID: <20240415191454.3259243-3-denkenz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240415191454.3259243-1-denkenz@gmail.com> References: <20240415191454.3259243-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 These macros can set or clear an arbitrary number of bits in a single call, as follows: uint64_t bitmap = 0; L_BITS_CLEAR(&bitmap, 0, 1, 5, 15, 32, 63); --- ell/util.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ell/util.h b/ell/util.h index 53a932c771e6..1d14ffb1570d 100644 --- a/ell/util.h +++ b/ell/util.h @@ -384,6 +384,20 @@ inline __attribute__((always_inline)) void _l_close_cleanup(void *p) #define L_BIT_TEST(bits, nr) \ ((~_L_BIT_TO_OFFSET(bits, nr) & _L_BIT_TO_MASK(bits, nr)) == 0) +#define L_BITS_SET(bits, ...) __extension__ ({ \ + const unsigned int __elems[] = {__VA_ARGS__}; \ + size_t __i; \ + for (__i = 0; __i < L_ARRAY_SIZE(__elems); __i++) \ + L_BIT_SET(bits, __elems[__i]); \ +}) + +#define L_BITS_CLEAR(bits, ...) __extension__ ({ \ + const unsigned int __elems[] = {__VA_ARGS__}; \ + size_t __i; \ + for (__i = 0; __i < L_ARRAY_SIZE(__elems); __i++) \ + L_BIT_CLEAR(bits, __elems[__i]); \ +}) + /* * Taken from https://github.com/chmike/cst_time_memcmp, adding a volatile to * ensure the compiler does not try to optimize the constant time behavior. From patchwork Mon Apr 15 19:14:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13630497 Received: from mail-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A0C1C155A56 for ; Mon, 15 Apr 2024 19:14:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713208501; cv=none; b=BHOtAllR4UcXiUzbC2rmMtO/hji/AblD8zfR+9FAXR0yBbiw3LJ/t62UuTjzTZdJoKeYFQqitHU3CBXSvc9K1DxcWykc6b25pvfcYPtdqDMt/l0W6/ybi+oLBmonu5JwV9DDd8Bc+QNI1Ydr7bHFDawwJn2PggPGRlDteDC7qXI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713208501; c=relaxed/simple; bh=BedpNYth8Yhlu6kw2fK5DRRsEaW6up9cmeBc+vkds2M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g/Is0T2xexPll67lVW5KuzqidfKS7durzTx5iPceDp4hcVkEPH9233o5oPTXezo8NvcnjzhM1TwOvndDW1E0oEj/355vO6LxXMxu1YFyje0fUSOVsvm0wgau9y9h5MB+xGoQ/LMmEGd3X6MAbELhzW5TcXK4ir32b2RAgt1drm8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=TSNJK9a8; arc=none smtp.client-ip=209.85.210.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TSNJK9a8" Received: by mail-ot1-f41.google.com with SMTP id 46e09a7af769-6ea156bee62so1474620a34.0 for ; Mon, 15 Apr 2024 12:14:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713208498; x=1713813298; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hcXbxCazyvp8pYGsZSf1RhXr8bFq70dKEKToGtuxJ6o=; b=TSNJK9a8VOeFJbXhhjFcz39FpoE31oUP3l6dMy2CMCqFYf3m9gtMa34f9yJjQ6+xDu M4DdMgr9GWRsj5GRrkGsc8qclkBbp+3xKG7UsulbVw4ArVbGdeRr7fcKeL2panQejItO p/mX9wg8AfAKkNaOyRTgz1B7+sCpIG7QNlGLgheGb1Cl/+Q1Q02zvVOBMvBz8/yng93Z DcUcWiBgwKB145/zfRzjsTC6h0JnNY3MiL6FlrweY4YKFs4nZbwwzEt988Xy4D9Votdj ua1AVMWxYNsVHXHxrDlWaNf5F71v4oxyneklKi6ZcFQ2ZQla22e7q4YMTRfu0i4gc5bS Y8aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713208498; x=1713813298; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hcXbxCazyvp8pYGsZSf1RhXr8bFq70dKEKToGtuxJ6o=; b=fFP93GRVshiyhw3ppme460sHk9HcxrrpIatmb6XHS8p91TZGyMQlsvvkrROlXpJMqZ 7W0NcmPTbUz+SjtX2UivbnztcPWAW5VvQ6cvopU2oX1pYcQhJUOik8INXjFEyj9T0OHU r8jdd3EiaPh+UrYtJzBEUQ82g1SeC0SfSS1A1W4FyiJrIqMakcCyuVk/R7zyDAEm43wY WtxvNqU5+BSyJuKxW6X7WZFLsfwWaHcBXLX6xEsoNXeUkW5aAtRnVrxIEevJTYk5+jVk 7R+u4fHfZt/cwUbZutmZwfDo0hDKrAgvEEPXZkoMUXNQFwVx5On140m9mgEywIxCmAdN xpoA== X-Gm-Message-State: AOJu0YwAJo6d3nLc2np26ErY3rXklytrj3ocUi2fNN2IaFe6groCokBa mQo/DMbnR4HGpLC/eSCcQO1RO6o7cCBzyGRFAonsgx70E3lCSL0jMC9uig== X-Google-Smtp-Source: AGHT+IEqVSJyZ06C9pfC7/+wDS2205gtONQC+mpw0L+y4HSGRuLJxgvg3Spomul7uCLEtYPZxWQU1w== X-Received: by 2002:a05:6870:d8d1:b0:22a:7d7:1007 with SMTP id of17-20020a056870d8d100b0022a07d71007mr222325oac.5.1713208498494; Mon, 15 Apr 2024 12:14:58 -0700 (PDT) Received: from localhost.localdomain ([70.114.247.242]) by smtp.gmail.com with ESMTPSA id w8-20020a4ae9e8000000b005a586b0906esm2206526ooc.26.2024.04.15.12.14.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Apr 2024 12:14:58 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 4/4] unit: Add tests for the new bitmap utilities Date: Mon, 15 Apr 2024 14:14:36 -0500 Message-ID: <20240415191454.3259243-4-denkenz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240415191454.3259243-1-denkenz@gmail.com> References: <20240415191454.3259243-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 --- unit/test-util.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/unit/test-util.c b/unit/test-util.c index f7a895f911d5..14724f2a608b 100644 --- a/unit/test-util.c +++ b/unit/test-util.c @@ -195,6 +195,81 @@ static void test_safe_atoux(const void *test_data) assert(l_safe_atox8("0xffff", &c) == -ERANGE); } +static void test_set_bit(const void *test_data) +{ + unsigned int bitmap[2] = { }; + int one = 0; + + L_BIT_SET(&bitmap[0], 0); + L_BIT_SET(bitmap, 1); + L_BIT_SET(bitmap, 2); + L_BIT_SET(bitmap, 3); + + assert(bitmap[0] == 0x0f); + assert(bitmap[1] == 0); + + L_BIT_SET(bitmap, 63); + L_BIT_SET(bitmap, 62); + L_BIT_SET(bitmap, 61); + L_BIT_SET(bitmap, 60); + + assert(bitmap[0] == 0x0fU); + assert(bitmap[1] == 0xf0000000U); + + L_BIT_SET(&one, 0); + assert(one == 1); +} + +static void test_clear_bit(const void *test_data) +{ + unsigned int bitmap[2] = { 0xfU, 0xf0000000U }; + + L_BIT_CLEAR(&bitmap[0], 3); + L_BIT_CLEAR(bitmap, 63); + + assert(bitmap[0] == 0x07U); + assert(bitmap[1] == 0x70000000U); +} + +static void test_is_bit_set(const void *test_data) +{ + unsigned int bitmap[2] = { 0xfU, 0xf0000000U }; + uint8_t one = 1; + + assert(L_BIT_TEST(&bitmap[0], 0) == true); + assert(L_BIT_TEST(bitmap, 1) == true); + assert(L_BIT_TEST(bitmap, 2) == true); + assert(L_BIT_TEST(bitmap, 3) == true); + assert(L_BIT_TEST(bitmap, 4) == false); + + assert(L_BIT_TEST(bitmap, 63) == true); + assert(L_BIT_TEST(bitmap, 55) == false); + + assert(L_BIT_TEST(&one, 0) == true); + assert(L_BIT_TEST(&one, 1) == false); +} + +static void test_set_bits(const void *test_data) +{ + uint16_t bitmap[4] = {}; + + L_BITS_SET(bitmap, 0, 1, 16, 32, 48); + + assert(bitmap[0] == 0x3); + assert(bitmap[1] == 0x1); + assert(bitmap[2] == 0x1); + assert(bitmap[3] == 0x1); +} + +static void test_clear_bits(const void *test_data) +{ + uint16_t bitmap[4] = { 0x3, 0x1, 0x1, 0x1 }; + + L_BITS_CLEAR(bitmap, 0, 1, 16, 32, 48); + + assert(l_memeqzero(bitmap, sizeof(bitmap))); +} + int main(int argc, char *argv[]) { l_test_init(&argc, &argv); @@ -212,5 +287,11 @@ int main(int argc, char *argv[]) l_test_add("l_safe_atoux", test_safe_atoux, NULL); + l_test_add("L_BIT_SET", test_set_bit, NULL); + l_test_add("L_BIT_CLEAR", test_clear_bit, NULL); + l_test_add("L_BIT_TEST", test_is_bit_set, NULL); + l_test_add("L_BITS_SET", test_set_bits, NULL); + l_test_add("L_BITS_CLEAR", test_clear_bits, NULL); + return l_test_run(); }