@@ -732,29 +732,38 @@ kdump_clear_attr(kdump_ctx_t *ctx, const char *key)
732
732
* @param valp Value (filled on successful return).
733
733
* @returns Error status.
734
734
*
735
- * Note that the caller does not hold a reference to the attribute, so
736
- * it is not generally safe to use this function in a multi-threaded
737
- * program, or across another library call which modifies the attribute
738
- * (explicitly or implicitly).
735
+ * This function is useful only when the caller can fully control the
736
+ * lifetime of the returned attribute value. In that case, the value
737
+ * is valid only as long as no changes are made to the containing dump
738
+ * file object.
739
+ *
740
+ * Note that attribute data (or the attribute hierarchy itself) may
741
+ * change as a side effect of another operation. Merely reading dump
742
+ * data or attributes will never invalidate the attribute value, i.e.
743
+ * it is safe to dereference pointers. However, the returned value may
744
+ * become stale, for example cache statistics.
745
+ *
746
+ * A more robust API is provided by @ref kdump_attr_ref_get.
739
747
*/
740
748
kdump_status kdump_get_attr (kdump_ctx_t * ctx , const char * key ,
741
749
kdump_attr_t * valp );
742
750
743
- /** Get a dump file attribute, checking its type.
751
+ /** Get a dump file attribute with a given type.
744
752
* @param ctx Dump file object.
745
753
* @param key Attribute key.
754
+ * @param type Expected attribute type.
746
755
* @param valp Value (updated on return).
747
756
* @returns Error status.
748
757
*
749
- * The @c type field in @c valp must be set to the expected attribute
750
- * type by the caller. It is an error if the attribute is of a different
751
- * type, but @c valp is updated to its value anyway.
758
+ * If the attribute is of a different type, this function returns
759
+ * KDUMP_ERR_INVALID and does not change @p valp.
752
760
*
753
- * Note that the caller does not hold a reference to the attribute . See
754
- * the description of @ref kdump_get_attr for limitations .
761
+ * The caller must ensure the lifetime of the returned data is valid . See
762
+ * the description of @ref kdump_get_attr for more details .
755
763
*/
756
764
kdump_status kdump_get_typed_attr (kdump_ctx_t * ctx , const char * key ,
757
- kdump_attr_t * valp );
765
+ kdump_attr_type_t type ,
766
+ kdump_attr_value_t * valp );
758
767
759
768
/** Get a numeric attribute.
760
769
*
@@ -766,14 +775,8 @@ kdump_status kdump_get_typed_attr(kdump_ctx_t *ctx, const char *key,
766
775
static inline kdump_status
767
776
kdump_get_number_attr (kdump_ctx_t * ctx , const char * key , kdump_num_t * num )
768
777
{
769
- kdump_attr_t attr ;
770
- kdump_status ret ;
771
-
772
- attr .type = KDUMP_NUMBER ;
773
- ret = kdump_get_typed_attr (ctx , key , & attr );
774
- if (ret == KDUMP_OK )
775
- * num = attr .val .number ;
776
- return ret ;
778
+ return kdump_get_typed_attr (ctx , key , KDUMP_NUMBER ,
779
+ (kdump_attr_value_t * )num );
777
780
}
778
781
779
782
/** Get an address attribute.
@@ -786,14 +789,8 @@ kdump_get_number_attr(kdump_ctx_t *ctx, const char *key, kdump_num_t *num)
786
789
static inline kdump_status
787
790
kdump_get_address_attr (kdump_ctx_t * ctx , const char * key , kdump_addr_t * addr )
788
791
{
789
- kdump_attr_t attr ;
790
- kdump_status ret ;
791
-
792
- attr .type = KDUMP_ADDRESS ;
793
- ret = kdump_get_typed_attr (ctx , key , & attr );
794
- if (ret == KDUMP_OK )
795
- * addr = attr .val .address ;
796
- return ret ;
792
+ return kdump_get_typed_attr (ctx , key , KDUMP_ADDRESS ,
793
+ (kdump_attr_value_t * )addr );
797
794
}
798
795
799
796
/** Get a string attribute.
@@ -803,20 +800,14 @@ kdump_get_address_attr(kdump_ctx_t *ctx, const char *key, kdump_addr_t *addr)
803
800
* @param str[out] Filled with the attribute value on successful return.
804
801
* @returns Error status.
805
802
*
806
- * Note that the caller does not hold a reference to the string . See
807
- * the description of @ref kdump_get_attr for limitations .
803
+ * The caller must ensure the lifetime of the returned data is valid . See
804
+ * the description of @ref kdump_get_attr for more details .
808
805
*/
809
806
static inline kdump_status
810
807
kdump_get_string_attr (kdump_ctx_t * ctx , const char * key , const char * * str )
811
808
{
812
- kdump_attr_t attr ;
813
- kdump_status ret ;
814
-
815
- attr .type = KDUMP_STRING ;
816
- ret = kdump_get_typed_attr (ctx , key , & attr );
817
- if (ret == KDUMP_OK )
818
- * str = attr .val .string ;
819
- return ret ;
809
+ return kdump_get_typed_attr (ctx , key , KDUMP_STRING ,
810
+ (kdump_attr_value_t * )str );
820
811
}
821
812
822
813
/** Get a reference to an attribute
@@ -863,14 +854,42 @@ int kdump_attr_ref_isset(kdump_attr_ref_t *ref);
863
854
/** Get attribute data by reference.
864
855
* @param ctx Dump file object.
865
856
* @param[in] ref Attribute reference.
866
- * @param[out] valp Attribute value (filled on successful return).
857
+ * @param[out] valp Attribute value (filled on return).
858
+ * @returns Error status.
867
859
*
868
- * This works just like @ref kdump_get_attr, except that the attribute
869
- * is denoted by a reference rather than by its key path.
860
+ * This works similarly to @ref kdump_get_attr, except:
861
+ * - the attribute is denoted by a reference rather than by path,
862
+ * - the returned data stays valid even if changes are made to the dump
863
+ * object.
864
+ *
865
+ * The returned value may be a dynamically allocated copy of the attribute
866
+ * value and/or it may hold an extra reference to the underlying objects.
867
+ * You should free up these resources with @ref kdump_attr_discard when
868
+ * the data is no longer needed.
869
+ *
870
+ * If the function returns an error, it is not necessary to discard the
871
+ * result. However, the type of @p valp is set to @c KDUMP_NIL, so it is
872
+ * always safe to call @ref kdump_attr_discard on the result. The goal of
873
+ * this feature is to simplify code flow in callers.
870
874
*/
871
875
kdump_status kdump_attr_ref_get (kdump_ctx_t * ctx , const kdump_attr_ref_t * ref ,
872
876
kdump_attr_t * valp );
873
877
878
+ /** Discard attribute data value.
879
+ * @param ctx Dump file object.
880
+ * @param attr Attribute data.
881
+ *
882
+ * Perform any actions necessary to release resources by the attribute data.
883
+ * This function may decrement object references, free dynamically allocated
884
+ * memory, or even do nothing, depending on the data type.
885
+ *
886
+ * Call this function when the data returned by @ref kdump_attr_ref_get is no
887
+ * longer needed. Do *NOT* call this function on attribute data returned by
888
+ * @ref kdump_get_attr.
889
+ */
890
+ void
891
+ kdump_attr_discard (kdump_ctx_t * ctx , kdump_attr_t * attr );
892
+
874
893
/** Set attribute data by reference.
875
894
* @param ctx Dump file object.
876
895
* @param[in] ref Attribute reference.
0 commit comments