MCPcopy Create free account
hub / github.com/numpy/numpy / npyiter_allocate_buffers

Function npyiter_allocate_buffers

numpy/core/src/multiarray/nditer_api.c:1754–1804  ·  view source on GitHub ↗

* If errmsg is non-NULL, it should point to a variable which will * receive the error message, and no Python exception will be set. * This is so that the function can be called from code not holding * the GIL. */

Source from the content-addressed store, hash-verified

1752 * the GIL.
1753 */
1754NPY_NO_EXPORT int
1755npyiter_allocate_buffers(NpyIter *iter, char **errmsg)
1756{
1757 /*npy_uint32 itflags = NIT_ITFLAGS(iter);*/
1758 /*int ndim = NIT_NDIM(iter);*/
1759 int iop = 0, nop = NIT_NOP(iter);
1760
1761 npy_intp i;
1762 npyiter_opitflags *op_itflags = NIT_OPITFLAGS(iter);
1763 NpyIter_BufferData *bufferdata = NIT_BUFFERDATA(iter);
1764 PyArray_Descr **op_dtype = NIT_DTYPES(iter);
1765 npy_intp buffersize = NBF_BUFFERSIZE(bufferdata);
1766 char *buffer, **buffers = NBF_BUFFERS(bufferdata);
1767
1768 for (iop = 0; iop < nop; ++iop) {
1769 npyiter_opitflags flags = op_itflags[iop];
1770
1771 /*
1772 * If we have determined that a buffer may be needed,
1773 * allocate one.
1774 */
1775 if (!(flags&NPY_OP_ITFLAG_BUFNEVER)) {
1776 npy_intp itemsize = op_dtype[iop]->elsize;
1777 buffer = PyArray_malloc(itemsize*buffersize);
1778 if (buffer == NULL) {
1779 if (errmsg == NULL) {
1780 PyErr_NoMemory();
1781 }
1782 else {
1783 *errmsg = "out of memory";
1784 }
1785 goto fail;
1786 }
1787 if (PyDataType_FLAGCHK(op_dtype[iop], NPY_NEEDS_INIT)) {
1788 memset(buffer, '\0', itemsize*buffersize);
1789 }
1790 buffers[iop] = buffer;
1791 }
1792 }
1793
1794 return 1;
1795
1796fail:
1797 for (i = 0; i < iop; ++i) {
1798 if (buffers[i] != NULL) {
1799 PyArray_free(buffers[i]);
1800 buffers[i] = NULL;
1801 }
1802 }
1803 return 0;
1804}
1805
1806/*
1807 * This sets the AXISDATA portion of the iterator to the specified

Callers 3

NpyIter_AdvancedNewFunction · 0.85
NpyIter_ResetFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected