Skip to content

Commit

Permalink
Remove seek and tell callbacks, introduce size instead
Browse files Browse the repository at this point in the history
  • Loading branch information
JanSellner committed May 13, 2024
1 parent 5e1a292 commit 0c8787d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 86 deletions.
67 changes: 13 additions & 54 deletions blosc/blosc2-stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,41 +44,29 @@ int blosc2_stdio_close(void *stream) {
return err;
}

int64_t blosc2_stdio_tell(void *stream) {
int64_t blosc2_stdio_size(void *stream) {
blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
int64_t pos;
#if defined(_MSC_VER)
pos = _ftelli64(my_fp->file);
#else
pos = (int64_t)ftell(my_fp->file);
#endif
return pos;
}

int blosc2_stdio_seek(void *stream, int64_t offset, int whence) {
blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
int rc;
#if defined(_MSC_VER)
rc = _fseeki64(my_fp->file, offset, whence);
#else
rc = fseek(my_fp->file, (long) offset, whence);
#endif
return rc;
fseek(my_fp->file, 0, SEEK_END);
int64_t size = ftell(my_fp->file);
fseek(my_fp->file, 0, SEEK_SET);

return size;
}

int64_t blosc2_stdio_write(const void *ptr, int64_t size, int64_t nitems, int64_t position, void *stream) {
blosc2_stdio_seek(stream, position, SEEK_SET);

blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
fseek(my_fp->file, position, SEEK_SET);

size_t nitems_ = fwrite(ptr, (size_t) size, (size_t) nitems, my_fp->file);
return (int64_t) nitems_;
}

int64_t blosc2_stdio_read(void **ptr, int64_t size, int64_t nitems, int64_t position, void *stream) {
blosc2_stdio_seek(stream, position, SEEK_SET);
blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
fseek(my_fp->file, position, SEEK_SET);

void* data_ptr = *ptr;
blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
size_t nitems_ = fread(data_ptr, (size_t) size, (size_t) nitems, my_fp->file);
return (int64_t) nitems_;
}
Expand Down Expand Up @@ -123,7 +111,6 @@ void *blosc2_stdio_mmap_open(const char *urlpath, const char *mode, void* params
blosc2_stdio_mmap *mmap_file = (blosc2_stdio_mmap *) params;
if (mmap_file->addr != NULL) {
/* A memory-mapped file is only opened once */
mmap_file->offset = 0;
return mmap_file;
}

Expand Down Expand Up @@ -243,8 +230,7 @@ void *blosc2_stdio_mmap_open(const char *urlpath, const char *mode, void* params
#else
mmap_file->fd = fileno(mmap_file->file);

/* Offset where the mapping should start
(different to mmap_file->offset which denotes the current position and may change) */
/* Offset where the mapping should start */
int64_t offset = 0;
mmap_file->addr = mmap(
NULL, mmap_file->mapping_size, mmap_file->access_flags, mmap_file->map_flags, mmap_file->fd, offset);
Expand All @@ -262,35 +248,9 @@ int blosc2_stdio_mmap_close(void *stream) {
return 0;
}

int64_t blosc2_stdio_mmap_tell(void *stream) {
blosc2_stdio_mmap *mmap_file = (blosc2_stdio_mmap *) stream;
return mmap_file->offset;
}

int blosc2_stdio_mmap_seek(void *stream, int64_t offset, int whence) {
int64_t blosc2_stdio_mmap_size(void *stream) {
blosc2_stdio_mmap *mmap_file = (blosc2_stdio_mmap *) stream;

switch (whence) {
case SEEK_SET:
mmap_file->offset = offset;
break;
case SEEK_CUR:
mmap_file->offset += offset;
break;
case SEEK_END:
mmap_file->offset = mmap_file->file_size + offset;
break;
default:
BLOSC_TRACE_ERROR("Invalid whence %d argument.", whence);
return -1;
}

if (mmap_file->offset < 0) {
BLOSC_TRACE_ERROR("Cannot seek to a negative offset.");
return -1;
}

return 0;
return mmap_file->file_size;
}

int64_t blosc2_stdio_mmap_write(const void *ptr, int64_t size, int64_t nitems, int64_t position, void *stream) {
Expand Down Expand Up @@ -421,7 +381,6 @@ int64_t blosc2_stdio_mmap_write(const void *ptr, int64_t size, int64_t nitems, i
}
#endif

mmap_file->offset = position_end;
return nitems;
}

Expand Down
6 changes: 2 additions & 4 deletions blosc/blosc2.c
Original file line number Diff line number Diff line change
Expand Up @@ -3801,8 +3801,7 @@ void blosc2_init(void) {
BLOSC2_IO_CB_DEFAULTS.is_allocation_necessary = true;
BLOSC2_IO_CB_DEFAULTS.open = (blosc2_open_cb) blosc2_stdio_open;
BLOSC2_IO_CB_DEFAULTS.close = (blosc2_close_cb) blosc2_stdio_close;
BLOSC2_IO_CB_DEFAULTS.tell = (blosc2_tell_cb) blosc2_stdio_tell;
BLOSC2_IO_CB_DEFAULTS.seek = (blosc2_seek_cb) blosc2_stdio_seek;
BLOSC2_IO_CB_DEFAULTS.size = (blosc2_size_cb) blosc2_stdio_size;
BLOSC2_IO_CB_DEFAULTS.write = (blosc2_write_cb) blosc2_stdio_write;
BLOSC2_IO_CB_DEFAULTS.read = (blosc2_read_cb) blosc2_stdio_read;
BLOSC2_IO_CB_DEFAULTS.truncate = (blosc2_truncate_cb) blosc2_stdio_truncate;
Expand All @@ -3814,8 +3813,7 @@ void blosc2_init(void) {
BLOSC2_IO_CB_MMAP.open = (blosc2_open_cb) blosc2_stdio_mmap_open;
BLOSC2_IO_CB_MMAP.close = (blosc2_close_cb) blosc2_stdio_mmap_close;
BLOSC2_IO_CB_MMAP.read = (blosc2_read_cb) blosc2_stdio_mmap_read;
BLOSC2_IO_CB_MMAP.tell = (blosc2_tell_cb) blosc2_stdio_mmap_tell;
BLOSC2_IO_CB_MMAP.seek = (blosc2_seek_cb) blosc2_stdio_mmap_seek;
BLOSC2_IO_CB_MMAP.size = (blosc2_size_cb) blosc2_stdio_mmap_size;
BLOSC2_IO_CB_MMAP.write = (blosc2_write_cb) blosc2_stdio_mmap_write;
BLOSC2_IO_CB_MMAP.truncate = (blosc2_truncate_cb) blosc2_stdio_mmap_truncate;
BLOSC2_IO_CB_MMAP.destroy = (blosc2_destroy_cb) blosc2_stdio_mmap_destroy;
Expand Down
12 changes: 2 additions & 10 deletions blosc/schunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,19 +403,11 @@ int64_t append_frame_to_file(blosc2_frame_s* frame, const char* urlpath) {
return BLOSC2_ERROR_PLUGIN_IO;
}
void* fp = io_cb->open(urlpath, "ab", frame->schunk->storage->io);
int64_t offset;

# if (UNIX)
offset = io_cb->tell(fp);
# else
io_cb->seek(fp, 0, SEEK_END);
offset = io_cb->tell(fp);
# endif

int64_t io_pos = offset;
int64_t io_pos = io_cb->size(fp);
io_cb->write(frame->cframe, frame->len, 1, io_pos, fp);
io_cb->close(fp);
return offset;
return io_pos;
}


Expand Down
3 changes: 1 addition & 2 deletions blosc/sframe.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ int32_t sframe_get_chunk(blosc2_frame_s* frame, int64_t nchunk, uint8_t** chunk,
return BLOSC2_ERROR_PLUGIN_IO;
}

io_cb->seek(fpc, 0L, SEEK_END);
int64_t chunk_cbytes = io_cb->tell(fpc);
int64_t chunk_cbytes = io_cb->size(fpc);

if (io_cb->is_allocation_necessary) {
*chunk = malloc((size_t)chunk_cbytes);
Expand Down
9 changes: 3 additions & 6 deletions include/blosc2.h
Original file line number Diff line number Diff line change
Expand Up @@ -1037,8 +1037,7 @@ enum {

typedef void* (*blosc2_open_cb)(const char *urlpath, const char *mode, void *params);
typedef int (*blosc2_close_cb)(void *stream);
typedef int64_t (*blosc2_tell_cb)(void *stream);
typedef int (*blosc2_seek_cb)(void *stream, int64_t offset, int whence);
typedef int64_t (*blosc2_size_cb)(void *stream);
typedef int64_t (*blosc2_write_cb)(const void *ptr, int64_t size, int64_t nitems, int64_t position, void *stream);
typedef int64_t (*blosc2_read_cb)(void **ptr, int64_t size, int64_t nitems, int64_t position, void *stream);
typedef int (*blosc2_truncate_cb)(void *stream, int64_t size);
Expand All @@ -1060,10 +1059,8 @@ typedef struct {
//!< The IO open callback.
blosc2_close_cb close;
//!< The IO close callback.
blosc2_tell_cb tell;
//!< The IO tell callback.
blosc2_seek_cb seek;
//!< The IO seek callback.
blosc2_size_cb size;
//!< The IO size callback.
blosc2_write_cb write;
//!< The IO write callback.
blosc2_read_cb read;
Expand Down
10 changes: 3 additions & 7 deletions include/blosc2/blosc2-stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ typedef struct {

BLOSC_EXPORT void *blosc2_stdio_open(const char *urlpath, const char *mode, void* params);
BLOSC_EXPORT int blosc2_stdio_close(void *stream);
BLOSC_EXPORT int64_t blosc2_stdio_tell(void *stream);
BLOSC_EXPORT int blosc2_stdio_seek(void *stream, int64_t offset, int whence);
BLOSC_EXPORT int64_t blosc2_stdio_size(void *stream);
BLOSC_EXPORT int64_t blosc2_stdio_write(const void *ptr, int64_t size, int64_t nitems, int64_t position, void *stream);
BLOSC_EXPORT int64_t blosc2_stdio_read(void **ptr, int64_t size, int64_t nitems, int64_t position, void *stream);
BLOSC_EXPORT int blosc2_stdio_truncate(void *stream, int64_t size);
Expand Down Expand Up @@ -69,8 +68,6 @@ typedef struct {
//!< The size of the file.
int64_t mapping_size;
//!< The size of the mapping (mapping_size >= file_size).
int64_t offset;
//!< The current position inside the mapping.
FILE* file;
//!< The underlying file handle.
int fd;
Expand All @@ -89,16 +86,15 @@ typedef struct {
* @brief Default struct for memory-mapped I/O for user initialization.
*/
static const blosc2_stdio_mmap BLOSC2_STDIO_MMAP_DEFAULTS = {
"r", (1 << 30), false, NULL, -1, -1, 0, NULL, -1, -1, -1
"r", (1 << 30), false, NULL, -1, -1, NULL, -1, -1, -1
#if defined(_WIN32)
, INVALID_HANDLE_VALUE
#endif
};

BLOSC_EXPORT void *blosc2_stdio_mmap_open(const char *urlpath, const char *mode, void* params);
BLOSC_EXPORT int blosc2_stdio_mmap_close(void *stream);
BLOSC_EXPORT int64_t blosc2_stdio_mmap_tell(void *stream);
BLOSC_EXPORT int blosc2_stdio_mmap_seek(void *stream, int64_t offset, int whence);
BLOSC_EXPORT int64_t blosc2_stdio_mmap_size(void *stream);
BLOSC_EXPORT int64_t blosc2_stdio_mmap_write(
const void *ptr, int64_t size, int64_t nitems, int64_t position, void *stream);
BLOSC_EXPORT int64_t blosc2_stdio_mmap_read(void **ptr, int64_t size, int64_t nitems, int64_t position, void *stream);
Expand Down
6 changes: 3 additions & 3 deletions tests/test_udio.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ int test_close(void *stream) {
return err;
}

int64_t test_tell(void *stream) {
int64_t test_size(void *stream) {
test_file *my = (test_file *) stream;
my->params->tell++;
return blosc2_stdio_tell(my->bfile);
return blosc2_stdio_size(my->bfile);
}

int64_t test_write(const void *ptr, int64_t size, int64_t nitems, int64_t position, void *stream) {
Expand Down Expand Up @@ -88,7 +88,7 @@ CUTEST_TEST_SETUP(udio) {
io_cb.open = (blosc2_open_cb) test_open;
io_cb.close = (blosc2_close_cb) test_close;
io_cb.read = (blosc2_read_cb) test_read;
io_cb.tell = (blosc2_tell_cb) test_tell;
io_cb.size = (blosc2_size_cb) test_size;
io_cb.write = (blosc2_write_cb) test_write;
io_cb.truncate = (blosc2_truncate_cb) test_truncate;

Expand Down

0 comments on commit 0c8787d

Please sign in to comment.