From patchwork Fri Aug 14 03:02:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Blain via GitGitGadget X-Patchwork-Id: 11713483 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1DE6F109B for ; Fri, 14 Aug 2020 03:02:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F401D2078B for ; Fri, 14 Aug 2020 03:02:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AKCGk0Ng" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726593AbgHNDCU (ORCPT ); Thu, 13 Aug 2020 23:02:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726567AbgHNDCT (ORCPT ); Thu, 13 Aug 2020 23:02:19 -0400 Received: from mail-ej1-x641.google.com (mail-ej1-x641.google.com [IPv6:2a00:1450:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43E42C061757 for ; Thu, 13 Aug 2020 20:02:19 -0700 (PDT) Received: by mail-ej1-x641.google.com with SMTP id jp10so8428742ejb.0 for ; Thu, 13 Aug 2020 20:02:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=uIgFIt8Ho9VBTnemjPpMjOLiOzEVlYlY0UxocbEAnyo=; b=AKCGk0NgyINFFLVLGDxJKNDxOWLDR2FQQoVd9T5TAe2z2Qlu/o/lIC1TquVkcLlvAA TTXuDD2kTsYy0sE4gKDAdy7z+L4VvZM3vuREu8H+fdualNgN9EvJjMivtSLoL4BWE1n7 lMRnULPvtJuZAvVq4h9GnsA5juzJsnenulcgdF7+vAkwCtdHfXH+JQ1I8jW4sH9u22Zq i6vt5g80VO/JIvHrFK15NXrFnNQfkq2hCDG2w9e3H33lSN7QH8MmPx6pL3Ixwg2+t4eg UQO70F8fxtgO0Bvgh+DAokourwTsGmxLOdACnSZqfHhLtYedB0WBl9bYEnM0eiOHagNC wK4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=uIgFIt8Ho9VBTnemjPpMjOLiOzEVlYlY0UxocbEAnyo=; b=NSVqsGsDM07cSgY6JSQb+FYTIlOwE/IObu3qCBHH3YwUr+eQX3Myemq1BphfewSCQa 4N6/23BKVOIo2x/LKVmyKKNU1wZxSpJyKWaaAGpHBDmbtCe6MhA+ls35UtPnuL/jwyKe 7IRHH7VaJ/4+POVId/WDWRwNysHGb4GPSbekE7RHhJOTmwUhAJQgbWKO4XsSBN7Fyhep XcdcA7VZ1YHs6UmOw0Xw0dZHJ+fpHOHIprB8QfAMfNJJ08r8g77J7Qxv1KSUOH/9LhFH DHC0U03eJBOqYrMQhTdDJa3l/3wXxrrsnBKRzAeLWD+EqlEI5Fd/u5PKgFpI8FB58Ucq uSVw== X-Gm-Message-State: AOAM533y7PQOmEk+ZIKJP0CthR+tDYMgGcYCcVpYjMSlxuM6ed9+jQM/ iZpfBDF+ioDDsDTf3nPrgdINidCm X-Google-Smtp-Source: ABdhPJySBPRItU0GWL8V/a0lYoCPqTyUSi/Vhx5gKj37FmOdBVca901GaJnkZnmS9QRxRvmYHjRBaQ== X-Received: by 2002:a17:906:7698:: with SMTP id o24mr524116ejm.182.1597374137797; Thu, 13 Aug 2020 20:02:17 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id a16sm5634782ejy.78.2020.08.13.20.02.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:02:17 -0700 (PDT) Message-Id: In-Reply-To: References: From: "Elijah Newren via GitGitGadget" Date: Fri, 14 Aug 2020 03:02:13 +0000 Subject: [PATCH 1/3] mem-pool: add convenience functions for xstrdup and xstrndup Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Matheus Tavares , Elijah Newren , Elijah Newren Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren fast-import had a special mem_pool_xstrdup() convenience function that I want to be able to use from the new merge algorithm I am writing. Move it from fast-import to mem-pool, and also add a mem_pool_xstrndup() while at it that I also want to use. Signed-off-by: Elijah Newren --- fast-import.c | 12 ++---------- mem-pool.c | 23 +++++++++++++++++++++++ mem-pool.h | 6 ++++++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/fast-import.c b/fast-import.c index ce47794db6..dd5b563950 100644 --- a/fast-import.c +++ b/fast-import.c @@ -526,14 +526,6 @@ static unsigned int hc_str(const char *s, size_t len) return r; } -static char *pool_strdup(const char *s) -{ - size_t len = strlen(s) + 1; - char *r = mem_pool_alloc(&fi_mem_pool, len); - memcpy(r, s, len); - return r; -} - static void insert_mark(struct mark_set *s, uintmax_t idnum, struct object_entry *oe) { while ((idnum >> s->shift) >= 1024) { @@ -615,7 +607,7 @@ static struct branch *new_branch(const char *name) die("Branch name doesn't conform to GIT standards: %s", name); b = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct branch)); - b->name = pool_strdup(name); + b->name = mem_pool_xstrdup(&fi_mem_pool, name); b->table_next_branch = branch_table[hc]; b->branch_tree.versions[0].mode = S_IFDIR; b->branch_tree.versions[1].mode = S_IFDIR; @@ -2806,7 +2798,7 @@ static void parse_new_tag(const char *arg) t = mem_pool_alloc(&fi_mem_pool, sizeof(struct tag)); memset(t, 0, sizeof(struct tag)); - t->name = pool_strdup(arg); + t->name = mem_pool_xstrdup(&fi_mem_pool, arg); if (last_tag) last_tag->next_tag = t; else diff --git a/mem-pool.c b/mem-pool.c index a2841a4a9a..3a8c54d9df 100644 --- a/mem-pool.c +++ b/mem-pool.c @@ -102,6 +102,29 @@ void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size) return r; } +char *mem_pool_xstrdup(struct mem_pool *pool, const char *str) +{ + size_t len = strlen(str) + 1; + char *ret = mem_pool_alloc(pool, len); + + if (!ret) + die("Out of memory, mem_pool_xstrdup failed"); + + return memcpy(ret, str, len); +} + +char *mem_pool_xstrndup(struct mem_pool *pool, const char *str, size_t len) +{ + size_t minlen = strnlen(str, len); + char *ret = mem_pool_alloc(pool, minlen+1); + + if (!ret) + die("Out of memory, mem_pool_xstrndup failed"); + + ret[minlen] = '\0'; + return memcpy(ret, str, minlen); +} + int mem_pool_contains(struct mem_pool *mem_pool, void *mem) { struct mp_block *p; diff --git a/mem-pool.h b/mem-pool.h index 999d3c3a52..fcaa2d462b 100644 --- a/mem-pool.h +++ b/mem-pool.h @@ -41,6 +41,12 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len); */ void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size); +/* + * Allocate memory from the memory pool and copy str into it. + */ +char *mem_pool_xstrdup(struct mem_pool *pool, const char *str); +char *mem_pool_xstrndup(struct mem_pool *pool, const char *str, size_t len); + /* * Move the memory associated with the 'src' pool to the 'dst' pool. The 'src' * pool will be empty and not contain any memory. It still needs to be free'd From patchwork Fri Aug 14 03:02:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Blain via GitGitGadget X-Patchwork-Id: 11713487 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B70F8109B for ; Fri, 14 Aug 2020 03:02:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 99F2C2078B for ; Fri, 14 Aug 2020 03:02:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EDf8Nryy" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726621AbgHNDCV (ORCPT ); Thu, 13 Aug 2020 23:02:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726567AbgHNDCU (ORCPT ); Thu, 13 Aug 2020 23:02:20 -0400 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30814C061383 for ; Thu, 13 Aug 2020 20:02:20 -0700 (PDT) Received: by mail-ej1-x62c.google.com with SMTP id m22so8375010eje.10 for ; Thu, 13 Aug 2020 20:02:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=iYrgsQPCqRRvg41wuz0WKoA7INv92D0AhW5p1p69vHQ=; b=EDf8NryynNjHbJN/tHLe5jEleTbhJMU6epHyUypc0MfPfRMVToIbieI9drawDnfyWB offeIinGy25MkOuOz3BNRXRxm1oYApgIICx0jKwf493NDNtuVeGBJnkSmey9zoutQ398 OW55bMmpNRukoZW0/UqS+NifPZ4lt9e1X90Kl5fmhYafvUuDHd4pMJOrAYqQxzoGymHh BCztLAMRwUIIoW4CnmU9FtUyDPG9x7S60tdMMDYTPXHfL4QsSTRaiZ27tyZsSPZ/4QjR GrTQdGSWZwPnGItxqhgBizDhBOzqL/ucewgDlfCiJYEJj0CJuJVskgLCmBfsuaDKK53e Tl5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=iYrgsQPCqRRvg41wuz0WKoA7INv92D0AhW5p1p69vHQ=; b=FY67KnvDg1vfuysqoNEvGbS5SFAJ2DLaJBN7bpKLJF2WrCvkguSOuVsNXXGBOl/Seb sJlDD6HZa6BXLTAQFhfqAlkaGaYS6a3Hzz/SPO+fufaEyJiuY9X53GFjI06zUJFDpP2y zphsmXXplPd106fa5mnoy/TeojPiXpeouXWCCVcq5QjrCe1jXb1eeERU42Prjxq+3FMl DEt1SvCkuZ6e+2PhHfj7SwTvpnowTKojeDZAkycN5pQUtbCaubpzD/YKROImywvME9tP xWt0loM2ho0fZ412a2JNDFfjRyPNTCFtHGmriZdmI2HLPRTisoNpwjgDPCr0IPVf22Wt p+Ow== X-Gm-Message-State: AOAM532cd/HXbMRaKzZykOzB4DjMymkK8+UTcZy29hCFcjGCeLRCOul9 GyjV4KYteX2BMl1i6e+ZFc0E+Xr8 X-Google-Smtp-Source: ABdhPJzbkXtDajYcwtNjV0zIqHNqu16GC0s/Nw1YH0zlhLAbuJbw/nyTIo6rSxCU/ieZOKYGtlq3/g== X-Received: by 2002:a17:906:1ccd:: with SMTP id i13mr497540ejh.424.1597374138623; Thu, 13 Aug 2020 20:02:18 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id ar2sm2692044ejc.61.2020.08.13.20.02.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:02:18 -0700 (PDT) Message-Id: In-Reply-To: References: From: "Elijah Newren via GitGitGadget" Date: Fri, 14 Aug 2020 03:02:14 +0000 Subject: [PATCH 2/3] mem-pool: use more standard initialization and finalization Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Matheus Tavares , Elijah Newren , Elijah Newren Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren A typical memory type, such as strbuf, hashmap, or string_list can be stored on the stack or embedded within another structure. mem_pool cannot be, because of how mem_pool_init() and mem_pool_discard() are written. mem_pool_init() does essentially the following (simplified for purposes of explanation here): void mem_pool_init(struct mem_pool **pool...) { *pool = xcalloc(1, sizeof(*pool)); It seems weird to require that mem_pools can only be accessed through a pointer. It also seems slightly dangerous: unlike strbuf_release() or strbuf_reset() or string_list_clear(), all of which put the data structure into a state where it can be re-used after the call, mem_pool_discard(pool) will leave pool pointing at free'd memory. read-cache (and split-index) are the only current users of mem_pools, and they haven't fallen into a use-after-free mistake here, but it seems likely to be problematic for future users especially since several of the current callers of mem_pool_init() will only call it when the mem_pool* is not already allocated (i.e. is NULL). This type of mechanism also prevents finding synchronization points where one can free existing memory and then resume more operations. It would be natural at such points to run something like mem_pool_discard(pool...); and, if necessary, mem_pool_init(&pool...); and then carry on continuing to use the pool. However, this fails badly if several objects had a copy of the value of pool from before these commands; in such a case, those objects won't get the updated value of pool that mem_pool_init() overwrites pool with and they'll all instead be reading and writing from free'd memory. Modify mem_pool_init()/mem_pool_discard() to behave more like strbuf_init()/strbuf_release() or string_list_init()/string_list_clear() In particular: (1) make mem_pool_init() just take a mem_pool* and have it only worry about allocating struct mp_blocks, not the struct mem_pool itself, (2) make mem_pool_discard() free the memory that the pool was responsible for, but leave it in a state where it can be used to allocate more memory afterward (without the need to call mem_pool_init() again). Signed-off-by: Elijah Newren --- mem-pool.c | 20 +++++++------------- mem-pool.h | 4 ++-- read-cache.c | 21 +++++++++++++-------- split-index.c | 6 ++++-- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/mem-pool.c b/mem-pool.c index 3a8c54d9df..b7d789823e 100644 --- a/mem-pool.c +++ b/mem-pool.c @@ -33,21 +33,14 @@ static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t b return p; } -void mem_pool_init(struct mem_pool **mem_pool, size_t initial_size) +void mem_pool_init(struct mem_pool *mem_pool, size_t initial_size) { - struct mem_pool *pool; - - if (*mem_pool) - return; - - pool = xcalloc(1, sizeof(*pool)); - - pool->block_alloc = BLOCK_GROWTH_SIZE; + mem_pool->mp_block = NULL; + mem_pool->pool_alloc = 0; + mem_pool->block_alloc = BLOCK_GROWTH_SIZE; if (initial_size > 0) - mem_pool_alloc_block(pool, initial_size, NULL); - - *mem_pool = pool; + mem_pool_alloc_block(mem_pool, initial_size, NULL); } void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory) @@ -66,7 +59,8 @@ void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory) free(block_to_free); } - free(mem_pool); + mem_pool->mp_block = NULL; + mem_pool->pool_alloc = 0; } void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len) diff --git a/mem-pool.h b/mem-pool.h index fcaa2d462b..30b7a8c03b 100644 --- a/mem-pool.h +++ b/mem-pool.h @@ -24,10 +24,10 @@ struct mem_pool { /* * Initialize mem_pool with specified initial size. */ -void mem_pool_init(struct mem_pool **mem_pool, size_t initial_size); +void mem_pool_init(struct mem_pool *mem_pool, size_t initial_size); /* - * Discard a memory pool and free all the memory it is responsible for. + * Discard all the memory the memory pool is responsible for. */ void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory); diff --git a/read-cache.c b/read-cache.c index 8ed1c29b54..fa291cdbee 100644 --- a/read-cache.c +++ b/read-cache.c @@ -89,8 +89,10 @@ static struct mem_pool *find_mem_pool(struct index_state *istate) else pool_ptr = &istate->ce_mem_pool; - if (!*pool_ptr) - mem_pool_init(pool_ptr, 0); + if (!*pool_ptr) { + *pool_ptr = xmalloc(sizeof(**pool_ptr)); + mem_pool_init(*pool_ptr, 0); + } return *pool_ptr; } @@ -2006,11 +2008,12 @@ static unsigned long load_all_cache_entries(struct index_state *istate, { unsigned long consumed; + istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool)); if (istate->version == 4) { - mem_pool_init(&istate->ce_mem_pool, + mem_pool_init(istate->ce_mem_pool, estimate_cache_size_from_compressed(istate->cache_nr)); } else { - mem_pool_init(&istate->ce_mem_pool, + mem_pool_init(istate->ce_mem_pool, estimate_cache_size(mmap_size, istate->cache_nr)); } @@ -2070,7 +2073,8 @@ static unsigned long load_cache_entries_threaded(struct index_state *istate, con if (istate->name_hash_initialized) BUG("the name hash isn't thread safe"); - mem_pool_init(&istate->ce_mem_pool, 0); + istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool)); + mem_pool_init(istate->ce_mem_pool, 0); /* ensure we have no more threads than we have blocks to process */ if (nr_threads > ieot->nr) @@ -2097,11 +2101,12 @@ static unsigned long load_cache_entries_threaded(struct index_state *istate, con nr = 0; for (j = p->ieot_start; j < p->ieot_start + p->ieot_blocks; j++) nr += p->ieot->entries[j].nr; + istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool)); if (istate->version == 4) { - mem_pool_init(&p->ce_mem_pool, + mem_pool_init(p->ce_mem_pool, estimate_cache_size_from_compressed(nr)); } else { - mem_pool_init(&p->ce_mem_pool, + mem_pool_init(p->ce_mem_pool, estimate_cache_size(mmap_size, nr)); } @@ -2358,7 +2363,7 @@ int discard_index(struct index_state *istate) if (istate->ce_mem_pool) { mem_pool_discard(istate->ce_mem_pool, should_validate_cache_entries()); - istate->ce_mem_pool = NULL; + FREE_AND_NULL(istate->ce_mem_pool); } return 0; diff --git a/split-index.c b/split-index.c index e6154e4ea9..c0e8ad670d 100644 --- a/split-index.c +++ b/split-index.c @@ -79,8 +79,10 @@ void move_cache_to_base_index(struct index_state *istate) if (si->base && si->base->ce_mem_pool) { - if (!istate->ce_mem_pool) - mem_pool_init(&istate->ce_mem_pool, 0); + if (!istate->ce_mem_pool) { + istate->ce_mem_pool = xmalloc(sizeof(struct mem_pool)); + mem_pool_init(istate->ce_mem_pool, 0); + } mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool); } From patchwork Fri Aug 14 03:02:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Blain via GitGitGadget X-Patchwork-Id: 11713489 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D305A1744 for ; Fri, 14 Aug 2020 03:02:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BB2532078B for ; Fri, 14 Aug 2020 03:02:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Wo0jbEsY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726631AbgHNDCW (ORCPT ); Thu, 13 Aug 2020 23:02:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726604AbgHNDCV (ORCPT ); Thu, 13 Aug 2020 23:02:21 -0400 Received: from mail-ej1-x643.google.com (mail-ej1-x643.google.com [IPv6:2a00:1450:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C78E2C061757 for ; Thu, 13 Aug 2020 20:02:20 -0700 (PDT) Received: by mail-ej1-x643.google.com with SMTP id bo3so8373182ejb.11 for ; Thu, 13 Aug 2020 20:02:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=ms+Sz4Uhn67ieoWyWfXaUOw5+wXjv/qK6vxQpnPAkSo=; b=Wo0jbEsYJO2DYKXQXyOEfl+11v8WHXVSL+Bp4yVCk49W6t+yZplLtDyOdOqaKPmLGt IhjZ8fwjfAC9ikNfx2k9a27f/rzbk8Xpbssl3o/p0SwoRIVoGwAV3tqjx3Iv3OalE8sf uGlFnmRdIDgxpf4q+Cjp1mKTy/pgONUmnGVYzn/g++m+aVeKstQx6T5Qxxr+7j07ovmY WqqaPbcDfJHD1KbLbgyxQwtBKoDz55O6RsEd4CzeD5uUVhJBhnQFw/84mS+kD+Nkxluy eyJpaCbDmc07WI7T8rNxMk2DrPiEbpsF9mleUG3+mcctqyxVJiH08bRwMbAqORDY5LT+ hf1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=ms+Sz4Uhn67ieoWyWfXaUOw5+wXjv/qK6vxQpnPAkSo=; b=mazi+xs6U8q7wTDA2ulVxBMxG2oH8xHBa0D6PVAGWdzACLq3oRH2XlKbsVsuxzRjPs epvRgJ6ZsTHrBLb0I3icWAow/qyIVKOJfDqD/ypxWlwsjDpQ+SlnACuM+18KvVb0yXKX PAIClIyU5YbLduq8CEnNDxPv5D+PeC7LztYc0Q2pepEYQ4hkSlUIm9O5jkvPZzsuoXx/ 86Y8Mh3UubQ1gubDR8KDxNV1JQyKfSEhfNEnD7ALz21DA6TBtmoJrasKKqfu4R9pmj6N L9EYZRbhvpJP3LI+SA0AtFTztD1Le8PWkW5ity4nNBVEoqTy0BiFZgGsOHlBTVhuI1v1 Ziug== X-Gm-Message-State: AOAM530B0Li3cGGe972JtxLvWmj5X3FaRadzZCF0TDRohL+49hknttQk SlOn1mXROeUGFdPQ+MnSrfIhJP0Z X-Google-Smtp-Source: ABdhPJxhThYDslTSmX/mbhJYLZ3coa5zZWsK43iIfiTjmq7xO9aSuorBJipXfubcuVTDOBT/yDTUcQ== X-Received: by 2002:a17:906:8316:: with SMTP id j22mr488614ejx.20.1597374139339; Thu, 13 Aug 2020 20:02:19 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id cc9sm5626237edb.14.2020.08.13.20.02.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:02:18 -0700 (PDT) Message-Id: <62c2479fe6068f31c3cae1b1a13d67527121a15b.1597374135.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Elijah Newren via GitGitGadget" Date: Fri, 14 Aug 2020 03:02:15 +0000 Subject: [PATCH 3/3] mem-pool: use consistent pool variable name Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Matheus Tavares , Elijah Newren , Elijah Newren Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren About half the function declarations in mem-pool.h used 'struct mem_pool *pool', while the other half used 'struct mem_pool *mem_pool'. Make the code a bit more consistent by just using 'pool' in preference to 'mem_pool' everywhere. No behavioral changes included; this is just a mechanical rename (though a line or two was rewrapped as well). Signed-off-by: Elijah Newren --- mem-pool.c | 50 ++++++++++++++++++++++++++------------------------ mem-pool.h | 6 +++--- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/mem-pool.c b/mem-pool.c index b7d789823e..08039ed069 100644 --- a/mem-pool.c +++ b/mem-pool.c @@ -12,11 +12,13 @@ * `insert_after`. If `insert_after` is NULL, then insert block at the * head of the linked list. */ -static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t block_alloc, struct mp_block *insert_after) +static struct mp_block *mem_pool_alloc_block(struct mem_pool *pool, + size_t block_alloc, + struct mp_block *insert_after) { struct mp_block *p; - mem_pool->pool_alloc += sizeof(struct mp_block) + block_alloc; + pool->pool_alloc += sizeof(struct mp_block) + block_alloc; p = xmalloc(st_add(sizeof(struct mp_block), block_alloc)); p->next_free = (char *)p->space; @@ -26,28 +28,28 @@ static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t b p->next_block = insert_after->next_block; insert_after->next_block = p; } else { - p->next_block = mem_pool->mp_block; - mem_pool->mp_block = p; + p->next_block = pool->mp_block; + pool->mp_block = p; } return p; } -void mem_pool_init(struct mem_pool *mem_pool, size_t initial_size) +void mem_pool_init(struct mem_pool *pool, size_t initial_size) { - mem_pool->mp_block = NULL; - mem_pool->pool_alloc = 0; - mem_pool->block_alloc = BLOCK_GROWTH_SIZE; + pool->mp_block = NULL; + pool->pool_alloc = 0; + pool->block_alloc = BLOCK_GROWTH_SIZE; if (initial_size > 0) - mem_pool_alloc_block(mem_pool, initial_size, NULL); + mem_pool_alloc_block(pool, initial_size, NULL); } -void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory) +void mem_pool_discard(struct mem_pool *pool, int invalidate_memory) { struct mp_block *block, *block_to_free; - block = mem_pool->mp_block; + block = pool->mp_block; while (block) { block_to_free = block; @@ -59,11 +61,11 @@ void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory) free(block_to_free); } - mem_pool->mp_block = NULL; - mem_pool->pool_alloc = 0; + pool->mp_block = NULL; + pool->pool_alloc = 0; } -void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len) +void *mem_pool_alloc(struct mem_pool *pool, size_t len) { struct mp_block *p = NULL; void *r; @@ -72,15 +74,15 @@ void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len) if (len & (sizeof(uintmax_t) - 1)) len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1)); - if (mem_pool->mp_block && - mem_pool->mp_block->end - mem_pool->mp_block->next_free >= len) - p = mem_pool->mp_block; + if (pool->mp_block && + pool->mp_block->end - pool->mp_block->next_free >= len) + p = pool->mp_block; if (!p) { - if (len >= (mem_pool->block_alloc / 2)) - return mem_pool_alloc_block(mem_pool, len, mem_pool->mp_block); + if (len >= (pool->block_alloc / 2)) + return mem_pool_alloc_block(pool, len, pool->mp_block); - p = mem_pool_alloc_block(mem_pool, mem_pool->block_alloc, NULL); + p = mem_pool_alloc_block(pool, pool->block_alloc, NULL); } r = p->next_free; @@ -88,10 +90,10 @@ void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len) return r; } -void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size) +void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size) { size_t len = st_mult(count, size); - void *r = mem_pool_alloc(mem_pool, len); + void *r = mem_pool_alloc(pool, len); memset(r, 0, len); return r; } @@ -119,12 +121,12 @@ char *mem_pool_xstrndup(struct mem_pool *pool, const char *str, size_t len) return memcpy(ret, str, minlen); } -int mem_pool_contains(struct mem_pool *mem_pool, void *mem) +int mem_pool_contains(struct mem_pool *pool, void *mem) { struct mp_block *p; /* Check if memory is allocated in a block */ - for (p = mem_pool->mp_block; p; p = p->next_block) + for (p = pool->mp_block; p; p = p->next_block) if ((mem >= ((void *)p->space)) && (mem < ((void *)p->end))) return 1; diff --git a/mem-pool.h b/mem-pool.h index 30b7a8c03b..022b3097e9 100644 --- a/mem-pool.h +++ b/mem-pool.h @@ -24,12 +24,12 @@ struct mem_pool { /* * Initialize mem_pool with specified initial size. */ -void mem_pool_init(struct mem_pool *mem_pool, size_t initial_size); +void mem_pool_init(struct mem_pool *pool, size_t initial_size); /* * Discard all the memory the memory pool is responsible for. */ -void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory); +void mem_pool_discard(struct mem_pool *pool, int invalidate_memory); /* * Alloc memory from the mem_pool. @@ -58,6 +58,6 @@ void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src); * Check if a memory pointed at by 'mem' is part of the range of * memory managed by the specified mem_pool. */ -int mem_pool_contains(struct mem_pool *mem_pool, void *mem); +int mem_pool_contains(struct mem_pool *pool, void *mem); #endif