1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#ifndef _RANDOMDGEN__ALIGNED_MALLOC_H_
#define _RANDOMDGEN__ALIGNED_MALLOC_H_
#include <Python.h>
#include "numpy/npy_common.h"
#define NPY_MEMALIGN 16 /* 16 for SSE2, 32 for AVX, 64 for Xeon Phi */
static inline void *PyArray_realloc_aligned(void *p, size_t n)
{
void *p1, **p2, *base;
size_t old_offs, offs = NPY_MEMALIGN - 1 + sizeof(void *);
if (NPY_UNLIKELY(p != NULL))
{
base = *(((void **)p) - 1);
if (NPY_UNLIKELY((p1 = PyMem_Realloc(base, n + offs)) == NULL))
return NULL;
if (NPY_LIKELY(p1 == base))
return p;
p2 = (void **)(((Py_uintptr_t)(p1) + offs) & ~(NPY_MEMALIGN - 1));
old_offs = (size_t)((Py_uintptr_t)p - (Py_uintptr_t)base);
memmove((void *)p2, ((char *)p1) + old_offs, n);
}
else
{
if (NPY_UNLIKELY((p1 = PyMem_Malloc(n + offs)) == NULL))
return NULL;
p2 = (void **)(((Py_uintptr_t)(p1) + offs) & ~(NPY_MEMALIGN - 1));
}
*(p2 - 1) = p1;
return (void *)p2;
}
static inline void *PyArray_malloc_aligned(size_t n)
{
return PyArray_realloc_aligned(NULL, n);
}
static inline void *PyArray_calloc_aligned(size_t n, size_t s)
{
void *p;
if (NPY_UNLIKELY((p = PyArray_realloc_aligned(NULL, n * s)) == NULL))
return NULL;
memset(p, 0, n * s);
return p;
}
static inline void PyArray_free_aligned(void *p)
{
void *base = *(((void **)p) - 1);
PyMem_Free(base);
}
#endif
|