From 536a1f9cda27011992a6a5dbe894bdc3621fa3c2 Mon Sep 17 00:00:00 2001 From: Alex Rousskov Date: Mon, 28 Mar 2022 15:56:24 +0000 Subject: [PATCH] mem/libminimal.la to facilitate helper reuse of convenience libs (#1004) Squid helpers/tools cannot reuse features like SBuf because, in part, the corresponding convenience libraries depend on libmem.la which drags in heavy dependencies, including Store and time-based Event modules. The new mem/libminimal convenience library implements enough of src/mem/ APIs to make the new memory library usable by helpers/tools without causing a dependency explosion. Do not use tests/stub_libmem.cc in the deployed/non-test squidclient, cachemgr.cgi, and pinger programs. --- src/icmp/Makefile.am | 5 +- src/mem/Makefile.am | 17 ++++-- src/mem/minimal.cc | 100 ++++++++++++++++++++++++++++++++++ tools/Makefile.am | 7 +-- tools/squidclient/Makefile.am | 7 +-- 5 files changed, 118 insertions(+), 18 deletions(-) create mode 100644 src/mem/minimal.cc diff --git a/src/icmp/Makefile.am b/src/icmp/Makefile.am index e5fb6bd9fb..30c7d8be1a 100644 --- a/src/icmp/Makefile.am +++ b/src/icmp/Makefile.am @@ -40,7 +40,6 @@ COPIED_SOURCE= \ SquidConfig.cc \ tests/stub_fd.cc \ tests/stub_HelperChildConfig.cc \ - tests/stub_libmem.cc \ tests/STUB.h \ time.cc @@ -62,6 +61,7 @@ pinger_LDADD=\ $(top_builddir)/src/sbuf/libsbuf.la \ $(top_builddir)/src/debug/libdebug.la \ $(top_builddir)/src/base/libbase.la \ + $(top_builddir)/src/mem/libminimal.la \ $(COMPAT_LIB) \ $(SSLLIB) \ $(XTRA_LIBS) @@ -98,9 +98,6 @@ tests/stub_HelperChildConfig.cc: $(top_srcdir)/src/tests/stub_HelperChildConfig. tests/stub_fd.cc: $(top_srcdir)/src/tests/stub_fd.cc | tests cp $(top_srcdir)/src/tests/stub_fd.cc $@ -tests/stub_libmem.cc: $(top_srcdir)/src/tests/stub_libmem.cc | tests - cp $(top_srcdir)/src/tests/stub_libmem.cc $@ - tests/STUB.h: $(top_srcdir)/src/tests/STUB.h | tests cp $(top_srcdir)/src/tests/STUB.h $@ diff --git a/src/mem/Makefile.am b/src/mem/Makefile.am index 19cdeebc02..68760861d7 100644 --- a/src/mem/Makefile.am +++ b/src/mem/Makefile.am @@ -8,11 +8,18 @@ include $(top_srcdir)/src/Common.am include $(top_srcdir)/src/TestHeaders.am -noinst_LTLIBRARIES = libmem.la +noinst_LTLIBRARIES = \ + libmem.la \ + libminimal.la +noinst_HEADERS = \ + AllocatorProxy.h \ + Sensitive.h \ + forward.h + +# a full-featured memory management library for sbin/squid use libmem_la_SOURCES = \ AllocatorProxy.cc \ - AllocatorProxy.h \ Meter.h \ Pool.cc \ Pool.h \ @@ -21,6 +28,8 @@ libmem_la_SOURCES = \ PoolMalloc.cc \ PoolMalloc.h \ PoolingAllocator.h \ - Sensitive.h \ - forward.h \ old_api.cc + +# a bare-bones implementation of few libmem.la APIs sufficient for helpers use +libminimal_la_SOURCES = \ + minimal.cc diff --git a/src/mem/minimal.cc b/src/mem/minimal.cc new file mode 100644 index 0000000000..b766e2de3d --- /dev/null +++ b/src/mem/minimal.cc @@ -0,0 +1,100 @@ +/* + * Copyright (C) 1996-2022 The Squid Software Foundation and contributors + * + * Squid software is distributed under GPLv2+ license and includes + * contributions from numerous individuals and organizations. + * Please see the COPYING and CONTRIBUTORS files for details. + */ + +#include "squid.h" +#include "mem/AllocatorProxy.h" +#include "mem/forward.h" + +/// The number of currently alive objects (poor man's meter.alloc=meter.inuse). +/// Technically, this is supposed to be a per-allocator statistics, but +/// AllocatorProxy is not a MemAllocator so we maintain a global counter +/// instead. We probably do not have to maintain this statistics at all. +static int Alive = 0; + +void * +Mem::AllocatorProxy::alloc() +{ + const auto memory = doZero ? xcalloc(1, size) : xmalloc(size); + ++Alive; + return memory; +} + +void +Mem::AllocatorProxy::freeOne(void *memory) { + xfree(memory); + --Alive; +} + +int +Mem::AllocatorProxy::inUseCount() const +{ + return Alive; +} + +int +Mem::AllocatorProxy::getStats(MemPoolStats *) +{ + return Alive; +} + +void * +memAllocBuf(const size_t netSize, size_t * const grossSize) +{ + *grossSize = netSize; + return xcalloc(1, netSize); +} + +void * +memReallocBuf(void * const oldBuf, const size_t netSize, size_t * const grossSize) +{ + *grossSize = netSize; + return xrealloc(oldBuf, netSize); +} + +void +memFree(void *memory, int) +{ + xfree(memory); +} + +void * +memAllocString(const size_t netSize, size_t * const grossSize) +{ + return memAllocBuf(netSize, grossSize); +} + +void +memFreeString(size_t, void *memory) +{ + xfree(memory); +} + +void * +memAllocRigid(const size_t netSize) +{ + return xmalloc(netSize); +} + +void +memFreeBuf(size_t, void * const buf) +{ + xfree(buf); +} + +static void +myFree(void * const buf) +{ + xfree(buf); +} + +FREE * +memFreeBufFunc(size_t) +{ + return &myFree; +} + diff --git a/tools/Makefile.am b/tools/Makefile.am index 4ee6199d6d..80a09f7d8c 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -17,6 +17,7 @@ DISTCLEANFILES= LDADD= \ $(top_builddir)/src/ip/libip.la \ + $(top_builddir)/src/mem/libminimal.la \ $(top_builddir)/lib/libmiscencoding.la \ $(top_builddir)/lib/libmiscutil.la \ $(COMPAT_LIB) \ @@ -49,9 +50,6 @@ time.cc: $(top_srcdir)/src/time.cc tests/stub_cbdata.cc: $(top_srcdir)/src/tests/stub_cbdata.cc | tests cp $(top_srcdir)/src/tests/stub_cbdata.cc $@ -tests/stub_libmem.cc: $(top_srcdir)/src/tests/stub_libmem.cc | tests - cp $(top_srcdir)/src/tests/stub_libmem.cc $@ - tests/STUB.h: $(top_srcdir)/src/tests/STUB.h | tests cp $(top_srcdir)/src/tests/STUB.h $@ @@ -63,7 +61,7 @@ tests: # globals.cc is needed by test_tools.cc. # Neither of these should be disted from here. TESTSOURCES= test_tools.cc -CLEANFILES += test_tools.cc Here.cc CharacterSet.cc MemBuf.cc tests/stub_debug.cc time.cc tests/stub_cbdata.cc tests/stub_libmem.cc tests/STUB.h +CLEANFILES += test_tools.cc Here.cc CharacterSet.cc MemBuf.cc tests/stub_debug.cc time.cc tests/stub_cbdata.cc tests/STUB.h ## Test Scripts EXTRA_DIST += helper-ok-dying.pl helper-ok.pl @@ -85,7 +83,6 @@ cachemgr__CGIEXT__SOURCES = \ nodist_cachemgr__CGIEXT__SOURCES = \ tests/stub_cbdata.cc \ tests/stub_debug.cc \ - tests/stub_libmem.cc \ tests/STUB.h cachemgr__CGIEXT__CXXFLAGS = \ diff --git a/tools/squidclient/Makefile.am b/tools/squidclient/Makefile.am index 575bee62f6..2ff2477c4f 100644 --- a/tools/squidclient/Makefile.am +++ b/tools/squidclient/Makefile.am @@ -14,6 +14,7 @@ DISTCLEANFILES = LDADD = \ $(top_builddir)/src/ip/libip.la \ + $(top_builddir)/src/mem/libminimal.la \ $(top_builddir)/src/base/libbase.la \ $(top_builddir)/lib/libmiscencoding.la \ $(top_builddir)/lib/libmiscutil.la \ @@ -36,9 +37,6 @@ tests/stub_debug.cc: $(top_srcdir)/src/tests/stub_debug.cc | tests time.cc: $(top_srcdir)/src/time.cc cp $(top_srcdir)/src/time.cc $@ -tests/stub_libmem.cc: $(top_srcdir)/src/tests/stub_libmem.cc | tests - cp $(top_srcdir)/src/tests/stub_libmem.cc $@ - tests/STUB.h: $(top_srcdir)/src/tests/STUB.h | tests cp $(top_srcdir)/src/tests/STUB.h $@ @@ -50,7 +48,7 @@ tests: # globals.cc is needed by test_tools.cc. # Neither of these should be disted from here. TESTSOURCES= test_tools.cc -CLEANFILES += test_tools.cc tests/stub_debug.cc time.cc tests/stub_libmem.cc tests/STUB.h +CLEANFILES += test_tools.cc tests/stub_debug.cc time.cc tests/STUB.h ## ##### squidclient ##### @@ -70,5 +68,4 @@ squidclient_SOURCES = \ nodist_squidclient_SOURCES = \ tests/stub_debug.cc \ - tests/stub_libmem.cc \ tests/STUB.h -- 2.47.2