Commit 8c890fa4 8c890fa48ddfd15c1beae99e7adf4473362d63f4 by Sergey Poznyakoff

New file

1 parent 02757b7c
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General
15 Public License along with this library; if not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301 USA */
18
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <stdlib.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/error.h>
26 #include <mu_umaxtostr.h>
27
28 static char **buffer_pool;
29 static size_t buffer_size;
30 #define BUFFER_SIZE_INIT 4
31 #define BUFFER_SIZE_INCR 4
32
33 static char *
34 get_buffer (unsigned slot)
35 {
36 if (!buffer_pool)
37 {
38 buffer_size = BUFFER_SIZE_INIT;
39 buffer_pool = calloc (buffer_size, sizeof *buffer_pool);
40 }
41 else if (slot >= buffer_size)
42 {
43 buffer_size += (slot + BUFFER_SIZE_INCR - 1) / BUFFER_SIZE_INCR;
44 buffer_pool = realloc (buffer_pool, buffer_size * sizeof *buffer_pool);
45 }
46 if (!buffer_pool)
47 return NULL;
48 if (buffer_pool[slot] == NULL)
49 buffer_pool[slot] = malloc (UINTMAX_STRSIZE_BOUND);
50 return buffer_pool[slot];
51 }
52
53 const char *
54 mu_umaxtostr (unsigned slot, uintmax_t val)
55 {
56 char *s = get_buffer (slot);
57 if (!s)
58 return mu_strerror(ENOMEM);
59 return umaxtostr (val, s);
60 }
1 #include <inttostr.h>
2
3 #define UINTMAX_STRSIZE_BOUND INT_BUFSIZE_BOUND (uintmax_t)
4
5 const char *mu_umaxtostr (unsigned slot, uintmax_t val);