Sources Pipelines Documentation

Allocators  
mallocator.ipp
1 // Copyright (c) 2016 Lukasz Laszko
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #pragma once
6 
7 #include <boost/memory/memory_block.hpp>
8 
9 #include <new>
10 #include <cstdlib>
11 
12 
13 namespace boost { namespace memory {
14 
15 
17 {
18  auto result = malloc(sizeof(mallocator));
19  if (result == NULL)
20  throw std::bad_alloc();
21 
22  mallocator* instance = reinterpret_cast<mallocator*>(result);
23  new (instance) mallocator;
24  return instance;
25 }
26 
27 inline memory_block mallocator::allocate(std::size_t size)
28 {
29  auto result = malloc(size);
30  if (result == NULL)
31  throw std::bad_alloc();
32 
33  return { result, size };
34 }
35 
37 {
38  free(block.address);
39  block = null_block;
40 }
41 
42 inline bool mallocator::owns(memory_block& block)
43 {
44  return block.address != nullptr;
45 }
46 
47 } }
48 
49 
bool owns(memory_block &block)
Checks if the block has been allocated with this allocator.
Definition: mallocator.ipp:42
void deallocate(memory_block &block)
Deallocates memory block using free.
Definition: mallocator.ipp:36
static mallocator * self_allocate()
Allocates an instance of mallocator on the heap.
Definition: mallocator.ipp:16
An allocator backed by malloc and free system calls.
Definition: mallocator.hpp:14
memory_block allocate(std::size_t size)
Allocates a memory_block using malloc.
Definition: mallocator.ipp:27
Represents an allocated memory block.