1 /* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
2 
3   This program is free software; you can redistribute it and/or modify
4   it under the terms of the GNU General Public License, version 2.0,
5   as published by the Free Software Foundation.
6 
7   This program is also distributed with certain software (including
8   but not limited to OpenSSL) that is licensed under separate terms,
9   as designated in a particular file or component or in included license
10   documentation.  The authors of MySQL hereby grant you an additional
11   permission to link the program and your derivative works with the
12   separately licensed software that they have included with MySQL.
13 
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License, version 2.0, for more details.
18 
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software Foundation,
21   51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
22 
23 #include <my_global.h>
24 #include <my_sys.h>
25 #include <pfs_global.h>
26 #include <string.h>
27 
28 bool pfs_initialized= false;
29 
30 bool stub_alloc_always_fails= true;
31 int stub_alloc_fails_after_count= 0;
32 
pfs_malloc(size_t size,myf)33 void *pfs_malloc(size_t size, myf)
34 {
35   /*
36     Catch non initialized sizing parameter in the unit tests.
37   */
38   DBUG_ASSERT(size <= 100*1024*1024);
39 
40   if (stub_alloc_always_fails)
41     return NULL;
42 
43   if (--stub_alloc_fails_after_count <= 0)
44     return NULL;
45 
46   void *ptr= malloc(size);
47   if (ptr != NULL)
48     memset(ptr, 0, size);
49   return ptr;
50 }
51 
pfs_free(void * ptr)52 void pfs_free(void *ptr)
53 {
54   if (ptr != NULL)
55     free(ptr);
56 }
57 
pfs_malloc_array(size_t n,size_t size,myf flags)58 void *pfs_malloc_array(size_t n, size_t size, myf flags)
59 {
60   size_t array_size= n * size;
61   /* Check for overflow before allocating. */
62   if (is_overflow(array_size, n, size))
63     return NULL;
64   return pfs_malloc(array_size, flags);
65 }
66 
is_overflow(size_t product,size_t n1,size_t n2)67 bool is_overflow(size_t product, size_t n1, size_t n2)
68 {
69   if (n1 != 0 && (product / n1 != n2))
70     return true;
71   else
72     return false;
73 }
74 
pfs_print_error(const char * format,...)75 void pfs_print_error(const char *format, ...)
76 {
77 }
78 
79