Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Move memory functions to Cython
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemeyer committed Mar 2, 2015
1 parent b22c33b commit 145de10
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 166 deletions.
4 changes: 2 additions & 2 deletions src/c_lib/SConstruct
Expand Up @@ -128,12 +128,12 @@ env['PYV']=platform.python_version().rsplit('.', 1)[0]
# whitespace without the syntax clutter of lists of strings.
includes = ['$SAGE_LOCAL/include/', '$SAGE_LOCAL/include/python$PYV/',
'$SAGE_LOCAL/include/NTL/', 'include']
cFiles = Split( "interrupt.c memory.c mpn_pylong.c mpz_pylong.c") + \
cFiles = Split( "interrupt.c mpn_pylong.c mpz_pylong.c") + \
Split( "mpz_longlong.c stdsage.c" )
cppFiles = Split( "ZZ_pylong.cpp ntl_wrap.cpp" )
srcFiles = cFiles + cppFiles
incFiles = Split( "ccobject.h" ) + \
Split( "interrupt.h memory.h mpn_pylong.h mpz_longlong.h" ) + \
Split( "interrupt.h mpn_pylong.h mpz_longlong.h" ) + \
Split( "mpz_pylong.h ntl_wrap.h parisage.h stdsage.h ZZ_pylong.h" )

lib = env.SharedLibrary( "csage", [ "src/" + x for x in srcFiles ],
Expand Down
81 changes: 0 additions & 81 deletions src/c_lib/include/memory.h

This file was deleted.

1 change: 0 additions & 1 deletion src/c_lib/include/stdsage.h
Expand Up @@ -33,7 +33,6 @@
#define STDSAGE_H

#include "Python.h"
#include "memory.h"

/* Building with this not commented out causes
serious problems on RHEL5 64-bit for Kiran Kedlaya... i.e., it doesn't work. */
Expand Down
56 changes: 0 additions & 56 deletions src/c_lib/src/memory.c

This file was deleted.

1 change: 0 additions & 1 deletion src/c_lib/src/stdsage.c
Expand Up @@ -47,7 +47,6 @@ void global_NTL_error_callback(const char* s, void* context)
* Cygwin, this is also called from init_csage_module(). */
void init_csage() {
init_global_empty_tuple();
init_memory_functions();
setup_sage_signal_handler();
setup_NTL_error_callback(global_NTL_error_callback, NULL);
}
73 changes: 60 additions & 13 deletions src/sage/ext/memory.pxd
@@ -1,9 +1,9 @@
"""
Sage low-level memory allocation functions
Low-level memory allocation functions
"""

#*****************************************************************************
# Copyright (C) 2014 Jeroen Demeyer <jdemeyer@cage.ugent.be>
# Copyright (C) 2011-2015 Jeroen Demeyer <jdemeyer@cage.ugent.be>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -13,12 +13,55 @@ Sage low-level memory allocation functions
#*****************************************************************************


cdef extern from "memory.h":
size_t mul_overflowcheck(size_t a, size_t b)
void sage_free(void* ptr) nogil
void* sage_malloc(size_t) nogil
void* sage_realloc(void* ptr, size_t n) nogil
void* sage_calloc(size_t nmemb, size_t size) nogil
include 'sage/ext/interrupt.pxi'
cimport cython
from libc.stdlib cimport malloc, calloc, realloc, free

cdef extern from *:
int unlikely(int) nogil # Defined by Cython


cdef inline void* sage_malloc "sage_malloc"(size_t n) nogil:
sig_block()
cdef void* ret = malloc(n)
sig_unblock()
return ret


cdef inline void* sage_realloc "sage_realloc"(void* ptr, size_t size) nogil:
sig_block()
cdef void* ret = realloc(ptr, size)
sig_unblock()
return ret


cdef inline void* sage_calloc "sage_calloc"(size_t nmemb, size_t size) nogil:
sig_block()
cdef void* ret = calloc(nmemb, size)
sig_unblock()
return ret


cdef inline void sage_free "sage_free"(void* ptr) nogil:
sig_block()
free(ptr)
sig_unblock()


@cython.cdivision(True)
cdef inline size_t mul_overflowcheck(size_t a, size_t b) nogil:
"""
Return a*b, checking for overflow. Assume that a > 0.
If overflow occurs, return <size_t>(-1).
We assume that malloc(<size_t>-1) always fails.
"""
# If a and b both less than MUL_NO_OVERFLOW, no overflow can occur
cdef size_t MUL_NO_OVERFLOW = ((<size_t>1) << (4*sizeof(size_t)))
if a >= MUL_NO_OVERFLOW or b >= MUL_NO_OVERFLOW:
if unlikely(b > (<size_t>-1) // a):
return <size_t>(-1)
return a*b


cdef inline void* check_allocarray(size_t nmemb, size_t size) except? NULL:
"""
Expand All @@ -28,10 +71,11 @@ cdef inline void* check_allocarray(size_t nmemb, size_t size) except? NULL:
return NULL
cdef size_t n = mul_overflowcheck(nmemb, size)
cdef void* ret = sage_malloc(n)
if ret == NULL:
if unlikely(ret == NULL):
raise MemoryError("failed to allocate %s * %s bytes" % (nmemb, size))
return ret


cdef inline void* check_reallocarray(void* ptr, size_t nmemb, size_t size) except? NULL:
"""
Re-allocate memory at ``ptr`` to hold ``nmemb`` elements of size
Expand All @@ -45,21 +89,23 @@ cdef inline void* check_reallocarray(void* ptr, size_t nmemb, size_t size) excep
return NULL
cdef size_t n = mul_overflowcheck(nmemb, size)
cdef void* ret = sage_realloc(ptr, n)
if ret == NULL:
if unlikely(ret == NULL):
raise MemoryError("failed to allocate %s * %s bytes" % (nmemb, size))
return ret


cdef inline void* check_malloc(size_t n) except? NULL:
"""
Allocate ``n`` bytes of memory.
"""
if n == 0:
return NULL
cdef void* ret = sage_malloc(n)
if ret == NULL:
if unlikely(ret == NULL):
raise MemoryError("failed to allocate %s bytes" % n)
return ret


cdef inline void* check_realloc(void* ptr, size_t n) except? NULL:
"""
Re-allocate memory at ``ptr`` to hold ``n`` bytes.
Expand All @@ -69,10 +115,11 @@ cdef inline void* check_realloc(void* ptr, size_t n) except? NULL:
sage_free(ptr)
return NULL
cdef void* ret = sage_realloc(ptr, n)
if ret == NULL:
if unlikely(ret == NULL):
raise MemoryError("failed to allocate %s bytes" % n)
return ret


cdef inline void* check_calloc(size_t nmemb, size_t size) except? NULL:
"""
Allocate memory for ``nmemb`` elements of size ``size``. The
Expand All @@ -81,6 +128,6 @@ cdef inline void* check_calloc(size_t nmemb, size_t size) except? NULL:
if nmemb == 0:
return NULL
cdef void* ret = sage_calloc(nmemb, size)
if ret == NULL:
if unlikely(ret == NULL):
raise MemoryError("failed to allocate %s * %s bytes" % (nmemb, size))
return ret

0 comments on commit 145de10

Please sign in to comment.