| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #ifndef Py_INTERNAL_BLOCKS_OUTPUT_BUFFER_H |
| #define Py_INTERNAL_BLOCKS_OUTPUT_BUFFER_H |
| #ifdef __cplusplus |
| extern "C" { |
| #endif |
|
|
| #include "Python.h" |
|
|
| #ifndef Py_BUILD_CORE |
| # error "this header requires Py_BUILD_CORE define" |
| #endif |
|
|
| typedef struct { |
| |
| PyObject *list; |
| |
| Py_ssize_t allocated; |
| |
| Py_ssize_t max_length; |
| } _BlocksOutputBuffer; |
|
|
| static const char unable_allocate_msg[] = "Unable to allocate output buffer."; |
|
|
| |
| #define OUTPUT_BUFFER_MAX_BLOCK_SIZE (256*1024*1024) |
|
|
| |
| #define KB (1024) |
| #define MB (1024*1024) |
| static const Py_ssize_t BUFFER_BLOCK_SIZE[] = |
| { 32*KB, 64*KB, 256*KB, 1*MB, 4*MB, 8*MB, 16*MB, 16*MB, |
| 32*MB, 32*MB, 32*MB, 32*MB, 64*MB, 64*MB, 128*MB, 128*MB, |
| OUTPUT_BUFFER_MAX_BLOCK_SIZE }; |
| #undef KB |
| #undef MB |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| static inline Py_ssize_t |
| _BlocksOutputBuffer_InitAndGrow(_BlocksOutputBuffer *buffer, |
| const Py_ssize_t max_length, |
| void **next_out) |
| { |
| PyObject *b; |
| Py_ssize_t block_size; |
|
|
| |
| assert(buffer->list == NULL); |
|
|
| |
| if (0 <= max_length && max_length < BUFFER_BLOCK_SIZE[0]) { |
| block_size = max_length; |
| } else { |
| block_size = BUFFER_BLOCK_SIZE[0]; |
| } |
|
|
| |
| b = PyBytes_FromStringAndSize(NULL, block_size); |
| if (b == NULL) { |
| return -1; |
| } |
|
|
| |
| buffer->list = PyList_New(1); |
| if (buffer->list == NULL) { |
| Py_DECREF(b); |
| return -1; |
| } |
| PyList_SET_ITEM(buffer->list, 0, b); |
|
|
| |
| buffer->allocated = block_size; |
| buffer->max_length = max_length; |
|
|
| *next_out = PyBytes_AS_STRING(b); |
| return block_size; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| static inline Py_ssize_t |
| _BlocksOutputBuffer_InitWithSize(_BlocksOutputBuffer *buffer, |
| const Py_ssize_t init_size, |
| void **next_out) |
| { |
| PyObject *b; |
|
|
| |
| assert(buffer->list == NULL); |
|
|
| |
| b = PyBytes_FromStringAndSize(NULL, init_size); |
| if (b == NULL) { |
| PyErr_SetString(PyExc_MemoryError, unable_allocate_msg); |
| return -1; |
| } |
|
|
| |
| buffer->list = PyList_New(1); |
| if (buffer->list == NULL) { |
| Py_DECREF(b); |
| return -1; |
| } |
| PyList_SET_ITEM(buffer->list, 0, b); |
|
|
| |
| buffer->allocated = init_size; |
| buffer->max_length = -1; |
|
|
| *next_out = PyBytes_AS_STRING(b); |
| return init_size; |
| } |
|
|
| |
| |
| |
| |
| |
| static inline Py_ssize_t |
| _BlocksOutputBuffer_Grow(_BlocksOutputBuffer *buffer, |
| void **next_out, |
| const Py_ssize_t avail_out) |
| { |
| PyObject *b; |
| const Py_ssize_t list_len = Py_SIZE(buffer->list); |
| Py_ssize_t block_size; |
|
|
| |
| if (avail_out != 0) { |
| PyErr_SetString(PyExc_SystemError, |
| "avail_out is non-zero in _BlocksOutputBuffer_Grow()."); |
| return -1; |
| } |
|
|
| |
| if (list_len < (Py_ssize_t) Py_ARRAY_LENGTH(BUFFER_BLOCK_SIZE)) { |
| block_size = BUFFER_BLOCK_SIZE[list_len]; |
| } else { |
| block_size = BUFFER_BLOCK_SIZE[Py_ARRAY_LENGTH(BUFFER_BLOCK_SIZE) - 1]; |
| } |
|
|
| |
| if (buffer->max_length >= 0) { |
| |
| Py_ssize_t rest = buffer->max_length - buffer->allocated; |
| assert(rest > 0); |
|
|
| |
| if (block_size > rest) { |
| block_size = rest; |
| } |
| } |
|
|
| |
| if (block_size > PY_SSIZE_T_MAX - buffer->allocated) { |
| PyErr_SetString(PyExc_MemoryError, unable_allocate_msg); |
| return -1; |
| } |
|
|
| |
| b = PyBytes_FromStringAndSize(NULL, block_size); |
| if (b == NULL) { |
| PyErr_SetString(PyExc_MemoryError, unable_allocate_msg); |
| return -1; |
| } |
| if (PyList_Append(buffer->list, b) < 0) { |
| Py_DECREF(b); |
| return -1; |
| } |
| Py_DECREF(b); |
|
|
| |
| buffer->allocated += block_size; |
|
|
| *next_out = PyBytes_AS_STRING(b); |
| return block_size; |
| } |
|
|
| |
| static inline Py_ssize_t |
| _BlocksOutputBuffer_GetDataSize(_BlocksOutputBuffer *buffer, |
| const Py_ssize_t avail_out) |
| { |
| return buffer->allocated - avail_out; |
| } |
|
|
| |
| |
| |
| |
| |
| static inline PyObject * |
| _BlocksOutputBuffer_Finish(_BlocksOutputBuffer *buffer, |
| const Py_ssize_t avail_out) |
| { |
| PyObject *result, *block; |
| const Py_ssize_t list_len = Py_SIZE(buffer->list); |
|
|
| |
| if ((list_len == 1 && avail_out == 0) || |
| (list_len == 2 && Py_SIZE(PyList_GET_ITEM(buffer->list, 1)) == avail_out)) |
| { |
| block = PyList_GET_ITEM(buffer->list, 0); |
| Py_INCREF(block); |
|
|
| Py_CLEAR(buffer->list); |
| return block; |
| } |
|
|
| |
| result = PyBytes_FromStringAndSize(NULL, buffer->allocated - avail_out); |
| if (result == NULL) { |
| PyErr_SetString(PyExc_MemoryError, unable_allocate_msg); |
| return NULL; |
| } |
|
|
| |
| if (list_len > 0) { |
| char *posi = PyBytes_AS_STRING(result); |
|
|
| |
| Py_ssize_t i = 0; |
| for (; i < list_len-1; i++) { |
| block = PyList_GET_ITEM(buffer->list, i); |
| memcpy(posi, PyBytes_AS_STRING(block), Py_SIZE(block)); |
| posi += Py_SIZE(block); |
| } |
| |
| block = PyList_GET_ITEM(buffer->list, i); |
| memcpy(posi, PyBytes_AS_STRING(block), Py_SIZE(block) - avail_out); |
| } else { |
| assert(Py_SIZE(result) == 0); |
| } |
|
|
| Py_CLEAR(buffer->list); |
| return result; |
| } |
|
|
| |
| static inline void |
| _BlocksOutputBuffer_OnError(_BlocksOutputBuffer *buffer) |
| { |
| Py_CLEAR(buffer->list); |
| } |
|
|
| #ifdef __cplusplus |
| } |
| #endif |
| #endif |
|
|