#include <fio_tmpfile.h>
#include <fiobj_data.h>
#include <fiobj_str.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fio.h>
/* *****************************************************************************
Numbers Type
***************************************************************************** */
typedef struct {
fiobj_object_header_s head;
uint8_t *buffer; /* reader buffer */
union {
FIOBJ parent;
void (*dealloc)(void *); /* buffer deallocation function */
size_t fpos; /* the file reader's position */
} source;
size_t capa; /* total buffer capacity / slice offset */
size_t len; /* length of valid data in buffer */
size_t pos; /* position of reader */
int fd; /* file descriptor (-1 if invalid). */
} fiobj_data_s;
#define obj2io(o) ((fiobj_data_s *)(o))
/* *****************************************************************************
Object required VTable and functions
***************************************************************************** */
#define REQUIRE_MEM(mem) \
do { \
if ((mem) == NULL) { \
perror("FATAL ERROR: fiobj IO couldn't allocate memory"); \
exit(errno); \
} \
} while (0)
static void fiobj_data_copy_buffer(FIOBJ o) {
obj2io(o)->capa = (((obj2io(o)->len) >> 12) + 1) << 12;
void *tmp = fio_malloc(obj2io(o)->capa);
REQUIRE_MEM(tmp);
memcpy(tmp, obj2io(o)->buffer, obj2io(o)->len);
if (obj2io(o)->source.dealloc)
obj2io(o)->source.dealloc(obj2io(o)->buffer);
obj2io(o)->source.dealloc = fio_free;
obj2io(o)->buffer = tmp;
}
static void fiobj_data_copy_parent(FIOBJ o) {
switch (obj2io(obj2io(o)->source.parent)->fd) {
case -1:
obj2io(o)->buffer = fio_malloc(obj2io(o)->len + 1);
memcpy(obj2io(o)->buffer,
obj2io(obj2io(o)->source.parent)->buffer + obj2io(o)->capa,
obj2io(o)->len);
obj2io(o)->buffer[obj2io(o)->len] = 0;
obj2io(o)->capa = obj2io(o)->len;
obj2io(o)->fd = -1;
fiobj_free(obj2io(o)->source.parent);
obj2io(o)->source.dealloc = fio_free;
return;
default:
obj2io(o)->fd = fio_tmpfile();
if (obj2io(o)->fd < 0) {
perror("FATAL ERROR: (fiobj_data) can't create temporary file");
exit(errno);
}
fio_str_info_s data;
size_t pos = 0;
do {
ssize_t written;
data = fiobj_data_pread(obj2io(o)->source.parent, pos + obj2io(o)->capa,
4096);
if (data.len + pos > obj2io(o)->len)
data.len = obj2io(o)->len - pos;
retry_int:
written = write(obj2io(o)->fd, data.data, data.len);
if (written < 0) {
if (errno == EINTR)
goto retry_int;
perror("FATAL ERROR: (fiobj_data) can't write to temporary file");
exit(errno);
}
pos += written;
} while (data.len == 4096);
fiobj_free(obj2io(o)->source.parent);
obj2io(o)->capa = 0;
obj2io(o)->len = pos;
obj2io(o)->source.fpos = obj2io(o)->pos;
obj2io(o)->pos = 0;
obj2io(o)->buffer = NULL;
break;
}
}
static inline void fiobj_data_pre_write(FIOBJ o, uintptr_t length) {
switch (obj2io(o)->fd) {
case -1:
if (obj2io(o)->source.dealloc != fio_free) {
fiobj_data_copy_buffer(o);
}
break;
case -2:
fiobj_data_copy_parent(o);
break;
}
if (obj2io(o)->capa >= obj2io(o)->len + length)
return;
/* add rounded pages (4096) to capacity */
obj2io(o)->capa = (((obj2io(o)->len + length) >> 12) + 1) << 12;
obj2io(o)->buffer = fio_realloc(obj2io(o)->buffer, obj2io(o)->capa);
REQUIRE_MEM(obj2io(o)->buffer);
}
static inline int64_t fiobj_data_get_fd_size(const FIOBJ o) {
struct stat stat;
retry:
if (fstat(obj2io(o)->fd, &stat)) {
if (errno == EINTR)
goto retry;
return -1;
}
return stat.st_size;
}
static FIOBJ fiobj_data_alloc(void *buffer, int fd) {
fiobj_data_s *io = fio_malloc(sizeof(*io));
REQUIRE_MEM(io);
*io = (fiobj_data_s){
.head = {.ref = 1, .type = FIOBJ_T_DATA},
.buffer = buffer,
.fd = fd,
};
return (FIOBJ)io;
}
static void fiobj_data_dealloc(FIOBJ o, void (*task)(FIOBJ, void *),
void *arg) {
switch (obj2io(o)->fd) {
case -1:
if (obj2io(o)->source.dealloc && obj2io(o)->buffer)
obj2io(o)->source.dealloc(obj2io(o)->buffer);
break;
case -2:
fiobj_free(obj2io(o)->source.parent);
break;
default:
close(obj2io(o)->fd);
fio_free(obj2io(o)->buffer);
break;
}
fio_free((void *)o);
(void)task;
(void)arg;
}
нашёл.
ты учишь си
ПАПА МОЖЕТ В СИ, ПАПАПА МОЖЕТ В СИ
ошибка в строке номер (sqrt(12656))^(sin(0.53434))
Компилятор си это пиздец конечно.
лет ми спик фром май харт - си вилл невер дай
Может Си который дзы пин? Угадал 🤔