Skip to content

Commit

Permalink
replace csize by csize_t
Browse files Browse the repository at this point in the history
This is an important change. Previously in 2019:
nim-lang/Nim#12187
the Nim documentation stated that `csize` was the same as `size_t` in
C. However, `csize` was always a signed integer.

I trusted this when wrapping the HDF5 library (it was my first Nim
project :P) which means any `csize` arguments that overflowed were
always buggy.
The only place where I stumbled on problems though are `H5T_VARIABLE`
which is defined as `csize_t.high` (or previously `csize.high`). This
then of course was different from the actual value expected for this
"special" value in the HDF5 library.

This commit should hopefully replace all occurences of `csize` by `csize_t`.
  • Loading branch information
Vindaar committed Nov 25, 2020
1 parent 5f0e467 commit 70e7bfb
Show file tree
Hide file tree
Showing 32 changed files with 288 additions and 288 deletions.
32 changes: 16 additions & 16 deletions src/blosc/blosc_plugin.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ when HasBloscSupport:

# need malloc and free, since we allocate memory, which needs to be freed
# by the HDF5 library
proc malloc(size: csize): pointer {.importc: "malloc", header: "stdlib.h".}
proc malloc(size: csize_t): pointer {.importc: "malloc", header: "stdlib.h".}
proc free(p: pointer) {.importc: "free", header: "stdlib.h".}

## Prototypes for filter function in bloscFilter.c
proc bloscFilter(flags: cuint, cd_nelmts: cint,
cd_values: ptr cuint, nbytes: csize,
cd_values: ptr cuint, nbytes: csize_t,
buf_size: ptr cint, buf: ptr pointer):
csize {.exportc: "blosc_filter", cdecl.}
csize_t {.exportc: "blosc_filter", cdecl.}

proc bloscSetLocal(dcpl, dtype, space: hid_t):
herr_t {.exportc: "blosc_set_local", cdecl.}
Expand Down Expand Up @@ -78,11 +78,11 @@ when HasBloscSupport:
## 3. Compute the chunk size in bytes and store it in slot 3.
var
r: herr_t
basetypesize: csize
bufsize: csize
basetypesize: csize_t
bufsize: csize_t
chunkdims = newSeq[hsize_t](32)
flags: cuint
nelements = 8.csize
nelements = 8.csize_t
values = newSeq[cuint](8)

r = getFilter(dcpl, FILTER_BLOSC, addr flags, addr nelements, addr values[0], 0, nil)
Expand Down Expand Up @@ -127,7 +127,7 @@ when HasBloscSupport:
# Get the size of the chunk
bufsize = typesize
for i in 0 ..< ndims:
bufsize = bufsize * chunkdims[i].csize
bufsize = bufsize * chunkdims[i].csize_t
values[3] = bufsize.cuint

when defined(BLOSC_DEBUG):
Expand All @@ -141,22 +141,22 @@ when HasBloscSupport:


proc bloscFilter(flags: cuint, cd_nelmts: cint,
cd_values: ptr cuint, nbytes: csize,
cd_values: ptr cuint, nbytes: csize_t,
buf_size: ptr cint, buf: ptr pointer):
csize {.exportc: "blosc_filter", cdecl.} =
csize_t {.exportc: "blosc_filter", cdecl.} =
## The filter function
var
outbuf: pointer
status = 0.cint # Return code from Blosc routines
outbuf_size: csize
outbuf_size: csize_t
clevel = 5.cint
doshuffle = 1.cint # Shuffle default
compname = "blosclz".cstring # The compressor by default
cd_val_arr = cast[ptr UncheckedArray[cuint]](cd_values)

# Filter params that are always set
let typesize = cd_val_arr[2].csize # The datatype size
outbuf_size = cd_val_arr[3].csize # Precomputed buffer guess
let typesize = cd_val_arr[2].csize_t # The datatype size
outbuf_size = cd_val_arr[3].csize_t # Precomputed buffer guess
# Optional params
if cd_nelmts >= 5.cint:
clevel = cd_val_arr[4].cint # The compression level
Expand Down Expand Up @@ -188,7 +188,7 @@ when HasBloscSupport:
# as optional, so HDF5 marks the chunk as uncompressed and
# proceeds.

outbuf_size = buf_size[].csize
outbuf_size = buf_size[].csize_t

when defined(BLOSC_DEBUG):
debugEcho "Blosc: Compress ", nbytes, " chunk w/buffer ", outbuf_size
Expand All @@ -212,8 +212,8 @@ when HasBloscSupport:
# We're decompressing
# declare dummy variables
var
cbytes: csize
blocksize: csize
cbytes: csize_t
blocksize: csize_t

# Extract the exact outbuf_size from the buffer header.
#
Expand Down Expand Up @@ -245,7 +245,7 @@ when HasBloscSupport:
free buf[]
buf[] = outbuf
buf_size[] = outbuf_size.cint
return status.csize # Size of compressed/decompressed data
return status.csize_t # Size of compressed/decompressed data

result = 0

Expand Down
4 changes: 2 additions & 2 deletions src/nimhdf5/attributes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ proc getAttrName*[T: SomeInteger](attr_id: hid_t, buf_space: T = 200): string =
debugEcho "Call to getAttrName! with size $#" % $buf_space
var name = newString(buf_space)
# read the name
let length = attr_id.H5Aget_name(len(name).csize, name)
let length = attr_id.H5Aget_name(len(name).csize_t, name)
# H5Aget_name returns the length of the name. In case the name
# is longer than the given buffer, we call this function again with
# a buffer with the correct length
if length <= name.len.csize:
if length <= name.len.csize_t:
result = name.strip
# now set the length of the resulting string to the size
# it actually occupies
Expand Down
2 changes: 1 addition & 1 deletion src/nimhdf5/datasets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ proc select_elements[T](dset: H5DataSet, coord: seq[T]): hid_t {.inline, discard
result = dset.dataspace_id
let res = H5Sselect_elements(result,
H5S_SELECT_SET,
csize(coord.len),
csize_t(coord.len),
addr(flat_coord[0]))
if res < 0:
raise newException(HDF5LibraryError, "Call to HDF5 library failed in `select_elements` " &
Expand Down
6 changes: 3 additions & 3 deletions src/nimhdf5/datatypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,15 @@ template toH5vlen*[T](data: var seq[T]): untyped =
when T is seq:
mapIt(toSeq(0..data.high)) do:
if data[it].len > 0:
hvl_t(`len`: csize(data[it].len), p: addr(data[it][0]))
hvl_t(`len`: csize_t(data[it].len), p: addr(data[it][0]))
else:
hvl_t(`len`: csize(0), p: nil)
hvl_t(`len`: csize_t(0), p: nil)
else:
# this doesn't make sense ?!...
static:
warning("T is " & T.name)
warning("Cannot be converted to VLEN data!")
#mapIt(toSeq(0 .. data.high), hvl_t(`len`: csize(data[it]), p: addr(data[it][0])))
#mapIt(toSeq(0 .. data.high), hvl_t(`len`: csize_t(data[it]), p: addr(data[it][0])))

proc vlenToSeq*[T](data: seq[hvl_t]): seq[seq[T]] =
# converting the raw data from the C library to a Nim sequence is sort of ugly, but
Expand Down
2 changes: 1 addition & 1 deletion src/nimhdf5/filters.nim
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ proc setFilters*(dset: H5DataSet, filter: H5Filter) =
filterVals[6] = filter.compressor.cuint
# set the filter
status = H5Pset_filter(dset.dcpl_id, FILTER_BLOSC, H5Z_FLAG_OPTIONAL,
filterVals.len.csize, addr filterVals[0])
filterVals.len.csize_t, addr filterVals[0])
else:
raise newException(NotImplementedError, "Blosc support not available, due " &
"to missing `nblosc` library!")
Expand Down
12 changes: 6 additions & 6 deletions src/nimhdf5/hl/H5DOpublic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ when not declared(libname_hl):
else:
const
libname_hl* = "libhdf5_hl.so"


## -------------------------------------------------------------------------
##
##
## "Optimized dataset" routines.
##
##
## -------------------------------------------------------------------------
##
##

proc H5DOwrite_chunk*(dset_id: hid_t; dxpl_id: hid_t; filters: uint32_t;
offset: ptr hsize_t; data_size: csize; buf: pointer): herr_t {.
offset: ptr hsize_t; data_size: csize_t; buf: pointer): herr_t {.
cdecl, importc: "H5DOwrite_chunk", dynlib: libname_hl.}
proc H5DOappend*(dset_id: hid_t; dxpl_id: hid_t; axis: cuint; extension: csize;
proc H5DOappend*(dset_id: hid_t; dxpl_id: hid_t; axis: cuint; extension: csize_t;
memtype: hid_t; buf: pointer): herr_t {.cdecl, importc: "H5DOappend",
dynlib: libname_hl.}
4 changes: 2 additions & 2 deletions src/nimhdf5/hl/H5DSpublic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ proc H5DSget_num_scales*(did: hid_t; dim: cuint): cint {.cdecl,
importc: "H5DSget_num_scales", dynlib: libname_hl.}
proc H5DSset_label*(did: hid_t; idx: cuint; label: cstring): herr_t {.cdecl,
importc: "H5DSset_label", dynlib: libname_hl.}
proc H5DSget_label*(did: hid_t; idx: cuint; label: cstring; size: csize): ssize_t {.cdecl,
proc H5DSget_label*(did: hid_t; idx: cuint; label: cstring; size: csize_t): ssize_t {.cdecl,
importc: "H5DSget_label", dynlib: libname_hl.}
proc H5DSget_scale_name*(did: hid_t; name: cstring; size: csize): ssize_t {.cdecl,
proc H5DSget_scale_name*(did: hid_t; name: cstring; size: csize_t): ssize_t {.cdecl,
importc: "H5DSget_scale_name", dynlib: libname_hl.}
proc H5DSis_scale*(did: hid_t): htri_t {.cdecl, importc: "H5DSis_scale",
dynlib: libname_hl.}
Expand Down
2 changes: 1 addition & 1 deletion src/nimhdf5/hl/H5LDpublic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ when not declared(libname_hl):

proc H5LDget_dset_dims*(did: hid_t; cur_dims: ptr hsize_t): herr_t {.cdecl,
importc: "H5LDget_dset_dims", dynlib: libname_hl.}
proc H5LDget_dset_type_size*(did: hid_t; fields: cstring): csize {.cdecl,
proc H5LDget_dset_type_size*(did: hid_t; fields: cstring): csize_t {.cdecl,
importc: "H5LDget_dset_type_size", dynlib: libname_hl.}
proc H5LDget_dset_elmts*(did: hid_t; prev_dims: ptr hsize_t; cur_dims: ptr hsize_t;
fields: cstring; buf: pointer): herr_t {.cdecl,
Expand Down
84 changes: 42 additions & 42 deletions src/nimhdf5/hl/H5LTpublic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ type


## -------------------------------------------------------------------------
##
##
## Make dataset functions
##
##
## -------------------------------------------------------------------------
##
##

proc H5LTmake_dataset*(loc_id: hid_t; dset_name: cstring; rank: cint;
dims: ptr hsize_t; type_id: hid_t; buffer: pointer): herr_t {.
Expand All @@ -80,11 +80,11 @@ proc H5LTmake_dataset_double*(loc_id: hid_t; dset_name: cstring; rank: cint;
proc H5LTmake_dataset_string*(loc_id: hid_t; dset_name: cstring; buf: cstring): herr_t {.
cdecl, importc: "H5LTmake_dataset_string", dynlib: libname_hl.}
## -------------------------------------------------------------------------
##
##
## Read dataset functions
##
##
## -------------------------------------------------------------------------
##
##

proc H5LTread_dataset*(loc_id: hid_t; dset_name: cstring; type_id: hid_t;
buffer: pointer): herr_t {.cdecl, importc: "H5LTread_dataset",
Expand All @@ -104,69 +104,69 @@ proc H5LTread_dataset_double*(loc_id: hid_t; dset_name: cstring; buffer: ptr cdo
proc H5LTread_dataset_string*(loc_id: hid_t; dset_name: cstring; buf: cstring): herr_t {.
cdecl, importc: "H5LTread_dataset_string", dynlib: libname_hl.}
## -------------------------------------------------------------------------
##
##
## Query dataset functions
##
##
## -------------------------------------------------------------------------
##
##

proc H5LTget_dataset_ndims*(loc_id: hid_t; dset_name: cstring; rank: ptr cint): herr_t {.
cdecl, importc: "H5LTget_dataset_ndims", dynlib: libname_hl.}
proc H5LTget_dataset_info*(loc_id: hid_t; dset_name: cstring; dims: ptr hsize_t;
type_class: ptr H5T_class_t; type_size: ptr csize): herr_t {.
type_class: ptr H5T_class_t; type_size: ptr csize_t): herr_t {.
cdecl, importc: "H5LTget_dataset_info", dynlib: libname_hl.}
proc H5LTfind_dataset*(loc_id: hid_t; name: cstring): herr_t {.cdecl,
importc: "H5LTfind_dataset", dynlib: libname_hl.}
## -------------------------------------------------------------------------
##
##
## Set attribute functions
##
##
## -------------------------------------------------------------------------
##
##

proc H5LTset_attribute_string*(loc_id: hid_t; obj_name: cstring; attr_name: cstring;
attr_data: cstring): herr_t {.cdecl,
importc: "H5LTset_attribute_string", dynlib: libname_hl.}
proc H5LTset_attribute_char*(loc_id: hid_t; obj_name: cstring; attr_name: cstring;
buffer: cstring; size: csize): herr_t {.cdecl,
buffer: cstring; size: csize_t): herr_t {.cdecl,
importc: "H5LTset_attribute_char", dynlib: libname_hl.}
proc H5LTset_attribute_uchar*(loc_id: hid_t; obj_name: cstring; attr_name: cstring;
buffer: ptr cuchar; size: csize): herr_t {.cdecl,
buffer: ptr cuchar; size: csize_t): herr_t {.cdecl,
importc: "H5LTset_attribute_uchar", dynlib: libname_hl.}
proc H5LTset_attribute_short*(loc_id: hid_t; obj_name: cstring; attr_name: cstring;
buffer: ptr cshort; size: csize): herr_t {.cdecl,
buffer: ptr cshort; size: csize_t): herr_t {.cdecl,
importc: "H5LTset_attribute_short", dynlib: libname_hl.}
proc H5LTset_attribute_ushort*(loc_id: hid_t; obj_name: cstring; attr_name: cstring;
buffer: ptr cushort; size: csize): herr_t {.cdecl,
buffer: ptr cushort; size: csize_t): herr_t {.cdecl,
importc: "H5LTset_attribute_ushort", dynlib: libname_hl.}
proc H5LTset_attribute_int*(loc_id: hid_t; obj_name: cstring; attr_name: cstring;
buffer: ptr cint; size: csize): herr_t {.cdecl,
buffer: ptr cint; size: csize_t): herr_t {.cdecl,
importc: "H5LTset_attribute_int", dynlib: libname_hl.}
proc H5LTset_attribute_uint*(loc_id: hid_t; obj_name: cstring; attr_name: cstring;
buffer: ptr cuint; size: csize): herr_t {.cdecl,
buffer: ptr cuint; size: csize_t): herr_t {.cdecl,
importc: "H5LTset_attribute_uint", dynlib: libname_hl.}
proc H5LTset_attribute_long*(loc_id: hid_t; obj_name: cstring; attr_name: cstring;
buffer: ptr clong; size: csize): herr_t {.cdecl,
buffer: ptr clong; size: csize_t): herr_t {.cdecl,
importc: "H5LTset_attribute_long", dynlib: libname_hl.}
proc H5LTset_attribute_long_long*(loc_id: hid_t; obj_name: cstring;
attr_name: cstring; buffer: ptr clonglong;
size: csize): herr_t {.cdecl,
size: csize_t): herr_t {.cdecl,
importc: "H5LTset_attribute_long_long", dynlib: libname_hl.}
proc H5LTset_attribute_ulong*(loc_id: hid_t; obj_name: cstring; attr_name: cstring;
buffer: ptr culong; size: csize): herr_t {.cdecl,
buffer: ptr culong; size: csize_t): herr_t {.cdecl,
importc: "H5LTset_attribute_ulong", dynlib: libname_hl.}
proc H5LTset_attribute_float*(loc_id: hid_t; obj_name: cstring; attr_name: cstring;
buffer: ptr cfloat; size: csize): herr_t {.cdecl,
buffer: ptr cfloat; size: csize_t): herr_t {.cdecl,
importc: "H5LTset_attribute_float", dynlib: libname_hl.}
proc H5LTset_attribute_double*(loc_id: hid_t; obj_name: cstring; attr_name: cstring;
buffer: ptr cdouble; size: csize): herr_t {.cdecl,
buffer: ptr cdouble; size: csize_t): herr_t {.cdecl,
importc: "H5LTset_attribute_double", dynlib: libname_hl.}
## -------------------------------------------------------------------------
##
##
## Get attribute functions
##
##
## -------------------------------------------------------------------------
##
##

proc H5LTget_attribute*(loc_id: hid_t; obj_name: cstring; attr_name: cstring;
mem_type_id: hid_t; data: pointer): herr_t {.cdecl,
Expand Down Expand Up @@ -208,48 +208,48 @@ proc H5LTget_attribute_double*(loc_id: hid_t; obj_name: cstring; attr_name: cstr
data: ptr cdouble): herr_t {.cdecl,
importc: "H5LTget_attribute_double", dynlib: libname_hl.}
## -------------------------------------------------------------------------
##
##
## Query attribute functions
##
##
## -------------------------------------------------------------------------
##
##

proc H5LTget_attribute_ndims*(loc_id: hid_t; obj_name: cstring; attr_name: cstring;
rank: ptr cint): herr_t {.cdecl,
importc: "H5LTget_attribute_ndims", dynlib: libname_hl.}
proc H5LTget_attribute_info*(loc_id: hid_t; obj_name: cstring; attr_name: cstring;
dims: ptr hsize_t; type_class: ptr H5T_class_t;
type_size: ptr csize): herr_t {.cdecl,
type_size: ptr csize_t): herr_t {.cdecl,
importc: "H5LTget_attribute_info", dynlib: libname_hl.}
## -------------------------------------------------------------------------
##
##
## General functions
##
##
## -------------------------------------------------------------------------
##
##

proc H5LTtext_to_dtype*(text: cstring; lang_type: H5LT_lang_t): hid_t {.cdecl,
importc: "H5LTtext_to_dtype", dynlib: libname_hl.}
proc H5LTdtype_to_text*(dtype: hid_t; str: cstring; lang_type: H5LT_lang_t;
len: ptr csize): herr_t {.cdecl, importc: "H5LTdtype_to_text",
len: ptr csize_t): herr_t {.cdecl, importc: "H5LTdtype_to_text",
dynlib: libname_hl.}
## -------------------------------------------------------------------------
##
##
## Utility functions
##
##
## -------------------------------------------------------------------------
##
##

proc H5LTfind_attribute*(loc_id: hid_t; name: cstring): herr_t {.cdecl,
importc: "H5LTfind_attribute", dynlib: libname_hl.}
proc H5LTpath_valid*(loc_id: hid_t; path: cstring; check_object_valid: hbool_t): htri_t {.
cdecl, importc: "H5LTpath_valid", dynlib: libname_hl.}
## -------------------------------------------------------------------------
##
##
## File image operations functions
##
##
## -------------------------------------------------------------------------
##
##

proc H5LTopen_file_image*(buf_ptr: pointer; buf_size: csize; flags: cuint): hid_t {.
proc H5LTopen_file_image*(buf_ptr: pointer; buf_size: csize_t; flags: cuint): hid_t {.
cdecl, importc: "H5LTopen_file_image", dynlib: libname_hl.}
Loading

0 comments on commit 70e7bfb

Please sign in to comment.