xref: /isa-l_crypto/aes/gcm_test_alloc.h (revision 38e16e11defa07d24d9d593354096a7ef33c75b7)
1 /**********************************************************************
2   Copyright(c) 2024 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 #ifndef GCM_TEST_ALLOC_H_
31 #define GCM_TEST_ALLOC_H_
32 
33 #include "types.h" // aligned_free() and posix_memalign() wrappers
34 
35 #define DIM(x) (sizeof(x) / sizeof(x[0]))
36 
37 static int
38 vector_allocate(void **memory[], const size_t length[], const size_t align[], const size_t num)
39 {
40         int ret = 0;
41 
42         for (size_t n = 0; n < num; n++) {
43                 if (length[n] != 0) {
44                         const int use_memalign = ((align != NULL) && (align[n] != 0));
45                         void *ptr = NULL;
46                         int posix_ret = 0;
47 
48                         if (use_memalign)
49                                 posix_ret = posix_memalign(&ptr, align[n], length[n]);
50                         else
51                                 ptr = malloc(length[n]);
52 
53                         if (ptr == NULL || posix_ret != 0)
54                                 ret = 1; /* operation error */
55 
56                         *memory[n] = ptr;
57                 } else {
58                         /* NULL pointer for zero length input */
59                         *memory[n] = NULL;
60                 }
61         }
62 
63         if (ret)
64                 fprintf(stderr, "ERROR: Can't allocate required memory\n");
65 
66         return ret;
67 }
68 
69 static int
70 vector_free(void **memory[], const size_t align[], const size_t num)
71 {
72         int ret = 0;
73 
74         for (size_t n = 0; n < num; n++) {
75                 if (memory[n] != NULL) {
76                         const int used_memalign = ((align != NULL) && (align[n] != 0));
77 
78                         if (used_memalign)
79                                 aligned_free(*memory[n]);
80                         else
81                                 free(*memory[n]);
82 
83                         *memory[n] = NULL;
84                 }
85         }
86 
87         return ret;
88 }
89 
90 #endif /* GCM_TEST_ALLOC_H_ */
91