You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/include/llarp/buffer.h

43 lines
824 B
C

7 years ago
#ifndef LLARP_BUFFER_H_
#define LLARP_BUFFER_H_
6 years ago
#include <llarp/common.h>
#include <stdbool.h>
7 years ago
#include <stdlib.h>
6 years ago
#include <string.h>
7 years ago
#ifdef __cplusplus
extern "C" {
#endif
7 years ago
typedef struct llarp_buffer_t {
char *base;
size_t sz;
char *cur;
} llarp_buffer_t;
6 years ago
size_t INLINE llarp_buffer_size_left(llarp_buffer_t *buff) {
6 years ago
size_t diff = buff->cur - buff->base;
6 years ago
if (diff > buff->sz)
return 0;
else
return buff->sz - diff;
6 years ago
}
6 years ago
bool INLINE llarp_buffer_write(llarp_buffer_t *buff, const void *data,
size_t sz) {
6 years ago
size_t left = llarp_buffer_size_left(buff);
6 years ago
if (left >= sz) {
6 years ago
memcpy(buff->cur, data, sz);
buff->cur += sz;
return true;
}
return false;
}
6 years ago
bool llarp_buffer_writef(llarp_buffer_t *buff, const char *fmt, ...);
7 years ago
#ifdef __cplusplus
}
#endif
7 years ago
7 years ago
#endif