Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / tests / malloc_wrapper.c
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "malloc_wrapper.h"
26
27 int nghttp2_failmalloc = 0;
28 int nghttp2_failstart = 0;
29 int nghttp2_countmalloc = 1;
30 int nghttp2_nmalloc = 0;
31
32 #define CHECK_PREREQ                                                           \
33   do {                                                                         \
34     if (nghttp2_failmalloc && nghttp2_nmalloc >= nghttp2_failstart) {          \
35       return NULL;                                                             \
36     }                                                                          \
37     if (nghttp2_countmalloc) {                                                 \
38       ++nghttp2_nmalloc;                                                       \
39     }                                                                          \
40   } while (0)
41
42 static void *my_malloc(size_t size, void *mud _U_) {
43   CHECK_PREREQ;
44   return malloc(size);
45 }
46
47 static void my_free(void *ptr, void *mud _U_) { free(ptr); }
48
49 static void *my_calloc(size_t nmemb, size_t size, void *mud _U_) {
50   CHECK_PREREQ;
51   return calloc(nmemb, size);
52 }
53
54 static void *my_realloc(void *ptr, size_t size, void *mud _U_) {
55   CHECK_PREREQ;
56   return realloc(ptr, size);
57 }
58
59 static nghttp2_mem mem = {NULL, my_malloc, my_free, my_calloc, my_realloc};
60
61 nghttp2_mem *nghttp2_mem_fm(void) { return &mem; }
62
63 static int failmalloc_bk, countmalloc_bk;
64
65 void nghttp2_failmalloc_pause(void) {
66   failmalloc_bk = nghttp2_failmalloc;
67   countmalloc_bk = nghttp2_countmalloc;
68   nghttp2_failmalloc = 0;
69   nghttp2_countmalloc = 0;
70 }
71
72 void nghttp2_failmalloc_unpause(void) {
73   nghttp2_failmalloc = failmalloc_bk;
74   nghttp2_countmalloc = countmalloc_bk;
75 }