Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
51140159
"testing/python/vscode:/vscode.git/clone" did not exist on "2a286ae68cdeb63f9e504f03ca0e6658d39e1506"
Commit
51140159
authored
Jul 02, 2016
by
Ivan Smirnov
Browse files
Use malloc insterad of calloc for numpy arrays
parent
5412a05c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
4 deletions
+5
-4
include/pybind11/numpy.h
include/pybind11/numpy.h
+5
-4
No files found.
include/pybind11/numpy.h
View file @
51140159
...
@@ -126,16 +126,17 @@ public:
...
@@ -126,16 +126,17 @@ public:
Py_ssize_t
dims
[
32
];
Py_ssize_t
dims
[
32
];
API
&
api
=
lookup_api
();
API
&
api
=
lookup_api
();
// Allocate zeroed memory if it hasn't been provided by the caller.
// Allocate
non-
zeroed memory if it hasn't been provided by the caller.
// Normally, we could leave this null for NumPy to allocate memory for us, but
// Normally, we could leave this null for NumPy to allocate memory for us, but
// since we need a memoryview, the data pointer has to be non-null. NumPy uses
// since we need a memoryview, the data pointer has to be non-null. NumPy uses
// malloc if NPY_NEEDS_INIT is not set (in which case it uses calloc); however,
// malloc if NPY_NEEDS_INIT is not set (in which case it uses calloc); however,
// we don't have a descriptor yet (only a buffer format string), so we can't
// we don't have a desriptor yet (only a buffer format string), so we can't
// access the flags. The safest thing to do is thus to use calloc.
// access the flags. As long as we're not dealing with object dtypes/fields
// though, the memory doesn't have to be zeroed so we use malloc.
auto
buf_info
=
info
;
auto
buf_info
=
info
;
if
(
!
buf_info
.
ptr
)
if
(
!
buf_info
.
ptr
)
// always allocate at least 1 element, same way as NumPy does it
// always allocate at least 1 element, same way as NumPy does it
buf_info
.
ptr
=
std
::
c
alloc
(
std
::
max
(
info
.
size
,
(
size_t
)
1
)
,
info
.
itemsize
);
buf_info
.
ptr
=
std
::
m
alloc
(
std
::
max
(
info
.
size
,
(
size_t
)
1
)
*
info
.
itemsize
);
if
(
!
buf_info
.
ptr
)
if
(
!
buf_info
.
ptr
)
pybind11_fail
(
"NumPy: failed to allocate memory for buffer"
);
pybind11_fail
(
"NumPy: failed to allocate memory for buffer"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment