1 /* Copyright (c) 2008, 2021, Oracle and/or its affiliates.
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, Suite 500, 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 size_t pfs_allocated_memory_size= 0;
30 size_t pfs_allocated_memory_count= 0;
31 
32 bool stub_alloc_always_fails= true;
33 int stub_alloc_fails_after_count= 0;
34 
pfs_malloc(PFS_builtin_memory_class * klass,size_t size,myf)35 void *pfs_malloc(PFS_builtin_memory_class *klass, size_t size, myf)
36 {
37   /*
38     Catch non initialized sizing parameter in the unit tests.
39   */
40   assert(size <= 100*1024*1024);
41 
42   if (stub_alloc_always_fails)
43     return NULL;
44 
45   if (--stub_alloc_fails_after_count <= 0)
46     return NULL;
47 
48   void *ptr= malloc(size);
49   if (ptr != NULL)
50     memset(ptr, 0, size);
51   return ptr;
52 }
53 
pfs_free(PFS_builtin_memory_class *,size_t,void * ptr)54 void pfs_free(PFS_builtin_memory_class *, size_t, void *ptr)
55 {
56   if (ptr != NULL)
57     free(ptr);
58 }
59 
pfs_malloc_array(PFS_builtin_memory_class * klass,size_t n,size_t size,myf flags)60 void *pfs_malloc_array(PFS_builtin_memory_class *klass, size_t n, size_t size, myf flags)
61 {
62   size_t array_size= n * size;
63   /* Check for overflow before allocating. */
64   if (is_overflow(array_size, n, size))
65     return NULL;
66   return pfs_malloc(klass, array_size, flags);
67 }
68 
pfs_free_array(PFS_builtin_memory_class * klass,size_t n,size_t size,void * ptr)69 void pfs_free_array(PFS_builtin_memory_class *klass, size_t n, size_t size, void *ptr)
70 {
71   if (ptr == NULL)
72     return;
73   size_t array_size= n * size;
74   return pfs_free(klass, array_size, ptr);
75 }
76 
is_overflow(size_t product,size_t n1,size_t n2)77 bool is_overflow(size_t product, size_t n1, size_t n2)
78 {
79   if (n1 != 0 && (product / n1 != n2))
80     return true;
81   else
82     return false;
83 }
84 
pfs_print_error(const char * format,...)85 void pfs_print_error(const char *format, ...)
86 {
87 }
88 
89 
90