/usr/include/loki/yasli/yasli_memory.h is in libloki-dev 0.1.7-3.
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | #ifndef YASLI_MEMORY_H_
#define YASLI_MEMORY_H_
// $Id: yasli_memory.h 754 2006-10-17 19:59:11Z syntheticpp $
#include "yasli_traits.h"
#include "yasli_protocols.h"//!
#include <cassert>
#include <cstddef>
#include <misc/mojo.h>//NOT A SAFE WAY TO INCLUDE IT
namespace yasli {
// 20.4.1, the default allocator:
template <class T> class allocator;
template <> class allocator<void>;
// 20.4.1.2, allocator globals
template <class T, class U>
bool operator==(const allocator<T>&, const allocator<U>&) throw()
{ return true; }
template <class T, class U>
bool operator!=(const allocator<T>&, const allocator<U>&) throw()
{ return false; }
// 20.4.2, raw storage iterator:
// @@@ not defined, use the std one @@@
//template <class OutputIterator, class T> class raw_storage_iterator;
// 20.4.3, temporary buffers:
// @@@ not defined, use the std one @@@
//template <class T>
//pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n);
// @@@ not defined, use the std one @@@
// template <class T>
// void return_temporary_buffer(T* p);
// 20.4.4, specialized algorithms:
template <class InputIterator, class ForwardIterator>
ForwardIterator
uninitialized_copy(InputIterator first, InputIterator last,
ForwardIterator result);
template <class ForwardIterator, class Size, class T>
void uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
// 20.4.5, pointers:
// @@@ not defined, use the std one @@@
// template<class X> class auto_ptr;
}
namespace yasli {
template <class T> class allocator;
// specialize for void:
template <> class allocator<void>
{
public:
typedef void* pointer;
typedef const void* const_pointer;
// reference-to-void members are impossible.
typedef void value_type;
template <class U> struct rebind { typedef allocator<U> other; };
};
template <class T> class allocator
{
public:
typedef size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template <class U> struct rebind { typedef allocator<U> other; };
allocator() throw() {}
allocator(const allocator&) throw() {}
template <class U> allocator(const allocator<U>&) throw() {}
~allocator() throw() {}
pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) { return &x; }
pointer allocate(size_type n, allocator<void>::const_pointer = 0)
{
return static_cast<pointer>(::operator new(n * sizeof(T)));
}
void deallocate(pointer p, size_type)
{
::operator delete(p);
}
size_type max_size() const throw()
{
return size_type(-1);
}
void construct(pointer p, const T& val)
{
new((void *) p) T(val);
}
void destroy(pointer p)
{
((T*) p)->~T();
}
};
} // namespace yasli
namespace yasli_nstd
{
template <class T> class mallocator
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template <class U> struct rebind { typedef mallocator<U> other; };
mallocator() throw() {}
mallocator(const mallocator&) throw() {}
template <class U> mallocator(const mallocator<U>&) throw() {}
~mallocator() throw() {}
pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) { return &x; }
pointer allocate(size_type n, yasli::allocator<void>::const_pointer = 0)
{
return static_cast<pointer>(malloc(n * sizeof(T)));
}
void deallocate(pointer p, size_type)
{
free(p);
}
size_type max_size() const throw()
{
return size_type(-1);
}
void construct(pointer p, const T& val)
{
new((void *) p) T(val);
}
void destroy(pointer p)
{
((T*) p)->~T();
}
};
//--------------destroy--------
namespace _impl
{
struct non_destroyer
{
template <class A, class T>
static void destroy(A& a, T* p, typename A::size_type n) {}
template <class ForwardIterator>
static void destroy_range(ForwardIterator b, ForwardIterator e) {}
};
struct destroyer
{
template <class A, class T>
static void destroy(A& a, T* p, typename A::size_type n)
{
const typename A::pointer p1 = p + n;
for (; p < p1; ++p) a.destroy(p);
}
template <class ForwardIterator>
static void destroy_range(ForwardIterator b, ForwardIterator e)
{
typedef typename std::iterator_traits<ForwardIterator>::value_type
value_type;
for (; b != e; ++b) (*b).~value_type();
}
};
}
template <class A, class T>
void destroy(A& a, T* p, typename A::size_type n)
{
yasli_nstd::type_selector<yasli_nstd::is_class<T>::value != 0,
_impl::destroyer,
_impl::non_destroyer
>::result::destroy(a, p, n);
}
template <class ForwardIterator>
void destroy_range(ForwardIterator b, ForwardIterator e)
{
yasli_nstd::type_selector<
yasli_nstd::is_class<typename std::iterator_traits<ForwardIterator>
::value_type>::value != 0,
_impl::destroyer,
_impl::non_destroyer
>::result::destroy_range(b, e);
}
//---------------
template <class It1, class It2>
It2 uninitialized_move(It1 b, It1 e, It2 d)
{
return mojo::uninitialized_move(b, e, d);
}
template <class A>
struct generic_allocator_traits
{
static typename A::pointer
reallocate(
A& a,
typename A::pointer b,
typename A::pointer e,
typename A::size_type newSize)
{
typename A::pointer p1 = a.allocate(newSize, b);
const typename A::size_type oldSize = e - b;
if (oldSize <= newSize) // expand
{
yasli_protocols::move_traits<typename A::value_type>::destructive_move(
b, b + oldSize, p1);
}
else // shrink
{
yasli_protocols::move_traits<typename A::value_type>::destructive_move(
b, b + newSize, p1);
yasli_nstd::destroy(a, b + newSize, oldSize - newSize);
}
a.deallocate(b, oldSize);
return p1;
}
static bool reallocate_inplace(
A& a,
typename A::pointer b,
typename A::size_type newSize)
{
return false;
}
private:
generic_allocator_traits();
};
template <class A>
struct allocator_traits : public generic_allocator_traits<A>
{
};
template <class T>
struct allocator_traits< yasli::allocator<T> >
: public generic_allocator_traits< yasli::allocator<T> >
{
#if YASLI_NEW_IS_MALLOC != 0
static bool reallocate_inplace(
A& a,
typename A::pointer b,
typename A::size_type newSize)
{
allocator_traits< yasli_nstd::mallocator<T> >
::reallocate_inplace(a, b, newSize);
}
static typename yasli::allocator<T>::pointer
reallocate(
yasli::allocator<T>& a,
typename yasli::allocator<T>::pointer b,
typename yasli::allocator<T>::pointer e,
typename yasli::allocator<T>::size_type newSize)
{
allocator_traits< yasli_nstd::mallocator<T> >
::reallocate(a, b, e, newSize);
}
#endif//yasli_new_is_malloc
};
template <class T>
struct allocator_traits< yasli_nstd::mallocator<T> >
: public generic_allocator_traits< yasli_nstd::mallocator<T> >
{
#if YASLI_HAS_EXPAND && YASLI_HAS_EFFICIENT_MSIZE
static bool reallocate_inplace(
yasli_nstd::mallocator<T>& a,
typename yasli_nstd::mallocator<T>::pointer b,
typename yasli_nstd::mallocator<T>::size_type newSize)
{
if (b == 0) return malloc(newSize);
if (newSize == 0) {free(b); return false;}
return b == yasli_platform::expand(b, newSize)
&& yasli_platform::msize(b) >= newSize;
}
#endif
static typename yasli_nstd::mallocator<T>::pointer
reallocate(
yasli_nstd::mallocator<T>& a,
typename yasli_nstd::mallocator<T>::pointer b,
typename yasli_nstd::mallocator<T>::pointer e,
typename yasli_nstd::mallocator<T>::size_type newSize)
{
if (yasli_nstd::is_memmoveable<T>::value)
{
return static_cast<T*>(realloc(b, newSize));
}
if(reallocate_inplace(a, b, newSize)) return b;
return generic_allocator_traits< yasli_nstd::mallocator<T> >::
reallocate(a, b, e, newSize);
}
};
}
namespace yasli
{
//Here is where type_selector is really much more ugly than
//enable_if.
//----------------UNINIT COPY--------
namespace _impl
{
//safe
template <class InputItr, class FwdItr>
struct uninitialized_safe_copier
{
static FwdItr execute(InputItr first, InputItr last, FwdItr result)
{
//
struct ScopeGuard
{
FwdItr begin;
FwdItr* current;
~ScopeGuard()
{
if (!current) return;
FwdItr end = *current;
typedef typename std::iterator_traits<FwdItr>::value_type T;
for (; begin != end; ++begin) (&*begin)->~T();
}
} guard = { result, &result };
for (; first != last; ++first, ++result)
new(&*result) typename std::iterator_traits<FwdItr>::value_type(*first);
// commit
return result;
}
};
template <class T>
struct uninitialized_memcopier
{
static T* execute(const T* first, const T* last, T* result)
{
yasli_nstd::is_memcopyable<T>::value;
const size_t s = last - first;
memmove(result, first, s * sizeof(T));
return result + s;
}
};
}// _impl
// @@@ TODO: specialize for yasli_nstd::fill_iterator
template <class InputItr, class FwdItr>
FwdItr uninitialized_copy(InputItr first, InputItr last, FwdItr result)
{
std::cout<<"neither\n";
return _impl::uninitialized_safe_copier<InputItr, FwdItr>::execute(first, last, result);
}
template <class T>
T* uninitialized_copy(const T* first, const T* last, T* result)
{
std::cout<<"const\n";
return yasli_nstd::type_selector<yasli_nstd::is_memcopyable<T>::value != 0,
_impl::uninitialized_memcopier<T>,
_impl::uninitialized_safe_copier<const T*, T*>
>::result::execute(first, last, result);
}
template <class T>
T* uninitialized_copy(T* first, T* last, T* result)
{
std::cout<<"non-const\n";
return uninitialized_copy(static_cast<const T*>(first),
static_cast<const T*>(last), result);
}
//-------------------------UNINIT FILL------
template <class ForwardIterator, class T>
void
uninitialized_fill(ForwardIterator first, ForwardIterator last,
const T& x)
{
struct ScopeGuard
{
ForwardIterator first;
ForwardIterator* pCrt;
~ScopeGuard()
{
if (pCrt) yasli_nstd::destroy_range(first, *pCrt);
}
} guard = { first, &first };
for (; first != last; ++first)
new(&*first) T(x);
// Commit
guard.pCrt = 0;
}
template <class T, class U>
void
uninitialized_fill(T* first, T* last, const U& x)
{
struct ScopeGuard
{
T* first;
T** pCrt;
~ScopeGuard()
{
if (pCrt) yasli_nstd::destroy_range(first, *pCrt);
}
} guard = { first, &first };
assert(first <= last);
switch ((last - first) & 7u)
{
case 0:
while (first != last)
{
new(first) T(x); ++first;
case 7: new(first) T(x); ++first;
case 6: new(first) T(x); ++first;
case 5: new(first) T(x); ++first;
case 4: new(first) T(x); ++first;
case 3: new(first) T(x); ++first;
case 2: new(first) T(x); ++first;
case 1: new(first) T(x); ++first;
assert(first <= last);
}
}
// Commit
guard.pCrt = 0;
}
}// yasli
#endif // YASLI_MEMORY_H_
|