blob: 451c5800543bf9299aae8cff971be0f9a5e140d5 [file] [log] [blame]
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -08004 * 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 Quintasa5aa19b2015-12-09 14:17:52 -080034#include <grpc/support/alloc.h>
Craig Tillerf40df232016-03-25 13:38:14 -070035#include <grpc/support/log.h>
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -080036#include "test/core/util/test_config.h"
37
Craig Tiller620e9652015-12-14 12:02:50 -080038static void *fake_malloc(size_t size) { return (void *)size; }
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -080039
Craig Tiller620e9652015-12-14 12:02:50 -080040static void *fake_realloc(void *addr, size_t size) { return (void *)size; }
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -080041
Jan Tattermuscha99687b2016-01-27 13:59:08 -080042static void fake_free(void *addr) {
43 *((intptr_t *)addr) = (intptr_t)0xdeadd00d;
44}
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -080045
46static void test_custom_allocs() {
47 const gpr_allocation_functions default_fns = gpr_get_allocation_functions();
Craig Tiller7536af02015-12-22 13:49:30 -080048 intptr_t addr_to_free = 0;
Nicolas "Pixel" Noble7c9a1542016-03-26 01:33:34 +010049 char *i;
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -080050 gpr_allocation_functions fns = {fake_malloc, fake_realloc, fake_free};
51
52 gpr_set_allocation_functions(fns);
Craig Tillerbe52c6e2016-01-04 15:35:26 -080053 GPR_ASSERT((void *)(size_t)0xdeadbeef == gpr_malloc(0xdeadbeef));
54 GPR_ASSERT((void *)(size_t)0xcafed00d == gpr_realloc(0, 0xcafed00d));
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -080055
56 gpr_free(&addr_to_free);
Jan Tattermuscha99687b2016-01-27 13:59:08 -080057 GPR_ASSERT(addr_to_free == (intptr_t)0xdeadd00d);
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -080058
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" Noble7c9a1542016-03-26 01:33:34 +010061 GPR_ASSERT((void *)sizeof(*i) != (i = gpr_malloc(sizeof(*i))));
Craig Tiller620e9652015-12-14 12:02:50 -080062 GPR_ASSERT((void *)2 != (i = gpr_realloc(i, 2)));
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -080063 gpr_free(i);
64}
65
66int main(int argc, char **argv) {
67 grpc_test_init(argc, argv);
68 test_custom_allocs();
69 return 0;
70}