]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/mem/old_api.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / mem / old_api.cc
index 3ef4419885a1b2a54f4759a02d33a58729df33d1..0561a755358e4b42e1406adec8a74e8d1bc3091f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
+ * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
  *
  * Squid software is distributed under GPLv2+ license and includes
  * contributions from numerous individuals and organizations.
@@ -9,8 +9,6 @@
 /* DEBUG: section 13    High Level Memory Pool Management */
 
 #include "squid.h"
-#include "acl/AclDenyInfoList.h"
-#include "acl/AclNameList.h"
 #include "base/PackableStream.h"
 #include "ClientInfo.h"
 #include "dlink.h"
 #include "MemBuf.h"
 #include "mgr/Registration.h"
 #include "SquidConfig.h"
-#include "SquidList.h"
 #include "SquidTime.h"
 #include "Store.h"
 
 #include <iomanip>
-#include <vector>
 
 /* forward declarations */
 static void memFree2K(void *);
@@ -61,6 +57,9 @@ static Mem::Meter HugeBufCountMeter;
 static Mem::Meter HugeBufVolumeMeter;
 
 /* local routines */
+
+// XXX: refactor objects using these pools to use MEMPROXY classes instead
+// then remove this function entirely
 static MemAllocator *&
 GetPool(size_t type)
 {
@@ -70,12 +69,15 @@ GetPool(size_t type)
     if (!initialized) {
         memset(pools, '\0', sizeof(pools));
         initialized = true;
+        // Mem::Init() makes use of GetPool(type) to initialize
+        // the actual pools. So must come after the flag is true
+        Mem::Init();
     }
 
     return pools[type];
 }
 
-static MemAllocator *&
+static MemAllocator &
 GetStrPool(size_t type)
 {
     static MemAllocator *strPools[mem_str_pool_count];
@@ -108,7 +110,7 @@ GetStrPool(size_t type)
         initialized = true;
     }
 
-    return strPools[type];
+    return *strPools[type];
 }
 
 /* Find the best fit string pool type */
@@ -117,13 +119,11 @@ memFindStringSizeType(size_t net_size, bool fuzzy)
 {
     mem_type type = MEM_NONE;
     for (unsigned int i = 0; i < mem_str_pool_count; ++i) {
-        auto pool = GetStrPool(i);
-        if (!pool)
-            continue;
-        if (fuzzy && net_size < pool->objectSize()) {
+        auto &pool = GetStrPool(i);
+        if (fuzzy && net_size < pool.objectSize()) {
             type = static_cast<mem_type>(i);
             break;
-        } else if (net_size == pool->objectSize()) {
+        } else if (net_size == pool.objectSize()) {
             type = static_cast<mem_type>(i);
             break;
         }
@@ -143,13 +143,13 @@ memStringStats(std::ostream &stream)
     /* table body */
 
     for (i = 0; i < mem_str_pool_count; ++i) {
-        const MemAllocator *pool = GetStrPool(i);
-        const auto plevel = pool->getMeter().inuse.currentLevel();
-        stream << std::setw(20) << std::left << pool->objectType();
+        const auto &pool = GetStrPool(i);
+        const auto plevel = pool.getMeter().inuse.currentLevel();
+        stream << std::setw(20) << std::left << pool.objectType();
         stream << std::right << "\t " << xpercentInt(plevel, StrCountMeter.currentLevel());
-        stream << "\t " << xpercentInt(plevel * pool->objectSize(), StrVolumeMeter.currentLevel()) << "\n";
+        stream << "\t " << xpercentInt(plevel * pool.objectSize(), StrVolumeMeter.currentLevel()) << "\n";
         pooled_count += plevel;
-        pooled_volume += plevel * pool->objectSize();
+        pooled_volume += plevel * pool.objectSize();
     }
 
     /* malloc strings */
@@ -234,18 +234,22 @@ memFree(void *p, int type)
 void *
 memAllocString(size_t net_size, size_t * gross_size)
 {
-    MemAllocator *pool = NULL;
     assert(gross_size);
 
     auto type = memFindStringSizeType(net_size, true);
-    if (type != MEM_NONE)
-        pool = GetStrPool(type);
+    if (type != MEM_NONE) {
+        auto &pool = GetStrPool(type);
+        *gross_size = pool.objectSize();
+        assert(*gross_size >= net_size);
+        ++StrCountMeter;
+        StrVolumeMeter += *gross_size;
+        return pool.alloc();
+    }
 
-    *gross_size = pool ? pool->objectSize() : net_size;
-    assert(*gross_size >= net_size);
+    *gross_size = net_size;
     ++StrCountMeter;
     StrVolumeMeter += *gross_size;
-    return pool ? pool->alloc() : xcalloc(1, net_size);
+    return xcalloc(1, net_size);
 }
 
 size_t
@@ -254,7 +258,7 @@ memStringCount()
     size_t result = 0;
 
     for (int counter = 0; counter < mem_str_pool_count; ++counter)
-        result += memPoolInUseCount(GetStrPool(counter));
+        result += GetStrPool(counter).inUseCount();
 
     return result;
 }
@@ -263,16 +267,16 @@ memStringCount()
 void
 memFreeString(size_t size, void *buf)
 {
-    MemAllocator *pool = NULL;
     assert(buf);
 
     auto type = memFindStringSizeType(size, false);
     if (type != MEM_NONE)
-        pool = GetStrPool(type);
+        GetStrPool(type).freeOne(buf);
+    else
+        xfree(buf);
 
     --StrCountMeter;
     StrVolumeMeter -= size;
-    pool ? pool->freeOne(buf) : xfree(buf);
 }
 
 /* Find the best fit MEM_X_BUF type */
@@ -404,9 +408,6 @@ memConfigure(void)
     MemPools::GetInstance().setIdleLimit(new_pool_limit);
 }
 
-/* XXX make these classes do their own memory management */
-#include "HttpHdrContRange.h"
-
 void
 Mem::Init(void)
 {
@@ -438,17 +439,8 @@ Mem::Init(void)
     memDataInit(MEM_16K_BUF, "16K Buffer", 16384, 10, false);
     memDataInit(MEM_32K_BUF, "32K Buffer", 32768, 10, false);
     memDataInit(MEM_64K_BUF, "64K Buffer", 65536, 10, false);
-    memDataInit(MEM_ACL_DENY_INFO_LIST, "AclDenyInfoList",
-                sizeof(AclDenyInfoList), 0);
-    memDataInit(MEM_ACL_NAME_LIST, "acl_name_list", sizeof(AclNameList), 0);
-    memDataInit(MEM_LINK_LIST, "link_list", sizeof(link_list), 10);
-    memDataInit(MEM_DLINK_NODE, "dlink_node", sizeof(dlink_node), 10);
     memDataInit(MEM_DREAD_CTRL, "dread_ctrl", sizeof(dread_ctrl), 0);
     memDataInit(MEM_DWRITE_Q, "dwrite_q", sizeof(dwrite_q), 0);
-    memDataInit(MEM_HTTP_HDR_CONTENT_RANGE, "HttpHdrContRange", sizeof(HttpHdrContRange), 0);
-    memDataInit(MEM_NETDBENTRY, "netdbEntry", sizeof(netdbEntry), 0);
-    memDataInit(MEM_NET_DB_NAME, "net_db_name", sizeof(net_db_name), 0);
-    memDataInit(MEM_CLIENT_INFO, "ClientInfo", sizeof(ClientInfo), 0);
     memDataInit(MEM_MD5_DIGEST, "MD5 digest", SQUID_MD5_DIGEST_LENGTH, 0);
     GetPool(MEM_MD5_DIGEST)->setChunkSize(512 * 1024);
 
@@ -482,11 +474,10 @@ memCheckInit(void)
 {
     mem_type t = MEM_NONE;
 
-    while (++t < MEM_DONTFREE) {
+    while (++t < MEM_MAX) {
         /*
          * If you hit this assertion, then you forgot to add a
          * memDataInit() line for type 't'.
-         * Or placed the pool type in the wrong section of the enum list.
          */
         assert(GetPool(t));
     }
@@ -510,7 +501,7 @@ memClean(void)
 int
 memInUse(mem_type type)
 {
-    return memPoolInUseCount(GetPool(type));
+    return GetPool(type)->inUseCount();
 }
 
 /* ick */