Skip to content

Commit 398a48a

Browse files
committed
Memory management improvments.
1 parent 3e7147f commit 398a48a

11 files changed

Lines changed: 654 additions & 43 deletions

File tree

libs/server-sdk/include/launchdarkly/server_side/bindings/c/evaluation_series_data.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
* - Data returned from hook callbacks transfers ownership to the SDK
1313
* - Data received in callbacks can be modified and returned (ownership transfer)
1414
* - Keys are copied by the SDK
15-
* - Values retrieved with GetValue() are heap-allocated copies that must be
16-
* freed by the caller using LDValue_Free()
15+
* - Values retrieved with GetValue() are temporary - valid only during callback
16+
* - Do not call LDValue_Free() on values retrieved with GetValue()
1717
* - Values stored with SetValue() are copied into the data object
1818
* - Pointers (void*) lifetime is managed by the application
1919
*
@@ -53,26 +53,25 @@ LDEvaluationSeriesData_New(void);
5353
/**
5454
* @brief Get a Value from the evaluation series data.
5555
*
56-
* OWNERSHIP: Returns a heap-allocated copy of the value. The caller is
57-
* responsible for freeing it with LDValue_Free().
56+
* LIFETIME: Returns a temporary value valid only during the callback.
57+
* Do not call LDValue_Free() on the returned value.
5858
*
5959
* USAGE:
6060
* @code
6161
* LDValue value;
6262
* if (LDEvaluationSeriesData_GetValue(data, "timestamp", &value)) {
63-
* // Use value
63+
* // Use value (valid only during callback)
6464
* double timestamp = LDValue_GetNumber(value);
65-
* // Must free when done
66-
* LDValue_Free(value);
65+
* // Do NOT call LDValue_Free(value)
6766
* }
6867
* @endcode
6968
*
7069
* @param data Data object. Must not be NULL.
7170
* @param key Key to look up. Must be null-terminated UTF-8 string.
7271
* Must not be NULL.
7372
* @param out_value Pointer to receive the value. Must not be NULL.
74-
* Set to a heap-allocated LDValue that must be freed with
75-
* LDValue_Free() when no longer needed.
73+
* Set to a temporary LDValue (valid only during callback).
74+
* Do not call LDValue_Free() on this value.
7675
* @return true if key was found and contains a Value, false otherwise.
7776
*/
7877
LD_EXPORT(bool)

libs/server-sdk/include/launchdarkly/server_side/bindings/c/sdk.h

Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <launchdarkly/server_side/bindings/c/all_flags_state/all_flags_state.h>
88
#include <launchdarkly/server_side/bindings/c/config/builder.h>
9+
#include <launchdarkly/server_side/bindings/c/hook_context.h>
910

1011
#include <launchdarkly/bindings/c/context.h>
1112
#include <launchdarkly/bindings/c/data/evaluation_detail.h>
@@ -430,6 +431,304 @@ LDServerSDK_JsonVariationDetail(LDServerSDK sdk,
430431
LDValue default_value,
431432
LDEvalDetail* out_detail);
432433

434+
/**
435+
* Tracks that the given context performed an event with the given event name,
436+
* passing a hook context for custom hook data.
437+
*
438+
* @code
439+
* LDHookContext hook_ctx = LDHookContext_New();
440+
* // Set custom data in hook_ctx...
441+
* LDServerSDK_TrackEvent_WithHookContext(sdk, context, "my-event", hook_ctx);
442+
* LDHookContext_Free(hook_ctx);
443+
* @endcode
444+
*
445+
* @param sdk SDK. Must not be NULL.
446+
* @param context The context. Ownership is NOT transferred. Must not be NULL.
447+
* @param event_name Name of the event. Must not be NULL.
448+
* @param hook_context Hook context for passing data to hooks. Ownership is NOT
449+
* transferred. May be NULL.
450+
*/
451+
LD_EXPORT(void)
452+
LDServerSDK_TrackEvent_WithHookContext(LDServerSDK sdk,
453+
LDContext context,
454+
char const* event_name,
455+
LDHookContext hook_context);
456+
457+
/**
458+
* Tracks that the given context performed an event with the given event
459+
* name, associates it with a numeric metric and value, and passes a hook
460+
* context for custom hook data.
461+
*
462+
* @param sdk SDK. Must not be NULL.
463+
* @param context The context. Ownership is NOT transferred. Must not be NULL.
464+
* @param event_name The name of the event. Must not be NULL.
465+
* @param metric_value This value is used by the LaunchDarkly experimentation
466+
* feature in numeric custom metrics, and will also be returned as part of the
467+
* custom event for Data Export.
468+
* @param data A JSON value containing additional data associated with the
469+
* event. Ownership is transferred into the SDK. Must not be NULL.
470+
* @param hook_context Hook context for passing data to hooks. Ownership is NOT
471+
* transferred. May be NULL.
472+
*/
473+
LD_EXPORT(void)
474+
LDServerSDK_TrackMetric_WithHookContext(LDServerSDK sdk,
475+
LDContext context,
476+
char const* event_name,
477+
double metric_value,
478+
LDValue data,
479+
LDHookContext hook_context);
480+
481+
/**
482+
* Tracks that the given context performed an event with the given event
483+
* name, with additional JSON data, and passes a hook context for custom
484+
* hook data.
485+
*
486+
* @param sdk SDK. Must not be NULL.
487+
* @param context The context. Ownership is NOT transferred. Must not be NULL.
488+
* @param event_name The name of the event. Must not be NULL.
489+
* @param data A JSON value containing additional data associated with the
490+
* event. Ownership is transferred. Must not be NULL.
491+
* @param hook_context Hook context for passing data to hooks. Ownership is NOT
492+
* transferred. May be NULL.
493+
*/
494+
LD_EXPORT(void)
495+
LDServerSDK_TrackData_WithHookContext(LDServerSDK sdk,
496+
LDContext context,
497+
char const* event_name,
498+
LDValue data,
499+
LDHookContext hook_context);
500+
501+
/**
502+
* Returns the boolean value of a feature flag for a given flag key and context,
503+
* passing a hook context for custom hook data.
504+
*
505+
* @param sdk SDK. Must not be NULL.
506+
* @param context The context. Ownership is NOT transferred. Must not be NULL.
507+
* @param flag_key The unique key for the feature flag. Must not be NULL.
508+
* @param default_value The default value of the flag.
509+
* @param hook_context Hook context for passing data to hooks. Ownership is NOT
510+
* transferred. May be NULL.
511+
* @return The variation for the given context, or default_value if the
512+
* flag is disabled in the LaunchDarkly control panel.
513+
*/
514+
LD_EXPORT(bool)
515+
LDServerSDK_BoolVariation_WithHookContext(LDServerSDK sdk,
516+
LDContext context,
517+
char const* flag_key,
518+
bool default_value,
519+
LDHookContext hook_context);
520+
521+
/**
522+
* Returns the boolean value of a feature flag for a given flag key and context,
523+
* details that describe the way the value was determined, and passes a hook
524+
* context for custom hook data.
525+
*
526+
* @param sdk SDK. Must not be NULL.
527+
* @param context The context. Ownership is NOT transferred. Must not be NULL.
528+
* @param flag_key The unique key for the feature flag. Must not be NULL.
529+
* @param default_value The default value of the flag.
530+
* @param hook_context Hook context for passing data to hooks. Ownership is NOT
531+
* transferred. May be NULL.
532+
* @param out_detail Pointer to evaluation detail. Must not be NULL. The value
533+
* written to the pointer must be freed with LDEvalDetail_Free when no longer
534+
* needed.
535+
* @return The variation for the given context, or default_value if the
536+
* flag is disabled in the LaunchDarkly control panel.
537+
*/
538+
LD_EXPORT(bool)
539+
LDServerSDK_BoolVariationDetail_WithHookContext(LDServerSDK sdk,
540+
LDContext context,
541+
char const* flag_key,
542+
bool default_value,
543+
LDHookContext hook_context,
544+
LDEvalDetail* out_detail);
545+
546+
/**
547+
* Returns the string value of a feature flag for a given flag key and context,
548+
* passing a hook context for custom hook data.
549+
*
550+
* @param sdk SDK. Must not be NULL.
551+
* @param context The context. Ownership is NOT transferred. Must not be NULL.
552+
* @param flag_key The unique key for the feature flag. Must not be NULL.
553+
* @param default_value The default value of the flag. Must not be NULL.
554+
* @param hook_context Hook context for passing data to hooks. Ownership is NOT
555+
* transferred. May be NULL.
556+
* @return The variation for the given context, or default_value if the
557+
* flag is disabled in the LaunchDarkly control panel. Must be freed with
558+
* LDMemory_FreeString when no longer needed.
559+
*/
560+
LD_EXPORT(char*)
561+
LDServerSDK_StringVariation_WithHookContext(LDServerSDK sdk,
562+
LDContext context,
563+
char const* flag_key,
564+
char const* default_value,
565+
LDHookContext hook_context);
566+
567+
/**
568+
* Returns the string value of a feature flag for a given flag key and context,
569+
* details that describe the way the value was determined, and passes a hook
570+
* context for custom hook data.
571+
*
572+
* @param sdk SDK. Must not be NULL.
573+
* @param context The context. Ownership is NOT transferred. Must not be NULL.
574+
* @param flag_key The unique key for the feature flag. Must not be NULL.
575+
* @param default_value The default value of the flag. Must not be NULL.
576+
* @param hook_context Hook context for passing data to hooks. Ownership is NOT
577+
* transferred. May be NULL.
578+
* @param out_detail Pointer to evaluation detail. Must not be NULL. The value
579+
* written to the pointer must be freed with LDEvalDetail_Free when no longer
580+
* needed.
581+
* @return The variation for the given context, or default_value if the
582+
* flag is disabled in the LaunchDarkly control panel. Must be freed with
583+
* LDMemory_FreeString when no longer needed.
584+
*/
585+
LD_EXPORT(char*)
586+
LDServerSDK_StringVariationDetail_WithHookContext(LDServerSDK sdk,
587+
LDContext context,
588+
char const* flag_key,
589+
char const* default_value,
590+
LDHookContext hook_context,
591+
LDEvalDetail* out_detail);
592+
593+
/**
594+
* Returns the integer value of a feature flag for a given flag key and context,
595+
* passing a hook context for custom hook data.
596+
*
597+
* @param sdk SDK. Must not be NULL.
598+
* @param context The context. Ownership is NOT transferred. Must not be NULL.
599+
* @param flag_key The unique key for the feature flag. Must not be NULL.
600+
* @param default_value The default value of the flag.
601+
* @param hook_context Hook context for passing data to hooks. Ownership is NOT
602+
* transferred. May be NULL.
603+
* @return The variation for the given context, or default_value if the
604+
* flag is disabled in the LaunchDarkly control panel.
605+
*/
606+
LD_EXPORT(int)
607+
LDServerSDK_IntVariation_WithHookContext(LDServerSDK sdk,
608+
LDContext context,
609+
char const* flag_key,
610+
int default_value,
611+
LDHookContext hook_context);
612+
613+
/**
614+
* Returns the integer value of a feature flag for a given flag key and context,
615+
* details that describe the way the value was determined, and passes a hook
616+
* context for custom hook data.
617+
*
618+
* @param sdk SDK. Must not be NULL.
619+
* @param context The context. Ownership is NOT transferred. Must not be NULL.
620+
* @param flag_key The unique key for the feature flag. Must not be NULL.
621+
* @param default_value The default value of the flag.
622+
* @param hook_context Hook context for passing data to hooks. Ownership is NOT
623+
* transferred. May be NULL.
624+
* @param out_detail Pointer to evaluation detail. Must not be NULL. The value
625+
* written to the pointer must be freed with LDEvalDetail_Free when no longer
626+
* needed.
627+
* @return The variation for the given context, or default_value if the
628+
* flag is disabled in the LaunchDarkly control panel.
629+
*/
630+
LD_EXPORT(int)
631+
LDServerSDK_IntVariationDetail_WithHookContext(LDServerSDK sdk,
632+
LDContext context,
633+
char const* flag_key,
634+
int default_value,
635+
LDHookContext hook_context,
636+
LDEvalDetail* out_detail);
637+
638+
/**
639+
* Returns the double value of a feature flag for a given flag key and context,
640+
* passing a hook context for custom hook data.
641+
*
642+
* @param sdk SDK. Must not be NULL.
643+
* @param context The context. Ownership is NOT transferred. Must not be NULL.
644+
* @param flag_key The unique key for the feature flag. Must not be NULL.
645+
* @param default_value The default value of the flag.
646+
* @param hook_context Hook context for passing data to hooks. Ownership is NOT
647+
* transferred. May be NULL.
648+
* @return The variation for the given context, or default_value if the
649+
* flag is disabled in the LaunchDarkly control panel.
650+
*/
651+
LD_EXPORT(double)
652+
LDServerSDK_DoubleVariation_WithHookContext(LDServerSDK sdk,
653+
LDContext context,
654+
char const* flag_key,
655+
double default_value,
656+
LDHookContext hook_context);
657+
658+
/**
659+
* Returns the double value of a feature flag for a given flag key and context,
660+
* details that describe the way the value was determined, and passes a hook
661+
* context for custom hook data.
662+
*
663+
* @param sdk SDK. Must not be NULL.
664+
* @param context The context. Ownership is NOT transferred. Must not be NULL.
665+
* @param flag_key The unique key for the feature flag. Must not be NULL.
666+
* @param default_value The default value of the flag.
667+
* @param hook_context Hook context for passing data to hooks. Ownership is NOT
668+
* transferred. May be NULL.
669+
* @param out_detail Pointer to evaluation detail. Must not be NULL. The value
670+
* written to the pointer must be freed with LDEvalDetail_Free when no longer
671+
* needed.
672+
* @return The variation for the given context, or default_value if the
673+
* flag is disabled in the LaunchDarkly control panel.
674+
*/
675+
LD_EXPORT(double)
676+
LDServerSDK_DoubleVariationDetail_WithHookContext(LDServerSDK sdk,
677+
LDContext context,
678+
char const* flag_key,
679+
double default_value,
680+
LDHookContext hook_context,
681+
LDEvalDetail* out_detail);
682+
683+
/**
684+
* Returns the JSON value of a feature flag for a given flag key and context,
685+
* passing a hook context for custom hook data.
686+
*
687+
* @param sdk SDK. Must not be NULL.
688+
* @param context The context. Ownership is NOT transferred. Must not be NULL.
689+
* @param flag_key The unique key for the feature flag. Must not be NULL.
690+
* @param default_value The default value of the flag. Ownership is NOT
691+
* transferred. Must not be NULL.
692+
* @param hook_context Hook context for passing data to hooks. Ownership is NOT
693+
* transferred. May be NULL.
694+
* @return The variation for the given context, or default_value if the
695+
* flag is disabled in the LaunchDarkly control panel. Must be freed with
696+
* LDValue_Free when no longer needed.
697+
*/
698+
LD_EXPORT(LDValue)
699+
LDServerSDK_JsonVariation_WithHookContext(LDServerSDK sdk,
700+
LDContext context,
701+
char const* flag_key,
702+
LDValue default_value,
703+
LDHookContext hook_context);
704+
705+
/**
706+
* Returns the JSON value of a feature flag for a given flag key and context,
707+
* details that describe the way the value was determined, and passes a hook
708+
* context for custom hook data.
709+
*
710+
* @param sdk SDK. Must not be NULL.
711+
* @param context The context. Ownership is NOT transferred. Must not be NULL.
712+
* @param flag_key The unique key for the feature flag. Must not be NULL.
713+
* @param default_value The default value of the flag. Ownership is NOT
714+
* transferred. Must not be NULL.
715+
* @param hook_context Hook context for passing data to hooks. Ownership is NOT
716+
* transferred. May be NULL.
717+
* @param out_detail Pointer to evaluation detail. Must not be NULL. The value
718+
* written to the pointer must be freed with LDEvalDetail_Free when no longer
719+
* needed.
720+
* @return The variation for the given context, or default_value if the
721+
* flag is disabled in the LaunchDarkly control panel. Must be freed with
722+
* LDValue_Free when no longer needed.
723+
*/
724+
LD_EXPORT(LDValue)
725+
LDServerSDK_JsonVariationDetail_WithHookContext(LDServerSDK sdk,
726+
LDContext context,
727+
char const* flag_key,
728+
LDValue default_value,
729+
LDHookContext hook_context,
730+
LDEvalDetail* out_detail);
731+
433732
/**
434733
* Evaluates all flags for a context, returning a data structure containing
435734
* the results and additional flag metadata.

0 commit comments

Comments
 (0)