aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/compression/comp_util.cpp
blob: 5f2e37d49e48104820b17cee8bd68790e2e8552c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* Allocation Tracker
* (C) 2014 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/internal/comp_util.h>
#include <cstring>
#include <cstdlib>

namespace Botan {

void* Compression_Alloc_Info::do_malloc(size_t n, size_t size)
   {
   const size_t total_sz = n * size;

   void* ptr = std::malloc(total_sz);
   m_current_allocs[ptr] = total_sz;
   return ptr;
   }

void Compression_Alloc_Info::do_free(void* ptr)
   {
   if(ptr)
      {
      auto i = m_current_allocs.find(ptr);

      if(i == m_current_allocs.end())
         throw std::runtime_error("Compression_Alloc_Info::free got pointer not allocated by us");

      std::memset(ptr, 0, i->second);
      std::free(ptr);
      m_current_allocs.erase(i);
      }
   }

}