project_files/frontlib/util/buffer.h
changeset 7179 f84805e6df03
parent 7162 fe76d24a25d7
child 7224 5143861c83bd
equal deleted inserted replaced
7177:bf6cf4dd847a 7179:f84805e6df03
       
     1 #ifndef BUFFER_H_
       
     2 #define BUFFER_H_
       
     3 
       
     4 #include <stdint.h>
       
     5 #include <stddef.h>
       
     6 
       
     7 /**
       
     8  * A simple struct to hold both the pointer to an array and its size,
       
     9  * for e.g. conveniently returning it from a function.
       
    10  *
       
    11  * Convention: Size is zero iff data is a NULL pointer.
       
    12  */
       
    13 typedef struct {
       
    14 	void *data;
       
    15 	size_t size;
       
    16 } flib_buffer;
       
    17 
       
    18 /**
       
    19  * Just like flib_buffer, but the contents are not supposed to be modified.
       
    20  */
       
    21 typedef struct {
       
    22 	const void *data;
       
    23 	size_t size;
       
    24 } flib_constbuffer;
       
    25 
       
    26 /**
       
    27  * Simple variable-capacity data structure (opaque type).
       
    28  */
       
    29 struct _flib_vector;
       
    30 typedef struct _flib_vector *flib_vector;
       
    31 
       
    32 /**
       
    33  * Create a new vector. Needs to be destroyed again later with flib_vector_destroy.
       
    34  * May return NULL if memory runs out.
       
    35  */
       
    36 flib_vector flib_vector_create();
       
    37 
       
    38 /**
       
    39  * Free the memory of this vector and set it to NULL.
       
    40  */
       
    41 void flib_vector_destroy(flib_vector *vec);
       
    42 
       
    43 /**
       
    44  * Append the provided data to the end of the vector, enlarging it as required.
       
    45  * Returns the ammount of data appended, which is either len (success) or 0 (out of memory).
       
    46  * The vector remains unchanged if an out of memory situation occurs.
       
    47  */
       
    48 int flib_vector_append(flib_vector vec, const void *data, size_t len);
       
    49 
       
    50 /**
       
    51  * Return a buffer or constbuffer pointing to the current contents of the vector.
       
    52  * These will become invalid if the vector size or capacity is changed.
       
    53  */
       
    54 flib_buffer flib_vector_as_buffer(flib_vector vec);
       
    55 flib_constbuffer flib_vector_as_constbuffer(flib_vector vec);
       
    56 
       
    57 
       
    58 #endif