Skip to content

Commit 891a02f

Browse files
Copilotwysaid
andcommitted
Optimize CcapDeviceInfo structure with fixed-size arrays
Co-authored-by: wysaid <1430725+wysaid@users.noreply.github.com>
1 parent ac60aa4 commit 891a02f

3 files changed

Lines changed: 38 additions & 45 deletions

File tree

examples/desktop/0-print_camera_c.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ int main() {
7575
deviceInfo.supportedResolutions[0].width,
7676
deviceInfo.supportedResolutions[0].height);
7777
}
78-
79-
ccap_provider_free_device_info(&deviceInfo);
8078
}
8179

8280
// Set camera properties

include/ccap_c.h

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ typedef enum {
6161
CCAP_PROPERTY_FRAME_ORIENTATION = 0x40000
6262
} CcapPropertyName;
6363

64+
/* ========== Constants ========== */
65+
66+
/** @brief Maximum length for device name including null terminator */
67+
#define CCAP_MAX_DEVICE_NAME_LENGTH 128
68+
69+
/** @brief Maximum number of supported pixel formats */
70+
#define CCAP_MAX_PIXEL_FORMATS 32
71+
72+
/** @brief Maximum number of supported resolutions */
73+
#define CCAP_MAX_RESOLUTIONS 64
74+
6475
/* ========== Data Structures ========== */
6576

6677
/** @brief Video frame data structure for C interface */
@@ -85,11 +96,11 @@ typedef struct {
8596

8697
/** @brief Device information structure */
8798
typedef struct {
88-
char* deviceName; /**< Device name (caller must free) */
89-
CcapPixelFormat* supportedPixelFormats; /**< Array of supported pixel formats (caller must free) */
90-
size_t pixelFormatCount; /**< Number of supported pixel formats */
91-
CcapResolution* supportedResolutions; /**< Array of supported resolutions (caller must free) */
92-
size_t resolutionCount; /**< Number of supported resolutions */
99+
char deviceName[CCAP_MAX_DEVICE_NAME_LENGTH]; /**< Device name */
100+
CcapPixelFormat supportedPixelFormats[CCAP_MAX_PIXEL_FORMATS]; /**< Array of supported pixel formats */
101+
size_t pixelFormatCount; /**< Number of supported pixel formats */
102+
CcapResolution supportedResolutions[CCAP_MAX_RESOLUTIONS]; /**< Array of supported resolutions */
103+
size_t resolutionCount; /**< Number of supported resolutions */
93104
} CcapDeviceInfo;
94105

95106
/** @brief Callback function type for new frame notifications */
@@ -173,17 +184,11 @@ bool ccap_provider_is_opened(const CcapProvider* provider);
173184
/**
174185
* @brief Get device information
175186
* @param provider Pointer to CcapProvider instance
176-
* @param deviceInfo Output parameter for device information (caller must free using ccap_provider_free_device_info)
187+
* @param deviceInfo Output parameter for device information
177188
* @return true on success, false on failure
178189
*/
179190
bool ccap_provider_get_device_info(const CcapProvider* provider, CcapDeviceInfo* deviceInfo);
180191

181-
/**
182-
* @brief Free device info structure
183-
* @param deviceInfo Pointer to device info structure
184-
*/
185-
void ccap_provider_free_device_info(CcapDeviceInfo* deviceInfo);
186-
187192
/**
188193
* @brief Close camera device
189194
* @param provider Pointer to CcapProvider instance

src/ccap_c.cpp

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -190,46 +190,36 @@ bool ccap_provider_get_device_info(const CcapProvider* provider, CcapDeviceInfo*
190190
// Initialize structure
191191
memset(deviceInfo, 0, sizeof(CcapDeviceInfo));
192192

193-
// Copy device name
194-
deviceInfo->deviceName = allocate_c_string(info.deviceName);
193+
// Copy device name (with bounds checking)
194+
size_t deviceNameLen = info.deviceName.size();
195+
if (deviceNameLen >= CCAP_MAX_DEVICE_NAME_LENGTH) {
196+
deviceNameLen = CCAP_MAX_DEVICE_NAME_LENGTH - 1;
197+
}
198+
strncpy(deviceInfo->deviceName, info.deviceName.c_str(), deviceNameLen);
199+
deviceInfo->deviceName[deviceNameLen] = '\0';
195200

196-
// Copy supported pixel formats
201+
// Copy supported pixel formats (with bounds checking)
197202
deviceInfo->pixelFormatCount = info.supportedPixelFormats.size();
198-
if (deviceInfo->pixelFormatCount > 0) {
199-
deviceInfo->supportedPixelFormats = static_cast<CcapPixelFormat*>(
200-
malloc(deviceInfo->pixelFormatCount * sizeof(CcapPixelFormat)));
203+
if (deviceInfo->pixelFormatCount > CCAP_MAX_PIXEL_FORMATS) {
204+
deviceInfo->pixelFormatCount = CCAP_MAX_PIXEL_FORMATS;
205+
}
201206

202-
if (deviceInfo->supportedPixelFormats) {
203-
for (size_t i = 0; i < deviceInfo->pixelFormatCount; ++i) {
204-
deviceInfo->supportedPixelFormats[i] = convert_pixel_format_to_c(info.supportedPixelFormats[i]);
205-
}
206-
}
207+
for (size_t i = 0; i < deviceInfo->pixelFormatCount; ++i) {
208+
deviceInfo->supportedPixelFormats[i] = convert_pixel_format_to_c(info.supportedPixelFormats[i]);
207209
}
208210

209-
// Copy supported resolutions
211+
// Copy supported resolutions (with bounds checking)
210212
deviceInfo->resolutionCount = info.supportedResolutions.size();
211-
if (deviceInfo->resolutionCount > 0) {
212-
deviceInfo->supportedResolutions = static_cast<CcapResolution*>(
213-
malloc(deviceInfo->resolutionCount * sizeof(CcapResolution)));
214-
215-
if (deviceInfo->supportedResolutions) {
216-
for (size_t i = 0; i < deviceInfo->resolutionCount; ++i) {
217-
deviceInfo->supportedResolutions[i].width = info.supportedResolutions[i].width;
218-
deviceInfo->supportedResolutions[i].height = info.supportedResolutions[i].height;
219-
}
220-
}
213+
if (deviceInfo->resolutionCount > CCAP_MAX_RESOLUTIONS) {
214+
deviceInfo->resolutionCount = CCAP_MAX_RESOLUTIONS;
221215
}
222216

223-
return true;
224-
}
225-
226-
void ccap_provider_free_device_info(CcapDeviceInfo* deviceInfo) {
227-
if (deviceInfo) {
228-
free(deviceInfo->deviceName);
229-
free(deviceInfo->supportedPixelFormats);
230-
free(deviceInfo->supportedResolutions);
231-
memset(deviceInfo, 0, sizeof(CcapDeviceInfo));
217+
for (size_t i = 0; i < deviceInfo->resolutionCount; ++i) {
218+
deviceInfo->supportedResolutions[i].width = info.supportedResolutions[i].width;
219+
deviceInfo->supportedResolutions[i].height = info.supportedResolutions[i].height;
232220
}
221+
222+
return true;
233223
}
234224

235225
void ccap_provider_close(CcapProvider* provider) {

0 commit comments

Comments
 (0)