Bug Summary

File:nnc/ccv_nnc_xpu_alloc.c
Warning:line 16, column 1
Access to field 'node' results in a dereference of a null pointer (loaded from variable 'nodep')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name ccv_nnc_xpu_alloc.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -target-feature +sse2 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/home/liu/buildslave/linux-x64-runtests/build/lib/nnc -resource-dir /usr/local/lib/clang/14.0.0 -I ../ -I /usr/local/cuda/include -D HAVE_CBLAS -D HAVE_LIBPNG -D HAVE_LIBJPEG -D HAVE_FFTW3 -D HAVE_PTHREAD -D HAVE_LIBLINEAR -D HAVE_TESSERACT -D HAVE_AVCODEC -D HAVE_AVFORMAT -D HAVE_AVUTIL -D HAVE_SWSCALE -D USE_DISPATCH -D HAVE_SSE2 -D HAVE_GSL -D HAVE_CUDA -D HAVE_CUDNN -D HAVE_NCCL -D USE_SYSTEM_CUB -I /usr/local/include -internal-isystem /usr/local/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/9/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -fdebug-compilation-dir=/home/liu/buildslave/linux-x64-runtests/build/lib/nnc -ferror-limit 19 -fblocks -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/liu/buildslave/public_html/analyze/2022-06-24-190241-827281-1 -x c ccv_nnc_xpu_alloc.c
1#include "ccv_nnc.h"
2#include "ccv_nnc_easy.h"
3#include "ccv_nnc_internal.h"
4#include "ccv_nnc_easy.h"
5#include "ccv_internal.h"
6#include "_ccv_nnc_xpu_alloc.h"
7#ifdef HAVE_CUDA1
8#include "gpu/ccv_nnc_compat.h"
9#include <stdbool.h>
10
11static int dy_alloc_tree_cmp(const dy_alloc_metadata_t* const a_node, const dy_alloc_metadata_t* const b_node)
12{
13 return (a_node->size > b_node->size) - (b_node->size > a_node->size);
14}
15
16rb_gen(, dy_alloc_tree_, dy_alloc_tree_t, dy_alloc_metadata_t, link, dy_alloc_tree_cmp)void dy_alloc_tree_new(dy_alloc_tree_t *rbtree) { do { (rbtree
)->rbt_root = ((void*)0); } while (0); } _Bool dy_alloc_tree_empty
(dy_alloc_tree_t *rbtree) { return (rbtree->rbt_root == ((
void*)0)); } dy_alloc_metadata_t * dy_alloc_tree_first(dy_alloc_tree_t
*rbtree) { dy_alloc_metadata_t *ret; do { (ret) = (rbtree->
rbt_root); if ((ret) != ((void*)0)) { for (; (((ret))->link
.rbn_left) != ((void*)0); (ret) = (((ret))->link.rbn_left)
) { } } } while (0); return ret; } dy_alloc_metadata_t * dy_alloc_tree_last
(dy_alloc_tree_t *rbtree) { dy_alloc_metadata_t *ret; do { (ret
) = (rbtree->rbt_root); if ((ret) != ((void*)0)) { for (; (
(dy_alloc_metadata_t *) (((intptr_t) ((ret))->link.rbn_right_red
) & ((ssize_t)-2))) != ((void*)0); (ret) = ((dy_alloc_metadata_t
*) (((intptr_t) ((ret))->link.rbn_right_red) & ((ssize_t
)-2)))) { } } } while (0); return ret; } dy_alloc_metadata_t *
dy_alloc_tree_next(dy_alloc_tree_t *rbtree, dy_alloc_metadata_t
*node) { dy_alloc_metadata_t *ret; if (((dy_alloc_metadata_t
*) (((intptr_t) (node)->link.rbn_right_red) & ((ssize_t
)-2))) != ((void*)0)) { do { (ret) = (((dy_alloc_metadata_t *
) (((intptr_t) (node)->link.rbn_right_red) & ((ssize_t
)-2)))); if ((ret) != ((void*)0)) { for (; (((ret))->link.
rbn_left) != ((void*)0); (ret) = (((ret))->link.rbn_left))
{ } } } while (0); } else { dy_alloc_metadata_t *tnode = rbtree
->rbt_root; ((void) sizeof ((tnode != ((void*)0)) ? 1 : 0)
, __extension__ ({ if (tnode != ((void*)0)) ; else __assert_fail
("tnode != NULL", "ccv_nnc_xpu_alloc.c", 16, __extension__ __PRETTY_FUNCTION__
); })); ret = ((void*)0); while (1) { int cmp = (dy_alloc_tree_cmp
)(node, tnode); if (cmp < 0) { ret = tnode; tnode = ((tnode
)->link.rbn_left); } else if (cmp > 0) { tnode = ((dy_alloc_metadata_t
*) (((intptr_t) (tnode)->link.rbn_right_red) & ((ssize_t
)-2))); } else { break; } ((void) sizeof ((tnode != ((void*)0
)) ? 1 : 0), __extension__ ({ if (tnode != ((void*)0)) ; else
__assert_fail ("tnode != NULL", "ccv_nnc_xpu_alloc.c", 16, __extension__
__PRETTY_FUNCTION__); })); } } return ret; } dy_alloc_metadata_t
* dy_alloc_tree_prev(dy_alloc_tree_t *rbtree, dy_alloc_metadata_t
*node) { dy_alloc_metadata_t *ret; if (((node)->link.rbn_left
) != ((void*)0)) { do { (ret) = (((node)->link.rbn_left));
if ((ret) != ((void*)0)) { for (; ((dy_alloc_metadata_t *) (
((intptr_t) ((ret))->link.rbn_right_red) & ((ssize_t)-
2))) != ((void*)0); (ret) = ((dy_alloc_metadata_t *) (((intptr_t
) ((ret))->link.rbn_right_red) & ((ssize_t)-2)))) { } }
} while (0); } else { dy_alloc_metadata_t *tnode = rbtree->
rbt_root; ((void) sizeof ((tnode != ((void*)0)) ? 1 : 0), __extension__
({ if (tnode != ((void*)0)) ; else __assert_fail ("tnode != NULL"
, "ccv_nnc_xpu_alloc.c", 16, __extension__ __PRETTY_FUNCTION__
); })); ret = ((void*)0); while (1) { int cmp = (dy_alloc_tree_cmp
)(node, tnode); if (cmp < 0) { tnode = ((tnode)->link.rbn_left
); } else if (cmp > 0) { ret = tnode; tnode = ((dy_alloc_metadata_t
*) (((intptr_t) (tnode)->link.rbn_right_red) & ((ssize_t
)-2))); } else { break; } ((void) sizeof ((tnode != ((void*)0
)) ? 1 : 0), __extension__ ({ if (tnode != ((void*)0)) ; else
__assert_fail ("tnode != NULL", "ccv_nnc_xpu_alloc.c", 16, __extension__
__PRETTY_FUNCTION__); })); } } return ret; } dy_alloc_metadata_t
* dy_alloc_tree_search(dy_alloc_tree_t *rbtree, const dy_alloc_metadata_t
*key) { dy_alloc_metadata_t *ret; int cmp; ret = rbtree->
rbt_root; while (ret != ((void*)0) && (cmp = (dy_alloc_tree_cmp
)(key, ret)) != 0) { if (cmp < 0) { ret = ((ret)->link.
rbn_left); } else { ret = ((dy_alloc_metadata_t *) (((intptr_t
) (ret)->link.rbn_right_red) & ((ssize_t)-2))); } } return
ret; } dy_alloc_metadata_t * dy_alloc_tree_nsearch(dy_alloc_tree_t
*rbtree, const dy_alloc_metadata_t *key) { dy_alloc_metadata_t
*ret; dy_alloc_metadata_t *tnode = rbtree->rbt_root; ret =
((void*)0); while (tnode != ((void*)0)) { int cmp = (dy_alloc_tree_cmp
)(key, tnode); if (cmp < 0) { ret = tnode; tnode = ((tnode
)->link.rbn_left); } else if (cmp > 0) { tnode = ((dy_alloc_metadata_t
*) (((intptr_t) (tnode)->link.rbn_right_red) & ((ssize_t
)-2))); } else { ret = tnode; break; } } return ret; } dy_alloc_metadata_t
* dy_alloc_tree_psearch(dy_alloc_tree_t *rbtree, const dy_alloc_metadata_t
*key) { dy_alloc_metadata_t *ret; dy_alloc_metadata_t *tnode
= rbtree->rbt_root; ret = ((void*)0); while (tnode != ((void
*)0)) { int cmp = (dy_alloc_tree_cmp)(key, tnode); if (cmp <
0) { tnode = ((tnode)->link.rbn_left); } else if (cmp >
0) { ret = tnode; tnode = ((dy_alloc_metadata_t *) (((intptr_t
) (tnode)->link.rbn_right_red) & ((ssize_t)-2))); } else
{ ret = tnode; break; } } return ret; } void dy_alloc_tree_insert
(dy_alloc_tree_t *rbtree, dy_alloc_metadata_t *node) { struct
{ dy_alloc_metadata_t *node; int cmp; } path[sizeof(void *) <<
4], *pathp; do { ((void) sizeof ((((uintptr_t)(node) & 0x1
) == 0) ? 1 : 0), __extension__ ({ if (((uintptr_t)(node) &
0x1) == 0) ; else __assert_fail ("((uintptr_t)(node) & 0x1) == 0"
, "ccv_nnc_xpu_alloc.c", 16, __extension__ __PRETTY_FUNCTION__
); })); do { ((node))->link.rbn_left = ((void*)0); } while
(0); do { ((node))->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) ((void*)0)) | (((uintptr_t) ((node))->link
.rbn_right_red) & ((size_t)1))); } while (0); do { ((node
))->link.rbn_right_red = (dy_alloc_metadata_t *) (((uintptr_t
) ((node))->link.rbn_right_red) | ((size_t)1)); } while (0
); } while (0); path->node = rbtree->rbt_root; for (pathp
= path; pathp->node != ((void*)0); pathp++) { int cmp = pathp
->cmp = dy_alloc_tree_cmp(node, pathp->node); ((void) sizeof
((cmp != 0) ? 1 : 0), __extension__ ({ if (cmp != 0) ; else __assert_fail
("cmp != 0", "ccv_nnc_xpu_alloc.c", 16, __extension__ __PRETTY_FUNCTION__
); })); if (cmp < 0) { pathp[1].node = ((pathp->node)->
link.rbn_left); } else { pathp[1].node = ((dy_alloc_metadata_t
*) (((intptr_t) (pathp->node)->link.rbn_right_red) &
((ssize_t)-2))); } } pathp->node = node; for (pathp--; (uintptr_t
)pathp >= (uintptr_t)path; pathp--) { dy_alloc_metadata_t *
cnode = pathp->node; if (pathp->cmp < 0) { dy_alloc_metadata_t
*left = pathp[1].node; do { (cnode)->link.rbn_left = left
; } while (0); if (((_Bool) (((uintptr_t) (left)->link.rbn_right_red
) & ((size_t)1)))) { dy_alloc_metadata_t *leftleft = ((left
)->link.rbn_left); if (leftleft != ((void*)0) && (
(_Bool) (((uintptr_t) (leftleft)->link.rbn_right_red) &
((size_t)1)))) { dy_alloc_metadata_t *tnode; do { (leftleft)
->link.rbn_right_red = (dy_alloc_metadata_t *) (((intptr_t
) (leftleft)->link.rbn_right_red) & ((ssize_t)-2)); } while
(0); do { (tnode) = (((cnode))->link.rbn_left); do { ((cnode
))->link.rbn_left = ((dy_alloc_metadata_t *) (((intptr_t) (
(tnode))->link.rbn_right_red) & ((ssize_t)-2))); } while
(0); do { ((tnode))->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) (cnode)) | (((uintptr_t) ((tnode))->link
.rbn_right_red) & ((size_t)1))); } while (0); } while (0)
; cnode = tnode; } } else { return; } } else { dy_alloc_metadata_t
*right = pathp[1].node; do { (cnode)->link.rbn_right_red =
(dy_alloc_metadata_t *) (((uintptr_t) right) | (((uintptr_t)
(cnode)->link.rbn_right_red) & ((size_t)1))); } while
(0); if (((_Bool) (((uintptr_t) (right)->link.rbn_right_red
) & ((size_t)1)))) { dy_alloc_metadata_t *left = ((cnode)
->link.rbn_left); if (left != ((void*)0) && ((_Bool
) (((uintptr_t) (left)->link.rbn_right_red) & ((size_t
)1)))) { do { (left)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((intptr_t) (left)->link.rbn_right_red) & ((ssize_t
)-2)); } while (0); do { (right)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((intptr_t) (right)->link.rbn_right_red) & ((ssize_t
)-2)); } while (0); do { (cnode)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) (cnode)->link.rbn_right_red) | ((size_t)
1)); } while (0); } else { dy_alloc_metadata_t *tnode; _Bool tred
= ((_Bool) (((uintptr_t) (cnode)->link.rbn_right_red) &
((size_t)1))); do { (tnode) = ((dy_alloc_metadata_t *) (((intptr_t
) ((cnode))->link.rbn_right_red) & ((ssize_t)-2))); do
{ ((cnode))->link.rbn_right_red = (dy_alloc_metadata_t *)
(((uintptr_t) (((tnode))->link.rbn_left)) | (((uintptr_t)
((cnode))->link.rbn_right_red) & ((size_t)1))); } while
(0); do { ((tnode))->link.rbn_left = (cnode); } while (0)
; } while (0); do { (tnode)->link.rbn_right_red = (dy_alloc_metadata_t
*) ((((intptr_t) (tnode)->link.rbn_right_red) & ((ssize_t
)-2)) | ((ssize_t)tred)); } while (0); do { (cnode)->link.
rbn_right_red = (dy_alloc_metadata_t *) (((uintptr_t) (cnode)
->link.rbn_right_red) | ((size_t)1)); } while (0); cnode =
tnode; } } else { return; } } pathp->node = cnode; } rbtree
->rbt_root = path->node; do { (rbtree->rbt_root)->
link.rbn_right_red = (dy_alloc_metadata_t *) (((intptr_t) (rbtree
->rbt_root)->link.rbn_right_red) & ((ssize_t)-2)); }
while (0); } void dy_alloc_tree_remove(dy_alloc_tree_t *rbtree
, dy_alloc_metadata_t *node) { struct { dy_alloc_metadata_t *
node; int cmp; } *pathp, *nodep, path[sizeof(void *) <<
4]; nodep = ((void*)0); path->node = rbtree->rbt_root;
for (pathp = path; pathp->node != ((void*)0); pathp++) { int
cmp = pathp->cmp = dy_alloc_tree_cmp(node, pathp->node
); if (cmp < 0) { pathp[1].node = ((pathp->node)->link
.rbn_left); } else { pathp[1].node = ((dy_alloc_metadata_t *)
(((intptr_t) (pathp->node)->link.rbn_right_red) & (
(ssize_t)-2))); if (cmp == 0) { pathp->cmp = 1; nodep = pathp
; for (pathp++; pathp->node != ((void*)0); pathp++) { pathp
->cmp = -1; pathp[1].node = ((pathp->node)->link.rbn_left
); } break; } } } ((void) sizeof ((nodep->node == node) ? 1
: 0), __extension__ ({ if (nodep->node == node) ; else __assert_fail
("nodep->node == node", "ccv_nnc_xpu_alloc.c", 16, __extension__
__PRETTY_FUNCTION__); })); pathp--; if (pathp->node != node
) { _Bool tred = ((_Bool) (((uintptr_t) (pathp->node)->
link.rbn_right_red) & ((size_t)1))); do { (pathp->node
)->link.rbn_right_red = (dy_alloc_metadata_t *) ((((intptr_t
) (pathp->node)->link.rbn_right_red) & ((ssize_t)-2
)) | ((ssize_t)((_Bool) (((uintptr_t) (node)->link.rbn_right_red
) & ((size_t)1))))); } while (0); do { (pathp->node)->
link.rbn_left = ((node)->link.rbn_left); } while (0); do {
(pathp->node)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) ((dy_alloc_metadata_t *) (((intptr_t) (node
)->link.rbn_right_red) & ((ssize_t)-2)))) | (((uintptr_t
) (pathp->node)->link.rbn_right_red) & ((size_t)1))
); } while (0); do { (node)->link.rbn_right_red = (dy_alloc_metadata_t
*) ((((intptr_t) (node)->link.rbn_right_red) & ((ssize_t
)-2)) | ((ssize_t)tred)); } while (0); nodep->node = pathp
->node; pathp->node = node; if (nodep == path) { rbtree
->rbt_root = nodep->node; } else { if (nodep[-1].cmp <
0) { do { (nodep[-1].node)->link.rbn_left = nodep->node
; } while (0); } else { do { (nodep[-1].node)->link.rbn_right_red
= (dy_alloc_metadata_t *) (((uintptr_t) nodep->node) | ((
(uintptr_t) (nodep[-1].node)->link.rbn_right_red) & ((
size_t)1))); } while (0); } } } else { dy_alloc_metadata_t *left
= ((node)->link.rbn_left); if (left != ((void*)0)) { ((void
) sizeof ((!((_Bool) (((uintptr_t) (node)->link.rbn_right_red
) & ((size_t)1)))) ? 1 : 0), __extension__ ({ if (!((_Bool
) (((uintptr_t) (node)->link.rbn_right_red) & ((size_t
)1)))) ; else __assert_fail ("!rbtn_red_get(dy_alloc_metadata_t, link, node)"
, "ccv_nnc_xpu_alloc.c", 16, __extension__ __PRETTY_FUNCTION__
); })); ((void) sizeof ((((_Bool) (((uintptr_t) (left)->link
.rbn_right_red) & ((size_t)1)))) ? 1 : 0), __extension__ (
{ if (((_Bool) (((uintptr_t) (left)->link.rbn_right_red) &
((size_t)1)))) ; else __assert_fail ("rbtn_red_get(dy_alloc_metadata_t, link, left)"
, "ccv_nnc_xpu_alloc.c", 16, __extension__ __PRETTY_FUNCTION__
); })); do { (left)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((intptr_t) (left)->link.rbn_right_red) & ((ssize_t
)-2)); } while (0); if (pathp == path) { rbtree->rbt_root =
left; } else { if (pathp[-1].cmp < 0) { do { (pathp[-1].node
)->link.rbn_left = left; } while (0); } else { do { (pathp
[-1].node)->link.rbn_right_red = (dy_alloc_metadata_t *) (
((uintptr_t) left) | (((uintptr_t) (pathp[-1].node)->link.
rbn_right_red) & ((size_t)1))); } while (0); } } return; }
else if (pathp == path) { rbtree->rbt_root = ((void*)0); return
; } } if (((_Bool) (((uintptr_t) (pathp->node)->link.rbn_right_red
) & ((size_t)1)))) { ((void) sizeof ((pathp[-1].cmp < 0
) ? 1 : 0), __extension__ ({ if (pathp[-1].cmp < 0) ; else
__assert_fail ("pathp[-1].cmp < 0", "ccv_nnc_xpu_alloc.c"
, 16, __extension__ __PRETTY_FUNCTION__); })); do { (pathp[-1
].node)->link.rbn_left = ((void*)0); } while (0); return; }
pathp->node = ((void*)0); for (pathp--; (uintptr_t)pathp >=
(uintptr_t)path; pathp--) { ((void) sizeof ((pathp->cmp !=
0) ? 1 : 0), __extension__ ({ if (pathp->cmp != 0) ; else
__assert_fail ("pathp->cmp != 0", "ccv_nnc_xpu_alloc.c", 16
, __extension__ __PRETTY_FUNCTION__); })); if (pathp->cmp <
0) { do { (pathp->node)->link.rbn_left = pathp[1].node
; } while (0); if (((_Bool) (((uintptr_t) (pathp->node)->
link.rbn_right_red) & ((size_t)1)))) { dy_alloc_metadata_t
*right = ((dy_alloc_metadata_t *) (((intptr_t) (pathp->node
)->link.rbn_right_red) & ((ssize_t)-2))); dy_alloc_metadata_t
*rightleft = ((right)->link.rbn_left); dy_alloc_metadata_t
*tnode; if (rightleft != ((void*)0) && ((_Bool) (((uintptr_t
) (rightleft)->link.rbn_right_red) & ((size_t)1)))) { do
{ (pathp->node)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((intptr_t) (pathp->node)->link.rbn_right_red) &
((ssize_t)-2)); } while (0); do { (tnode) = (((right))->link
.rbn_left); do { ((right))->link.rbn_left = ((dy_alloc_metadata_t
*) (((intptr_t) ((tnode))->link.rbn_right_red) & ((ssize_t
)-2))); } while (0); do { ((tnode))->link.rbn_right_red = (
dy_alloc_metadata_t *) (((uintptr_t) (right)) | (((uintptr_t)
((tnode))->link.rbn_right_red) & ((size_t)1))); } while
(0); } while (0); do { (pathp->node)->link.rbn_right_red
= (dy_alloc_metadata_t *) (((uintptr_t) tnode) | (((uintptr_t
) (pathp->node)->link.rbn_right_red) & ((size_t)1))
); } while (0); do { (tnode) = ((dy_alloc_metadata_t *) (((intptr_t
) ((pathp->node))->link.rbn_right_red) & ((ssize_t)
-2))); do { ((pathp->node))->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) (((tnode))->link.rbn_left)) | (((uintptr_t
) ((pathp->node))->link.rbn_right_red) & ((size_t)1
))); } while (0); do { ((tnode))->link.rbn_left = (pathp->
node); } while (0); } while (0); } else { do { (tnode) = ((dy_alloc_metadata_t
*) (((intptr_t) ((pathp->node))->link.rbn_right_red) &
((ssize_t)-2))); do { ((pathp->node))->link.rbn_right_red
= (dy_alloc_metadata_t *) (((uintptr_t) (((tnode))->link.
rbn_left)) | (((uintptr_t) ((pathp->node))->link.rbn_right_red
) & ((size_t)1))); } while (0); do { ((tnode))->link.rbn_left
= (pathp->node); } while (0); } while (0); } ((void) sizeof
(((uintptr_t)pathp > (uintptr_t)path) ? 1 : 0), __extension__
({ if ((uintptr_t)pathp > (uintptr_t)path) ; else __assert_fail
("(uintptr_t)pathp > (uintptr_t)path", "ccv_nnc_xpu_alloc.c"
, 16, __extension__ __PRETTY_FUNCTION__); })); if (pathp[-1].
cmp < 0) { do { (pathp[-1].node)->link.rbn_left = tnode
; } while (0); } else { do { (pathp[-1].node)->link.rbn_right_red
= (dy_alloc_metadata_t *) (((uintptr_t) tnode) | (((uintptr_t
) (pathp[-1].node)->link.rbn_right_red) & ((size_t)1))
); } while (0); } return; } else { dy_alloc_metadata_t *right
= ((dy_alloc_metadata_t *) (((intptr_t) (pathp->node)->
link.rbn_right_red) & ((ssize_t)-2))); dy_alloc_metadata_t
*rightleft = ((right)->link.rbn_left); if (rightleft != (
(void*)0) && ((_Bool) (((uintptr_t) (rightleft)->link
.rbn_right_red) & ((size_t)1)))) { dy_alloc_metadata_t *tnode
; do { (rightleft)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((intptr_t) (rightleft)->link.rbn_right_red) & ((
ssize_t)-2)); } while (0); do { (tnode) = (((right))->link
.rbn_left); do { ((right))->link.rbn_left = ((dy_alloc_metadata_t
*) (((intptr_t) ((tnode))->link.rbn_right_red) & ((ssize_t
)-2))); } while (0); do { ((tnode))->link.rbn_right_red = (
dy_alloc_metadata_t *) (((uintptr_t) (right)) | (((uintptr_t)
((tnode))->link.rbn_right_red) & ((size_t)1))); } while
(0); } while (0); do { (pathp->node)->link.rbn_right_red
= (dy_alloc_metadata_t *) (((uintptr_t) tnode) | (((uintptr_t
) (pathp->node)->link.rbn_right_red) & ((size_t)1))
); } while (0); do { (tnode) = ((dy_alloc_metadata_t *) (((intptr_t
) ((pathp->node))->link.rbn_right_red) & ((ssize_t)
-2))); do { ((pathp->node))->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) (((tnode))->link.rbn_left)) | (((uintptr_t
) ((pathp->node))->link.rbn_right_red) & ((size_t)1
))); } while (0); do { ((tnode))->link.rbn_left = (pathp->
node); } while (0); } while (0); if (pathp == path) { rbtree->
rbt_root = tnode; } else { if (pathp[-1].cmp < 0) { do { (
pathp[-1].node)->link.rbn_left = tnode; } while (0); } else
{ do { (pathp[-1].node)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) tnode) | (((uintptr_t) (pathp[-1].node)->
link.rbn_right_red) & ((size_t)1))); } while (0); } } return
; } else { dy_alloc_metadata_t *tnode; do { (pathp->node)->
link.rbn_right_red = (dy_alloc_metadata_t *) (((uintptr_t) (pathp
->node)->link.rbn_right_red) | ((size_t)1)); } while (0
); do { (tnode) = ((dy_alloc_metadata_t *) (((intptr_t) ((pathp
->node))->link.rbn_right_red) & ((ssize_t)-2))); do
{ ((pathp->node))->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) (((tnode))->link.rbn_left)) | (((uintptr_t
) ((pathp->node))->link.rbn_right_red) & ((size_t)1
))); } while (0); do { ((tnode))->link.rbn_left = (pathp->
node); } while (0); } while (0); pathp->node = tnode; } } }
else { dy_alloc_metadata_t *left; do { (pathp->node)->
link.rbn_right_red = (dy_alloc_metadata_t *) (((uintptr_t) pathp
[1].node) | (((uintptr_t) (pathp->node)->link.rbn_right_red
) & ((size_t)1))); } while (0); left = ((pathp->node)->
link.rbn_left); if (((_Bool) (((uintptr_t) (left)->link.rbn_right_red
) & ((size_t)1)))) { dy_alloc_metadata_t *tnode; dy_alloc_metadata_t
*leftright = ((dy_alloc_metadata_t *) (((intptr_t) (left)->
link.rbn_right_red) & ((ssize_t)-2))); dy_alloc_metadata_t
*leftrightleft = ((leftright)->link.rbn_left); if (leftrightleft
!= ((void*)0) && ((_Bool) (((uintptr_t) (leftrightleft
)->link.rbn_right_red) & ((size_t)1)))) { dy_alloc_metadata_t
*unode; do { (leftrightleft)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((intptr_t) (leftrightleft)->link.rbn_right_red) &
((ssize_t)-2)); } while (0); do { (unode) = (((pathp->node
))->link.rbn_left); do { ((pathp->node))->link.rbn_left
= ((dy_alloc_metadata_t *) (((intptr_t) ((unode))->link.rbn_right_red
) & ((ssize_t)-2))); } while (0); do { ((unode))->link
.rbn_right_red = (dy_alloc_metadata_t *) (((uintptr_t) (pathp
->node)) | (((uintptr_t) ((unode))->link.rbn_right_red)
& ((size_t)1))); } while (0); } while (0); do { (tnode) =
(((pathp->node))->link.rbn_left); do { ((pathp->node
))->link.rbn_left = ((dy_alloc_metadata_t *) (((intptr_t) (
(tnode))->link.rbn_right_red) & ((ssize_t)-2))); } while
(0); do { ((tnode))->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) (pathp->node)) | (((uintptr_t) ((tnode))
->link.rbn_right_red) & ((size_t)1))); } while (0); } while
(0); do { (unode)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) tnode) | (((uintptr_t) (unode)->link.rbn_right_red
) & ((size_t)1))); } while (0); do { (tnode) = ((dy_alloc_metadata_t
*) (((intptr_t) ((unode))->link.rbn_right_red) & ((ssize_t
)-2))); do { ((unode))->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) (((tnode))->link.rbn_left)) | (((uintptr_t
) ((unode))->link.rbn_right_red) & ((size_t)1))); } while
(0); do { ((tnode))->link.rbn_left = (unode); } while (0)
; } while (0); } else { ((void) sizeof ((leftright != ((void*
)0)) ? 1 : 0), __extension__ ({ if (leftright != ((void*)0)) ;
else __assert_fail ("leftright != NULL", "ccv_nnc_xpu_alloc.c"
, 16, __extension__ __PRETTY_FUNCTION__); })); do { (leftright
)->link.rbn_right_red = (dy_alloc_metadata_t *) (((uintptr_t
) (leftright)->link.rbn_right_red) | ((size_t)1)); } while
(0); do { (tnode) = (((pathp->node))->link.rbn_left); do
{ ((pathp->node))->link.rbn_left = ((dy_alloc_metadata_t
*) (((intptr_t) ((tnode))->link.rbn_right_red) & ((ssize_t
)-2))); } while (0); do { ((tnode))->link.rbn_right_red = (
dy_alloc_metadata_t *) (((uintptr_t) (pathp->node)) | (((uintptr_t
) ((tnode))->link.rbn_right_red) & ((size_t)1))); } while
(0); } while (0); do { (tnode)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((intptr_t) (tnode)->link.rbn_right_red) & ((ssize_t
)-2)); } while (0); } if (pathp == path) { rbtree->rbt_root
= tnode; } else { if (pathp[-1].cmp < 0) { do { (pathp[-1
].node)->link.rbn_left = tnode; } while (0); } else { do {
(pathp[-1].node)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) tnode) | (((uintptr_t) (pathp[-1].node)->
link.rbn_right_red) & ((size_t)1))); } while (0); } } return
; } else if (((_Bool) (((uintptr_t) (pathp->node)->link
.rbn_right_red) & ((size_t)1)))) { dy_alloc_metadata_t *leftleft
= ((left)->link.rbn_left); if (leftleft != ((void*)0) &&
((_Bool) (((uintptr_t) (leftleft)->link.rbn_right_red) &
((size_t)1)))) { dy_alloc_metadata_t *tnode; do { (pathp->
node)->link.rbn_right_red = (dy_alloc_metadata_t *) (((intptr_t
) (pathp->node)->link.rbn_right_red) & ((ssize_t)-2
)); } while (0); do { (left)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) (left)->link.rbn_right_red) | ((size_t)1
)); } while (0); do { (leftleft)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((intptr_t) (leftleft)->link.rbn_right_red) & ((ssize_t
)-2)); } while (0); do { (tnode) = (((pathp->node))->link
.rbn_left); do { ((pathp->node))->link.rbn_left = ((dy_alloc_metadata_t
*) (((intptr_t) ((tnode))->link.rbn_right_red) & ((ssize_t
)-2))); } while (0); do { ((tnode))->link.rbn_right_red = (
dy_alloc_metadata_t *) (((uintptr_t) (pathp->node)) | (((uintptr_t
) ((tnode))->link.rbn_right_red) & ((size_t)1))); } while
(0); } while (0); ((void) sizeof (((uintptr_t)pathp > (uintptr_t
)path) ? 1 : 0), __extension__ ({ if ((uintptr_t)pathp > (
uintptr_t)path) ; else __assert_fail ("(uintptr_t)pathp > (uintptr_t)path"
, "ccv_nnc_xpu_alloc.c", 16, __extension__ __PRETTY_FUNCTION__
); })); if (pathp[-1].cmp < 0) { do { (pathp[-1].node)->
link.rbn_left = tnode; } while (0); } else { do { (pathp[-1].
node)->link.rbn_right_red = (dy_alloc_metadata_t *) (((uintptr_t
) tnode) | (((uintptr_t) (pathp[-1].node)->link.rbn_right_red
) & ((size_t)1))); } while (0); } return; } else { do { (
left)->link.rbn_right_red = (dy_alloc_metadata_t *) (((uintptr_t
) (left)->link.rbn_right_red) | ((size_t)1)); } while (0);
do { (pathp->node)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((intptr_t) (pathp->node)->link.rbn_right_red) &
((ssize_t)-2)); } while (0); return; } } else { dy_alloc_metadata_t
*leftleft = ((left)->link.rbn_left); if (leftleft != ((void
*)0) && ((_Bool) (((uintptr_t) (leftleft)->link.rbn_right_red
) & ((size_t)1)))) { dy_alloc_metadata_t *tnode; do { (leftleft
)->link.rbn_right_red = (dy_alloc_metadata_t *) (((intptr_t
) (leftleft)->link.rbn_right_red) & ((ssize_t)-2)); } while
(0); do { (tnode) = (((pathp->node))->link.rbn_left); do
{ ((pathp->node))->link.rbn_left = ((dy_alloc_metadata_t
*) (((intptr_t) ((tnode))->link.rbn_right_red) & ((ssize_t
)-2))); } while (0); do { ((tnode))->link.rbn_right_red = (
dy_alloc_metadata_t *) (((uintptr_t) (pathp->node)) | (((uintptr_t
) ((tnode))->link.rbn_right_red) & ((size_t)1))); } while
(0); } while (0); if (pathp == path) { rbtree->rbt_root =
tnode; } else { if (pathp[-1].cmp < 0) { do { (pathp[-1].
node)->link.rbn_left = tnode; } while (0); } else { do { (
pathp[-1].node)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) tnode) | (((uintptr_t) (pathp[-1].node)->
link.rbn_right_red) & ((size_t)1))); } while (0); } } return
; } else { do { (left)->link.rbn_right_red = (dy_alloc_metadata_t
*) (((uintptr_t) (left)->link.rbn_right_red) | ((size_t)1
)); } while (0); } } } } rbtree->rbt_root = path->node;
((void) sizeof ((!((_Bool) (((uintptr_t) (rbtree->rbt_root
)->link.rbn_right_red) & ((size_t)1)))) ? 1 : 0), __extension__
({ if (!((_Bool) (((uintptr_t) (rbtree->rbt_root)->link
.rbn_right_red) & ((size_t)1)))) ; else __assert_fail ("!rbtn_red_get(dy_alloc_metadata_t, link, rbtree->rbt_root)"
, "ccv_nnc_xpu_alloc.c", 16, __extension__ __PRETTY_FUNCTION__
); })); } dy_alloc_metadata_t * dy_alloc_tree_iter_recurse(dy_alloc_tree_t
*rbtree, dy_alloc_metadata_t *node, dy_alloc_metadata_t *(*cb
)(dy_alloc_tree_t *, dy_alloc_metadata_t *, void *), void *arg
) { if (node == ((void*)0)) { return ((void*)0); } else { dy_alloc_metadata_t
*ret; if ((ret = dy_alloc_tree_iter_recurse(rbtree, ((node)->
link.rbn_left), cb, arg)) != ((void*)0) || (ret = cb(rbtree, node
, arg)) != ((void*)0)) { return ret; } return dy_alloc_tree_iter_recurse
(rbtree, ((dy_alloc_metadata_t *) (((intptr_t) (node)->link
.rbn_right_red) & ((ssize_t)-2))), cb, arg); } } dy_alloc_metadata_t
* dy_alloc_tree_iter_start(dy_alloc_tree_t *rbtree, dy_alloc_metadata_t
*start, dy_alloc_metadata_t *node, dy_alloc_metadata_t *(*cb
)(dy_alloc_tree_t *, dy_alloc_metadata_t *, void *), void *arg
) { int cmp = dy_alloc_tree_cmp(start, node); if (cmp < 0)
{ dy_alloc_metadata_t *ret; if ((ret = dy_alloc_tree_iter_start
(rbtree, start, ((node)->link.rbn_left), cb, arg)) != ((void
*)0) || (ret = cb(rbtree, node, arg)) != ((void*)0)) { return
ret; } return dy_alloc_tree_iter_recurse(rbtree, ((dy_alloc_metadata_t
*) (((intptr_t) (node)->link.rbn_right_red) & ((ssize_t
)-2))), cb, arg); } else if (cmp > 0) { return dy_alloc_tree_iter_start
(rbtree, start, ((dy_alloc_metadata_t *) (((intptr_t) (node)->
link.rbn_right_red) & ((ssize_t)-2))), cb, arg); } else {
dy_alloc_metadata_t *ret; if ((ret = cb(rbtree, node, arg)) !=
((void*)0)) { return ret; } return dy_alloc_tree_iter_recurse
(rbtree, ((dy_alloc_metadata_t *) (((intptr_t) (node)->link
.rbn_right_red) & ((ssize_t)-2))), cb, arg); } } dy_alloc_metadata_t
* dy_alloc_tree_iter(dy_alloc_tree_t *rbtree, dy_alloc_metadata_t
*start, dy_alloc_metadata_t *(*cb)( dy_alloc_tree_t *, dy_alloc_metadata_t
*, void *), void *arg) { dy_alloc_metadata_t *ret; if (start
!= ((void*)0)) { ret = dy_alloc_tree_iter_start(rbtree, start
, rbtree->rbt_root, cb, arg); } else { ret = dy_alloc_tree_iter_recurse
(rbtree, rbtree->rbt_root, cb, arg); } return ret; } dy_alloc_metadata_t
* dy_alloc_tree_reverse_iter_recurse(dy_alloc_tree_t *rbtree
, dy_alloc_metadata_t *node, dy_alloc_metadata_t *(*cb)(dy_alloc_tree_t
*, dy_alloc_metadata_t *, void *), void *arg) { if (node == (
(void*)0)) { return ((void*)0); } else { dy_alloc_metadata_t *
ret; if ((ret = dy_alloc_tree_reverse_iter_recurse(rbtree, ((
dy_alloc_metadata_t *) (((intptr_t) (node)->link.rbn_right_red
) & ((ssize_t)-2))), cb, arg)) != ((void*)0) || (ret = cb
(rbtree, node, arg)) != ((void*)0)) { return ret; } return dy_alloc_tree_reverse_iter_recurse
(rbtree, ((node)->link.rbn_left), cb, arg); } } dy_alloc_metadata_t
* dy_alloc_tree_reverse_iter_start(dy_alloc_tree_t *rbtree, dy_alloc_metadata_t
*start, dy_alloc_metadata_t *node, dy_alloc_metadata_t *(*cb
)(dy_alloc_tree_t *, dy_alloc_metadata_t *, void *), void *arg
) { int cmp = dy_alloc_tree_cmp(start, node); if (cmp > 0)
{ dy_alloc_metadata_t *ret; if ((ret = dy_alloc_tree_reverse_iter_start
(rbtree, start, ((dy_alloc_metadata_t *) (((intptr_t) (node)->
link.rbn_right_red) & ((ssize_t)-2))), cb, arg)) != ((void
*)0) || (ret = cb(rbtree, node, arg)) != ((void*)0)) { return
ret; } return dy_alloc_tree_reverse_iter_recurse(rbtree, ((node
)->link.rbn_left), cb, arg); } else if (cmp < 0) { return
dy_alloc_tree_reverse_iter_start(rbtree, start, ((node)->
link.rbn_left), cb, arg); } else { dy_alloc_metadata_t *ret; if
((ret = cb(rbtree, node, arg)) != ((void*)0)) { return ret; }
return dy_alloc_tree_reverse_iter_recurse(rbtree, ((node)->
link.rbn_left), cb, arg); } } dy_alloc_metadata_t * dy_alloc_tree_reverse_iter
(dy_alloc_tree_t *rbtree, dy_alloc_metadata_t *start, dy_alloc_metadata_t
*(*cb)(dy_alloc_tree_t *, dy_alloc_metadata_t *, void *), void
*arg) { dy_alloc_metadata_t *ret; if (start != ((void*)0)) {
ret = dy_alloc_tree_reverse_iter_start(rbtree, start, rbtree
->rbt_root, cb, arg); } else { ret = dy_alloc_tree_reverse_iter_recurse
(rbtree, rbtree->rbt_root, cb, arg); } return ret; } void dy_alloc_tree_destroy_recurse
(dy_alloc_tree_t *rbtree, dy_alloc_metadata_t *node, void (*cb
)( dy_alloc_metadata_t *, void *), void *arg) { if (node == (
(void*)0)) { return; } dy_alloc_tree_destroy_recurse(rbtree, (
(node)->link.rbn_left), cb, arg); do { ((node))->link.rbn_left
= ((void*)0); } while (0); dy_alloc_tree_destroy_recurse(rbtree
, ((dy_alloc_metadata_t *) (((intptr_t) (node)->link.rbn_right_red
) & ((ssize_t)-2))), cb, arg); do { ((node))->link.rbn_right_red
= (dy_alloc_metadata_t *) (((uintptr_t) ((void*)0)) | (((uintptr_t
) ((node))->link.rbn_right_red) & ((size_t)1))); } while
(0); if (cb) { cb(node, arg); } } void dy_alloc_tree_destroy
(dy_alloc_tree_t *rbtree, void (*cb)(dy_alloc_metadata_t *, void
*), void *arg) { dy_alloc_tree_destroy_recurse(rbtree, rbtree
->rbt_root, cb, arg); rbtree->rbt_root = ((void*)0); }
1
Null pointer value stored to 'nodep'
2
Assuming field 'node' is equal to null
3
Loop condition is false. Execution continues on line 16
4
Access to field 'node' results in a dereference of a null pointer (loaded from variable 'nodep')
17
18static void _ccv_nnc_xpu_metadata_free(dy_alloc_metadata_t* node, void* arg)
19{
20 do {
21 dy_alloc_metadata_t* const next = node->next;
22 cufree(node->device, node->ptr);
23 ccfreefree(node);
24 node = next;
25 } while (node);
26}
27
28static void _ccv_nnc_xpu_alloc_drain(khash_t(dy_dev)kh_dy_dev_t* const dev, const ccv_nnc_stream_context_t* const stream)
29{
30 // Wait until the stream is free, and then do the free.
31 if (stream)
32 ccv_nnc_stream_context_wait(stream);
33 khiter_t k;
34 for (k = kh_begin(dev)(khint_t)(0); k != kh_end(dev)((dev)->n_buckets); ++k)
35 {
36 if (!kh_exist(dev, k)(!(((dev)->flags[(k)>>4]>>(((k)&0xfU)<<
1))&3))
)
37 continue;
38 dy_alloc_tree_t* const tree = &kh_val(dev, k)((dev)->vals[k]);
39 dy_alloc_tree_destroy(tree, _ccv_nnc_xpu_metadata_free, 0);
40 kh_del(dy_dev, dev, k)kh_del_dy_dev(dev, k);
41 }
42}
43
44static void _ccv_nnc_xpu_stream_destructor_hook(const ccv_nnc_stream_context_t* const stream, void* const context)
45{
46 ccv_nnc_xpu_alloc_t* const xpu_alloc = (ccv_nnc_xpu_alloc_t*)context;
47 khash_t(dy_str)kh_dy_str_t* const freed = xpu_alloc->freed;
48 const int64_t str = (int64_t)(intptr_t)stream;
49 khiter_t i = kh_get(dy_str, freed, str)kh_get_dy_str(freed, str);
50 assert(i != kh_end(freed))((void) sizeof ((i != ((freed)->n_buckets)) ? 1 : 0), __extension__
({ if (i != ((freed)->n_buckets)) ; else __assert_fail ("i != kh_end(freed)"
, "ccv_nnc_xpu_alloc.c", 50, __extension__ __PRETTY_FUNCTION__
); }))
;
51 khash_t(dy_dev)kh_dy_dev_t* const dev = kh_val(freed, i)((freed)->vals[i]).dev;
52 _ccv_nnc_xpu_alloc_drain(dev, stream);
53 kh_destroy(dy_dev, dev)kh_destroy_dy_dev(dev);
54 kh_del(dy_str, freed, i)kh_del_dy_str(freed, i);
55}
56
57void* ccv_nnc_xpu_alloc(ccv_nnc_xpu_alloc_t* const xpu_alloc, const int device, ccv_nnc_stream_context_t* const stream, const size_t size)
58{
59 khash_t(dy_str)kh_dy_str_t* const freed = xpu_alloc->freed;
60 const int64_t str = (int64_t)(intptr_t)stream;
61 int ret;
62 khiter_t i = kh_put(dy_str, freed, str, &ret)kh_put_dy_str(freed, str, &ret);
63 assert(ret >= 0)((void) sizeof ((ret >= 0) ? 1 : 0), __extension__ ({ if (
ret >= 0) ; else __assert_fail ("ret >= 0", "ccv_nnc_xpu_alloc.c"
, 63, __extension__ __PRETTY_FUNCTION__); }))
;
64 dy_alloc_metadata_t* node = 0;
65 if (ret == 0)
66 {
67 // If we can find stream related allocations, try to
68 // find the suitable ones.
69 khash_t(dy_dev)kh_dy_dev_t* const dev = kh_val(freed, i)((freed)->vals[i]).dev;
70 assert(dev)((void) sizeof ((dev) ? 1 : 0), __extension__ ({ if (dev) ; else
__assert_fail ("dev", "ccv_nnc_xpu_alloc.c", 70, __extension__
__PRETTY_FUNCTION__); }))
;
71 khiter_t j = kh_get(dy_dev, dev, device)kh_get_dy_dev(dev, device);
72 if (j != kh_end(dev)((dev)->n_buckets))
73 {
74 dy_alloc_tree_t* const tree = &kh_val(dev, j)((dev)->vals[j]);
75 dy_alloc_metadata_t key = {
76 .size = size
77 };
78 node = dy_alloc_tree_nsearch(tree, &key);
79 if (node)
80 {
81 if (node->next) // If it is a linked list, select the one.
82 {
83 dy_alloc_metadata_t* next_node = node->next;
84 node->next = node->next->next;
85 node = next_node;
86 } else
87 dy_alloc_tree_remove(tree, node);
88 }
89 }
90 } else {
91 // Otherwise, create it.
92 kh_val(freed, i)((freed)->vals[i]).dev = kh_init(dy_dev)kh_init_dy_dev();
93 kh_val(freed, i)((freed)->vals[i]).hook_id = stream ? ccv_nnc_stream_context_add_destructor_hook(stream, _ccv_nnc_xpu_stream_destructor_hook, xpu_alloc) : -1;
94
95 }
96 if (!node)
97 {
98 node = (dy_alloc_metadata_t*)ccmallocmalloc(sizeof(dy_alloc_metadata_t));
99 if (xpu_alloc->mp_hdr < 0)
100 xpu_alloc->mp_hdr = curegmp((cump_f)ccv_nnc_xpu_gc, xpu_alloc);
101 node->ptr = cumalloc(device, size);
102 if (!node->ptr) // If cannot allocate, drain the pool first and then allocate.
103 {
104 ccfreefree(node);
105 return 0;
106 }
107 node->device = device;
108 node->size = size;
109 node->str = str;
110 } else {
111 assert(node->size >= size)((void) sizeof ((node->size >= size) ? 1 : 0), __extension__
({ if (node->size >= size) ; else __assert_fail ("node->size >= size"
, "ccv_nnc_xpu_alloc.c", 111, __extension__ __PRETTY_FUNCTION__
); }))
;
112 assert(node->device == device)((void) sizeof ((node->device == device) ? 1 : 0), __extension__
({ if (node->device == device) ; else __assert_fail ("node->device == device"
, "ccv_nnc_xpu_alloc.c", 112, __extension__ __PRETTY_FUNCTION__
); }))
;
113 assert(node->str == str)((void) sizeof ((node->str == str) ? 1 : 0), __extension__
({ if (node->str == str) ; else __assert_fail ("node->str == str"
, "ccv_nnc_xpu_alloc.c", 113, __extension__ __PRETTY_FUNCTION__
); }))
;
114 }
115 node->next = 0;
116 khash_t(dy_alloc)kh_dy_alloc_t* const allocd = xpu_alloc->allocd;
117 i = kh_put(dy_alloc, allocd, (int64_t)(intptr_t)node->ptr, &ret)kh_put_dy_alloc(allocd, (int64_t)(intptr_t)node->ptr, &
ret)
;
118 assert(ret > 0)((void) sizeof ((ret > 0) ? 1 : 0), __extension__ ({ if (ret
> 0) ; else __assert_fail ("ret > 0", "ccv_nnc_xpu_alloc.c"
, 118, __extension__ __PRETTY_FUNCTION__); }))
;
119 kh_val(allocd, i)((allocd)->vals[i]) = node;
120 return node->ptr;
121}
122
123void ccv_nnc_xpu_free(ccv_nnc_xpu_alloc_t* const xpu_alloc, void* const ptr)
124{
125 khash_t(dy_alloc)kh_dy_alloc_t* const allocd = xpu_alloc->allocd;
126 khiter_t i = kh_get(dy_alloc, allocd, (int64_t)(intptr_t)ptr)kh_get_dy_alloc(allocd, (int64_t)(intptr_t)ptr);
127 assert(i != kh_end(allocd))((void) sizeof ((i != ((allocd)->n_buckets)) ? 1 : 0), __extension__
({ if (i != ((allocd)->n_buckets)) ; else __assert_fail (
"i != kh_end(allocd)", "ccv_nnc_xpu_alloc.c", 127, __extension__
__PRETTY_FUNCTION__); }))
;
128 dy_alloc_metadata_t* const node = kh_val(allocd, i)((allocd)->vals[i]);
129 kh_del(dy_alloc, allocd, i)kh_del_dy_alloc(allocd, i);
130 assert(node->ptr == ptr)((void) sizeof ((node->ptr == ptr) ? 1 : 0), __extension__
({ if (node->ptr == ptr) ; else __assert_fail ("node->ptr == ptr"
, "ccv_nnc_xpu_alloc.c", 130, __extension__ __PRETTY_FUNCTION__
); }))
;
131 khash_t(dy_str)kh_dy_str_t* const freed = xpu_alloc->freed;
132 i = kh_get(dy_str, freed, node->str)kh_get_dy_str(freed, node->str);
133 // If cannot find associated stream, that means this allocation associated
134 // stream has been freed. I have to do synchronous free of this pointer.
135 if (i == kh_end(freed)((freed)->n_buckets))
136 {
137 cufree(node->device, node->ptr);
138 ccfreefree(node);
139 return;
140 }
141 khash_t(dy_dev)kh_dy_dev_t* const dev = kh_val(freed, i)((freed)->vals[i]).dev;
142 int ret;
143 khiter_t j = kh_put(dy_dev, dev, node->device, &ret)kh_put_dy_dev(dev, node->device, &ret);
144 assert(ret >= 0)((void) sizeof ((ret >= 0) ? 1 : 0), __extension__ ({ if (
ret >= 0) ; else __assert_fail ("ret >= 0", "ccv_nnc_xpu_alloc.c"
, 144, __extension__ __PRETTY_FUNCTION__); }))
;
145 dy_alloc_tree_t* const tree = &kh_val(dev, j)((dev)->vals[j]);
146 if (ret != 0)
147 dy_alloc_tree_new(tree);
148 dy_alloc_metadata_t* const canon_node = dy_alloc_tree_search(tree, node);
149 if (!canon_node)
150 dy_alloc_tree_insert(tree, node);
151 else { // Insert into the linked list.
152 node->next = canon_node->next;
153 canon_node->next = node;
154 }
155}
156
157void ccv_nnc_xpu_alloc_destroy(ccv_nnc_xpu_alloc_t* const xpu_alloc)
158{
159 khash_t(dy_alloc)kh_dy_alloc_t* const allocd = xpu_alloc->allocd;
160 khiter_t k;
161 for (k = kh_begin(allocd)(khint_t)(0); k != kh_end(allocd)((allocd)->n_buckets); ++k)
162 {
163 if (!kh_exist(allocd, k)(!(((allocd)->flags[(k)>>4]>>(((k)&0xfU)<<
1))&3))
)
164 continue;
165 _ccv_nnc_xpu_metadata_free(kh_val(allocd, k)((allocd)->vals[k]), 0);
166 }
167 kh_destroy(dy_alloc, allocd)kh_destroy_dy_alloc(allocd);
168 khash_t(dy_str)kh_dy_str_t* const freed = xpu_alloc->freed;
169 for (k = kh_begin(freed)(khint_t)(0); k != kh_end(freed)((freed)->n_buckets); ++k)
170 {
171 if (!kh_exist(freed, k)(!(((freed)->flags[(k)>>4]>>(((k)&0xfU)<<
1))&3))
)
172 continue;
173 khash_t(dy_dev)kh_dy_dev_t* const dev = kh_val(freed, k)((freed)->vals[k]).dev;
174 ccv_nnc_stream_context_t* const stream = (ccv_nnc_stream_context_t*)(intptr_t)kh_key(freed, k)((freed)->keys[k]);
175 _ccv_nnc_xpu_alloc_drain(dev, stream);
176 if (stream)
177 {
178 const int hook_id = kh_val(freed, k)((freed)->vals[k]).hook_id;
179 ccv_nnc_stream_context_remove_destructor_hook(stream, hook_id);
180 }
181 kh_destroy(dy_dev, dev)kh_destroy_dy_dev(dev);
182 }
183 kh_destroy(dy_str, freed)kh_destroy_dy_str(freed);
184 if (xpu_alloc->mp_hdr >= 0)
185 cuunregmp(xpu_alloc->mp_hdr);
186}
187
188void ccv_nnc_xpu_gc(ccv_nnc_xpu_alloc_t* const xpu_alloc)
189{
190 khash_t(dy_str)kh_dy_str_t* const freed = xpu_alloc->freed;
191 khiter_t k;
192 for (k = kh_begin(freed)(khint_t)(0); k != kh_end(freed)((freed)->n_buckets); ++k)
193 {
194 if (!kh_exist(freed, k)(!(((freed)->flags[(k)>>4]>>(((k)&0xfU)<<
1))&3))
)
195 continue;
196 khash_t(dy_dev)kh_dy_dev_t* const dev = kh_val(freed, k)((freed)->vals[k]).dev;
197 ccv_nnc_stream_context_t* const stream = (ccv_nnc_stream_context_t*)(intptr_t)kh_key(freed, k)((freed)->keys[k]);
198 _ccv_nnc_xpu_alloc_drain(dev, stream);
199 }
200}
201#else
202void* ccv_nnc_xpu_alloc(ccv_nnc_xpu_alloc_t* const xpu_alloc, const int device, ccv_nnc_stream_context_t* const stream, const size_t size)
203{
204 return 0;
205}
206
207void ccv_nnc_xpu_free(ccv_nnc_xpu_alloc_t* const xpu_alloc, void* const ptr)
208{
209}
210
211void ccv_nnc_xpu_alloc_destroy(ccv_nnc_xpu_alloc_t* const xpu_alloc)
212{
213}
214
215void ccv_nnc_xpu_gc(ccv_nnc_xpu_alloc_t* const dynamic_graph)
216{
217}
218#endif