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 as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software Foundation,
14 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15
16#include <my_global.h>
17#include <my_sys.h>
18#include <pfs_global.h>
19#include <string.h>
20
21bool pfs_initialized= false;
22
23bool stub_alloc_always_fails= true;
24int stub_alloc_fails_after_count= 0;
25
26void *pfs_malloc(size_t size, myf)
27{
28 /*
29 Catch non initialized sizing parameter in the unit tests.
30 */
31 DBUG_ASSERT(size <= 100*1024*1024);
32
33 if (stub_alloc_always_fails)
34 return NULL;
35
36 if (--stub_alloc_fails_after_count <= 0)
37 return NULL;
38
39 void *ptr= malloc(size);
40 if (ptr != NULL)
41 memset(ptr, 0, size);
42 return ptr;
43}
44
45void pfs_free(void *ptr)
46{
47 if (ptr != NULL)
48 free(ptr);
49}
50
51void *pfs_malloc_array(size_t n, size_t size, myf flags)
52{
53 size_t array_size= n * size;
54 /* Check for overflow before allocating. */
55 if (is_overflow(array_size, n, size))
56 return NULL;
57 return pfs_malloc(array_size, flags);
58}
59
60bool is_overflow(size_t product, size_t n1, size_t n2)
61{
62 if (n1 != 0 && (product / n1 != n2))
63 return true;
64 else
65 return false;
66}
67
68void pfs_print_error(const char *format, ...)
69{
70}
71
72