-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemlib.h
137 lines (118 loc) · 3.23 KB
/
memlib.h
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
/**
* @file memlib.h
* @brief Support functions to assist with malloclab
*
* This file exists to support the emulated mode of malloclab. Much more
* extensive documentation exists in memlib.c.
*/
#ifndef MEMLIB_H__
#define MEMLIB_H__ 1
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
/**
* @brief
* @param[in] sparse
*/
void mem_init(bool sparse);
/**
* @brief
*/
void mem_deinit(void);
/**
* @brief Extends the heap by incr bytes.
*
* This function is a simple model of the sbrk() function, except for that
* with this implementation, the heap cannot be shrunk.
*
* @param[in] incr The amount of bytes by which to extend the heap
* @return The start address of the new heap area (i.e. the previous
* breakpoint)
* @pre `incr > 0`
*/
void *mem_sbrk(intptr_t incr);
/**
* @brief Resets the simulated brk pointer to make an empty heap.
*/
void mem_reset_brk(void);
/**
* @brief Finds the low address of the heap.
* @return The address of the first valid byte in the heap.
*/
void *mem_heap_lo(void);
/**
* @brief Finds the high address of the heap.
*
* Note that this address may not be aligned: if the heap is 8 bytes large,
* then the value returned will be 7 bytes from the start of the heap.
*
* @return The address of the last valid byte in the heap.
*/
void *mem_heap_hi(void);
/**
* @brief Returns the number of bytes being used by the heap.
* @return The size of the heap, in bytes
*/
size_t mem_heapsize(void);
/**
* @brief Returns the system page size.
* @return The page size of the system, in bytes
*/
size_t mem_pagesize(void);
/* Functions used for memory emulation */
/**
* @brief Performs a simulated memory read at an address.
* @param[in] addr Simulated memory address to read from
* @param[in] len Number of bytes to read
* @return The value read at the address, zero-extended to 64 bits
* @pre `0 <= len && len <= 8`
*/
uint64_t mem_read(const void *addr, size_t len);
/**
* @brief Performs a simulated 128-bit memory read at an address.
* @param[in] addr Simulated memory address to read from
* @return The value read at the address
*/
__int128_t mem_read128(const void *addr);
/**
* @brief Performs a simulated memory write at an address.
* @param[in] addr Simulated memory address to write to
* @param[in] val Value to write (the lower-order `len` bits are used)
* @param[in] len The number of bytes to write
* @pre `0 <= len && len <= 8`
*/
void mem_write(void *addr, uint64_t val, size_t len);
/*
* @brief Performs a simulated 128-bit memory write at an address.
* @param[in] addr Simulated memory address to write to
* @param[in] val Value to write
*/
void mem_write128(void *addr, __int128_t val);
/**
* @brief Emulation of memcpy
* @param[in] dst
* @param[in] src
* @param[in] n
* @return
*/
void *mem_memcpy(void *dst, const void *src, size_t n);
/**
* @brief Emulation of memset
* @param[in] dst
* @param[in] c
* @param[in] n
* @return
*/
void *mem_memset(void *dst, int c, size_t n);
/**
* @brief Debugging function to view region of heap
* @param[in] ptr
* @param[in] offset
* @param[in] count
*/
void hprobe(void *ptr, int offset, size_t count);
/**
* @brief Set whether the driver should check for UB
*/
void setUBCheck(bool);
#endif /* memlib.h */