/*1* *****************************************************************************2*3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 2018-2025 Gavin D. Howard and contributors.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions are met:9*10* * Redistributions of source code must retain the above copyright notice, this11* list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright notice,14* this list of conditions and the following disclaimer in the documentation15* and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"18* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE21* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS24* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN25* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)26* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGE.28*29* *****************************************************************************30*31* The private header for the bc library.32*33*/3435#ifndef LIBBC_PRIVATE_H36#define LIBBC_PRIVATE_H3738#ifndef _WIN323940#include <pthread.h>4142#endif // _WIN324344#include <bcl.h>4546#include <num.h>47#include <vm.h>4849#if BC_ENABLE_MEMCHECK5051/**52* A typedef for Valgrind builds. This is to add a generation index for error53* checking.54*/55typedef struct BclNum56{57/// The number.58BcNum n;5960/// The generation index.61size_t gen_idx;6263} BclNum;6465/**66* Clears the generation byte in a BclNumber and returns the value.67* @param n The BclNumber.68* @return The value of the index.69*/70#define BCL_NO_GEN(n) \71((n).i & ~(((size_t) UCHAR_MAX) << ((sizeof(size_t) - 1) * CHAR_BIT)))7273/**74* Gets the generation index in a BclNumber.75* @param n The BclNumber.76* @return The generation index.77*/78#define BCL_GET_GEN(n) ((n).i >> ((sizeof(size_t) - 1) * CHAR_BIT))7980/**81* Turns a BclNumber into a BcNum.82* @param c The context.83* @param n The BclNumber.84*/85#define BCL_NUM(c, n) ((BclNum*) bc_vec_item(&(c)->nums, BCL_NO_GEN(n)))8687/**88* Clears the generation index top byte in the BclNumber.89* @param n The BclNumber.90*/91#define BCL_CLEAR_GEN(n) \92do \93{ \94(n).i &= ~(((size_t) UCHAR_MAX) << ((sizeof(size_t) - 1) * CHAR_BIT)); \95} \96while (0)9798#define BCL_CHECK_NUM_GEN(c, bn) \99do \100{ \101size_t gen_ = BCL_GET_GEN(bn); \102BclNum* ptr_ = BCL_NUM(c, bn); \103if (BCL_NUM_ARRAY(ptr_) == NULL) \104{ \105bcl_nonexistentNum(); \106} \107if (gen_ != ptr_->gen_idx) \108{ \109bcl_invalidGeneration(); \110} \111} \112while (0)113114#define BCL_CHECK_NUM_VALID(c, bn) \115do \116{ \117size_t idx_ = BCL_NO_GEN(bn); \118if ((c)->nums.len <= idx_) \119{ \120bcl_numIdxOutOfRange(); \121} \122BCL_CHECK_NUM_GEN(c, bn); \123} \124while (0)125126/**127* Returns the limb array of the number.128* @param bn The number.129* @return The limb array.130*/131#define BCL_NUM_ARRAY(bn) ((bn)->n.num)132133/**134* Returns the limb array of the number for a non-pointer.135* @param bn The number.136* @return The limb array.137*/138#define BCL_NUM_ARRAY_NP(bn) ((bn).n.num)139140/**141* Returns the BcNum pointer.142* @param bn The number.143* @return The BcNum pointer.144*/145#define BCL_NUM_NUM(bn) (&(bn)->n)146147/**148* Returns the BcNum pointer for a non-pointer.149* @param bn The number.150* @return The BcNum pointer.151*/152#define BCL_NUM_NUM_NP(bn) (&(bn).n)153154// These functions only abort. They exist to give developers some idea of what155// went wrong when bugs are found, if they look at the Valgrind stack trace.156157BC_NORETURN void158bcl_invalidGeneration(void);159160BC_NORETURN void161bcl_nonexistentNum(void);162163BC_NORETURN void164bcl_numIdxOutOfRange(void);165166#else // BC_ENABLE_MEMCHECK167168/**169* A typedef for non-Valgrind builds.170*/171typedef BcNum BclNum;172173#define BCL_NO_GEN(n) ((n).i)174#define BCL_NUM(c, n) ((BclNum*) bc_vec_item(&(c)->nums, (n).i))175#define BCL_CLEAR_GEN(n) ((void) (n))176177#define BCL_CHECK_NUM_GEN(c, bn)178#define BCL_CHECK_NUM_VALID(c, n)179180#define BCL_NUM_ARRAY(bn) ((bn)->num)181#define BCL_NUM_ARRAY_NP(bn) ((bn).num)182183#define BCL_NUM_NUM(bn) (bn)184#define BCL_NUM_NUM_NP(bn) (&(bn))185186#endif // BC_ENABLE_MEMCHECK187188/**189* A header that sets a jump.190* @param vm The thread data.191* @param l The label to jump to on error.192*/193#define BC_FUNC_HEADER(vm, l) \194do \195{ \196BC_SETJMP(vm, l); \197vm->err = BCL_ERROR_NONE; \198} \199while (0)200201/**202* A footer for functions that do not return an error code.203*/204#define BC_FUNC_FOOTER_NO_ERR(vm) \205do \206{ \207BC_UNSETJMP(vm); \208} \209while (0)210211/**212* A footer for functions that *do* return an error code.213* @param vm The thread data.214* @param e The error variable to set.215*/216#define BC_FUNC_FOOTER(vm, e) \217do \218{ \219e = vm->err; \220BC_FUNC_FOOTER_NO_ERR(vm); \221} \222while (0)223224/**225* A footer that sets up n based the value of e and sets up the return value in226* idx.227* @param c The context.228* @param e The error.229* @param bn The number.230* @param idx The idx to set as the return value.231*/232#define BC_MAYBE_SETUP(c, e, bn, idx) \233do \234{ \235if (BC_ERR((e) != BCL_ERROR_NONE)) \236{ \237if (BCL_NUM_ARRAY_NP(bn) != NULL) bc_num_free(BCL_NUM_NUM_NP(bn)); \238idx.i = 0 - (size_t) (e); \239} \240else idx = bcl_num_insert(c, &(bn)); \241} \242while (0)243244/**245* A header to check the context and return an error encoded in a number if it246* is bad.247* @param c The context.248*/249#define BC_CHECK_CTXT(vm, c) \250do \251{ \252c = bcl_contextHelper(vm); \253if (BC_ERR(c == NULL)) \254{ \255BclNumber n_num_; \256n_num_.i = 0 - (size_t) BCL_ERROR_INVALID_CONTEXT; \257return n_num_; \258} \259} \260while (0)261262/**263* A header to check the context and return an error directly if it is bad.264* @param c The context.265*/266#define BC_CHECK_CTXT_ERR(vm, c) \267do \268{ \269c = bcl_contextHelper(vm); \270if (BC_ERR(c == NULL)) \271{ \272return BCL_ERROR_INVALID_CONTEXT; \273} \274} \275while (0)276277/**278* A header to check the context and abort if it is bad.279* @param c The context.280*/281#define BC_CHECK_CTXT_ASSERT(vm, c) \282do \283{ \284c = bcl_contextHelper(vm); \285assert(c != NULL); \286} \287while (0)288289/**290* A header to check the number in the context and return an error encoded as a291* @param c The context.292* number if it is bad.293* @param n The BclNumber.294*/295#define BC_CHECK_NUM(c, n) \296do \297{ \298size_t no_gen_ = BCL_NO_GEN(n); \299if (BC_ERR(no_gen_ >= (c)->nums.len)) \300{ \301if ((n).i > 0 - (size_t) BCL_ERROR_NELEMS) return (n); \302else \303{ \304BclNumber n_num_; \305n_num_.i = 0 - (size_t) BCL_ERROR_INVALID_NUM; \306return n_num_; \307} \308} \309BCL_CHECK_NUM_GEN(c, n); \310} \311while (0)312313//clang-format off314315/**316* A header to check the number in the context and return an error directly if317* it is bad.318* @param c The context.319* @param n The BclNumber.320*/321#define BC_CHECK_NUM_ERR(c, n) \322do \323{ \324size_t no_gen_ = BCL_NO_GEN(n); \325if (BC_ERR(no_gen_ >= (c)->nums.len)) \326{ \327if ((n).i > 0 - (size_t) BCL_ERROR_NELEMS) \328{ \329return (BclError) (0 - (n).i); \330} \331else return BCL_ERROR_INVALID_NUM; \332} \333BCL_CHECK_NUM_GEN(c, n); \334} \335while (0)336337//clang-format on338339/**340* Grows the context's nums array if necessary.341* @param c The context.342*/343#define BCL_GROW_NUMS(c) \344do \345{ \346if ((c)->free_nums.len == 0) \347{ \348bc_vec_grow(&((c)->nums), 1); \349} \350} \351while (0)352353/**354* Frees a BcNum for bcl. This is a destructor.355* @param num The BcNum to free, as a void pointer.356*/357void358bcl_num_destruct(void* num);359360/// The actual context struct.361typedef struct BclCtxt362{363/// The context's scale.364size_t scale;365366/// The context's ibase.367size_t ibase;368369/// The context's obase.370size_t obase;371372/// A vector of BcNum numbers.373BcVec nums;374375/// A vector of BclNumbers. These are the indices in nums that are currently376/// not used (because they were freed).377BcVec free_nums;378379} BclCtxt;380381/**382* Returns the @a BcVm for the current thread.383* @return The vm for the current thread.384*/385BcVm*386bcl_getspecific(void);387388#ifndef _WIN32389390typedef pthread_key_t BclTls;391392#else // _WIN32393394typedef DWORD BclTls;395396#endif // _WIN32397398#endif // LIBBC_PRIVATE_H399400401