Skip to content

Commit 0c8787d

Browse files
committed
Remove seek and tell callbacks, introduce size instead
1 parent 5e1a292 commit 0c8787d

File tree

7 files changed

+27
-86
lines changed

7 files changed

+27
-86
lines changed

blosc/blosc2-stdio.c

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,29 @@ int blosc2_stdio_close(void *stream) {
4444
return err;
4545
}
4646

47-
int64_t blosc2_stdio_tell(void *stream) {
47+
int64_t blosc2_stdio_size(void *stream) {
4848
blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
49-
int64_t pos;
50-
#if defined(_MSC_VER)
51-
pos = _ftelli64(my_fp->file);
52-
#else
53-
pos = (int64_t)ftell(my_fp->file);
54-
#endif
55-
return pos;
56-
}
5749

58-
int blosc2_stdio_seek(void *stream, int64_t offset, int whence) {
59-
blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
60-
int rc;
61-
#if defined(_MSC_VER)
62-
rc = _fseeki64(my_fp->file, offset, whence);
63-
#else
64-
rc = fseek(my_fp->file, (long) offset, whence);
65-
#endif
66-
return rc;
50+
fseek(my_fp->file, 0, SEEK_END);
51+
int64_t size = ftell(my_fp->file);
52+
fseek(my_fp->file, 0, SEEK_SET);
53+
54+
return size;
6755
}
6856

6957
int64_t blosc2_stdio_write(const void *ptr, int64_t size, int64_t nitems, int64_t position, void *stream) {
70-
blosc2_stdio_seek(stream, position, SEEK_SET);
71-
7258
blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
59+
fseek(my_fp->file, position, SEEK_SET);
60+
7361
size_t nitems_ = fwrite(ptr, (size_t) size, (size_t) nitems, my_fp->file);
7462
return (int64_t) nitems_;
7563
}
7664

7765
int64_t blosc2_stdio_read(void **ptr, int64_t size, int64_t nitems, int64_t position, void *stream) {
78-
blosc2_stdio_seek(stream, position, SEEK_SET);
66+
blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
67+
fseek(my_fp->file, position, SEEK_SET);
7968

8069
void* data_ptr = *ptr;
81-
blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
8270
size_t nitems_ = fread(data_ptr, (size_t) size, (size_t) nitems, my_fp->file);
8371
return (int64_t) nitems_;
8472
}
@@ -123,7 +111,6 @@ void *blosc2_stdio_mmap_open(const char *urlpath, const char *mode, void* params
123111
blosc2_stdio_mmap *mmap_file = (blosc2_stdio_mmap *) params;
124112
if (mmap_file->addr != NULL) {
125113
/* A memory-mapped file is only opened once */
126-
mmap_file->offset = 0;
127114
return mmap_file;
128115
}
129116

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

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

265-
int64_t blosc2_stdio_mmap_tell(void *stream) {
266-
blosc2_stdio_mmap *mmap_file = (blosc2_stdio_mmap *) stream;
267-
return mmap_file->offset;
268-
}
269-
270-
int blosc2_stdio_mmap_seek(void *stream, int64_t offset, int whence) {
251+
int64_t blosc2_stdio_mmap_size(void *stream) {
271252
blosc2_stdio_mmap *mmap_file = (blosc2_stdio_mmap *) stream;
272-
273-
switch (whence) {
274-
case SEEK_SET:
275-
mmap_file->offset = offset;
276-
break;
277-
case SEEK_CUR:
278-
mmap_file->offset += offset;
279-
break;
280-
case SEEK_END:
281-
mmap_file->offset = mmap_file->file_size + offset;
282-
break;
283-
default:
284-
BLOSC_TRACE_ERROR("Invalid whence %d argument.", whence);
285-
return -1;
286-
}
287-
288-
if (mmap_file->offset < 0) {
289-
BLOSC_TRACE_ERROR("Cannot seek to a negative offset.");
290-
return -1;
291-
}
292-
293-
return 0;
253+
return mmap_file->file_size;
294254
}
295255

296256
int64_t blosc2_stdio_mmap_write(const void *ptr, int64_t size, int64_t nitems, int64_t position, void *stream) {
@@ -421,7 +381,6 @@ int64_t blosc2_stdio_mmap_write(const void *ptr, int64_t size, int64_t nitems, i
421381
}
422382
#endif
423383

424-
mmap_file->offset = position_end;
425384
return nitems;
426385
}
427386

blosc/blosc2.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3801,8 +3801,7 @@ void blosc2_init(void) {
38013801
BLOSC2_IO_CB_DEFAULTS.is_allocation_necessary = true;
38023802
BLOSC2_IO_CB_DEFAULTS.open = (blosc2_open_cb) blosc2_stdio_open;
38033803
BLOSC2_IO_CB_DEFAULTS.close = (blosc2_close_cb) blosc2_stdio_close;
3804-
BLOSC2_IO_CB_DEFAULTS.tell = (blosc2_tell_cb) blosc2_stdio_tell;
3805-
BLOSC2_IO_CB_DEFAULTS.seek = (blosc2_seek_cb) blosc2_stdio_seek;
3804+
BLOSC2_IO_CB_DEFAULTS.size = (blosc2_size_cb) blosc2_stdio_size;
38063805
BLOSC2_IO_CB_DEFAULTS.write = (blosc2_write_cb) blosc2_stdio_write;
38073806
BLOSC2_IO_CB_DEFAULTS.read = (blosc2_read_cb) blosc2_stdio_read;
38083807
BLOSC2_IO_CB_DEFAULTS.truncate = (blosc2_truncate_cb) blosc2_stdio_truncate;
@@ -3814,8 +3813,7 @@ void blosc2_init(void) {
38143813
BLOSC2_IO_CB_MMAP.open = (blosc2_open_cb) blosc2_stdio_mmap_open;
38153814
BLOSC2_IO_CB_MMAP.close = (blosc2_close_cb) blosc2_stdio_mmap_close;
38163815
BLOSC2_IO_CB_MMAP.read = (blosc2_read_cb) blosc2_stdio_mmap_read;
3817-
BLOSC2_IO_CB_MMAP.tell = (blosc2_tell_cb) blosc2_stdio_mmap_tell;
3818-
BLOSC2_IO_CB_MMAP.seek = (blosc2_seek_cb) blosc2_stdio_mmap_seek;
3816+
BLOSC2_IO_CB_MMAP.size = (blosc2_size_cb) blosc2_stdio_mmap_size;
38193817
BLOSC2_IO_CB_MMAP.write = (blosc2_write_cb) blosc2_stdio_mmap_write;
38203818
BLOSC2_IO_CB_MMAP.truncate = (blosc2_truncate_cb) blosc2_stdio_mmap_truncate;
38213819
BLOSC2_IO_CB_MMAP.destroy = (blosc2_destroy_cb) blosc2_stdio_mmap_destroy;

blosc/schunk.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -403,19 +403,11 @@ int64_t append_frame_to_file(blosc2_frame_s* frame, const char* urlpath) {
403403
return BLOSC2_ERROR_PLUGIN_IO;
404404
}
405405
void* fp = io_cb->open(urlpath, "ab", frame->schunk->storage->io);
406-
int64_t offset;
407406

408-
# if (UNIX)
409-
offset = io_cb->tell(fp);
410-
# else
411-
io_cb->seek(fp, 0, SEEK_END);
412-
offset = io_cb->tell(fp);
413-
# endif
414-
415-
int64_t io_pos = offset;
407+
int64_t io_pos = io_cb->size(fp);
416408
io_cb->write(frame->cframe, frame->len, 1, io_pos, fp);
417409
io_cb->close(fp);
418-
return offset;
410+
return io_pos;
419411
}
420412

421413

blosc/sframe.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ int32_t sframe_get_chunk(blosc2_frame_s* frame, int64_t nchunk, uint8_t** chunk,
110110
return BLOSC2_ERROR_PLUGIN_IO;
111111
}
112112

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

116115
if (io_cb->is_allocation_necessary) {
117116
*chunk = malloc((size_t)chunk_cbytes);

include/blosc2.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,8 +1037,7 @@ enum {
10371037

10381038
typedef void* (*blosc2_open_cb)(const char *urlpath, const char *mode, void *params);
10391039
typedef int (*blosc2_close_cb)(void *stream);
1040-
typedef int64_t (*blosc2_tell_cb)(void *stream);
1041-
typedef int (*blosc2_seek_cb)(void *stream, int64_t offset, int whence);
1040+
typedef int64_t (*blosc2_size_cb)(void *stream);
10421041
typedef int64_t (*blosc2_write_cb)(const void *ptr, int64_t size, int64_t nitems, int64_t position, void *stream);
10431042
typedef int64_t (*blosc2_read_cb)(void **ptr, int64_t size, int64_t nitems, int64_t position, void *stream);
10441043
typedef int (*blosc2_truncate_cb)(void *stream, int64_t size);
@@ -1060,10 +1059,8 @@ typedef struct {
10601059
//!< The IO open callback.
10611060
blosc2_close_cb close;
10621061
//!< The IO close callback.
1063-
blosc2_tell_cb tell;
1064-
//!< The IO tell callback.
1065-
blosc2_seek_cb seek;
1066-
//!< The IO seek callback.
1062+
blosc2_size_cb size;
1063+
//!< The IO size callback.
10671064
blosc2_write_cb write;
10681065
//!< The IO write callback.
10691066
blosc2_read_cb read;

include/blosc2/blosc2-stdio.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ typedef struct {
3838

3939
BLOSC_EXPORT void *blosc2_stdio_open(const char *urlpath, const char *mode, void* params);
4040
BLOSC_EXPORT int blosc2_stdio_close(void *stream);
41-
BLOSC_EXPORT int64_t blosc2_stdio_tell(void *stream);
42-
BLOSC_EXPORT int blosc2_stdio_seek(void *stream, int64_t offset, int whence);
41+
BLOSC_EXPORT int64_t blosc2_stdio_size(void *stream);
4342
BLOSC_EXPORT int64_t blosc2_stdio_write(const void *ptr, int64_t size, int64_t nitems, int64_t position, void *stream);
4443
BLOSC_EXPORT int64_t blosc2_stdio_read(void **ptr, int64_t size, int64_t nitems, int64_t position, void *stream);
4544
BLOSC_EXPORT int blosc2_stdio_truncate(void *stream, int64_t size);
@@ -69,8 +68,6 @@ typedef struct {
6968
//!< The size of the file.
7069
int64_t mapping_size;
7170
//!< The size of the mapping (mapping_size >= file_size).
72-
int64_t offset;
73-
//!< The current position inside the mapping.
7471
FILE* file;
7572
//!< The underlying file handle.
7673
int fd;
@@ -89,16 +86,15 @@ typedef struct {
8986
* @brief Default struct for memory-mapped I/O for user initialization.
9087
*/
9188
static const blosc2_stdio_mmap BLOSC2_STDIO_MMAP_DEFAULTS = {
92-
"r", (1 << 30), false, NULL, -1, -1, 0, NULL, -1, -1, -1
89+
"r", (1 << 30), false, NULL, -1, -1, NULL, -1, -1, -1
9390
#if defined(_WIN32)
9491
, INVALID_HANDLE_VALUE
9592
#endif
9693
};
9794

9895
BLOSC_EXPORT void *blosc2_stdio_mmap_open(const char *urlpath, const char *mode, void* params);
9996
BLOSC_EXPORT int blosc2_stdio_mmap_close(void *stream);
100-
BLOSC_EXPORT int64_t blosc2_stdio_mmap_tell(void *stream);
101-
BLOSC_EXPORT int blosc2_stdio_mmap_seek(void *stream, int64_t offset, int whence);
97+
BLOSC_EXPORT int64_t blosc2_stdio_mmap_size(void *stream);
10298
BLOSC_EXPORT int64_t blosc2_stdio_mmap_write(
10399
const void *ptr, int64_t size, int64_t nitems, int64_t position, void *stream);
104100
BLOSC_EXPORT int64_t blosc2_stdio_mmap_read(void **ptr, int64_t size, int64_t nitems, int64_t position, void *stream);

tests/test_udio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ int test_close(void *stream) {
4444
return err;
4545
}
4646

47-
int64_t test_tell(void *stream) {
47+
int64_t test_size(void *stream) {
4848
test_file *my = (test_file *) stream;
4949
my->params->tell++;
50-
return blosc2_stdio_tell(my->bfile);
50+
return blosc2_stdio_size(my->bfile);
5151
}
5252

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

0 commit comments

Comments
 (0)