This file is indexed.

/usr/include/libMems-1.6/libMems/SlotAllocator.h is in libmems-1.6-dev 1.6.0+4725-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*******************************************************************************
 * $Id: SlotAllocator.h,v 1.6 2004/02/27 23:08:55 darling Exp $
 * This file is copyright 2002-2007 Aaron Darling and authors listed in the AUTHORS file.
 * This file is licensed under the GPL.
 * Please see the file called COPYING for licensing details.
 * **************
 ******************************************************************************/

#ifndef _SlotAllocator_h_
#define _SlotAllocator_h_

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <vector>
#include <list>
#include <stdexcept>
#include <iostream>
#include "libMUSCLE/threadstorage.h"


namespace mems {


/** When more space is needed to store a datatype, the memory pool will grow by this factor */
const double POOL_GROWTH_RATE = 1.6;
	
/**
 * This class allocates memory according to the slot allocation scheme for
 * fixed size objects.  Each time all slots are full it allocates twice the
 * previous allocation.  If it is unable to allocate twice the previous 
 * allocation, it does a binary 'search' for the largest amount of memory it
 * can allocate. 
 * The current implementation does not allow memory to
 * be freed once allocated.
 */
template< class T >
class SlotAllocator {
public:
	static SlotAllocator<T>& GetSlotAllocator();
	T* Allocate();
	void Free( T* t );
	void Free( std::vector<T*>& chunk );
	~SlotAllocator(){ 
		Purge();
	};
	void Purge(){
//#pragma omp critical
//{
	std::vector<T*>& data = this->data.get();
	unsigned& tail_free = this->tail_free.get();
	unsigned& n_elems = this->n_elems.get();
	std::vector< T* >& free_list = this->free_list.get();
		for( unsigned dataI = 0; dataI < data.size(); dataI++ )
			free(data[dataI]);
		data.clear();
		free_list.clear();
		tail_free = 0;
		n_elems = 0;
//}
	}

protected:
	TLS< std::vector<T*> > data;
	TLS< unsigned > tail_free;
	TLS< unsigned > n_elems;	/**< number of T in the most recently allocated block */

	TLS< std::vector< T* > > free_list;

private:
	SlotAllocator() : tail_free(0), n_elems(0) {};
	SlotAllocator& operator=( SlotAllocator& sa ){ n_elems = sa.n_elems; data = sa.data; tail_free = sa.tail_free; return *this;};
	SlotAllocator( SlotAllocator& sa ){ *this = sa; };
		
};

template< class T >
inline
SlotAllocator< T >& SlotAllocator< T >::GetSlotAllocator(){
	static SlotAllocator< T >* sa = new SlotAllocator< T >();
	return *sa;
}


template< class T >
inline
T* SlotAllocator< T >::Allocate(){
	T* t_ptr = NULL;

{
	std::vector<T*>& data = this->data.get();
	unsigned& tail_free = this->tail_free.get();
	unsigned& n_elems = this->n_elems.get();
	std::vector< T* >& free_list = this->free_list.get();
//	omp_guard rex( locker );
	if( free_list.begin() != free_list.end() ){
		t_ptr = free_list.back();
		free_list.pop_back();
	}else if( tail_free > 0 ){
		int T_index = n_elems - tail_free--;
		t_ptr = &(data.back()[ T_index ]);
	}else{

		// Last resort:
		// increase the size of the data array
		unsigned new_size = (unsigned)(((double)n_elems * POOL_GROWTH_RATE)+0.5);
		if( new_size == 0 )
			new_size++;
		T* new_data = NULL;
		while( true ){
			try{
				new_data = (T*)malloc(sizeof(T)*new_size);
				break;
			}catch(...){
				new_size = new_size / 2;
				if( new_size == 0 )
					break;
			}
		}
		if( new_data == NULL || new_size == 0 ){
			throw std::out_of_range( "SlotAllocator::Allocate(): Unable to allocate more memory" );
		}
		data.push_back( new_data );
		tail_free = new_size - 1;
		t_ptr = & data.back()[0];
		n_elems = new_size;
	}
}
	return t_ptr;
}

template< class T >
inline
void SlotAllocator< T >::Free( T* t ){
	// for debugging double free
/*	for(size_t i = 0; i < free_list.size(); i++ )
		if( free_list[i] == t )
			std::cerr << "ERROR DOUBLE FREE\n";
*/	
	t->~T();
{
//	omp_guard rex( locker );
	std::vector< T* >& free_list = this->free_list.get();

	free_list.push_back( t );
}
}

template< class T >
inline
void SlotAllocator< T >::Free( std::vector<T*>& chunk ){
	// for debugging double free
/*	for(size_t i = 0; i < free_list.size(); i++ )
		if( free_list[i] == t )
			std::cerr << "ERROR DOUBLE FREE\n";
*/	
	for( size_t i = 0; i < chunk.size(); i++ )
		chunk[i]->~T();
{
//	omp_guard rex( locker );
	std::vector< T* >& free_list = this->free_list.get();
	free_list.insert(free_list.end(), chunk.begin(), chunk.end());
}
	chunk.clear();
}

}

#endif	// _SlotAllocator_h_