xref: /aosp_15_r20/external/bc/scripts/alloc.sh (revision 5a6e848804d15c18a0125914844ee4eb0bda4fcf)
1#!/bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2024 Gavin D. Howard and contributors.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are met:
9#
10# * Redistributions of source code must retain the above copyright notice, this
11#   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 documentation
15#   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, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# 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 THE
27# POSSIBILITY OF SUCH DAMAGE.
28#
29
30# This script is only really useful for running on Linux. It tests the code to
31# free temps in order to make an allocation work. In order to see it work, I
32# suggest adding code after the following line in src/vm.c:
33#
34# if (BC_ERR(ptr == NULL)) bc_vm_fatalError(BC_ERR_FATAL_ALLOC_ERR);
35#
36# The code you should add is the following:
37#
38# bc_file_printf(&vm.ferr, "If you see this, the code worked.\n");
39# bc_file_flush(&vm.ferr, bc_flush_none);
40#
41# If you do not see the that message printed, the code did not work. Or, in the
42# case of some allocators, like jemalloc, the allocator just isn't great with
43# turning a bunch of small allocations into a bigger allocation,
44
45script="$0"
46scriptdir=$(dirname "$script")
47
48. "$scriptdir/functions.sh"
49
50export LANG=C
51
52virtlimit=1000000
53
54ulimit -v $virtlimit
55
56# This script is designed to allocate lots of memory with a lot of caching of
57# numbers (the function f() specifically). Then, it's designed allocate one
58# large number and grow it until allocation failure (the function g()).
59"$scriptdir/../bin/bc" <<*EOF
60
61define f(i, n) {
62	if (n == 0) return i;
63	return f(i + 1, n - 1)
64}
65
66define g(n) {
67	t = (10^9)^(2^24)
68	while (n) {
69		n *= t
70		print "success\n"
71	}
72}
73
74iterations=2000000
75
76for (l=0; l < 100; l++) {
77	iterations
78	j = f(0, iterations$)
79	iterations += 100000
80	print "here\n"
81	n=10^235929600
82	g(n)
83	print "success\n"
84	n=0
85}
86*EOF
87