Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
| 4 | |
| 5 | #include "wpi/RawFrame.h" |
| 6 | |
| 7 | #include <wpi/MemAlloc.h> |
| 8 | |
| 9 | #include <cstring> |
| 10 | |
| 11 | extern "C" { |
| 12 | int WPI_AllocateRawFrameData(WPI_RawFrame* frame, size_t requestedSize) { |
| 13 | if (frame->capacity >= requestedSize) { |
| 14 | return 0; |
| 15 | } |
| 16 | WPI_FreeRawFrameData(frame); |
| 17 | frame->data = static_cast<uint8_t*>(wpi::safe_malloc(requestedSize)); |
| 18 | frame->capacity = requestedSize; |
| 19 | frame->size = 0; |
| 20 | return 1; |
| 21 | } |
| 22 | |
| 23 | void WPI_FreeRawFrameData(WPI_RawFrame* frame) { |
| 24 | if (frame->data) { |
| 25 | if (frame->freeFunc) { |
| 26 | frame->freeFunc(frame->freeCbData, frame->data, frame->capacity); |
| 27 | } else { |
| 28 | std::free(frame->data); |
| 29 | } |
| 30 | frame->data = nullptr; |
| 31 | frame->freeFunc = nullptr; |
| 32 | frame->freeCbData = nullptr; |
| 33 | frame->capacity = 0; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void WPI_SetRawFrameData(WPI_RawFrame* frame, void* data, size_t size, |
| 38 | size_t capacity, void* cbdata, |
| 39 | void (*freeFunc)(void* cbdata, void* data, |
| 40 | size_t capacity)) { |
| 41 | WPI_FreeRawFrameData(frame); |
| 42 | frame->data = static_cast<uint8_t*>(data); |
| 43 | frame->freeFunc = freeFunc; |
| 44 | frame->freeCbData = cbdata; |
| 45 | frame->capacity = capacity; |
| 46 | frame->size = size; |
| 47 | } |
| 48 | |
| 49 | } // extern "C" |