Turnstone Operating System
Loading...
Searching...
No Matches
buffer.64.c File Reference

buffer implementation More...

#include <buffer.h>
#include <cpu/sync.h>
#include <utils.h>
#include <strings.h>

Classes

struct  buffer_t
 

Macros

#define BUFFER_PRINTF_BUFFER_SIZE   2048
 

Typedefs

typedef struct buffer_t buffer_t
 opaque buffer_t struct
 

Functions

 MODULE ("turnstone.lib.buffer")
 
buffer_tbuffer_get_tmp_buffer_for_printf (void)
 returns a temporary buffer for printf, before gui is initialized, this is a static buffer, after gui is initialized, this is a buffer allocated with malloc More...
 
void buffer_reset_tmp_buffer_for_printf (void)
 resets temporary buffer for printf
 
buffer_tbuffer_new_with_capacity (memory_heap_t *heap, uint64_t capacity)
 buffer_new_with_capacity creates a new buffer with capacity More...
 
memory_heap_tbuffer_get_heap (buffer_t *buffer)
 buffer_get_heap returns heap of buffer More...
 
buffer_tbuffer_encapsulate (uint8_t *data, uint64_t length)
 returns a buffer encapsulating a byte array, byte array is not copied More...
 
boolean_t buffer_set_readonly (buffer_t *buffer, boolean_t ro)
 sets readonly flag of buffer More...
 
uint64_t buffer_get_length (buffer_t *buffer)
 returns length of buffer More...
 
uint64_t buffer_get_mark_position (buffer_t *buffer)
 
boolean_t buffer_set_mark_position (buffer_t *buffer, uint64_t position)
 
boolean_t buffer_reset (buffer_t *buffer)
 resets buffer, sets length and position to 0 and all bytes to 0, capacity stays same More...
 
uint64_t buffer_get_capacity (buffer_t *buffer)
 returns capacity of buffer More...
 
uint64_t buffer_get_position (buffer_t *buffer)
 returns position of buffer More...
 
static boolean_t buffer_resize_if_need (buffer_t *buffer, uint64_t length)
 
buffer_tbuffer_append_byte (buffer_t *buffer, uint8_t data)
 buffer_append_byte appends a byte to buffer More...
 
buffer_tbuffer_append_bytes (buffer_t *buffer, uint8_t *data, uint64_t length)
 appends bytes to buffer More...
 
buffer_tbuffer_append_buffer (buffer_t *buffer, buffer_t *appenden)
 appends buffer to buffer More...
 
uint8_tbuffer_get_bytes (buffer_t *buffer, uint64_t length)
 returns a byte array from buffer with length, byte array is copied, position is advanced with length More...
 
uint8_tbuffer_get_all_bytes (buffer_t *buffer, uint64_t *length)
 returns a byte array containing all bytes from buffer, byte array is copied, position is not advanced More...
 
uint8_tbuffer_get_all_bytes_and_reset (buffer_t *buffer, uint64_t *length)
 returns a byte array containing all bytes from buffer, byte array is copied, position is not advanced, buffer is resetted More...
 
uint8_tbuffer_get_all_bytes_and_destroy (buffer_t *buffer, uint64_t *length)
 returns a byte array containing all bytes from buffer, byte array is copied, position is not advanced, buffer is destroyed More...
 
uint8_t buffer_get_byte (buffer_t *buffer)
 returns a byte array from buffer with length, byte array is copied, position is advanced 1 More...
 
uint8_t buffer_peek_byte (buffer_t *buffer)
 returns a byte from buffer at current position, position is not changed More...
 
uint8_t buffer_peek_byte_at_position (buffer_t *buffer, uint64_t position)
 returns a byte from buffer at position, position is not changed More...
 
uint64_t buffer_peek_ints_at_position (buffer_t *buffer, uint64_t position, uint8_t bc)
 returns an int from buffer at position, position is not changed More...
 
uint64_t buffer_peek_ints (buffer_t *buffer, uint8_t bc)
 returns an int from buffer at current position, position is not changed More...
 
uint64_t buffer_remaining (buffer_t *buffer)
 returns how many bytes are remaining in buffer (length - position) More...
 
boolean_t buffer_seek (buffer_t *buffer, int64_t position, buffer_seek_direction_t direction)
 changes position of buffer More...
 
int8_t buffer_destroy (buffer_t *buffer)
 destroys buffer More...
 
boolean_t buffer_write_slice_into (buffer_t *buffer, uint64_t pos, uint64_t len, uint8_t *dest)
 copies bytes from buffer to dest (should be at least length long), position is not changed More...
 
uint8_tbuffer_get_view_at_position (buffer_t *buffer, uint64_t position, uint64_t length)
 returns a byte array from buffer with length, byte array is not copied, position is not changed More...
 
int64_t buffer_printf (buffer_t *buffer, const char_t *fmt,...)
 prints to buffer, returns number of bytes written More...
 
int64_t buffer_vprintf (buffer_t *buffer, const char_t *fmt, va_list args)
 prints to buffer, returns number of bytes written More...
 
char_tbuffer_read_line_ext (buffer_t *buffer, char_t line_continuation_char, char_t delimiter_char, uint64_t *length)
 reads a line from buffer, line is null terminated, position is advanced More...
 

Variables

uint8_t buffer_tmp_buffer_area_for_printf [BUFFER_PRINTF_BUFFER_SIZE]
 
buffer_t buffer_tmp_buffer_for_printf
 

Detailed Description

buffer implementation

This work is licensed under TURNSTONE OS Public License. Please read and understand latest version of Licence.

Function Documentation

◆ buffer_append_buffer()

buffer_t * buffer_append_buffer ( buffer_t buffer,
buffer_t appenden 
)

appends buffer to buffer

Parameters
[in]bufferbuffer to append
[in]appendenbuffer to append
Returns
buffer_t* pointer to buffer

◆ buffer_append_byte()

buffer_t * buffer_append_byte ( buffer_t buffer,
uint8_t  data 
)

buffer_append_byte appends a byte to buffer

Parameters
[in]bufferbuffer to append
[in]databyte to append
Returns
buffer_t* pointer to buffer

◆ buffer_append_bytes()

buffer_t * buffer_append_bytes ( buffer_t buffer,
uint8_t data,
uint64_t  length 
)

appends bytes to buffer

Parameters
[in]bufferbuffer to append
[in]databytes to append
[in]lengthlength of bytes
Returns
buffer_t* pointer to buffer

◆ buffer_destroy()

int8_t buffer_destroy ( buffer_t buffer)

destroys buffer

Parameters
[in]bufferbuffer to destroy
Returns
0 if success

◆ buffer_encapsulate()

buffer_t * buffer_encapsulate ( uint8_t data,
uint64_t  length 
)

returns a buffer encapsulating a byte array, byte array is not copied

Parameters
[in]databyte array to encapsulate
[in]lengthlength of byte array
Returns
buffer_t* pointer to buffer

◆ buffer_get_all_bytes()

uint8_t * buffer_get_all_bytes ( buffer_t buffer,
uint64_t length 
)

returns a byte array containing all bytes from buffer, byte array is copied, position is not advanced

Parameters
[in]bufferbuffer to get bytes
[out]lengthlength of bytes returned
Returns
uint8_t* pointer to byte array

◆ buffer_get_all_bytes_and_destroy()

uint8_t * buffer_get_all_bytes_and_destroy ( buffer_t buffer,
uint64_t length 
)

returns a byte array containing all bytes from buffer, byte array is copied, position is not advanced, buffer is destroyed

Parameters
[in]bufferbuffer to get bytes
[out]lengthlength of bytes returned
Returns
uint8_t* pointer to byte array

◆ buffer_get_all_bytes_and_reset()

uint8_t * buffer_get_all_bytes_and_reset ( buffer_t buffer,
uint64_t length 
)

returns a byte array containing all bytes from buffer, byte array is copied, position is not advanced, buffer is resetted

Parameters
[in]bufferbuffer to get bytes
[out]lengthlength of bytes returned
Returns
uint8_t* pointer to byte array

◆ buffer_get_byte()

uint8_t buffer_get_byte ( buffer_t buffer)

returns a byte array from buffer with length, byte array is copied, position is advanced 1

Parameters
[in]bufferbuffer to get bytes
Returns
a single byte

◆ buffer_get_bytes()

uint8_t * buffer_get_bytes ( buffer_t buffer,
uint64_t  length 
)

returns a byte array from buffer with length, byte array is copied, position is advanced with length

Parameters
[in]bufferbuffer to get bytes
[in]lengthlength of bytes
Returns
uint8_t* pointer to byte array

◆ buffer_get_capacity()

uint64_t buffer_get_capacity ( buffer_t buffer)

returns capacity of buffer

Parameters
[in]bufferbuffer to get capacity
Returns
uint64_t capacity of buffer

◆ buffer_get_heap()

memory_heap_t * buffer_get_heap ( buffer_t buffer)

buffer_get_heap returns heap of buffer

Parameters
[in]bufferbuffer to get heap
Returns
memory_heap_t* pointer to heap

◆ buffer_get_length()

uint64_t buffer_get_length ( buffer_t buffer)

returns length of buffer

Parameters
[in]bufferbuffer to get length
Returns
uint64_t length of buffer

◆ buffer_get_position()

uint64_t buffer_get_position ( buffer_t buffer)

returns position of buffer

Parameters
[in]bufferbuffer to get position
Returns
uint64_t position of buffer

◆ buffer_get_tmp_buffer_for_printf()

buffer_t * buffer_get_tmp_buffer_for_printf ( void  )

returns a temporary buffer for printf, before gui is initialized, this is a static buffer, after gui is initialized, this is a buffer allocated with malloc

Returns
buffer_t* pointer to buffer

◆ buffer_get_view_at_position()

uint8_t * buffer_get_view_at_position ( buffer_t buffer,
uint64_t  position,
uint64_t  length 
)

returns a byte array from buffer with length, byte array is not copied, position is not changed

Parameters
[in]bufferbuffer to get bytes
[in]positionposition to get bytes
[in]lengthlength of bytes
Returns
uint8_t* pointer to byte array, or null if position + length > buffer length

◆ buffer_new_with_capacity()

buffer_t * buffer_new_with_capacity ( memory_heap_t heap,
uint64_t  capacity 
)

buffer_new_with_capacity creates a new buffer with capacity

Parameters
heapheap to allocate buffer
capacitycapacity of buffer
Returns
buffer_t* pointer to buffer

◆ buffer_peek_byte()

uint8_t buffer_peek_byte ( buffer_t buffer)

returns a byte from buffer at current position, position is not changed

Parameters
[in]bufferbuffer to peek
Returns
uint8_t byte at position

◆ buffer_peek_byte_at_position()

uint8_t buffer_peek_byte_at_position ( buffer_t buffer,
uint64_t  position 
)

returns a byte from buffer at position, position is not changed

Parameters
[in]bufferbuffer to peek
[in]positionposition to peek
Returns
uint8_t byte at position

◆ buffer_peek_ints()

uint64_t buffer_peek_ints ( buffer_t buffer,
uint8_t  bc 
)

returns an int from buffer at current position, position is not changed

Parameters
[in]bufferbuffer to peek
[in]bcbyte count of int
Returns
uint64_t int at position

◆ buffer_peek_ints_at_position()

uint64_t buffer_peek_ints_at_position ( buffer_t buffer,
uint64_t  position,
uint8_t  bc 
)

returns an int from buffer at position, position is not changed

Parameters
[in]bufferbuffer to peek
[in]positionposition to peek
[in]bcbyte count of int
Returns
uint64_t int at position

◆ buffer_printf()

int64_t buffer_printf ( buffer_t buffer,
const char *  format,
  ... 
)

prints to buffer, returns number of bytes written

Parameters
[in]bufferbuffer to print to
[in]formatformat string
[in]...arguments
Returns
int64_t number of bytes written

◆ buffer_read_line_ext()

char_t * buffer_read_line_ext ( buffer_t buffer,
char_t  line_continuation_char,
char_t  delimiter_char,
uint64_t length 
)

reads a line from buffer, line is null terminated, position is advanced

Parameters
[in]bufferbuffer to read line
[in]line_continuation_charline continuation char
[in]delimiter_chardelimiter char
[out]lengthlength of line optional parameter
Returns
char_t* pointer to line

◆ buffer_remaining()

uint64_t buffer_remaining ( buffer_t buffer)

returns how many bytes are remaining in buffer (length - position)

Parameters
[in]bufferbuffer to get remaining bytes
Returns
uint64_t remaining bytes

◆ buffer_reset()

boolean_t buffer_reset ( buffer_t buffer)

resets buffer, sets length and position to 0 and all bytes to 0, capacity stays same

Parameters
[in]bufferbuffer to reset
Returns
boolean_t true if success

◆ buffer_seek()

boolean_t buffer_seek ( buffer_t buffer,
int64_t  position,
buffer_seek_direction_t  direction 
)

changes position of buffer

Parameters
[in]bufferbuffer to change position
[in]positionposition to change
[in]directiondirection to change
Returns
boolean_t true if success

◆ buffer_set_readonly()

boolean_t buffer_set_readonly ( buffer_t buffer,
boolean_t  ro 
)

sets readonly flag of buffer

Parameters
[in]bufferbuffer to set readonly
[in]roreadonly flag
Returns
boolean_t true if success

◆ buffer_vprintf()

int64_t buffer_vprintf ( buffer_t buffer,
const char *  format,
va_list  args 
)

prints to buffer, returns number of bytes written

Parameters
[in]bufferbuffer to print to
[in]formatformat string
[in]argsva_list of arguments
Returns
int64_t number of bytes written

◆ buffer_write_slice_into()

boolean_t buffer_write_slice_into ( buffer_t buffer,
uint64_t  pos,
uint64_t  len,
uint8_t dest 
)

copies bytes from buffer to dest (should be at least length long), position is not changed

Parameters
[in]bufferbuffer to copy
[in]posposition to copy
[in]lenlength to copy
[out]destdestination to copy to, dest should be at least len long
Returns
boolean_t true if success

Variable Documentation

◆ buffer_tmp_buffer_for_printf

buffer_t buffer_tmp_buffer_for_printf
Initial value:
= {
.heap = NULL,
.lock = NULL,
.capacity = BUFFER_PRINTF_BUFFER_SIZE,
.length = 0,
.position = 0,
.readonly = false,
.data = buffer_tmp_buffer_area_for_printf
}