Skip to content

Prefer dynamic allocations over large STATIC arrays #2146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ option(INSTRUMENTED_ALLOCATORS "Enable memory instrumentation" OFF)
option(ENABLE_AWS_SDK_IN_TESTS "Enable support for compiling AWS SDKs for tests" ON)
option(ENABLE_STATS_CALCULATION_CONTROL "Enable support for runtime control of ice agent stat calculations." OFF)
option(BUILD_OLD_MBEDTLS_VERSION "Use MbedTLS version 2.28.8." OFF)
option(PREFER_DYNAMIC_ALLOCS "Prefer dynamic allocations for signalingpayloads and URLs" OFF)

# Developer Flags
option(BUILD_TEST "Build the testing tree." OFF)
Expand Down Expand Up @@ -147,6 +148,10 @@ if (ENABLE_STATS_CALCULATION_CONTROL)
add_definitions(-DENABLE_STATS_CALCULATION_CONTROL)
endif()

if (PREFER_DYNAMIC_ALLOCS)
add_definitions(-DPREFER_DYNAMIC_ALLOCS=1)
endif()

if(USE_OPENSSL)
add_definitions(-DKVS_USE_OPENSSL)
elseif(USE_MBEDTLS)
Expand Down
36 changes: 35 additions & 1 deletion samples/Common.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ STATUS respondWithAnswer(PSampleStreamingSession pSampleStreamingSession)
SignalingMessage message;
UINT32 buffLen = MAX_SIGNALING_MESSAGE_LEN;

#ifdef DYNAMIC_SIGNALING_PAYLOAD
message.payload = (PCHAR) MEMALLOC(MAX_SIGNALING_MESSAGE_LEN + 1);
CHK(message.payload != NULL, STATUS_NOT_ENOUGH_MEMORY);
#endif
CHK_STATUS(serializeSessionDescriptionInit(&pSampleStreamingSession->answerSessionDescriptionInit, message.payload, &buffLen));

message.version = SIGNALING_MESSAGE_CURRENT_VERSION;
Expand All @@ -293,6 +297,10 @@ STATUS respondWithAnswer(PSampleStreamingSession pSampleStreamingSession)

CleanUp:

#ifdef DYNAMIC_SIGNALING_PAYLOAD
SAFE_MEMFREE(message.payload);
#endif

CHK_LOG_ERR(retStatus);
return retStatus;
}
Expand All @@ -312,7 +320,7 @@ VOID onIceCandidateHandler(UINT64 customData, PCHAR candidateJson)
{
STATUS retStatus = STATUS_SUCCESS;
PSampleStreamingSession pSampleStreamingSession = (PSampleStreamingSession) customData;
SignalingMessage message;
SignalingMessage message = {0};

CHK(pSampleStreamingSession != NULL, STATUS_NULL_ARG);

Expand All @@ -335,13 +343,21 @@ VOID onIceCandidateHandler(UINT64 customData, PCHAR candidateJson)
message.messageType = SIGNALING_MESSAGE_TYPE_ICE_CANDIDATE;
STRNCPY(message.peerClientId, pSampleStreamingSession->peerId, MAX_SIGNALING_CLIENT_ID_LEN);
message.payloadLen = (UINT32) STRNLEN(candidateJson, MAX_SIGNALING_MESSAGE_LEN);
#ifdef DYNAMIC_SIGNALING_PAYLOAD
message.payload = (PCHAR) MEMALLOC(message.payloadLen + 1);
CHK(message.payload != NULL, STATUS_NOT_ENOUGH_MEMORY);
#endif
STRNCPY(message.payload, candidateJson, message.payloadLen);
message.correlationId[0] = '\0';
CHK_STATUS(sendSignalingMessage(pSampleStreamingSession, &message));
}

CleanUp:

#ifdef DYNAMIC_SIGNALING_PAYLOAD
SAFE_MEMFREE(message.payload);
#endif

CHK_LOG_ERR(retStatus);
}

Expand Down Expand Up @@ -1410,6 +1426,9 @@ STATUS submitPendingIceCandidate(PPendingMessageQueue pPendingMessageQueue, PSam
if (pReceivedSignalingMessage->signalingMessage.messageType == SIGNALING_MESSAGE_TYPE_ICE_CANDIDATE) {
CHK_STATUS(handleRemoteCandidate(pSampleStreamingSession, &pReceivedSignalingMessage->signalingMessage));
}
#ifdef DYNAMIC_SIGNALING_PAYLOAD
SAFE_MEMFREE(pReceivedSignalingMessage->signalingMessage.payload);
#endif
SAFE_MEMFREE(pReceivedSignalingMessage);
}
} while (!noPendingSignalingMessageForClient);
Expand All @@ -1418,6 +1437,11 @@ STATUS submitPendingIceCandidate(PPendingMessageQueue pPendingMessageQueue, PSam

CleanUp:

#ifdef DYNAMIC_SIGNALING_PAYLOAD
if (pReceivedSignalingMessage != NULL) {
SAFE_MEMFREE(pReceivedSignalingMessage->signalingMessage.payload);
}
#endif
SAFE_MEMFREE(pReceivedSignalingMessage);
CHK_LOG_ERR(retStatus);
return retStatus;
Expand Down Expand Up @@ -1538,6 +1562,11 @@ STATUS signalingMessageReceived(UINT64 customData, PReceivedSignalingMessage pRe
pReceivedSignalingMessageCopy = (PReceivedSignalingMessage) MEMCALLOC(1, SIZEOF(ReceivedSignalingMessage));

*pReceivedSignalingMessageCopy = *pReceivedSignalingMessage;
#ifdef DYNAMIC_SIGNALING_PAYLOAD
pReceivedSignalingMessageCopy->signalingMessage.payload = (PCHAR) MEMALLOC(pReceivedSignalingMessage->signalingMessage.payloadLen + 1);
CHK(pReceivedSignalingMessageCopy->signalingMessage.payload != NULL, STATUS_NOT_ENOUGH_MEMORY);
STRNCPY(pReceivedSignalingMessageCopy->signalingMessage.payload, pReceivedSignalingMessage->signalingMessage.payload, pReceivedSignalingMessage->signalingMessage.payloadLen + 1);
#endif

CHK_STATUS(stackQueueEnqueue(pPendingMessageQueue->messageQueue, (UINT64) pReceivedSignalingMessageCopy));

Expand Down Expand Up @@ -1571,6 +1600,11 @@ STATUS signalingMessageReceived(UINT64 customData, PReceivedSignalingMessage pRe

CleanUp:

#ifdef DYNAMIC_SIGNALING_PAYLOAD
if (pReceivedSignalingMessage != NULL) {
SAFE_MEMFREE(pReceivedSignalingMessage->signalingMessage.payload);
}
#endif
SAFE_MEMFREE(pReceivedSignalingMessageCopy);
if (pPendingMessageQueue != NULL) {
freeMessageQueue(pPendingMessageQueue);
Expand Down
19 changes: 19 additions & 0 deletions src/include/com/amazonaws/kinesis/video/webrtcclient/Include.h
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,21 @@ typedef struct {
//!<
} RtcIceCandidateInit, *PRtcIceCandidateInit;

/**
* @brief Define this macro to use dynamically allocated payload in SignalingMessage
* This can be useful for platforms with limited memory as it avoids allocating
* MAX_SIGNALING_MESSAGE_LEN for each message when only a small payload is needed
*/

/**
* @brief If PREFER_DYNAMIC_ALLOCS is set to 1, use dynamic allocation for signaling payload
* Otherwise, use the existing DYNAMIC_SIGNALING_PAYLOAD setting
*/
#if PREFER_DYNAMIC_ALLOCS
#define DYNAMIC_SIGNALING_PAYLOAD 1
#define USE_DYNAMIC_URL 1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Remove this! Not used anymore.

#endif

/**
* @brief Structure defining the basic signaling message
*/
Expand All @@ -1293,7 +1308,11 @@ typedef struct {

UINT32 payloadLen; //!< Optional payload length. If 0, the length will be calculated

#ifdef DYNAMIC_SIGNALING_PAYLOAD
PCHAR payload; //!< Actual signaling message payload - dynamically allocated
#else
CHAR payload[MAX_SIGNALING_MESSAGE_LEN + 1]; //!< Actual signaling message payload
#endif
} SignalingMessage, *PSignalingMessage;

/**
Expand Down
Loading