-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSafeMemSet.cpp
More file actions
40 lines (33 loc) · 823 Bytes
/
SafeMemSet.cpp
File metadata and controls
40 lines (33 loc) · 823 Bytes
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
/**
* @file SafeMemSet.cpp
* @author Andrew Spaulding (Kasplat)
* @brief Implementation of the SafeMemSet function.
* @bug No known bugs.
*/
#include "SafeMemSet.h"
#include <cstdint>
#include "SafeWrite.h"
#include "common/IErrors.h"
/// @brief Gets the min of two numbers.
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
/**
* @brief Sets the given memory region to the given byte value.
*/
void
SafeMemSet(
uintptr_t a,
uint8_t c,
size_t n
) {
if (!n) { return; }
// We do the SafeWriteBuf operations in bulk.
const size_t buf_size = 256;
uint8_t buf[buf_size];
memset(buf, c, MIN(buf_size, n));
while (n) {
size_t chunk = MIN(buf_size, n);
SafeWriteBuf(a, buf, chunk);
a += chunk;
n -= chunk;
}
}