Implemented a resizable heap abstraction over mmap(). Allows us to
[panda.git] / src / st-large-integer.c
blobbfc17e232a195e8714aa483447a6dbbd2a274ab5
1 /*
2 * st-large-integer.c
4 * Copyright (C) 2008 Vincent Geddes
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "st-large-integer.h"
26 #include "st-universe.h"
27 #include "st-types.h"
28 #include "math.h"
30 #define VALUE(oop) (&(ST_LARGE_INTEGER(oop)->value))
32 st_oop
33 st_large_integer_new_from_string (const char *string, st_uint radix)
35 mp_int value;
36 int result;
38 st_assert (string != NULL);
40 result = mp_init (&value);
41 if (result != MP_OKAY)
42 goto out;
44 result = mp_read_radix (&value, string, radix);
45 if (result != MP_OKAY)
46 goto out;
48 return st_large_integer_new (&value);
50 out:
51 mp_clear (&value);
52 fprintf (stderr, mp_error_to_string (result));
53 return st_nil;
56 char *
57 st_large_integer_to_string (st_oop integer, st_uint radix)
59 int result;
60 int size;
62 result = mp_radix_size (VALUE (integer), radix, &size);
63 if (result != MP_OKAY)
64 goto out;
66 char *str = st_malloc (size);
68 mp_toradix (VALUE (integer), str, radix);
69 if (result != MP_OKAY)
70 goto out;
72 return str;
74 out:
75 fprintf (stderr, mp_error_to_string (result));
76 return NULL;
79 static st_oop
80 allocate_with_value (st_space *space, st_oop class, mp_int * value)
82 st_oop object;
84 object = st_space_allocate_object (space, class, sizeof (struct st_large_integer) / sizeof (st_oop));
86 if (value)
87 *VALUE (object) = *value;
88 else
89 mp_init (VALUE (object));
91 return object;
94 st_oop
95 st_large_integer_new (mp_int * value)
97 return allocate_with_value (memory->moving_space, st_large_integer_class, value);
101 static st_oop
102 allocate (st_space *space, st_oop class)
104 return allocate_with_value (space, class, NULL);
107 static st_oop
108 large_integer_copy (st_oop object)
110 mp_int value;
111 int result;
113 result = mp_init_copy (&value, VALUE (object));
114 if (result != MP_OKAY)
115 st_assert_not_reached ();
117 return st_large_integer_new (&value);
120 static st_uint
121 large_integer_size (st_oop object)
123 return (sizeof (struct st_large_integer) / sizeof (st_oop));
126 static void
127 large_integer_contents (st_oop object, struct contents *contents)
129 contents->oops = NULL;
130 contents->size = 0;
133 st_descriptor *
134 st_large_integer_descriptor (void)
136 static st_descriptor __descriptor =
137 { .allocate = allocate,
138 .allocate_arrayed = NULL,
139 .copy = large_integer_copy,
140 .size = large_integer_size,
141 .contents = large_integer_contents,
144 return & __descriptor;