include
new_delete.h
Go to the documentation of this file.
1
//
2
// Bareflank Hypervisor
3
//
4
// Copyright (C) 2015 Assured Information Security, Inc.
5
// Author: Rian Quinn <quinnr@ainfosec.com>
6
// Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7
//
8
// This library is free software; you can redistribute it and/or
9
// modify it under the terms of the GNU Lesser General Public
10
// License as published by the Free Software Foundation; either
11
// version 2.1 of the License, or (at your option) any later version.
12
//
13
// This library is distributed in the hope that it will be useful,
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
// Lesser General Public License for more details.
17
//
18
// You should have received a copy of the GNU Lesser General Public
19
// License along with this library; if not, write to the Free Software
20
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
22
#ifndef NEW_DELETE_H
23
#define NEW_DELETE_H
24
25
#pragma GCC system_header
26
27
// Note:
28
//
29
// This file provides a generic replacement for new / delete for unit testing
30
// so that things like bad_alloc and aligned memory can be unit tested. This
31
// file can only be included once in a unit test, and it should be included
32
// as a system header to prevent scanners from getting upset that we are using
33
// malloc / free which is exactly what the standard libraries are doing.
34
35
#include <stdlib.h>
36
#include <stddef.h>
37
#include <exception>
38
39
size_t
g_new_throws_bad_alloc
= 0;
40
41
static
void
*
42
malloc_aligned(std::size_t
size
)
43
{
44
int
ret = 0;
45
void
*ptr =
nullptr
;
46
47
ret =
posix_memalign
(&ptr,
MAX_PAGE_SIZE
, size);
48
(void) ret;
49
50
return
ptr;
51
}
52
53
static
void
*
54
custom_new(std::size_t
size
)
55
{
56
if
(size ==
g_new_throws_bad_alloc
|| size == 0xFFFFFFFFFFFFFFFF)
57
throw
std::bad_alloc();
58
59
if
((size & (
MAX_PAGE_SIZE
- 1)) == 0)
60
return
malloc_aligned(size);
61
62
return
malloc
(size);
63
}
64
65
static
void
66
custom_delete(
void
*ptr)
67
{
free
(ptr); }
68
69
void
*
70
operator
new
[](std::size_t
size
)
71
{
return
custom_new(size); }
72
73
void
*
74
operator
new
(std::size_t
size
)
75
{
return
custom_new(size); }
76
77
void
78
operator
delete
(
void
*ptr, std::size_t
/* size */
)
throw
()
79
{ custom_delete(ptr); }
80
81
void
82
operator
delete
(
void
*ptr)
throw
()
83
{ custom_delete(ptr); }
84
85
void
86
operator
delete
[](
void
*ptr)
throw
()
87
{ custom_delete(ptr); }
88
89
void
90
operator
delete
[](
void
*ptr, std::size_t
/* size */
)
throw
()
91
{ custom_delete(ptr); }
92
93
#endif
posix_memalign
int posix_memalign(void **memptr, size_t alignment, size_t size)
Definition:
syscall.cpp:305
free
void free(void *ptr)
Definition:
syscall.cpp:370
malloc
void * malloc(size_t size)
Definition:
syscall.cpp:364
x64::page_table::pml4::size
constexpr const auto size
Definition:
page_table_entry_x64.h:46
MAX_PAGE_SIZE
#define MAX_PAGE_SIZE
Definition:
constants.h:116
g_new_throws_bad_alloc
size_t g_new_throws_bad_alloc
Definition:
new_delete.h:39
Generated on Fri Apr 28 2017 22:12:08 by
1.8.14