Libft
Custom implementation of core libc functions with additional utility helpers.
Loading...
Searching...
No Matches
buff_internal.c File Reference
#include "libft.h"
#include "buff_internal.h"
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
Include dependency graph for buff_internal.c:

Macros

#define DEFAULT_BUFF_CAP   128
 
#define BUFF_GROWTH   2
 

Functions

size_t buff_get_required_cap (size_t current_cap, size_t target_len)
 Calculates the required capacity to accommodate a target length.
 
bool buff_grow (t_buff *buff, size_t target_len)
 Grows the buffer to accommodate the target length if necessary.
 
ssize_t buff_read_n_bytes (t_buff *buff, int fd, size_t n)
 Reads up to n bytes from a file descriptor into a buffer.
 

Macro Definition Documentation

◆ BUFF_GROWTH

#define BUFF_GROWTH   2

◆ DEFAULT_BUFF_CAP

#define DEFAULT_BUFF_CAP   128

Function Documentation

◆ buff_get_required_cap()

size_t buff_get_required_cap ( size_t  current_cap,
size_t  target_len 
)

Calculates the required capacity to accommodate a target length.

Uses exponential growth strategy (doubles capacity) to amortize multiple growth operations to O(1) on average.

Parameters
current_capCurrent capacity of the buffer.
target_lenTarget length to accommodate.
Returns
The new required capacity.
Here is the caller graph for this function:

◆ buff_grow()

bool buff_grow ( t_buff buff,
size_t  target_len 
)

Grows the buffer to accommodate the target length if necessary.

No-op if current capacity is already sufficient.

Warning
buff must be initialized before calling this function.
Parameters
buffPointer to an initialized buffer (borrowed).
target_lenThe minimum length the buffer should accommodate.
Returns
true on success, false on memory allocation failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ buff_read_n_bytes()

ssize_t buff_read_n_bytes ( t_buff buff,
int  fd,
size_t  n 
)

Reads up to n bytes from a file descriptor into a buffer.

Reads directly at the end of buff->data and increases buff->len by the number of bytes read. Reading stops after n bytes, EOF, or a read error.

Note
Interrupted reads (EINTR) are retried.
Warning
buff must be initialized before calling this function.
buff must have enough available capacity for n additional bytes.
Parameters
buffPointer to an initialized buffer (borrowed).
fdFile descriptor to read from.
nMaximum number of bytes to read.
Returns
Number of bytes read, or -1 on read error.
Here is the caller graph for this function: