David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 34 | #include <grpc/support/alloc.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 35 | #include <grpc/support/log.h> |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 36 | #include "test/core/util/test_config.h" |
| 37 | |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 38 | static void *fake_malloc(size_t size) { return (void *)size; } |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 39 | |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 40 | static void *fake_realloc(void *addr, size_t size) { return (void *)size; } |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 41 | |
Jan Tattermusch | a99687b | 2016-01-27 13:59:08 -0800 | [diff] [blame] | 42 | static void fake_free(void *addr) { |
| 43 | *((intptr_t *)addr) = (intptr_t)0xdeadd00d; |
| 44 | } |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 45 | |
| 46 | static void test_custom_allocs() { |
| 47 | const gpr_allocation_functions default_fns = gpr_get_allocation_functions(); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 48 | intptr_t addr_to_free = 0; |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 49 | char *i; |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 50 | gpr_allocation_functions fns = {fake_malloc, NULL, fake_realloc, fake_free}; |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 51 | |
| 52 | gpr_set_allocation_functions(fns); |
Craig Tiller | be52c6e | 2016-01-04 15:35:26 -0800 | [diff] [blame] | 53 | GPR_ASSERT((void *)(size_t)0xdeadbeef == gpr_malloc(0xdeadbeef)); |
| 54 | GPR_ASSERT((void *)(size_t)0xcafed00d == gpr_realloc(0, 0xcafed00d)); |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 55 | |
| 56 | gpr_free(&addr_to_free); |
Jan Tattermusch | a99687b | 2016-01-27 13:59:08 -0800 | [diff] [blame] | 57 | GPR_ASSERT(addr_to_free == (intptr_t)0xdeadd00d); |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 58 | |
| 59 | /* Restore and check we don't get funky values and that we don't leak */ |
| 60 | gpr_set_allocation_functions(default_fns); |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 61 | GPR_ASSERT((void *)sizeof(*i) != (i = gpr_malloc(sizeof(*i)))); |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 62 | GPR_ASSERT((void *)2 != (i = gpr_realloc(i, 2))); |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 63 | gpr_free(i); |
| 64 | } |
| 65 | |
| 66 | int main(int argc, char **argv) { |
| 67 | grpc_test_init(argc, argv); |
| 68 | test_custom_allocs(); |
| 69 | return 0; |
| 70 | } |