libexplain  1.4.D001
libexplain/buffer/errno/calloc.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2010, 2013 Peter Miller
00004  *
00005  * This program is free software; you can redistribute it and/or modify it
00006  * under the terms of the GNU Lesser General Public License as published by
00007  * the Free Software Foundation; either version 3 of the License, or (at
00008  * your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
00013  * General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <libexplain/ac/errno.h>
00020 
00021 #include <libexplain/buffer/enomem.h>
00022 #include <libexplain/buffer/errno/calloc.h>
00023 #include <libexplain/buffer/errno/malloc.h>
00024 #include <libexplain/buffer/size_t.h>
00025 #include <libexplain/explanation.h>
00026 #include <libexplain/option.h>
00027 
00028 
00029 static void
00030 explain_buffer_errno_calloc_system_call(explain_string_buffer_t *sb, int errnum,
00031     size_t nmemb, size_t size)
00032 {
00033     (void)errnum;
00034     explain_string_buffer_puts(sb, "calloc(nmemb = ");
00035     explain_buffer_size_t(sb, nmemb);
00036     explain_string_buffer_puts(sb, ", size = ");
00037     explain_buffer_size_t(sb, size);
00038     explain_string_buffer_putc(sb, ')');
00039 }
00040 
00041 
00042 void
00043 explain_buffer_errno_calloc_explanation(explain_string_buffer_t *sb, int errnum,
00044     const char *syscall_name, size_t nmemb, size_t size)
00045 {
00046     if (errnum == EINVAL)
00047     {
00048         /* this is deliberately conservative */
00049         size_t nmemb_avail = (((size_t)-1) / 3 * 2 / size);
00050         if (nmemb_avail <= nmemb)
00051         {
00052             explain_string_buffer_puts
00053             (
00054                 sb,
00055                 /* FIXME: i18n */
00056                 "the requested amount of memory exceeds the size "
00057                 "representable by a size_t argument"
00058             );
00059             if (explain_option_dialect_specific())
00060             {
00061                 explain_string_buffer_printf
00062                 (
00063                     sb,
00064                     " (%zu > %zu)",
00065                     nmemb,
00066                     nmemb_avail
00067                 );
00068             }
00069             return;
00070         }
00071     }
00072 
00073     explain_buffer_errno_malloc_explanation
00074     (
00075         sb,
00076         errnum,
00077         syscall_name,
00078         nmemb * size
00079     );
00080 }
00081 
00082 
00083 void
00084 explain_buffer_errno_calloc(explain_string_buffer_t *sb, int errnum, size_t
00085     nmemb, size_t size)
00086 {
00087     explain_explanation_t exp;
00088 
00089     explain_explanation_init(&exp, errnum);
00090     explain_buffer_errno_calloc_system_call(&exp.system_call_sb, errnum, nmemb,
00091         size);
00092     explain_buffer_errno_calloc_explanation(&exp.explanation_sb, errnum,
00093         "calloc", nmemb, size);
00094     explain_explanation_assemble(&exp, sb);
00095 }
00096 
00097 
00098 /* vim: set ts=8 sw=4 et : */