]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
mem/libminimal.la to facilitate helper reuse of convenience libs (#1004)
authorAlex Rousskov <rousskov@measurement-factory.com>
Mon, 28 Mar 2022 15:56:24 +0000 (15:56 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Tue, 29 Mar 2022 03:07:47 +0000 (03:07 +0000)
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
src/mem/Makefile.am
src/mem/minimal.cc [new file with mode: 0644]
tools/Makefile.am
tools/squidclient/Makefile.am

index e5fb6bd9fb43b0af17248a0fd0fae4471e3ea51b..30c7d8be1a068bbbff551c0d703738bd082efa78 100644 (file)
@@ -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 $@
 
index 19cdeebc02de77c15756f6f59a76e443d9d5d6da..68760861d7d73825b37a76e34cb824ea409f8290 100644 (file)
@@ -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 (file)
index 0000000..b766e2d
--- /dev/null
@@ -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;
+}
+
index 4ee6199d6dc6ebd803d47a494824fa09fcbc699d..80a09f7d8c695692de4dd69e3b00016e98de2330 100644 (file)
@@ -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 = \
index 575bee62f6609b2964501c5e633cc4e90066e8e1..2ff2477c4f8a8ada0618a392886baf7bcb7d4503 100644 (file)
@@ -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