xref: /aosp_15_r20/external/libsrtp2/crypto/kernel/alloc.c (revision 90e502c7aef8d77d0622bb67d75435c6190cfc1a)
1*90e502c7SAndroid Build Coastguard Worker /*
2*90e502c7SAndroid Build Coastguard Worker  * alloc.c
3*90e502c7SAndroid Build Coastguard Worker  *
4*90e502c7SAndroid Build Coastguard Worker  * memory allocation and deallocation
5*90e502c7SAndroid Build Coastguard Worker  *
6*90e502c7SAndroid Build Coastguard Worker  * David A. McGrew
7*90e502c7SAndroid Build Coastguard Worker  * Cisco Systems, Inc.
8*90e502c7SAndroid Build Coastguard Worker  */
9*90e502c7SAndroid Build Coastguard Worker /*
10*90e502c7SAndroid Build Coastguard Worker  *
11*90e502c7SAndroid Build Coastguard Worker  * Copyright (c) 2001-2017 Cisco Systems, Inc.
12*90e502c7SAndroid Build Coastguard Worker  * All rights reserved.
13*90e502c7SAndroid Build Coastguard Worker  *
14*90e502c7SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
15*90e502c7SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
16*90e502c7SAndroid Build Coastguard Worker  * are met:
17*90e502c7SAndroid Build Coastguard Worker  *
18*90e502c7SAndroid Build Coastguard Worker  *   Redistributions of source code must retain the above copyright
19*90e502c7SAndroid Build Coastguard Worker  *   notice, this list of conditions and the following disclaimer.
20*90e502c7SAndroid Build Coastguard Worker  *
21*90e502c7SAndroid Build Coastguard Worker  *   Redistributions in binary form must reproduce the above
22*90e502c7SAndroid Build Coastguard Worker  *   copyright notice, this list of conditions and the following
23*90e502c7SAndroid Build Coastguard Worker  *   disclaimer in the documentation and/or other materials provided
24*90e502c7SAndroid Build Coastguard Worker  *   with the distribution.
25*90e502c7SAndroid Build Coastguard Worker  *
26*90e502c7SAndroid Build Coastguard Worker  *   Neither the name of the Cisco Systems, Inc. nor the names of its
27*90e502c7SAndroid Build Coastguard Worker  *   contributors may be used to endorse or promote products derived
28*90e502c7SAndroid Build Coastguard Worker  *   from this software without specific prior written permission.
29*90e502c7SAndroid Build Coastguard Worker  *
30*90e502c7SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31*90e502c7SAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32*90e502c7SAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33*90e502c7SAndroid Build Coastguard Worker  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34*90e502c7SAndroid Build Coastguard Worker  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35*90e502c7SAndroid Build Coastguard Worker  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36*90e502c7SAndroid Build Coastguard Worker  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37*90e502c7SAndroid Build Coastguard Worker  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*90e502c7SAndroid Build Coastguard Worker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*90e502c7SAndroid Build Coastguard Worker  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40*90e502c7SAndroid Build Coastguard Worker  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41*90e502c7SAndroid Build Coastguard Worker  * OF THE POSSIBILITY OF SUCH DAMAGE.
42*90e502c7SAndroid Build Coastguard Worker  *
43*90e502c7SAndroid Build Coastguard Worker  */
44*90e502c7SAndroid Build Coastguard Worker 
45*90e502c7SAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
46*90e502c7SAndroid Build Coastguard Worker #include <config.h>
47*90e502c7SAndroid Build Coastguard Worker #endif
48*90e502c7SAndroid Build Coastguard Worker 
49*90e502c7SAndroid Build Coastguard Worker #include "alloc.h"
50*90e502c7SAndroid Build Coastguard Worker #include "crypto_kernel.h"
51*90e502c7SAndroid Build Coastguard Worker 
52*90e502c7SAndroid Build Coastguard Worker /* the debug module for memory allocation */
53*90e502c7SAndroid Build Coastguard Worker 
54*90e502c7SAndroid Build Coastguard Worker srtp_debug_module_t srtp_mod_alloc = {
55*90e502c7SAndroid Build Coastguard Worker     0,      /* debugging is off by default */
56*90e502c7SAndroid Build Coastguard Worker     "alloc" /* printable name for module   */
57*90e502c7SAndroid Build Coastguard Worker };
58*90e502c7SAndroid Build Coastguard Worker 
59*90e502c7SAndroid Build Coastguard Worker /*
60*90e502c7SAndroid Build Coastguard Worker  * Nota bene: the debugging statements for srtp_crypto_alloc() and
61*90e502c7SAndroid Build Coastguard Worker  * srtp_crypto_free() have identical prefixes, which include the addresses
62*90e502c7SAndroid Build Coastguard Worker  * of the memory locations on which they are operating.  This fact can
63*90e502c7SAndroid Build Coastguard Worker  * be used to locate memory leaks, by turning on memory debugging,
64*90e502c7SAndroid Build Coastguard Worker  * grepping for 'alloc', then matching alloc and free calls by
65*90e502c7SAndroid Build Coastguard Worker  * address.
66*90e502c7SAndroid Build Coastguard Worker  */
67*90e502c7SAndroid Build Coastguard Worker 
68*90e502c7SAndroid Build Coastguard Worker #if defined(HAVE_STDLIB_H)
69*90e502c7SAndroid Build Coastguard Worker 
srtp_crypto_alloc(size_t size)70*90e502c7SAndroid Build Coastguard Worker void *srtp_crypto_alloc(size_t size)
71*90e502c7SAndroid Build Coastguard Worker {
72*90e502c7SAndroid Build Coastguard Worker     void *ptr;
73*90e502c7SAndroid Build Coastguard Worker 
74*90e502c7SAndroid Build Coastguard Worker     if (!size) {
75*90e502c7SAndroid Build Coastguard Worker         return NULL;
76*90e502c7SAndroid Build Coastguard Worker     }
77*90e502c7SAndroid Build Coastguard Worker 
78*90e502c7SAndroid Build Coastguard Worker     ptr = calloc(1, size);
79*90e502c7SAndroid Build Coastguard Worker 
80*90e502c7SAndroid Build Coastguard Worker     if (ptr) {
81*90e502c7SAndroid Build Coastguard Worker         debug_print(srtp_mod_alloc, "(location: %p) allocated", ptr);
82*90e502c7SAndroid Build Coastguard Worker     } else {
83*90e502c7SAndroid Build Coastguard Worker         debug_print(srtp_mod_alloc, "allocation failed (asked for %d bytes)\n",
84*90e502c7SAndroid Build Coastguard Worker                     size);
85*90e502c7SAndroid Build Coastguard Worker     }
86*90e502c7SAndroid Build Coastguard Worker 
87*90e502c7SAndroid Build Coastguard Worker     return ptr;
88*90e502c7SAndroid Build Coastguard Worker }
89*90e502c7SAndroid Build Coastguard Worker 
srtp_crypto_free(void * ptr)90*90e502c7SAndroid Build Coastguard Worker void srtp_crypto_free(void *ptr)
91*90e502c7SAndroid Build Coastguard Worker {
92*90e502c7SAndroid Build Coastguard Worker     debug_print(srtp_mod_alloc, "(location: %p) freed", ptr);
93*90e502c7SAndroid Build Coastguard Worker 
94*90e502c7SAndroid Build Coastguard Worker     free(ptr);
95*90e502c7SAndroid Build Coastguard Worker }
96*90e502c7SAndroid Build Coastguard Worker 
97*90e502c7SAndroid Build Coastguard Worker #else /* we need to define our own memory allocation routines */
98*90e502c7SAndroid Build Coastguard Worker 
99*90e502c7SAndroid Build Coastguard Worker #error no memory allocation defined yet
100*90e502c7SAndroid Build Coastguard Worker 
101*90e502c7SAndroid Build Coastguard Worker #endif
102