|
| 1 | +# Copied from https://github.com/sagemath/cysignals/blob/master/src/cysignals/memory.pxd. |
| 2 | +# Note that this is no longer interruption safe, as we efficiently remove ``sig_block``/``sig_unblock``. |
| 3 | +""" |
| 4 | +Memory allocation functions which are interrupt-safe |
| 5 | +
|
| 6 | +The ``sig_`` variants are simple wrappers around the corresponding C |
| 7 | +functions. The ``check_`` variants check the return value and raise |
| 8 | +``MemoryError`` in case of failure. |
| 9 | +""" |
| 10 | + |
| 11 | +#***************************************************************************** |
| 12 | +# Copyright (C) 2011-2016 Jeroen Demeyer <J.Demeyer@UGent.be> |
| 13 | +# |
| 14 | +# cysignals is free software: you can redistribute it and/or modify it |
| 15 | +# under the terms of the GNU Lesser General Public License as published |
| 16 | +# by the Free Software Foundation, either version 3 of the License, or |
| 17 | +# (at your option) any later version. |
| 18 | +# |
| 19 | +# cysignals is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU Lesser General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU Lesser General Public License |
| 25 | +# along with cysignals. If not, see <http://www.gnu.org/licenses/>. |
| 26 | +# |
| 27 | +#***************************************************************************** |
| 28 | + |
| 29 | + |
| 30 | +cimport cython |
| 31 | +from libc.stdlib cimport malloc, calloc, realloc, free |
| 32 | +from .signals cimport sig_block, sig_unblock |
| 33 | + |
| 34 | +cdef extern from *: |
| 35 | + int unlikely(int) nogil # Defined by Cython |
| 36 | + |
| 37 | + |
| 38 | +cdef inline void* sig_malloc "sig_malloc"(size_t n) nogil: |
| 39 | + sig_block() |
| 40 | + cdef void* ret = malloc(n) |
| 41 | + sig_unblock() |
| 42 | + return ret |
| 43 | + |
| 44 | + |
| 45 | +cdef inline void* sig_realloc "sig_realloc"(void* ptr, size_t size) nogil: |
| 46 | + sig_block() |
| 47 | + cdef void* ret = realloc(ptr, size) |
| 48 | + sig_unblock() |
| 49 | + return ret |
| 50 | + |
| 51 | + |
| 52 | +cdef inline void* sig_calloc "sig_calloc"(size_t nmemb, size_t size) nogil: |
| 53 | + sig_block() |
| 54 | + cdef void* ret = calloc(nmemb, size) |
| 55 | + sig_unblock() |
| 56 | + return ret |
| 57 | + |
| 58 | + |
| 59 | +cdef inline void sig_free "sig_free"(void* ptr) nogil: |
| 60 | + sig_block() |
| 61 | + free(ptr) |
| 62 | + sig_unblock() |
| 63 | + |
| 64 | + |
| 65 | +@cython.cdivision(True) |
| 66 | +cdef inline size_t mul_overflowcheck(size_t a, size_t b) nogil: |
| 67 | + """ |
| 68 | + Return a*b, checking for overflow. Assume that a > 0. |
| 69 | + If overflow occurs, return <size_t>(-1). |
| 70 | + We assume that malloc(<size_t>-1) always fails. |
| 71 | + """ |
| 72 | + # If a and b both less than MUL_NO_OVERFLOW, no overflow can occur |
| 73 | + cdef size_t MUL_NO_OVERFLOW = ((<size_t>1) << (4*sizeof(size_t))) |
| 74 | + if a >= MUL_NO_OVERFLOW or b >= MUL_NO_OVERFLOW: |
| 75 | + if unlikely(b > (<size_t>-1) // a): |
| 76 | + return <size_t>(-1) |
| 77 | + return a*b |
| 78 | + |
| 79 | + |
| 80 | +cdef inline void* check_allocarray(size_t nmemb, size_t size) except? NULL: |
| 81 | + """ |
| 82 | + Allocate memory for ``nmemb`` elements of size ``size``. |
| 83 | + """ |
| 84 | + if nmemb == 0: |
| 85 | + return NULL |
| 86 | + cdef size_t n = mul_overflowcheck(nmemb, size) |
| 87 | + cdef void* ret = sig_malloc(n) |
| 88 | + if unlikely(ret == NULL): |
| 89 | + raise MemoryError("failed to allocate %s * %s bytes" % (nmemb, size)) |
| 90 | + return ret |
| 91 | + |
| 92 | + |
| 93 | +cdef inline void* check_reallocarray(void* ptr, size_t nmemb, size_t size) except? NULL: |
| 94 | + """ |
| 95 | + Re-allocate memory at ``ptr`` to hold ``nmemb`` elements of size |
| 96 | + ``size``. If ``ptr`` equals ``NULL``, this behaves as |
| 97 | + ``check_allocarray``. |
| 98 | +
|
| 99 | + When ``nmemb`` equals 0, then free the memory at ``ptr``. |
| 100 | + """ |
| 101 | + if nmemb == 0: |
| 102 | + sig_free(ptr) |
| 103 | + return NULL |
| 104 | + cdef size_t n = mul_overflowcheck(nmemb, size) |
| 105 | + cdef void* ret = sig_realloc(ptr, n) |
| 106 | + if unlikely(ret == NULL): |
| 107 | + raise MemoryError("failed to allocate %s * %s bytes" % (nmemb, size)) |
| 108 | + return ret |
| 109 | + |
| 110 | + |
| 111 | +cdef inline void* check_malloc(size_t n) except? NULL: |
| 112 | + """ |
| 113 | + Allocate ``n`` bytes of memory. |
| 114 | + """ |
| 115 | + if n == 0: |
| 116 | + return NULL |
| 117 | + cdef void* ret = sig_malloc(n) |
| 118 | + if unlikely(ret == NULL): |
| 119 | + raise MemoryError("failed to allocate %s bytes" % n) |
| 120 | + return ret |
| 121 | + |
| 122 | + |
| 123 | +cdef inline void* check_realloc(void* ptr, size_t n) except? NULL: |
| 124 | + """ |
| 125 | + Re-allocate memory at ``ptr`` to hold ``n`` bytes. |
| 126 | + If ``ptr`` equals ``NULL``, this behaves as ``check_malloc``. |
| 127 | + """ |
| 128 | + if n == 0: |
| 129 | + sig_free(ptr) |
| 130 | + return NULL |
| 131 | + cdef void* ret = sig_realloc(ptr, n) |
| 132 | + if unlikely(ret == NULL): |
| 133 | + raise MemoryError("failed to allocate %s bytes" % n) |
| 134 | + return ret |
| 135 | + |
| 136 | + |
| 137 | +cdef inline void* check_calloc(size_t nmemb, size_t size) except? NULL: |
| 138 | + """ |
| 139 | + Allocate memory for ``nmemb`` elements of size ``size``. The |
| 140 | + resulting memory is zeroed. |
| 141 | + """ |
| 142 | + if nmemb == 0: |
| 143 | + return NULL |
| 144 | + cdef void* ret = sig_calloc(nmemb, size) |
| 145 | + if unlikely(ret == NULL): |
| 146 | + raise MemoryError("failed to allocate %s * %s bytes" % (nmemb, size)) |
| 147 | + return ret |
0 commit comments