-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds an option to use a heap-buffer for the usually stack-based `MP_WARRAY`-sized temporary buffers. Per default it will reserve a single buffer, which can be modified * at compile-time via the `MP_WARRAY_NUM` define * at run-time by calling `mp_warray_init()` The internal structure can only be created once. If one wants to modify the maximum number of elements, the entire structure has to be free'd by calling `mp_warray_free()`. In case one wants to use this option with multiple threads, one shall use the `mp_warray_init()` function and pass appropriate locking functions. Signed-off-by: Steffen Jaeckel <[email protected]>
- Loading branch information
Showing
20 changed files
with
394 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "tommath_private.h" | ||
#ifdef MP_WARRAY_FREE_C | ||
/* LibTomMath, multiple-precision integer library -- Tom St Denis */ | ||
/* SPDX-License-Identifier: Unlicense */ | ||
|
||
/* static check that the multiplication won't overflow */ | ||
MP_STATIC_ASSERT(warray_free_sz_does_not_overflow, (sizeof(mp_word) * MP_WARRAY) >= MP_WARRAY) | ||
|
||
static int s_warray_free(void) | ||
{ | ||
int ret = 0; | ||
size_t n; | ||
S_MP_WARRAY_LOCK(); | ||
for (n = 0; n < s_mp_warray.allocated; ++n) { | ||
if (s_mp_warray.l_used[n].warray) { | ||
ret = -2; | ||
goto ERR_OUT; | ||
} | ||
} | ||
for (n = 0; n < s_mp_warray.allocated; ++n) { | ||
MP_FREE(s_mp_warray.l_free[n].warray, sizeof(mp_word) * MP_WARRAY); | ||
s_mp_warray.l_free[n].warray = NULL; | ||
} | ||
s_mp_warray_free(s_mp_warray.usable); | ||
ERR_OUT: | ||
S_MP_WARRAY_UNLOCK(); | ||
return ret; | ||
} | ||
|
||
int mp_warray_free(void) | ||
{ | ||
if (MP_HAS(MP_SMALL_STACK_SIZE)) return s_warray_free(); | ||
return -1; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "tommath_private.h" | ||
#ifdef MP_WARRAY_INIT_C | ||
/* LibTomMath, multiple-precision integer library -- Tom St Denis */ | ||
/* SPDX-License-Identifier: Unlicense */ | ||
|
||
static mp_err s_warray_init(size_t n_alloc, bool preallocate, mp_lock *lock) | ||
{ | ||
size_t n; | ||
if (s_mp_warray.l_free != NULL || s_mp_warray.l_used != NULL) { | ||
return MP_VAL; | ||
} | ||
|
||
if (MP_HAS(MP_USE_LOCKING) && (lock != NULL)) { | ||
if (lock->lock == NULL || lock->unlock == NULL) | ||
return MP_VAL; | ||
s_mp_warray.lock = *lock; | ||
s_mp_warray.locking_enabled = true; | ||
} else { | ||
s_mp_zero_buf(&s_mp_warray.lock, sizeof(s_mp_warray.lock)); | ||
} | ||
|
||
s_mp_warray.l_free = MP_CALLOC(n_alloc, sizeof(*(s_mp_warray.l_free))); | ||
s_mp_warray.l_used = MP_CALLOC(n_alloc, sizeof(*(s_mp_warray.l_used))); | ||
if (s_mp_warray.l_free == NULL || s_mp_warray.l_used == NULL) { | ||
s_mp_warray_free(n_alloc); | ||
return MP_MEM; | ||
} | ||
|
||
if (preallocate) { | ||
for (n = 0; n < n_alloc; ++n) { | ||
s_mp_warray.l_free[n].warray = MP_CALLOC(MP_WARRAY, sizeof(mp_word)); | ||
if (s_mp_warray.l_free[n].warray == NULL) { | ||
while (n > 0) { | ||
n--; | ||
MP_FREE(s_mp_warray.l_free[n].warray, MP_WARRAY * sizeof(mp_word)); | ||
s_mp_warray.l_free[n].warray = NULL; | ||
} | ||
s_mp_warray_free(n_alloc); | ||
return MP_MEM; | ||
} | ||
} | ||
s_mp_warray.allocated = n_alloc; | ||
} | ||
|
||
s_mp_warray.usable = n_alloc; | ||
return MP_OKAY; | ||
} | ||
|
||
mp_err mp_warray_init(size_t n_alloc, bool preallocate, mp_lock *lock) | ||
{ | ||
if (MP_HAS(MP_SMALL_STACK_SIZE)) return s_warray_init(n_alloc, preallocate, lock); | ||
return MP_ERR; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include "tommath_private.h" | ||
#ifdef S_MP_WARRAY_C | ||
/* LibTomMath, multiple-precision integer library -- Tom St Denis */ | ||
/* SPDX-License-Identifier: Unlicense */ | ||
|
||
st_warray s_mp_warray; | ||
|
||
#endif |
Oops, something went wrong.