uLib
ustream_base.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 
4 #ifndef AZ_USTREAM_BASE_H
5 #define AZ_USTREAM_BASE_H
6 
318 #include "ulib_config.h"
319 #include "ulib_result.h"
320 #include "az_pal_os.h"
321 #include "azure_macro_utils/macro_utils.h"
322 #include "umock_c/umock_c_prod.h"
323 
324 #ifdef __cplusplus
325 #include <cstdint>
326 #include <cstddef>
327 extern "C" {
328 #else
329 #include <stdint.h>
330 #include <stddef.h>
331 #endif /* __cplusplus */
332 
336 typedef size_t offset_t;
337 
341 typedef struct AZ_USTREAM_TAG AZ_USTREAM;
342 
352 {
353  AZ_ULIB_RESULT(*set_position)(AZ_USTREAM* ustream_instance, offset_t position);
354  AZ_ULIB_RESULT(*reset)(AZ_USTREAM* ustream_instance);
355  AZ_ULIB_RESULT(*read)(AZ_USTREAM* ustream_instance, uint8_t* const buffer,
356  size_t buffer_length, size_t* const size);
357  AZ_ULIB_RESULT(*get_remaining_size)(AZ_USTREAM* ustream_instance, size_t* const size);
358  AZ_ULIB_RESULT(*get_position)(AZ_USTREAM* ustream_instance, offset_t* const position);
359  AZ_ULIB_RESULT(*release)(AZ_USTREAM* ustream_instance, offset_t position);
360  AZ_ULIB_RESULT(*clone)(AZ_USTREAM* ustream_instance_clone,
361  AZ_USTREAM* ustream_instance, offset_t offset);
362  AZ_ULIB_RESULT(*dispose)(AZ_USTREAM* ustream_instance);
364 
372 typedef void (*AZ_RELEASE_CALLBACK)(void*);
373 
381 typedef void* AZ_USTREAM_DATA;
382 
393 {
400  volatile uint32_t ref_count;
407 
423 {
424  /* Control Block */
427  /* Instance controls */
431  size_t length;
432 };
433 
448 {
452  volatile uint32_t ustream_one_ref_count;
453  volatile uint32_t ustream_two_ref_count;
456 
463 #define AZ_USTREAM_IS_NOT_TYPE_OF(handle, type_api) ((handle == NULL) || (handle->control_block == NULL) || (handle->control_block->api == NULL) || (handle->control_block->api != &type_api))
464 
499 static inline AZ_ULIB_RESULT az_ustream_set_position(AZ_USTREAM* ustream_instance, offset_t position)
500 {
501  return ustream_instance->control_block->api->set_position(ustream_instance, position);
502 }
503 
538 static inline AZ_ULIB_RESULT az_ustream_reset(AZ_USTREAM* ustream_instance)
539 {
540  return ustream_instance->control_block->api->reset(ustream_instance);
541 }
542 
595 static inline AZ_ULIB_RESULT az_ustream_read(AZ_USTREAM* ustream_instance, uint8_t* const buffer, size_t buffer_length, size_t* const size)
596 {
597  return ustream_instance->control_block->api->read(ustream_instance, buffer, buffer_length, size);
598 }
599 
631 static inline AZ_ULIB_RESULT az_ustream_get_remaining_size(AZ_USTREAM* ustream_instance, size_t* const size)
632 {
633  return ustream_instance->control_block->api->get_remaining_size(ustream_instance, size);
634 }
635 
667 static inline AZ_ULIB_RESULT az_ustream_get_position(AZ_USTREAM* ustream_instance, offset_t* const position)
668 {
669  return ustream_instance->control_block->api->get_position(ustream_instance, position);
670 }
671 
716 static inline AZ_ULIB_RESULT az_ustream_release(AZ_USTREAM* ustream_instance, offset_t position)
717 {
718  return ustream_instance->control_block->api->release(ustream_instance, position);
719 }
720 
844 static inline AZ_ULIB_RESULT az_ustream_clone(AZ_USTREAM* ustream_instance_clone, AZ_USTREAM* ustream_instance, offset_t offset)
845 {
846  return ustream_instance->control_block->api->clone(ustream_instance_clone, ustream_instance, offset);
847 }
848 
872 static inline AZ_ULIB_RESULT az_ustream_dispose(AZ_USTREAM* ustream_instance)
873 {
874  return ustream_instance->control_block->api->dispose(ustream_instance);
875 }
876 
877 
878 #ifdef __cplusplus
879 }
880 #endif /* __cplusplus */
881 
882 #endif /* AZ_USTREAM_BASE_H */
Structure for data control block.
Definition: ustream_base.h:392
Structure to keep track of concatenated ustreams.
Definition: ustream_base.h:447
static AZ_ULIB_RESULT az_ustream_clone(AZ_USTREAM *ustream_instance_clone, AZ_USTREAM *ustream_instance, offset_t offset)
Initializes a new instance of the ustream and returns it.
Definition: ustream_base.h:844
struct AZ_USTREAM_DATA_CB_TAG AZ_USTREAM_DATA_CB
Structure for data control block.
struct AZ_USTREAM_MULTI_DATA_CB_TAG AZ_USTREAM_MULTI_DATA_CB
Structure to keep track of concatenated ustreams.
static AZ_ULIB_RESULT az_ustream_release(AZ_USTREAM *ustream_instance, offset_t position)
Releases all the resources related to the Data Source before and including the released position.
Definition: ustream_base.h:716
Structure for instance control block.
Definition: ustream_base.h:422
offset_t offset_diff
Definition: ustream_base.h:428
volatile uint32_t ustream_two_ref_count
Definition: ustream_base.h:453
enum AZ_ULIB_RESULT_TAG AZ_ULIB_RESULT
Enumeration of ulib results.
const AZ_USTREAM_INTERFACE * api
Definition: ustream_base.h:394
vTable with the ustream APIs.
Definition: ustream_base.h:351
size_t offset_t
Define offset_t with the same size as size_t.
Definition: ustream_base.h:336
static AZ_ULIB_RESULT az_ustream_dispose(AZ_USTREAM *ustream_instance)
Release all the resources allocated to control the instance of the ustream.
Definition: ustream_base.h:872
AZ_RELEASE_CALLBACK control_block_release
Definition: ustream_base.h:404
AZ_ULIB_RESULT(* clone)(AZ_USTREAM *ustream_instance_clone, AZ_USTREAM *ustream_instance, offset_t offset)
Definition: ustream_base.h:360
AZ_ULIB_RESULT(* set_position)(AZ_USTREAM *ustream_instance, offset_t position)
Definition: ustream_base.h:353
AZ_USTREAM_DATA * ptr
Definition: ustream_base.h:396
ulib return values
struct AZ_USTREAM_INTERFACE_TAG AZ_USTREAM_INTERFACE
vTable with the ustream APIs.
static AZ_ULIB_RESULT az_ustream_reset(AZ_USTREAM *ustream_instance)
Changes the current position to the first valid position.
Definition: ustream_base.h:538
AZ_ULIB_RESULT(* get_remaining_size)(AZ_USTREAM *ustream_instance, size_t *const size)
Definition: ustream_base.h:357
Configuration options for ulib.
offset_t inner_current_position
Definition: ustream_base.h:429
static AZ_ULIB_RESULT az_ustream_set_position(AZ_USTREAM *ustream_instance, offset_t position)
Change the current position of the ustream.
Definition: ustream_base.h:499
void * AZ_USTREAM_DATA
Pointer to the data from which to read.
Definition: ustream_base.h:381
AZ_ULIB_RESULT(* read)(AZ_USTREAM *ustream_instance, uint8_t *const buffer, size_t buffer_length, size_t *const size)
Definition: ustream_base.h:355
AZ_RELEASE_CALLBACK data_release
Definition: ustream_base.h:402
volatile uint32_t ref_count
Definition: ustream_base.h:400
AZ_ULIB_RESULT(* reset)(AZ_USTREAM *ustream_instance)
Definition: ustream_base.h:354
void(* AZ_RELEASE_CALLBACK)(void *)
Signature of the function to release memory passed to the ustream.
Definition: ustream_base.h:372
offset_t inner_first_valid_position
Definition: ustream_base.h:430
static AZ_ULIB_RESULT az_ustream_get_remaining_size(AZ_USTREAM *ustream_instance, size_t *const size)
Returns the remaining size of the ustream.
Definition: ustream_base.h:631
AZ_ULIB_RESULT(* get_position)(AZ_USTREAM *ustream_instance, offset_t *const position)
Definition: ustream_base.h:358
AZ_USTREAM_DATA_CB control_block
Definition: ustream_base.h:449
AZ_ULIB_RESULT(* dispose)(AZ_USTREAM *ustream_instance)
Definition: ustream_base.h:362
AZ_USTREAM_DATA_CB * control_block
Definition: ustream_base.h:425
volatile uint32_t ustream_one_ref_count
Definition: ustream_base.h:452
static AZ_ULIB_RESULT az_ustream_read(AZ_USTREAM *ustream_instance, uint8_t *const buffer, size_t buffer_length, size_t *const size)
Gets the next portion of the ustream starting at the current position.
Definition: ustream_base.h:595
static AZ_ULIB_RESULT az_ustream_get_position(AZ_USTREAM *ustream_instance, offset_t *const position)
Returns the current position in the ustream.
Definition: ustream_base.h:667
AZ_ULIB_RESULT(* release)(AZ_USTREAM *ustream_instance, offset_t position)
Definition: ustream_base.h:359
pointer to a platform specific struct for a lock implementation