-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathHAPTLVMemory.c
47 lines (40 loc) · 1.34 KB
/
HAPTLVMemory.c
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
// Copyright (c) 2015-2019 The HomeKit ADK Contributors
//
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// See [CONTRIBUTORS.md] for the list of HomeKit ADK project authors.
#include "HAP+Internal.h"
void* _Nullable HAPTLVScratchBufferAlloc(
void* _Nonnull* _Nonnull scratchBytes,
size_t* maxScratchBytes,
size_t numBytes) {
if (!scratchBytes || !*scratchBytes || !maxScratchBytes || !*maxScratchBytes) {
return NULL;
}
uint8_t* bytes = *scratchBytes;
HAP_DIAGNOSTIC_PUSH
HAP_DIAGNOSTIC_IGNORED_MSVC(4146)
uintptr_t o = (uintptr_t)(-(uintptr_t) bytes & 0x03);
if (*maxScratchBytes < numBytes + o) {
return NULL;
}
HAP_DIAGNOSTIC_POP
*scratchBytes = &bytes[o + numBytes];
*maxScratchBytes -= o + numBytes;
return &bytes[o];
}
void* _Nullable HAPTLVScratchBufferAllocUnaligned(
void* _Nonnull* _Nonnull scratchBytes,
size_t* numScratchBytes,
size_t numBytes) {
if (!scratchBytes || !*scratchBytes || !numScratchBytes || !*numScratchBytes) {
return NULL;
}
uint8_t* bytes = *scratchBytes;
if (*numScratchBytes < numBytes) {
return NULL;
}
*scratchBytes = &bytes[numBytes];
*numScratchBytes -= numBytes;
return bytes;
}