|
| 1 | +/* |
| 2 | + * Copyright 2026 Benoit Chesneau |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * @file py_buffer.h |
| 19 | + * @brief Zero-copy WSGI input buffer support |
| 20 | + * @author Benoit Chesneau |
| 21 | + * |
| 22 | + * This module provides a PyBuffer Python type that wraps a NIF-allocated |
| 23 | + * buffer resource and exposes it via the buffer protocol. Erlang can write |
| 24 | + * HTTP request body chunks to the buffer while Python reads them with |
| 25 | + * file-like methods (read, readline, readlines) or direct buffer access. |
| 26 | + * |
| 27 | + * The buffer supports blocking reads that release the GIL while waiting |
| 28 | + * for data from Erlang. |
| 29 | + * |
| 30 | + * Key features: |
| 31 | + * - Buffer protocol (memoryview(buf) works for zero-copy access) |
| 32 | + * - File-like interface (read, readline, readlines, seek, tell) |
| 33 | + * - Blocking reads with GIL released (uses pthread_cond) |
| 34 | + * - Iterator protocol for line-by-line reading |
| 35 | + */ |
| 36 | + |
| 37 | +#ifndef PY_BUFFER_H |
| 38 | +#define PY_BUFFER_H |
| 39 | + |
| 40 | +#include <Python.h> |
| 41 | +#include <erl_nif.h> |
| 42 | +#include <stdbool.h> |
| 43 | +#include <pthread.h> |
| 44 | + |
| 45 | +/* ============================================================================ |
| 46 | + * Configuration |
| 47 | + * ============================================================================ */ |
| 48 | + |
| 49 | +/** |
| 50 | + * @def PY_BUFFER_DEFAULT_CAPACITY |
| 51 | + * @brief Default buffer capacity when content_length is unknown (chunked) |
| 52 | + */ |
| 53 | +#define PY_BUFFER_DEFAULT_CAPACITY 65536 |
| 54 | + |
| 55 | +/** |
| 56 | + * @def PY_BUFFER_GROW_FACTOR |
| 57 | + * @brief Growth factor when buffer needs to expand |
| 58 | + */ |
| 59 | +#define PY_BUFFER_GROW_FACTOR 2 |
| 60 | + |
| 61 | +/* ============================================================================ |
| 62 | + * Buffer Resource Type |
| 63 | + * ============================================================================ */ |
| 64 | + |
| 65 | +/** |
| 66 | + * @brief Resource type for zero-copy input buffers |
| 67 | + */ |
| 68 | +extern ErlNifResourceType *PY_BUFFER_RESOURCE_TYPE; |
| 69 | + |
| 70 | +/** |
| 71 | + * @struct py_buffer_resource_t |
| 72 | + * @brief NIF resource that holds streaming input buffer data |
| 73 | + * |
| 74 | + * The buffer is written by Erlang (producer) and read by Python (consumer). |
| 75 | + * Uses pthread mutex/cond for thread-safe blocking reads. |
| 76 | + */ |
| 77 | +typedef struct { |
| 78 | + unsigned char *data; /**< Buffer data */ |
| 79 | + size_t capacity; /**< Allocated capacity */ |
| 80 | + size_t write_pos; /**< Current write position (producer) */ |
| 81 | + size_t read_pos; /**< Current read position (consumer) */ |
| 82 | + ssize_t content_length; /**< Expected total size, -1 for chunked */ |
| 83 | + pthread_mutex_t mutex; /**< Mutex for thread-safe access */ |
| 84 | + pthread_cond_t data_ready; /**< Condition for blocking reads */ |
| 85 | + bool eof; /**< End of data flag (close called) */ |
| 86 | + bool closed; /**< Buffer closed flag */ |
| 87 | + int view_count; /**< Active Python buffer view count */ |
| 88 | +} py_buffer_resource_t; |
| 89 | + |
| 90 | +/* ============================================================================ |
| 91 | + * Python Type |
| 92 | + * ============================================================================ */ |
| 93 | + |
| 94 | +/** |
| 95 | + * @brief The PyBuffer Python type object |
| 96 | + */ |
| 97 | +extern PyTypeObject PyBufferType; |
| 98 | + |
| 99 | +/** |
| 100 | + * @struct PyBufferObject |
| 101 | + * @brief Python object wrapping a py_buffer resource |
| 102 | + * |
| 103 | + * Provides file-like interface and buffer protocol for zero-copy access. |
| 104 | + */ |
| 105 | +typedef struct { |
| 106 | + PyObject_HEAD |
| 107 | + py_buffer_resource_t *resource; /**< NIF resource (we hold a reference) */ |
| 108 | + void *resource_ref; /**< For releasing the resource */ |
| 109 | +} PyBufferObject; |
| 110 | + |
| 111 | +/* ============================================================================ |
| 112 | + * Function Declarations - NIF Resource Management |
| 113 | + * ============================================================================ */ |
| 114 | + |
| 115 | +/** |
| 116 | + * @brief Allocate a new buffer resource |
| 117 | + * |
| 118 | + * @param content_length Expected size, or -1 for chunked encoding |
| 119 | + * @return New resource, or NULL on error |
| 120 | + */ |
| 121 | +py_buffer_resource_t *py_buffer_alloc(ssize_t content_length); |
| 122 | + |
| 123 | +/** |
| 124 | + * @brief Resource destructor |
| 125 | + */ |
| 126 | +void py_buffer_resource_dtor(ErlNifEnv *env, void *obj); |
| 127 | + |
| 128 | +/** |
| 129 | + * @brief Write data to the buffer (Erlang producer side) |
| 130 | + * |
| 131 | + * Appends data to the buffer, expanding if necessary. |
| 132 | + * Signals waiting readers when data is available. |
| 133 | + * |
| 134 | + * @param buf Buffer resource |
| 135 | + * @param data Data to write |
| 136 | + * @param size Size of data |
| 137 | + * @return 0 on success, -1 on error (buffer closed or alloc failure) |
| 138 | + */ |
| 139 | +int py_buffer_write(py_buffer_resource_t *buf, const unsigned char *data, size_t size); |
| 140 | + |
| 141 | +/** |
| 142 | + * @brief Close the buffer (Erlang producer side) |
| 143 | + * |
| 144 | + * Sets EOF flag and wakes up any waiting readers. |
| 145 | + * |
| 146 | + * @param buf Buffer resource |
| 147 | + */ |
| 148 | +void py_buffer_close(py_buffer_resource_t *buf); |
| 149 | + |
| 150 | +/* ============================================================================ |
| 151 | + * Function Declarations - Python Type |
| 152 | + * ============================================================================ */ |
| 153 | + |
| 154 | +/** |
| 155 | + * @brief Initialize the PyBuffer type |
| 156 | + * |
| 157 | + * Must be called during Python initialization with the GIL held. |
| 158 | + * |
| 159 | + * @return 0 on success, -1 on error |
| 160 | + */ |
| 161 | +int PyBuffer_init_type(void); |
| 162 | + |
| 163 | +/** |
| 164 | + * @brief Register PyBuffer with erlang module |
| 165 | + * |
| 166 | + * Makes PyBuffer accessible from Python. |
| 167 | + * |
| 168 | + * @return 0 on success, -1 on error |
| 169 | + * |
| 170 | + * @pre GIL must be held |
| 171 | + * @pre PyBuffer_init_type() must have been called |
| 172 | + * @pre erlang module must exist |
| 173 | + */ |
| 174 | +int PyBuffer_register_with_module(void); |
| 175 | + |
| 176 | +/** |
| 177 | + * @brief Create a PyBuffer from a NIF resource |
| 178 | + * |
| 179 | + * @param resource The buffer resource |
| 180 | + * @param resource_ref Resource reference (for enif_release_resource) |
| 181 | + * @return New PyBuffer object, or NULL on error |
| 182 | + * |
| 183 | + * @pre GIL must be held |
| 184 | + */ |
| 185 | +PyObject *PyBuffer_from_resource(py_buffer_resource_t *resource, |
| 186 | + void *resource_ref); |
| 187 | + |
| 188 | +#endif /* PY_BUFFER_H */ |
0 commit comments