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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*
* Copyright 2009-2024 Fabrice Colin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#ifdef HAVE_MALLINFO2
#include <malloc.h>
#else
#ifdef HAVE_MALLINFO
#include <malloc.h>
#else
#ifdef HAVE_MALLOC_TRIM
#include <malloc.h>
#endif
#endif
#endif
#include "Memory.h"
#ifdef HAVE_BOOST_POOL_POOLFWD_HPP
#ifdef HAVE_UMEM_H
// Memory pool with umem
typedef boost::singleton_pool<umem_allocator_tag,
sizeof(char), umem_allocator,
boost::details::pool::default_mutex, 131072> filter_pool;
#else
// Memory pool with malloc (glibc's or any other implementation)
// The tag is actually ignored
typedef boost::singleton_pool<boost::pool_allocator_tag,
sizeof(char), boost::default_user_allocator_malloc_free,
boost::details::pool::default_mutex, 131072> filter_pool;
#endif
#endif
using std::clog;
using std::endl;
using std::string;
Memory::Memory()
{
}
char *Memory::allocateBuffer(off_t length)
{
#ifdef HAVE_BOOST_POOL_POOLFWD_HPP
return static_cast<char*>(filter_pool::ordered_malloc(length));
#else
#ifdef HAVE_UMEM_H
return static_cast<char*>(umem_alloc(sizeof(char) * length, UMEM_DEFAULT));
#else
return static_cast<char*>(malloc(sizeof(char) * length));
#endif
#endif
}
void Memory::freeBuffer(char *pBuffer, off_t length)
{
#ifdef HAVE_BOOST_POOL_POOLFWD_HPP
filter_pool::ordered_free(static_cast<void*>(pBuffer), length);
#else
#ifdef HAVE_UMEM_H
umem_free(static_cast<void*>(pBuffer), n * sizeof(char));
#else
free(pBuffer);
#endif
#endif
}
int Memory::getUsage(void)
{
int inUse = 0;
#ifndef HAVE_UMEM_H
// All this only makes sense if umem isn't in use
#ifdef HAVE_MALLINFO2
struct mallinfo2 info = mallinfo2();
/* arena: total space allocated from the heap (that is, how much the “break” point has moved since the start of the process).
* uordblks:number of bytes allocated and in use.
* fordblks:number of bytes allocated but not in use.
* keepcost:size of the area that can be released with a malloc_trim ().
* hblks:number of chunks allocated via mmap ().
* hblkhd:total number of bytes allocated via mmap ().
*/
inUse = info.uordblks;
#ifdef DEBUG
clog << "Memory::getUsage: allocated on the heap " << info.arena << ", mmap'ed " << info.hblkhd
<< ", in use " << inUse << ", not in use " << info.fordblks << ", can be trimmed " << info.keepcost << endl;
#endif
#else
#ifdef HAVE_MALLINFO
struct mallinfo info = mallinfo();
/* arena: total space allocated from the heap (that is, how much the “break” point has moved since the start of the process).
* uordblks:number of bytes allocated and in use.
* fordblks:number of bytes allocated but not in use.
* keepcost:size of the area that can be released with a malloc_trim ().
* hblks:number of chunks allocated via mmap ().
* hblkhd:total number of bytes allocated via mmap ().
*/
inUse = info.uordblks;
#ifdef DEBUG
clog << "Memory::getUsage: allocated on the heap " << info.arena << ", mmap'ed " << info.hblkhd
<< ", in use " << inUse << ", not in use " << info.fordblks << ", can be trimmed " << info.keepcost << endl;
#endif
#endif
#endif
#endif
return inUse;
}
void Memory::reclaim(void)
{
#ifdef HAVE_BOOST_POOL_POOLFWD_HPP
bool releasedMemory = filter_pool::release_memory();
#ifdef DEBUG
clog << "Memory::reclaim: released allocated memory pool (" << releasedMemory << ")" << endl;
#endif
#endif
#ifdef HAVE_UMEM_H
umem_reap();
#else
#ifdef HAVE_MALLOC_TRIM
int trimmedMemory = malloc_trim(0);
#ifdef DEBUG
clog << "Memory::reclaim: trimmed allocated memory (" << trimmedMemory << ")" << endl;
#endif
#endif
#endif
}
|