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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
//------------------------------------------------------------------------------
// GB_werk_push: allocate werkspace from the Werk stack or malloc
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
#include "GB.h"
// The werkspace is allocated from the Werk static if it small enough and space
// is available. Otherwise it is allocated by malloc.
#ifdef comments_only
void *GB_werk_push // return pointer to newly allocated space
(
// output
size_t *size_allocated, // # of bytes actually allocated
bool *on_stack, // true if werkspace is from Werk stack
// input
size_t nitems, // # of items to allocate
size_t size_of_item, // size of each item
GB_Werk Werk
) ;
#endif
GB_CALLBACK_WERK_PUSH_PROTO (GB_werk_push)
{
//--------------------------------------------------------------------------
// check inputs
//--------------------------------------------------------------------------
ASSERT (on_stack != NULL) ;
ASSERT (size_allocated != NULL) ;
//--------------------------------------------------------------------------
// determine where to allocate the werkspace
//--------------------------------------------------------------------------
size_t size ;
if (Werk == NULL || nitems > GB_WERK_SIZE || size_of_item > GB_WERK_SIZE
#ifdef GBCOVER
// Werk stack can be disabled for test coverage
|| (GB_Global_hack_get (1) != 0)
#endif
)
{
// no context, or werkspace is too large to allocate from the Werk stack
(*on_stack) = false ;
}
else
{
// try to allocate from the Werk stack
size = GB_ROUND8 (nitems * size_of_item) ;
ASSERT (size % 8 == 0) ; // size is rounded up to a multiple of 8
size_t freespace = GB_WERK_SIZE - Werk->pwerk ;
ASSERT (freespace % 8 == 0) ; // thus freespace is also multiple of 8
(*on_stack) = (size <= freespace) ;
}
//--------------------------------------------------------------------------
// allocate the werkspace
//--------------------------------------------------------------------------
if (*on_stack)
{
// allocate the werkspace from the Werk stack
GB_void *p = Werk->Stack + Werk->pwerk ;
Werk->pwerk += (int) size ;
(*size_allocated) = size ;
return ((void *) p) ;
}
else
{
// allocate the werkspace from malloc
void *p = GB_MALLOC_MEMORY (nitems, size_of_item, size_allocated) ;
return (p) ;
}
}
|