Skip to content

Commit 09186fe

Browse files
authored
Merge pull request #109 from bashtage/simplify-bounded-int
Simplify bounded int
2 parents 0a55ecb + 859711a commit 09186fe

File tree

6 files changed

+720
-684
lines changed

6 files changed

+720
-684
lines changed

randomstate/aligned_malloc.h

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
11
#include "Python.h"
22
#include "numpy/npy_common.h"
33

4-
#define NPY_MEMALIGN 16 /* 16 for SSE2, 32 for AVX, 64 for Xeon Phi */
4+
#define NPY_MEMALIGN 16 /* 16 for SSE2, 32 for AVX, 64 for Xeon Phi */
55

6-
static NPY_INLINE
7-
void *PyArray_realloc_aligned(void *p, size_t n)
6+
static NPY_INLINE void *PyArray_realloc_aligned(void *p, size_t n)
87
{
98
void *p1, **p2, *base;
10-
size_t old_offs, offs = NPY_MEMALIGN - 1 + sizeof(void*);
11-
if (NPY_UNLIKELY(p != NULL)) {
12-
base = *(((void**)p)-1);
13-
if (NPY_UNLIKELY((p1 = PyMem_Realloc(base,n+offs)) == NULL)) return NULL;
14-
if (NPY_LIKELY(p1 == base)) return p;
15-
p2 = (void**)(((Py_uintptr_t)(p1)+offs) & ~(NPY_MEMALIGN-1));
9+
size_t old_offs, offs = NPY_MEMALIGN - 1 + sizeof(void *);
10+
if (NPY_UNLIKELY(p != NULL))
11+
{
12+
base = *(((void **)p) - 1);
13+
if (NPY_UNLIKELY((p1 = PyMem_Realloc(base, n + offs)) == NULL))
14+
return NULL;
15+
if (NPY_LIKELY(p1 == base))
16+
return p;
17+
p2 = (void **)(((Py_uintptr_t)(p1) + offs) & ~(NPY_MEMALIGN - 1));
1618
old_offs = (size_t)((Py_uintptr_t)p - (Py_uintptr_t)base);
17-
memmove((void*)p2,((char*)p1)+old_offs,n);
18-
} else {
19-
if (NPY_UNLIKELY((p1 = PyMem_Malloc(n + offs)) == NULL)) return NULL;
20-
p2 = (void**)(((Py_uintptr_t)(p1)+offs) & ~(NPY_MEMALIGN-1));
19+
memmove((void *)p2, ((char *)p1) + old_offs, n);
2120
}
22-
*(p2-1) = p1;
23-
return (void*)p2;
21+
else
22+
{
23+
if (NPY_UNLIKELY((p1 = PyMem_Malloc(n + offs)) == NULL))
24+
return NULL;
25+
p2 = (void **)(((Py_uintptr_t)(p1) + offs) & ~(NPY_MEMALIGN - 1));
26+
}
27+
*(p2 - 1) = p1;
28+
return (void *)p2;
2429
}
2530

26-
static NPY_INLINE
27-
void *PyArray_malloc_aligned(size_t n)
31+
static NPY_INLINE void *PyArray_malloc_aligned(size_t n)
2832
{
2933
return PyArray_realloc_aligned(NULL, n);
3034
}
3135

32-
static NPY_INLINE
33-
void *PyArray_calloc_aligned(size_t n, size_t s)
36+
static NPY_INLINE void *PyArray_calloc_aligned(size_t n, size_t s)
3437
{
3538
void *p;
36-
if (NPY_UNLIKELY((p = PyArray_realloc_aligned(NULL,n*s)) == NULL)) return NULL;
37-
memset(p, 0, n*s);
39+
if (NPY_UNLIKELY((p = PyArray_realloc_aligned(NULL, n * s)) == NULL))
40+
return NULL;
41+
memset(p, 0, n * s);
3842
return p;
3943
}
4044

41-
static NPY_INLINE
42-
void PyArray_free_aligned(void *p)
45+
static NPY_INLINE void PyArray_free_aligned(void *p)
4346
{
44-
void *base = *(((void**)p)-1);
47+
void *base = *(((void **)p) - 1);
4548
PyMem_Free(base);
4649
}

0 commit comments

Comments
 (0)